Client - display previous and next months on calendar

This commit is contained in:
Sam 2021-09-29 17:35:21 +02:00
parent 6b94a4715a
commit 808ce05c27
3 changed files with 84 additions and 27 deletions

View File

@ -25,7 +25,15 @@
<script lang="ts"> <script lang="ts">
import { addDays, format, isSameDay, isSameMonth, isToday } from 'date-fns' import { addDays, format, isSameDay, isSameMonth, isToday } from 'date-fns'
import { defineComponent, PropType, toRefs } from 'vue' import {
PropType,
Ref,
defineComponent,
ref,
toRefs,
watch,
onMounted,
} from 'vue'
import CalendarWorkouts from '@/components/Dashboard/UserCalendar/CalendarWorkouts.vue' import CalendarWorkouts from '@/components/Dashboard/UserCalendar/CalendarWorkouts.vue'
import { ISport } from '@/types/sports' import { ISport } from '@/types/sports'
@ -68,16 +76,22 @@
}, },
}, },
setup(props) { setup(props) {
const rows = [] const rows: Ref<Date[][]> = ref([])
let { startDate, endDate, weekStartingMonday } = toRefs(props) let { startDate, endDate, weekStartingMonday } = toRefs(props)
let day = startDate.value
while (day <= endDate.value) { onMounted(() => getDays())
const days = []
for (let i = 0; i < 7; i++) { function getDays() {
days.push(day) rows.value = []
day = addDays(day, 1) let day = startDate.value
while (day <= endDate.value) {
const days: Date[] = []
for (let i = 0; i < 7; i++) {
days.push(day)
day = addDays(day, 1)
}
rows.value.push(days)
} }
rows.push(days)
} }
function isWeekEnd(day: number): boolean { function isWeekEnd(day: number): boolean {
@ -100,6 +114,11 @@
return [] return []
} }
watch(
() => props.currentDay,
() => getDays()
)
return { rows, format, isSameMonth, isToday, isWeekEnd, filterWorkouts } return { rows, format, isSameMonth, isToday, isWeekEnd, filterWorkouts }
}, },
}) })

View File

@ -1,6 +1,9 @@
<template> <template>
<div class="calendar-header"> <div class="calendar-header">
<div class="calendar-arrow calendar-arrow-left"> <div
class="calendar-arrow calendar-arrow-left"
@click="emit('displayPreviousMonth')"
>
<i class="fa fa-chevron-left" aria-hidden="true" /> <i class="fa fa-chevron-left" aria-hidden="true" />
</div> </div>
<div class="calendar-month"> <div class="calendar-month">
@ -8,7 +11,10 @@
{{ format(day, 'MMM yyyy', localeOptions) }} {{ format(day, 'MMM yyyy', localeOptions) }}
</span> </span>
</div> </div>
<div class="calendar-arrow calendar-arrow-right"> <div
class="calendar-arrow calendar-arrow-right"
@click="emit('displayNextMonth')"
>
<i class="fa fa-chevron-right" aria-hidden="true" /> <i class="fa fa-chevron-right" aria-hidden="true" />
</div> </div>
</div> </div>
@ -29,8 +35,9 @@
required: true, required: true,
}, },
}, },
setup() { emits: ['displayNextMonth', 'displayPreviousMonth'],
return { format } setup(props, { emit }) {
return { emit, format }
}, },
}) })
</script> </script>
@ -48,9 +55,11 @@
} }
.calendar-arrow-left { .calendar-arrow-left {
text-align: left; text-align: left;
cursor: pointer;
} }
.calendar-arrow-right { .calendar-arrow-right {
text-align: right; text-align: right;
cursor: pointer;
} }
.calendar-month { .calendar-month {
font-weight: bold; font-weight: bold;

View File

@ -2,7 +2,12 @@
<div id="user-calendar"> <div id="user-calendar">
<Card class="calendar-card"> <Card class="calendar-card">
<template #content> <template #content>
<CalendarHeader :day="day" locale-options="enGB" /> <CalendarHeader
:day="day"
locale-options="enGB"
@displayNextMonth="displayNextMonth"
@displayPreviousMonth="displayPreviousMonth"
/>
<CalendarDays :start-date="calendarDates.start" locale-options="enGB" /> <CalendarDays :start-date="calendarDates.start" locale-options="enGB" />
<CalendarCells <CalendarCells
:currentDay="day" :currentDay="day"
@ -19,12 +24,13 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { format } from 'date-fns' import { addMonths, format, subMonths } from 'date-fns'
import { import {
ComputedRef, ComputedRef,
PropType, PropType,
computed, computed,
defineComponent, defineComponent,
ref,
onBeforeMount, onBeforeMount,
} from 'vue' } from 'vue'
@ -60,24 +66,47 @@
setup(props) { setup(props) {
const store = useStore() const store = useStore()
onBeforeMount(() => onBeforeMount(() => getCalendarWorkouts())
store.dispatch(WORKOUTS_STORE.ACTIONS.GET_CALENDAR_WORKOUTS, apiParams)
)
const dateFormat = 'yyyy-MM-dd' const dateFormat = 'yyyy-MM-dd'
const day = new Date() let day = ref(new Date())
const calendarDates = getCalendarStartAndEnd(day, props.user.weekm) let calendarDates = ref(
const apiParams: IWorkoutsPayload = { getCalendarStartAndEnd(day.value, props.user.weekm)
from: format(calendarDates.start, dateFormat), )
to: format(calendarDates.end, dateFormat),
order: 'desc',
per_page: 100,
}
const calendarWorkouts: ComputedRef<IWorkout[]> = computed( const calendarWorkouts: ComputedRef<IWorkout[]> = computed(
() => store.getters[WORKOUTS_STORE.GETTERS.CALENDAR_WORKOUTS] () => store.getters[WORKOUTS_STORE.GETTERS.CALENDAR_WORKOUTS]
) )
return { day, calendarDates, calendarWorkouts } function getCalendarWorkouts() {
calendarDates.value = getCalendarStartAndEnd(
day.value,
props.user.weekm
)
const apiParams: IWorkoutsPayload = {
from: format(calendarDates.value.start, dateFormat),
to: format(calendarDates.value.end, dateFormat),
order: 'desc',
per_page: 100,
}
store.dispatch(WORKOUTS_STORE.ACTIONS.GET_CALENDAR_WORKOUTS, apiParams)
}
function displayNextMonth() {
day.value = addMonths(day.value, 1)
getCalendarWorkouts()
}
function displayPreviousMonth() {
day.value = subMonths(day.value, 1)
getCalendarWorkouts()
}
return {
day,
calendarDates,
calendarWorkouts,
displayNextMonth,
displayPreviousMonth,
}
}, },
}) })
</script> </script>