Refactored event goto function into a sub function
This commit is contained in:
@@ -16,7 +16,7 @@ import { Badge } from '@/components/ui/badge'
|
|||||||
import { PopoverContent } from '@/components/ui/popover'
|
import { PopoverContent } from '@/components/ui/popover'
|
||||||
|
|
||||||
const { defaultDate, getFormattedDateTitle, jumpToDate } = useCalendar()
|
const { defaultDate, getFormattedDateTitle, jumpToDate } = useCalendar()
|
||||||
const { getRelativeEvent } = useCalendarEvents()
|
const { getRelativeEventFromEvent } = useCalendarEvents()
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
event: CalendarEvent
|
event: CalendarEvent
|
||||||
@@ -41,7 +41,7 @@ function handleJumpToDate(date: LeimDate) {
|
|||||||
|
|
||||||
function handleGotoRelativeEvent(position: 'next' | 'prev' = 'next') {
|
function handleGotoRelativeEvent(position: 'next' | 'prev' = 'next') {
|
||||||
try {
|
try {
|
||||||
const { targetDate } = getRelativeEvent(props.event, position, props.isEndEvent)
|
const { targetDate } = getRelativeEventFromEvent(props.event, position, props.isEndEvent)
|
||||||
|
|
||||||
handleJumpToDate(targetDate)
|
handleJumpToDate(targetDate)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -116,19 +116,27 @@ export const useCalendarEvents = defineStore('calendar-events', () => {
|
|||||||
* @param position Whether we should get the next or previous event
|
* @param position Whether we should get the next or previous event
|
||||||
* @returns The next event in chronological order
|
* @returns The next event in chronological order
|
||||||
*/
|
*/
|
||||||
function getRelativeEvent(
|
function getRelativeEventFromEvent(
|
||||||
event: CalendarEvent,
|
event: CalendarEvent,
|
||||||
position: 'next' | 'prev' = 'next',
|
position: 'next' | 'prev' = 'next',
|
||||||
initialIsEnd: boolean = false
|
initialIsEnd: boolean = false
|
||||||
): { event: CalendarEvent; targetDate: LeimDate } {
|
): { event: CalendarEvent; targetDate: LeimDate } {
|
||||||
let eventPivotValue: number // Day value of the date that the user interacted with
|
let dateToParse: LeimDate // Day value of the date that the user interacted with
|
||||||
|
|
||||||
if (initialIsEnd && event.endDate) {
|
if (initialIsEnd && event.endDate) {
|
||||||
eventPivotValue = convertDateToDays(event.endDate)
|
dateToParse = event.endDate
|
||||||
} else {
|
} else {
|
||||||
eventPivotValue = convertDateToDays(event.startDate)
|
dateToParse = event.startDate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return getRelativeEventFromDate(dateToParse, position)
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRelativeEventFromDate(
|
||||||
|
date: LeimDate,
|
||||||
|
position: 'next' | 'prev' = 'next'
|
||||||
|
): { event: CalendarEvent; targetDate: LeimDate } {
|
||||||
|
const pivotValue = convertDateToDays(date)
|
||||||
const t: { eventData: CalendarEvent; distance: number; targetKey: 'startDate' | 'endDate' }[] =
|
const t: { eventData: CalendarEvent; distance: number; targetKey: 'startDate' | 'endDate' }[] =
|
||||||
[]
|
[]
|
||||||
let eventPivotIndex: number | null = null // Pivot index to save
|
let eventPivotIndex: number | null = null // Pivot index to save
|
||||||
@@ -139,7 +147,7 @@ export const useCalendarEvents = defineStore('calendar-events', () => {
|
|||||||
const e: CalendarEvent = allEvents[i]
|
const e: CalendarEvent = allEvents[i]
|
||||||
// Estimate distance from pivot
|
// Estimate distance from pivot
|
||||||
const startDateDays: number = convertDateToDays(e.startDate)
|
const startDateDays: number = convertDateToDays(e.startDate)
|
||||||
const startDistance: number = startDateDays - eventPivotValue
|
const startDistance: number = startDateDays - pivotValue
|
||||||
|
|
||||||
// Push startDate to comparator array
|
// Push startDate to comparator array
|
||||||
t.push({
|
t.push({
|
||||||
@@ -157,7 +165,7 @@ export const useCalendarEvents = defineStore('calendar-events', () => {
|
|||||||
if (e.endDate) {
|
if (e.endDate) {
|
||||||
realPositon++
|
realPositon++
|
||||||
const endDateDays: number = convertDateToDays(e.endDate)
|
const endDateDays: number = convertDateToDays(e.endDate)
|
||||||
const endDistance: number = endDateDays - eventPivotValue
|
const endDistance: number = endDateDays - pivotValue
|
||||||
|
|
||||||
// Push optional endDate to comparator array
|
// Push optional endDate to comparator array
|
||||||
t.push({
|
t.push({
|
||||||
@@ -200,5 +208,5 @@ export const useCalendarEvents = defineStore('calendar-events', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { allEvents, currentEvents, getRelativeEvent }
|
return { allEvents, currentEvents, getRelativeEventFromDate, getRelativeEventFromEvent }
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user