From 31a43161861438880c41465f325a88f7062d2850 Mon Sep 17 00:00:00 2001
From: Alexis <35.alexis.pele@gmail.com>
Date: Thu, 6 Mar 2025 20:34:59 +0100
Subject: [PATCH] Commit stuff in case my computer explodes
---
components/calendar/PreviewCard.vue | 21 ++--
components/calendar/dialog/Update.vue | 49 +++++++++
components/calendar/form/Create.vue | 6 +-
components/calendar/form/Update.vue | 145 ++++++++++++++++++++++++++
i18n.config.ts | 15 ++-
models/CalendarConfig.ts | 1 +
pages/my/worlds/[id].vue | 33 ++++--
server/api/calendars/[id].patch.ts | 78 ++++++++++++++
server/api/calendars/query.get.ts | 8 +-
supabase/migrations/202401_init.sql | 5 +-
10 files changed, 333 insertions(+), 28 deletions(-)
create mode 100644 components/calendar/dialog/Update.vue
create mode 100644 components/calendar/form/Update.vue
create mode 100644 server/api/calendars/[id].patch.ts
diff --git a/components/calendar/PreviewCard.vue b/components/calendar/PreviewCard.vue
index 04887a6..4c87e96 100644
--- a/components/calendar/PreviewCard.vue
+++ b/components/calendar/PreviewCard.vue
@@ -1,5 +1,5 @@
+
+
+
+ e.preventDefault()">
+
+
+ {{ calendar?.data.world?.name }}
+ —
+
+ {{ calendarSkeletonName }}
+
+
+ {{ $t('entity.calendar.updateDialog.title') }}
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/components/calendar/form/Create.vue b/components/calendar/form/Create.vue
index e6e3c51..fa7c625 100644
--- a/components/calendar/form/Create.vue
+++ b/components/calendar/form/Create.vue
@@ -79,21 +79,21 @@ function handleFormCancel() {
- {{ $t('entity.calendar.createDialog.tabs.general.title') }}
+ {{ $t('entity.calendar.actionDialog.tabs.general.title') }}
- {{ $t('entity.calendar.createDialog.tabs.months.title') }}
+ {{ $t('entity.calendar.actionDialog.tabs.months.title') }}
- {{ $t('entity.calendar.createDialog.tabs.today.title') }}
+ {{ $t('entity.calendar.actionDialog.tabs.today.title') }}
diff --git a/components/calendar/form/Update.vue b/components/calendar/form/Update.vue
new file mode 100644
index 0000000..a0d9ebd
--- /dev/null
+++ b/components/calendar/form/Update.vue
@@ -0,0 +1,145 @@
+
+
+
+
+
+
+
diff --git a/i18n.config.ts b/i18n.config.ts
index 40c41ae..8ee5b1e 100644
--- a/i18n.config.ts
+++ b/i18n.config.ts
@@ -197,8 +197,7 @@ export default defineI18nConfig(() => ({
title: "The event \"{event}\" has been successfuly deleted.",
},
},
- createDialog: {
- title: "Create a calendar",
+ actionDialog: {
tabs: {
general: {
title: "General",
@@ -211,6 +210,9 @@ export default defineI18nConfig(() => ({
},
}
},
+ createDialog: {
+ title: "Create a calendar",
+ },
deleteDialog: {
title: "Are you sure you want to delete this calendar ?",
subtitle: "Its events won't be accessible anymore and you won't be able to retrieve the deleted data !",
@@ -460,8 +462,7 @@ export default defineI18nConfig(() => ({
title: "L'évènement \"{event}\" a été supprimé avec succès.",
},
},
- createDialog: {
- title: "Créer un calendrier",
+ actionDialog: {
tabs: {
general: {
title: "Général",
@@ -474,6 +475,12 @@ export default defineI18nConfig(() => ({
},
}
},
+ createDialog: {
+ title: "Créer un calendrier",
+ },
+ updateDialog: {
+ title: "Modifier le calendrier",
+ },
deleteDialog: {
title: "Êtes-vous sûr de supprimer ce calendrier ?",
subtitle: "Les évènements ne seront plus accessibles et vous ne pourrez plus récupérer les données !",
diff --git a/models/CalendarConfig.ts b/models/CalendarConfig.ts
index ca0971b..159a4f2 100644
--- a/models/CalendarConfig.ts
+++ b/models/CalendarConfig.ts
@@ -34,4 +34,5 @@ export const postCalendarSchema = z.object({
color: z.string().optional().nullable(),
months: z.array(calendarMonthSchema).min(1),
worldId: z.number().int(),
+ state: z.string().optional().nullable().default("draft"),
})
diff --git a/pages/my/worlds/[id].vue b/pages/my/worlds/[id].vue
index 275b426..c7a2eca 100644
--- a/pages/my/worlds/[id].vue
+++ b/pages/my/worlds/[id].vue
@@ -24,12 +24,6 @@ watch(user, (n) => {
}
})
-const isCreateCalendarModalOpen = ref(false)
-
-function hideCreateDialog() {
- isCreateCalendarModalOpen.value = false
-}
-
/**
* === Subscriptions ===
*/
@@ -120,14 +114,28 @@ onUnmounted(() => {
})
const markedCalendar = ref(null)
-const isDeleteCalendarModalOpen = ref(false)
const isEditWorldModalOpen = ref(false)
+const isCreateCalendarModalOpen = ref(false)
+const isUpdateCalendarModalOpen = ref(false)
+const isDeleteCalendarModalOpen = ref(false)
+
+function hideCreateDialog() {
+ isCreateCalendarModalOpen.value = false
+}
+
+function deployUpdateDialog(calendar: Calendar) {
+ markedCalendar.value = calendar
+ isUpdateCalendarModalOpen.value = true
+}
+function hideUpdateDialog() {
+ isUpdateCalendarModalOpen.value = false
+}
+
function deployDeleteCalendarModal(calendar: Calendar) {
isDeleteCalendarModalOpen.value = true
markedCalendar.value = calendar
}
-
function hideDeleteCalendarModal() {
isDeleteCalendarModalOpen.value = false
markedCalendar.value = null
@@ -136,7 +144,6 @@ function hideDeleteCalendarModal() {
function deployEditModal() {
isEditWorldModalOpen.value = true
}
-
function hideEditModal() {
isEditWorldModalOpen.value = false
}
@@ -193,7 +200,12 @@ function hideEditModal() {
-
- deployDeleteCalendarModal(calendar)" />
+ deployUpdateDialog(calendar)"
+ @on-delete="() => deployDeleteCalendarModal(calendar)" />
-
@@ -212,6 +224,7 @@ function hideEditModal() {
+
diff --git a/server/api/calendars/[id].patch.ts b/server/api/calendars/[id].patch.ts
new file mode 100644
index 0000000..2aab55d
--- /dev/null
+++ b/server/api/calendars/[id].patch.ts
@@ -0,0 +1,78 @@
+import { z } from "zod"
+import { serverSupabaseClient } from "#supabase/server"
+import type { Calendar} from "~/models/CalendarConfig";
+import { postCalendarSchema } from "~/models/CalendarConfig"
+
+const paramsSchema = z.object({
+ id: z.number({ coerce: true }).positive().int()
+})
+
+export default defineEventHandler(async (event) => {
+ const client = await serverSupabaseClient(event)
+
+ const { data: params, error: paramsError} = await getValidatedRouterParams(event, paramsSchema.safeParse)
+ const { data: bodyData, error: bodyError } = await readValidatedBody(event, body => postCalendarSchema.safeParse(body))
+
+ if (paramsError) {
+ throw createError({
+ cause: "Utilisateur",
+ fatal: false,
+ message: "L'identifiant du calendrier est manquant ou mal renseigné.",
+ status: 401,
+ })
+ }
+
+ if (bodyError) {
+ const error = createError({
+ cause: "Utilisateur",
+ fatal: false,
+ statusCode: 401,
+ statusMessage: "Validation Error",
+ message: "Erreur de validation du schéma, probablement dûe à une erreur utilisateur.",
+ data: {
+ errors: bodyError.issues.map(issue => ({
+ path: issue.path,
+ message: issue.message,
+ code: issue.code
+ }))
+ }
+ })
+
+ throw error
+ }
+
+ try {
+ const { data, error } = await client
+ .from("calendars")
+ .update(
+ {
+ name: bodyData.name,
+ world_id: bodyData.worldId,
+ today: bodyData.today,
+ state: bodyData.state
+ } as never
+ )
+ .eq("id", params.id)
+ .select(`
+ id,
+ name,
+ color,
+ state,
+ createdAt:created_at,
+ updatedAt:updated_at
+ `)
+ .single()
+
+ if (error) throw error
+
+ return data
+ } catch (err) {
+ console.log(err)
+ throw createError({
+ cause: "Serveur",
+ status: 500,
+ fatal: false,
+ message: "Une erreur inconnue est survenue."
+ })
+ }
+})
diff --git a/server/api/calendars/query.get.ts b/server/api/calendars/query.get.ts
index 9430ec8..9a78d3d 100644
--- a/server/api/calendars/query.get.ts
+++ b/server/api/calendars/query.get.ts
@@ -21,7 +21,12 @@ export default defineEventHandler(async (event) => {
state,
createdAt:created_at,
updatedAt:updated_at,
- eventNb:calendar_events(count)
+ eventNb:calendar_events(count),
+ world:worlds (
+ id,
+ name,
+ gmId:gm_id
+ )
`
const fullFields = `
@@ -48,6 +53,7 @@ export default defineEventHandler(async (event) => {
eventNb:calendar_events(count),
world:worlds (
id,
+ name,
gmId:gm_id
)
`
diff --git a/supabase/migrations/202401_init.sql b/supabase/migrations/202401_init.sql
index 479a716..867c32a 100644
--- a/supabase/migrations/202401_init.sql
+++ b/supabase/migrations/202401_init.sql
@@ -242,22 +242,25 @@ create policy "Allow GMs to see their calendars" on public.calendars for select
and worlds.gm_id = auth.uid()
)
);
-create policy "Allow GMs to add calendars to their worldd" on public.calendars for insert with check (
+create policy "Allow GMs to add calendars to their world" on public.calendars for insert with check (
exists (
select 1 from worlds
where worlds.id = calendars.world_id
+ and worlds.gm_id = auth.uid()
)
);
create policy "Allow GMs to edit their calendars" on public.calendars for update with check (
exists (
select 1 from worlds
where worlds.id = calendars.world_id
+ and worlds.gm_id = auth.uid()
)
);
create policy "Allow GMs to delete their calendars" on public.calendars for delete using (
exists (
select 1 from worlds
where worlds.id = calendars.world_id
+ and worlds.gm_id = auth.uid()
)
);