Skip to content

Commit

Permalink
Use hashes for navigating calendar modes (#430)
Browse files Browse the repository at this point in the history
* Use hashes for navigating calendar modes

* Fix the opacity that the frontend linter does not want us to have.
  • Loading branch information
MelissaAutumn authored May 28, 2024
1 parent 7ace7ba commit f80f74f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
23 changes: 21 additions & 2 deletions frontend/src/components/CalendarQalendar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ import {
qalendarSlotDurations,
} from '@/definitions';
import { getLocale, getPreferredTheme, timeFormat } from '@/utils';
import { useRoute, useRouter } from 'vue-router';
// component constants
const dj = inject('dayjs');
const router = useRouter();
const route = useRoute();
// component properties
const props = defineProps({
Expand All @@ -37,10 +40,12 @@ const {
const qalendarRef = ref();
const isValidMode = (mode) => ['#day', '#week', '#month'].indexOf(mode) !== -1;
const displayFormat = timeFormat();
const calendarColors = ref({});
const selectedDate = ref(null);
const calendarMode = ref('month');
const calendarMode = ref(isValidMode(route.hash) ? route.hash.slice(1) : 'month');
const preferredTheme = getPreferredTheme();
// component emits
Expand Down Expand Up @@ -109,7 +114,9 @@ const dateChange = (evt) => {
* On mode change. e.g. month, week, day.
*/
const modeChange = (evt) => {
calendarMode.value = evt?.mode ?? 'month';
const hash = evt?.mode ?? 'month';
calendarMode.value = hash;
router.push({ hash: `#${hash}` });
};
/* Functions */
Expand Down Expand Up @@ -339,6 +346,18 @@ watch(dayBoundary, () => {
qalendarRef.value.time.DAY_START = dayBoundary.value.start * 100;
qalendarRef.value.time.DAY_END = dayBoundary.value.end * 100;
});
watch(route, () => {
if (!qalendarRef?.value) {
return;
}
// Route.hash never returns null, so need to do falsey default here.
const hash = route.hash || '#month';
if (isValidMode(hash)) {
qalendarRef.value.mode = hash.replace('#', '');
}
});
</script>
<template>
<div
Expand Down
15 changes: 1 addition & 14 deletions frontend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const routes = [
redirect: { name: 'calendar' },
},
{
path: '/calendar/:view?/:date?',
path: '/calendar/:date?',
name: 'calendar',
component: CalendarView,
},
Expand Down Expand Up @@ -111,17 +111,4 @@ const router = createRouter({
routes,
});

// set default route parameters
router.beforeEach((to) => {
if (to.name === 'calendar' && !to.params.view) {
to.params.view = 'month';
return to;
}
if (to.name === 'appointments' && !to.params.view) {
to.params.view = 'all';
return to;
}
return null;
});

export default router;
2 changes: 1 addition & 1 deletion frontend/src/views/BookingView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const getViewBySlotDistribution = (slots) => {
* @returns {Promise<Object|null>}
*/
const getAppointment = async () => {
const request = call('schedule/public/availability').post({ url: window.location.href });
const request = call('schedule/public/availability').post({ url: window.location.href.split('#')[0] });
const { data, error } = await request.json();
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/views/CalendarView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const activeDateRange = computed(() => ({
}));
// active menu item for tab navigation of calendar views
const tabActive = ref(calendarViews[route.params.view]);
const tabActive = calendarViews.month;
// get remote calendar data for current year
const calendarEvents = ref([]);
Expand Down Expand Up @@ -147,7 +147,7 @@ const selectDate = async (d) => {
const date = dj(d);
await router.replace({
name: route.name,
params: { view: route.params.view, date: date.format('YYYY-MM-DD') },
params: { date: date.format('YYYY-MM-DD') },
});
// Check if we need to pull remote events
await onDateChange({ start: date.startOf('month'), end: date.endOf('month') });
Expand Down

0 comments on commit f80f74f

Please sign in to comment.