32
32
type =" mdi" />
33
33
</RouterLink >
34
34
<time
35
+ v-if =" state.selectedMonth"
35
36
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"
36
37
:datetime =" state.selectedMonth" >
37
38
{{ capitalize(dayjs(state.selectedMonth).format('MMM YYYY')) }}
74
75
<div class =" relative border border-gray-200" >
75
76
<LoadingProgressBar v-if =" isFetching" class =" absolute top-0 h-px w-full" />
76
77
<div
78
+ v-if =" calendarPeriod"
77
79
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" >
78
80
<div v-for =" weekIndex in 7" :key =" `weekday-${weekIndex}`" class =" bg-white py-2" >
79
81
{{
98
100
<AttendanceCalendarTile
99
101
:attendance =" day.attendance"
100
102
: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" />
103
109
</RouterLink >
104
110
</div >
105
111
</div >
@@ -130,14 +136,14 @@ import { useQuery } from '@tanstack/vue-query';
130
136
import { Head } from ' @unhead/vue/components' ;
131
137
import dayjs from ' dayjs' ;
132
138
import { capitalize } from ' lodash' ;
133
- import { computed , reactive , watch } from ' vue' ;
139
+ import { computed , reactive , watch , onMounted } from ' vue' ;
134
140
import { useI18n } from ' vue-i18n' ;
135
141
import { useRoute } from ' vue-router' ;
136
142
137
143
const props = defineProps ({
138
144
month: {
139
145
type: String ,
140
- default : () => dayjs (). format (). substring ( 0 , 7 ) ,
146
+ default: null ,
141
147
},
142
148
date: {
143
149
type: String ,
@@ -157,11 +163,14 @@ const currentRoute = useRoute();
157
163
const notificationsStore = useNotificationsStore ();
158
164
const i18n = useI18n ();
159
165
const state = reactive ({
160
- selectedMonth: ( props . date ? dayjs ( props . date ) : dayjs ()). format (). substring ( 0 , 7 ) as string ,
166
+ selectedMonth: null as string | null ,
161
167
selectedAttendance: null as AttendancePeriod <' day' > | null ,
162
168
});
163
169
164
170
const calendarPeriod = computed (() => {
171
+ if (! state .selectedMonth ) {
172
+ return null ;
173
+ }
165
174
const startOfMonth = dayjs (state .selectedMonth ).startOf (' month' );
166
175
const start = startOfMonth .day () === 0 ? startOfMonth .day (- 6 ) : startOfMonth .day (1 );
167
176
const end = start .add (5 , ' weeks' ).day (7 );
@@ -178,17 +187,19 @@ const {
178
187
error : attendanceError,
179
188
} = useQuery (
180
189
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 ),
183
193
staleTime: 300_000 ,
194
+ enabled: !! calendarPeriod .value ?.start && !! calendarPeriod .value ?.end ,
184
195
})),
185
196
);
186
197
187
198
const calendar = computed (() => {
188
199
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 ,
190
201
}).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 );
192
203
const attendanceForThisDate = attendance .value ?.find ((day ) => day .date === date );
193
204
194
205
return {
@@ -199,16 +210,6 @@ const calendar = computed(() => {
199
210
});
200
211
});
201
212
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
-
212
213
watch (
213
214
[() => props .date , () => attendance .value ],
214
215
([selectedDate , attendancePeriod ]) => {
@@ -233,11 +234,30 @@ watch(
233
234
notificationsStore .addErrorNotification (
234
235
error ,
235
236
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' ),
238
239
}),
239
240
);
240
241
}
241
242
},
242
243
);
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
+ );
243
263
</script >
0 commit comments