diff --git a/app/[slug]/dashboard/website/page.tsx b/app/[slug]/dashboard/website/page.tsx index f19d4a4..b298101 100644 --- a/app/[slug]/dashboard/website/page.tsx +++ b/app/[slug]/dashboard/website/page.tsx @@ -2,15 +2,34 @@ 'use client' -import Tiptap from '../../../components/Tiptap' +import { AbstractApi } from '@/app/api/abstract-api'; +import { Website, WebsiteSerializer } from '@/app/lib/website'; +import { useEffect, useState } from 'react'; +import Tiptap from '../../../components/Tiptap'; export default function Page() { + const [website, setWebsite] = useState() + const api = new AbstractApi(); + const serializer = new WebsiteSerializer(); + + useEffect(() => { + api.get(serializer, undefined, (loadedWebsite) => { + setWebsite(loadedWebsite); + console.log('Website loaded:', loadedWebsite); + }); + }, []); + + const updateWebsite = (newContent: string) => { + api.update(serializer, new Website('', newContent), () => { }); + } + return (
console.log(newContent)} + key={website?.content ?? 'empty'} + content={website?.content || ''} + onUpdate={updateWebsite} />
); diff --git a/app/api/abstract-api.tsx b/app/api/abstract-api.tsx index 19771a7..a41cc2f 100644 --- a/app/api/abstract-api.tsx +++ b/app/api/abstract-api.tsx @@ -6,6 +6,7 @@ import { getCsrfToken, getSlug } from '@/app/lib/utils'; export interface Api { getAll(serializable: Serializable, callback: (objets: T[]) => void): void; get(serializable: Serializable, id: string, callback: (object: T) => void): void; + getSingleton(serializable: Serializable, callback: (object: T) => void): void; create(serializable: Serializable, object: T, callback: (object: T) => void): void; update(serializable: Serializable, object: T, callback: () => void): void; destroy(serializable: Serializable, object: T, callback: () => void): void; @@ -30,8 +31,9 @@ export class AbstractApi implements Api { }); } - get(serializable: Serializable, id: string, callback: (object: T) => void): void { - fetch(`/api/${getSlug()}/${serializable.apiPath()}/${id}`) + get(serializable: Serializable, id: (string | undefined), callback: (object: T) => void): void { + const endpoint = id ? `/api/${getSlug()}/${serializable.apiPath()}/${id}` : `/api/${getSlug()}/${serializable.apiPath()}`; + fetch(endpoint) .then((response) => response.json()) .then((data) => { callback(serializable.fromJson(data)); @@ -41,7 +43,9 @@ export class AbstractApi implements Api { } update(serializable: Serializable, object: T, callback: () => void): void { - fetch(`/api/${getSlug()}/${serializable.apiPath()}/${object.id}`, { + const endpoint = object.id ? `/api/${getSlug()}/${serializable.apiPath()}/${object.id}` : `/api/${getSlug()}/${serializable.apiPath()}`; + + fetch(endpoint, { method: 'PUT', body: serializable.toJson(object), headers: { diff --git a/app/components/Tiptap.tsx b/app/components/Tiptap.tsx index 1b4f4e2..ddae1c7 100644 --- a/app/components/Tiptap.tsx +++ b/app/components/Tiptap.tsx @@ -13,6 +13,7 @@ const Tiptap = ({ content, onUpdate }: { content: string, onUpdate: (newContent: onUpdate({ editor }) { onUpdate(editor.getHTML()); }, + immediatelyRender: false, }) return diff --git a/app/lib/website.tsx b/app/lib/website.tsx new file mode 100644 index 0000000..0d72765 --- /dev/null +++ b/app/lib/website.tsx @@ -0,0 +1,30 @@ +/* Copyright (C) 2024-2025 LibreWeddingPlanner contributors*/ + +import { Serializable } from "../api/abstract-api"; +import { Entity } from "./definitions"; + + +export class Website implements Entity { + id?: string; + content?: string; + + constructor(id: string, content: string) { + this.id = id; + this.content = content; + } +} + +export class WebsiteSerializer implements Serializable { + fromJson(data: any): Website { + return new Website("", data.content); + } + + toJson(website: Website): string { + return JSON.stringify({ website: { content: website.content } }); + } + + apiPath(): string { + return 'website'; + } +} +