Added current event state

This commit is contained in:
Alexis
2024-04-01 00:19:13 +02:00
parent 2bf2ad2492
commit 196c06ece4
5 changed files with 82 additions and 20 deletions

View File

@@ -23,7 +23,7 @@ module.exports = {
ecmaVersion: 'latest' ecmaVersion: 'latest'
}, },
rules: { rules: {
indent: ['error', 2], indent: ['error', 2, { SwitchCase: 1 }],
'prettier/prettier': [ 'prettier/prettier': [
'error', 'error',
{ {

View File

@@ -1,8 +1,10 @@
import { computed, type Ref, type ComputedRef } from 'vue' import { computed, type Ref, type ComputedRef, ref } from 'vue'
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { useUrlSearchParams } from '@vueuse/core' import { useUrlSearchParams } from '@vueuse/core'
import type { LeimPeriod } from '@/models/Date' import type { LeimPeriod } from '@/models/Date'
type CalendarViewType = 'month' | 'year' | 'decade' | 'century'
type CalendarStaticConfig = { type CalendarStaticConfig = {
months: string[] months: string[]
monthsPerYear: number monthsPerYear: number
@@ -12,6 +14,10 @@ type CalendarStaticConfig = {
} }
type CalendarCurrentConfig = { type CalendarCurrentConfig = {
viewType: CalendarViewType
}
type CalendarCurrentDate = {
currentPeriod: Ref<LeimPeriod> currentPeriod: Ref<LeimPeriod>
currentYear: ComputedRef<string | string[]> currentYear: ComputedRef<string | string[]>
currentMonth: ComputedRef<string | string[]> currentMonth: ComputedRef<string | string[]>
@@ -20,7 +26,13 @@ type CalendarCurrentConfig = {
} }
export const useCalendar = defineStore('calendar', () => { export const useCalendar = defineStore('calendar', () => {
// Static calendar config /**
* Static calendar config
* This shouldn't change
*/
/**
* Month list
*/
const months = [ const months = [
'Jalen', 'Jalen',
'Malsen', 'Malsen',
@@ -38,6 +50,7 @@ export const useCalendar = defineStore('calendar', () => {
const daysPerMonth = computed(() => daysPerYear / monthsPerYear.value) const daysPerMonth = computed(() => daysPerYear / monthsPerYear.value)
const daysPerWeek = 6 const daysPerWeek = 6
// Assign the static config
const staticConfig: CalendarStaticConfig = { const staticConfig: CalendarStaticConfig = {
months, months,
monthsPerYear: monthsPerYear.value, monthsPerYear: monthsPerYear.value,
@@ -46,6 +59,10 @@ export const useCalendar = defineStore('calendar', () => {
daysPerWeek daysPerWeek
} }
const currentConfig: Ref<CalendarCurrentConfig> = ref({
viewType: 'century'
})
// Get date from URL params // Get date from URL params
const params = useUrlSearchParams('history', { const params = useUrlSearchParams('history', {
removeNullishValues: true removeNullishValues: true
@@ -53,7 +70,7 @@ export const useCalendar = defineStore('calendar', () => {
// Default date settings (current day) // Default date settings (current day)
const defaultDay = String(12) const defaultDay = String(12)
const defaultMonth = String(8) const defaultMonth = String(7)
const defaultYear = String(3209) const defaultYear = String(3209)
// Assign default values if no params exist in URL // Assign default values if no params exist in URL
@@ -85,7 +102,7 @@ export const useCalendar = defineStore('calendar', () => {
}) })
// Create base config // Create base config
const config: CalendarCurrentConfig = { const currentDate: CalendarCurrentDate = {
currentDay, currentDay,
currentMonth, currentMonth,
currentYear, currentYear,
@@ -159,7 +176,8 @@ export const useCalendar = defineStore('calendar', () => {
return { return {
staticConfig, staticConfig,
config, currentConfig,
currentDate,
params, params,
incrementMonth, incrementMonth,
decrementMonth, decrementMonth,

View File

@@ -1,7 +1,7 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import type { LeimDate } from '@/models/Date' import type { LeimDate } from '@/models/Date'
type Character = { export type Character = {
name: string name: string
birth?: LeimDate birth?: LeimDate
death?: LeimDate death?: LeimDate

View File

@@ -1,8 +1,9 @@
import { defineStore } from 'pinia' import { defineStore, storeToRefs } from 'pinia'
import { computed } from 'vue' import { computed, watch, type ComputedRef, toRefs, type Ref, ref } from 'vue'
import { useCharacters } from './characters' import { useCharacters, type Character } from './characters'
import type { LeimDate } from '@/models/Date' import type { LeimDate } from '@/models/Date'
import { useCalendar } from './calendar'
type CalendarEvent = { type CalendarEvent = {
title: string title: string
@@ -11,20 +12,61 @@ type CalendarEvent = {
export const useCalendarEvents = defineStore('calendar-events', () => { export const useCalendarEvents = defineStore('calendar-events', () => {
const { characters } = useCharacters() const { characters } = useCharacters()
const calendarStore = useCalendar()
const charactersWithBirthData: ComputedRef<Character[]> = computed(() =>
characters.filter((character) => character.birth)
)
const characterBirthEvents = computed(() => { const characterBirthEvents = computed(() => {
const charactersWithBirthData = characters.filter((character) => character.birth) return charactersWithBirthData.value.map((character) => {
return charactersWithBirthData.map((character) => {
return { title: `Naissance de ${character.name}`, date: character.birth } as CalendarEvent return { title: `Naissance de ${character.name}`, date: character.birth } as CalendarEvent
}) })
}) })
const charactersWithDeathData: ComputedRef<Character[]> = computed(() =>
characters.filter((character) => character.death)
)
const characterDeathEvents = computed(() => { const characterDeathEvents = computed(() => {
const charactersWithDeathData = characters.filter((character) => character.death) return charactersWithDeathData.value.map((character) => {
return charactersWithDeathData.map((character) => {
return { title: `Décès de ${character.name}`, date: character.death } as CalendarEvent return { title: `Décès de ${character.name}`, date: character.death } as CalendarEvent
}) })
}) })
return { characterBirthEvents, characterDeathEvents } const currentEvents: Ref<CalendarEvent[]> = ref(computeCurrentEvents())
watch(calendarStore.currentDate, () => {
currentEvents.value = computeCurrentEvents()
})
function shouldEventBeDisplayed(event: CalendarEvent): boolean {
switch (calendarStore.currentConfig.viewType) {
case 'month':
return event.date.month === Number(calendarStore.currentDate.currentMonth)
case 'year':
return event.date.year === Number(calendarStore.currentDate.currentYear)
case 'decade':
return (
event.date.year >= Number(calendarStore.currentDate.currentYear) - 10 &&
event.date.year <= Number(calendarStore.currentDate.currentYear) + 10
)
case 'century':
return (
event.date.year >= Number(calendarStore.currentDate.currentYear) - 100 &&
event.date.year <= Number(calendarStore.currentDate.currentYear) + 100
)
default:
return false
}
}
function computeCurrentEvents(): CalendarEvent[] {
const allEvents = [...characterBirthEvents.value, ...characterDeathEvents.value]
return allEvents.filter((event) => shouldEventBeDisplayed(event))
}
return { currentEvents }
}) })

View File

@@ -2,11 +2,12 @@
import { ref } from 'vue' import { ref } from 'vue'
import { useCalendar } from '@/stores/calendar' import { useCalendar } from '@/stores/calendar'
import { useCalendarEvents } from '@/stores/events' import { useCalendarEvents } from '@/stores/events'
import { storeToRefs } from 'pinia'
const { config, incrementMonth, decrementMonth, setMonth, incrementYear, decrementYear } = const { currentDate, incrementMonth, decrementMonth, setMonth, incrementYear, decrementYear } =
useCalendar() useCalendar()
const { characterBirthEvents, characterDeathEvents } = useCalendarEvents() const { currentEvents } = storeToRefs(useCalendarEvents())
const monthTarget = ref(0) const monthTarget = ref(0)
</script> </script>
@@ -14,7 +15,7 @@ const monthTarget = ref(0)
<template> <template>
<main class="container"> <main class="container">
<pre> <pre>
{{ config }} {{ currentDate }}
</pre> </pre>
<div class="grid gap-2"> <div class="grid gap-2">
<div class="flex gap-2"> <div class="flex gap-2">
@@ -50,7 +51,8 @@ const monthTarget = ref(0)
</div> </div>
<pre> <pre>
{{ characterDeathEvents }} Current events:
{{ currentEvents }}
</pre> </pre>
</main> </main>
</template> </template>