Skip to content

Commit 8bdd1b7

Browse files
committed
fix(attendance): properly select month when passing a date in url param
1 parent 4aecb4d commit 8bdd1b7

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

src/views/Private/Attendance/AttendanceCalendarTile.vue

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'flex h-14 flex-col items-center overflow-hidden py-2 focus:z-10 max-lg:justify-center lg:h-20 lg:items-start',
55
selected
66
? 'bg-amber-500 text-white shadow-inner hover:bg-amber-700 active:bg-amber-800'
7-
: isCurrentMonth
7+
: inCurrentMonth
88
? 'bg-white hover:bg-gray-100'
99
: 'bg-gray-50 hover:bg-gray-100',
1010
]">
@@ -58,10 +58,10 @@ const props = defineProps({
5858
type: Object as PropType<AttendancePeriod<'day'>>,
5959
default: null,
6060
},
61-
});
62-
63-
const isCurrentMonth = computed(() => {
64-
return props.selectedMonth && dayjs(props.date).isSame(props.selectedMonth, 'month');
61+
inCurrentMonth: {
62+
type: Boolean,
63+
default: false,
64+
},
6565
});
6666
6767
const debtCount = computed(() => {

src/views/Private/Attendance/AttendancePage.vue

+41-21
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
type="mdi" />
3333
</RouterLink>
3434
<time
35+
v-if="state.selectedMonth"
3536
class="flex flex-row items-center border-y border-gray-300 bg-white px-3.5 text-sm font-medium text-gray-900 focus:relative"
3637
:datetime="state.selectedMonth">
3738
{{ capitalize(dayjs(state.selectedMonth).format('MMM YYYY')) }}
@@ -74,6 +75,7 @@
7475
<div class="relative border border-gray-200">
7576
<LoadingProgressBar v-if="isFetching" class="absolute top-0 h-px w-full" />
7677
<div
78+
v-if="calendarPeriod"
7779
class="grid grid-cols-7 gap-px border-b border-gray-300 bg-gray-200 text-center text-xs font-semibold leading-6 text-gray-700 lg:flex-none">
7880
<div v-for="weekIndex in 7" :key="`weekday-${weekIndex}`" class="bg-white py-2">
7981
{{
@@ -98,8 +100,12 @@
98100
<AttendanceCalendarTile
99101
:attendance="day.attendance"
100102
:date="day.date"
101-
:selected="day.isSelected"
102-
:selected-month="state.selectedMonth" />
103+
:in-current-month="
104+
Boolean(
105+
state.selectedMonth && dayjs(day.date).isSame(state.selectedMonth, 'month'),
106+
)
107+
"
108+
:selected="day.isSelected" />
103109
</RouterLink>
104110
</div>
105111
</div>
@@ -130,14 +136,14 @@ import { useQuery } from '@tanstack/vue-query';
130136
import { Head } from '@unhead/vue/components';
131137
import dayjs from 'dayjs';
132138
import { capitalize } from 'lodash';
133-
import { computed, reactive, watch } from 'vue';
139+
import { computed, reactive, watch, onMounted } from 'vue';
134140
import { useI18n } from 'vue-i18n';
135141
import { useRoute } from 'vue-router';
136142
137143
const props = defineProps({
138144
month: {
139145
type: String,
140-
default: () => dayjs().format().substring(0, 7),
146+
default: null,
141147
},
142148
date: {
143149
type: String,
@@ -157,11 +163,14 @@ const currentRoute = useRoute();
157163
const notificationsStore = useNotificationsStore();
158164
const i18n = useI18n();
159165
const state = reactive({
160-
selectedMonth: (props.date ? dayjs(props.date) : dayjs()).format().substring(0, 7) as string,
166+
selectedMonth: null as string | null,
161167
selectedAttendance: null as AttendancePeriod<'day'> | null,
162168
});
163169
164170
const calendarPeriod = computed(() => {
171+
if (!state.selectedMonth) {
172+
return null;
173+
}
165174
const startOfMonth = dayjs(state.selectedMonth).startOf('month');
166175
const start = startOfMonth.day() === 0 ? startOfMonth.day(-6) : startOfMonth.day(1);
167176
const end = start.add(5, 'weeks').day(7);
@@ -178,17 +187,19 @@ const {
178187
error: attendanceError,
179188
} = useQuery(
180189
computed(() => ({
181-
queryKey: ['attendance', calendarPeriod.value.start, calendarPeriod.value.end],
182-
queryFn: () => getAttendancePerDay(calendarPeriod.value.start, calendarPeriod.value.end),
190+
queryKey: ['attendance', calendarPeriod.value?.start, calendarPeriod.value?.end],
191+
queryFn: ({ queryKey: [_attendance, start, end] }: { queryKey: any[] }) =>
192+
getAttendancePerDay(start, end),
183193
staleTime: 300_000,
194+
enabled: !!calendarPeriod.value?.start && !!calendarPeriod.value?.end,
184195
})),
185196
);
186197
187198
const calendar = computed(() => {
188199
return Array.from({
189-
length: dayjs(calendarPeriod.value.end).diff(calendarPeriod.value.start, 'days') + 1,
200+
length: dayjs(calendarPeriod.value?.end).diff(calendarPeriod.value?.start, 'days') + 1,
190201
}).map((_, index) => {
191-
const date = dayjs(calendarPeriod.value.start).add(index, 'day').format().substring(0, 10);
202+
const date = dayjs(calendarPeriod.value?.start).add(index, 'day').format().substring(0, 10);
192203
const attendanceForThisDate = attendance.value?.find((day) => day.date === date);
193204
194205
return {
@@ -199,16 +210,6 @@ const calendar = computed(() => {
199210
});
200211
});
201212
202-
watch(
203-
() => props.month,
204-
(month) => {
205-
if (month && dayjs(month, 'YYYY-MM', true).isValid()) {
206-
state.selectedMonth = month;
207-
}
208-
},
209-
{ immediate: true },
210-
);
211-
212213
watch(
213214
[() => props.date, () => attendance.value],
214215
([selectedDate, attendancePeriod]) => {
@@ -233,11 +234,30 @@ watch(
233234
notificationsStore.addErrorNotification(
234235
error,
235236
i18n.t('attendance.onFetch.fail', {
236-
start: dayjs(calendarPeriod.value.start).format('L'),
237-
end: dayjs(calendarPeriod.value.end).format('L'),
237+
start: dayjs(calendarPeriod.value?.start).format('L'),
238+
end: dayjs(calendarPeriod.value?.end).format('L'),
238239
}),
239240
);
240241
}
241242
},
242243
);
244+
245+
onMounted(() => {
246+
if (props.month && dayjs(props.month, 'YYYY-MM', true).isValid()) {
247+
state.selectedMonth = props.month;
248+
} else if (props.date) {
249+
state.selectedMonth = dayjs(props.date).format('YYYY-MM');
250+
} else {
251+
state.selectedMonth = dayjs().format('YYYY-MM');
252+
}
253+
});
254+
255+
watch(
256+
() => props.month,
257+
(month) => {
258+
if (month && dayjs(month, 'YYYY-MM', true).isValid()) {
259+
state.selectedMonth = month;
260+
}
261+
},
262+
);
243263
</script>

0 commit comments

Comments
 (0)