Skip to content

Commit

Permalink
Store compare selection in query
Browse files Browse the repository at this point in the history
  • Loading branch information
PassiDel committed May 23, 2024
1 parent 9b3df85 commit 8d7eaaa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
28 changes: 28 additions & 0 deletions src/composables/useQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useRoute, useRouter } from 'vue-router';
import { ref, watch } from 'vue';

export function useQuery() {
const route = useRoute();
const router = useRouter();

const selectedLeft = ref('');
if (route.query.left && typeof route.query.left === 'string') {
selectedLeft.value = route.query.left;
}

const selectedRight = ref('');
if (route.query.right && typeof route.query.right === 'string') {
selectedRight.value = route.query.right;
}

function updateQuery() {
router.push({
path: route.path,
query: { left: selectedLeft.value, right: selectedRight.value }
});
}
watch(selectedLeft, updateQuery);
watch(selectedRight, updateQuery);

return { selectedLeft, selectedRight };
}
14 changes: 7 additions & 7 deletions src/views/CompareDegreesView.vue
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<script setup lang="ts">
import DataSelect from '@/components/DataSelect.vue';
import { computed, ref } from 'vue';
import { computed } from 'vue';
import { faculties } from '@/data';
import CompareDegree from '@/components/CompareDegree.vue';
import { useQuery } from '@/composables/useQuery';
const selectedDegreeLeft = ref('');
const selectedDegreeRight = ref('');
const { selectedLeft, selectedRight } = useQuery();
const leftDegree = computed(() =>
faculties
.flatMap(({ degrees, ...f }) => degrees.map((d) => ({ ...d, faculty: f })))
.find((d) => d.slug === selectedDegreeLeft.value)
.find((d) => d.slug === selectedLeft.value)
);
const rightDegree = computed(() =>
faculties
.flatMap(({ degrees, ...f }) => degrees.map((d) => ({ ...d, faculty: f })))
.find((d) => d.slug === selectedDegreeRight.value)
.find((d) => d.slug === selectedRight.value)
);
</script>

<template>
<main class="grid grid-cols-6 gap-3 p-3">
<DataSelect v-model:selected="selectedDegreeLeft" />
<DataSelect v-model:selected="selectedLeft" />
<CompareDegree v-if="leftDegree" :degree="leftDegree" class="col-span-2" />
<div v-else class="col-span-2">
<h2>Wähle links einen Studiengang aus!</h2>
Expand All @@ -34,7 +34,7 @@ const rightDegree = computed(() =>
<div v-else class="col-span-2">
<h2>Wähle rechts einen Studiengang aus!</h2>
</div>
<DataSelect v-model:selected="selectedDegreeRight" right />
<DataSelect v-model:selected="selectedRight" right />
</main>
</template>

Expand Down
15 changes: 7 additions & 8 deletions src/views/CompareFacultiesView.vue
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
<script setup lang="ts">
import DataSelect from '@/components/DataSelect.vue';
import { computed, ref } from 'vue';
import { computed } from 'vue';
import { faculties } from '@/data';
import CompareFaculty from '@/components/CompareFaculty.vue';
import { useQuery } from '@/composables/useQuery';
const selectedFacultyLeft = ref('');
const selectedFacultyRight = ref('');
const { selectedLeft, selectedRight } = useQuery();
const leftFaculty = computed(() =>
faculties.find((f) => f.name === selectedFacultyLeft.value)
faculties.find((f) => f.name === selectedLeft.value)
);
const rightFaculty = computed(() =>
faculties.find((f) => f.name === selectedFacultyRight.value)
faculties.find((f) => f.name === selectedRight.value)
);
</script>

<template>
<main class="grid grid-cols-6 gap-3 p-3">
<DataSelect v-model:selected="selectedFacultyLeft" hide-degrees />
<DataSelect v-model:selected="selectedLeft" hide-degrees />
<CompareFaculty
v-if="leftFaculty"
:faculty="leftFaculty"
Expand All @@ -34,7 +33,7 @@ const rightFaculty = computed(() =>
<div v-else class="col-span-2">
<h2>Wähle rechts eine Fakultät aus!</h2>
</div>
<DataSelect v-model:selected="selectedFacultyRight" hide-degrees right />
<DataSelect v-model:selected="selectedRight" hide-degrees right />
</main>
</template>

Expand Down

0 comments on commit 8d7eaaa

Please sign in to comment.