249 lines
7.7 KiB
Vue
249 lines
7.7 KiB
Vue
<script setup lang="ts">
|
|
/**
|
|
* The personal calendars and the deadlines of the courses and associations in a month
|
|
* calendar, then everything left to do. Each source can be hidden. Events (personal
|
|
* calendars) and tasks (courses, associations) are created apart: they are not the same.
|
|
*/
|
|
import { computed, ref, watch } from 'vue'
|
|
import { BookOpen, CalendarPlus, ListTodo, Pencil, Plus, Users } from '@lucide/vue'
|
|
|
|
import CalendarFormDialog from '@/components/calendar/CalendarFormDialog.vue'
|
|
import EventFormDialog from '@/components/calendar/EventFormDialog.vue'
|
|
import MonthCalendar from '@/components/calendar/MonthCalendar.vue'
|
|
import TodoList from '@/components/calendar/TodoList.vue'
|
|
import TaskFormDialog from '@/components/course/TaskFormDialog.vue'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
|
import { useCalendars } from '@/services/api/calendars'
|
|
import type { Calendar, CalendarEvent, CourseKind } from '@/utils/types'
|
|
|
|
const KINDS: { kind: CourseKind; icon: typeof BookOpen }[] = [
|
|
{ kind: 'course', icon: BookOpen },
|
|
{ kind: 'association', icon: Users },
|
|
]
|
|
|
|
// Remembered in the browser: only a convenience. The hidden calendars are stored rather
|
|
// than the shown ones, so a new calendar is shown.
|
|
const STORAGE_KEY = 'calendar-kinds'
|
|
const HIDDEN_KEY = 'calendar-hidden'
|
|
|
|
function load<T>(key: string, parse: (value: unknown) => T | undefined, fallback: T): T {
|
|
try {
|
|
return parse(JSON.parse(window.localStorage.getItem(key) ?? 'null')) ?? fallback
|
|
} catch {
|
|
// Unavailable or invalid: everything is shown
|
|
return fallback
|
|
}
|
|
}
|
|
|
|
function remember(key: string, value: unknown) {
|
|
try {
|
|
window.localStorage.setItem(key, JSON.stringify(value))
|
|
} catch {
|
|
// Not remembered
|
|
}
|
|
}
|
|
|
|
const kinds = ref<CourseKind[]>(
|
|
load(
|
|
STORAGE_KEY,
|
|
(saved) =>
|
|
Array.isArray(saved)
|
|
? saved.filter((k): k is CourseKind => k === 'course' || k === 'association')
|
|
: undefined,
|
|
['course', 'association'],
|
|
),
|
|
)
|
|
const hiddenCalendars = ref<number[]>(
|
|
load(
|
|
HIDDEN_KEY,
|
|
(saved) =>
|
|
Array.isArray(saved) ? saved.filter((id): id is number => typeof id === 'number') : undefined,
|
|
[],
|
|
),
|
|
)
|
|
watch(kinds, (value) => remember(STORAGE_KEY, value))
|
|
watch(hiddenCalendars, (value) => remember(HIDDEN_KEY, value))
|
|
|
|
function toggleKind(kind: CourseKind) {
|
|
kinds.value = kinds.value.includes(kind)
|
|
? kinds.value.filter((k) => k !== kind)
|
|
: [...kinds.value, kind]
|
|
}
|
|
|
|
function toggleCalendar(id: number) {
|
|
hiddenCalendars.value = hiddenCalendars.value.includes(id)
|
|
? hiddenCalendars.value.filter((hidden) => hidden !== id)
|
|
: [...hiddenCalendars.value, id]
|
|
}
|
|
|
|
const { data: calendars } = useCalendars()
|
|
|
|
// Tasks
|
|
const taskFormOpen = ref(false)
|
|
const defaultDeadline = ref<string>()
|
|
|
|
/** New task, due at the end of `day` when given */
|
|
function newTask(day?: Date) {
|
|
if (day) {
|
|
const end = new Date(day)
|
|
end.setHours(23, 59, 0, 0)
|
|
defaultDeadline.value = end.toISOString()
|
|
} else {
|
|
defaultDeadline.value = undefined
|
|
}
|
|
taskFormOpen.value = true
|
|
}
|
|
|
|
// Events
|
|
const eventFormOpen = ref(false)
|
|
const editedEvent = ref<CalendarEvent>()
|
|
const eventDay = ref<Date>()
|
|
|
|
function newEvent(day?: Date) {
|
|
editedEvent.value = undefined
|
|
eventDay.value = day
|
|
eventFormOpen.value = true
|
|
}
|
|
|
|
function editEvent(event: CalendarEvent) {
|
|
editedEvent.value = event
|
|
eventFormOpen.value = true
|
|
}
|
|
|
|
// Calendars
|
|
const calendarFormOpen = ref(false)
|
|
const editedCalendar = ref<Calendar>()
|
|
|
|
function openCalendarForm(calendar?: Calendar) {
|
|
editedCalendar.value = calendar
|
|
calendarFormOpen.value = true
|
|
}
|
|
|
|
const nothingShown = computed(
|
|
() =>
|
|
!kinds.value.length &&
|
|
(calendars.value ?? []).every((calendar) => hiddenCalendars.value.includes(calendar.id)),
|
|
)
|
|
|
|
const chip =
|
|
'inline-flex items-center gap-2 rounded-full border px-3 py-1.5 text-sm transition-colors'
|
|
</script>
|
|
|
|
<template>
|
|
<div class="mx-auto flex max-w-5xl flex-col gap-4">
|
|
<div class="flex flex-wrap items-center gap-2">
|
|
<div class="flex flex-wrap gap-2" role="group" :aria-label="$t('calendar.show')">
|
|
<button
|
|
v-for="{ kind, icon } in KINDS"
|
|
:key="kind"
|
|
type="button"
|
|
:class="[
|
|
chip,
|
|
kinds.includes(kind)
|
|
? 'border-primary bg-primary/10 font-medium text-primary'
|
|
: 'text-muted-foreground hover:bg-accent',
|
|
]"
|
|
:aria-pressed="kinds.includes(kind)"
|
|
@click="toggleKind(kind)"
|
|
>
|
|
<component :is="icon" class="size-4" />
|
|
{{ $t(`nav.${kind}s`) }}
|
|
</button>
|
|
|
|
<!-- Personal calendars: shown or hidden, and edited with the pencil -->
|
|
<span
|
|
v-for="calendar in calendars"
|
|
:key="calendar.id"
|
|
class="group inline-flex items-center rounded-full border transition-colors"
|
|
:class="
|
|
hiddenCalendars.includes(calendar.id)
|
|
? 'text-muted-foreground hover:bg-accent'
|
|
: 'font-medium'
|
|
"
|
|
:style="
|
|
hiddenCalendars.includes(calendar.id)
|
|
? undefined
|
|
: {
|
|
borderColor: calendar.color,
|
|
color: calendar.color,
|
|
backgroundColor: `color-mix(in oklab, ${calendar.color} 10%, transparent)`,
|
|
}
|
|
"
|
|
>
|
|
<button
|
|
type="button"
|
|
class="inline-flex items-center gap-2 py-1.5 pr-1 pl-3 text-sm"
|
|
:aria-pressed="!hiddenCalendars.includes(calendar.id)"
|
|
@click="toggleCalendar(calendar.id)"
|
|
>
|
|
<span
|
|
class="size-2.5 rounded-full border-2"
|
|
:style="{
|
|
borderColor: calendar.color,
|
|
backgroundColor: hiddenCalendars.includes(calendar.id)
|
|
? 'transparent'
|
|
: calendar.color,
|
|
}"
|
|
/>
|
|
{{ calendar.name }}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
class="mr-1 rounded-full p-1 opacity-50 transition-opacity group-hover:opacity-100 hover:bg-foreground/10 focus-visible:opacity-100"
|
|
:title="$t('calendars.edit')"
|
|
@click="openCalendarForm(calendar)"
|
|
>
|
|
<Pencil class="size-3" />
|
|
<span class="sr-only">{{ $t('calendars.edit') }}</span>
|
|
</button>
|
|
</span>
|
|
|
|
<button
|
|
type="button"
|
|
:class="[chip, 'border-dashed text-muted-foreground hover:bg-accent']"
|
|
@click="openCalendarForm()"
|
|
>
|
|
<Plus class="size-4" />
|
|
{{ $t('calendars.new') }}
|
|
</button>
|
|
</div>
|
|
|
|
<div class="ml-auto flex gap-2">
|
|
<Button variant="outline" @click="newTask()">
|
|
<ListTodo />
|
|
{{ $t('tasks.new') }}
|
|
</Button>
|
|
<Button @click="newEvent()">
|
|
<CalendarPlus />
|
|
{{ $t('events.new') }}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
<p v-if="nothingShown" class="text-sm text-muted-foreground">
|
|
{{ $t('calendar.nothing-shown') }}
|
|
</p>
|
|
|
|
<MonthCalendar
|
|
:kinds="kinds"
|
|
:hidden-calendars="hiddenCalendars"
|
|
@create-event="newEvent"
|
|
@create-task="newTask"
|
|
@edit-event="editEvent"
|
|
/>
|
|
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle class="text-base">{{ $t('calendar.todo.title') }}</CardTitle>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<TodoList :kinds="kinds" />
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<TaskFormDialog v-model:open="taskFormOpen" :default-deadline="defaultDeadline" />
|
|
<EventFormDialog v-model:open="eventFormOpen" :event="editedEvent" :default-day="eventDay" />
|
|
<CalendarFormDialog v-model:open="calendarFormOpen" :calendar="editedCalendar" />
|
|
</div>
|
|
</template>
|