diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..90eb1d9 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,75 @@ +kind: pipeline +type: docker +name: default + +steps: + - name: lint frontend + image: node:20-alpine + commands: + - npm install + - npm run lint + + - name: build and push frontend + image: docker:24 + volumes: + - name: dockersock + path: /var/run/docker.sock + commands: + - docker build + -t localhost:5000/eu:latest + -f ./Dockerfile + eu + - docker push localhost:5000/eu:latest + + - name: deploy + image: docker:24 + volumes: + - name: dockersock + path: /var/run/docker.sock + - name: appdir + path: /opt/app + environment: + PUBLIC_URL: + from_secret: PUBLIC_URL + DATABASE_URL: + from_secret: DATABASE_URL + KOTOSOFT_ID_API_URL: + from_secret: KOTOSOFT_ID_API_URL + KOTOSOFT_CLIENT_ID: + from_secret: KOTOSOFT_CLIENT_ID + KOTOSOFT_CLIENT_SECRET: + from_secret: KOTOSOFT_CLIENT_SECRET + JWT_SECRET: + from_secret: JWT_SECRET + MINIO_URL: + from_secret: MINIO_URL + MINIO_ACCESS_KEY: + from_secret: MINIO_ACCESS_KEY + MINIO_SECRET_KEY: + from_secret: MINIO_SECRET_KEY + MINIO_PUBLIC_URL: + from_secret: MINIO_PUBLIC_URL + VIRTUAL_HOST: + from_secret: API_DOMAIN + LETSENCRYPT_HOST: + from_secret: API_DOMAIN + LETSENCRYPT_EMAIL: + from_secret: LETSENCRYPT_EMAIL + commands: + - apk add --no-cache docker-cli-compose + - cp docker-compose.yml /opt/app/docker-compose.yml + - cd /opt/app + - docker compose pull + - docker compose up -d --remove-orphans + +volumes: + - name: dockersock + host: + path: /var/run/docker.sock + - name: appdir + host: + path: /opt/drone/eu + +trigger: + branch: + - main \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7f6710a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM node:20 AS builder +WORKDIR /app +COPY package.json package-lock.json* ./ +RUN npm install --include=dev +COPY . . +RUN npm run build + +FROM node:20 AS production +WORKDIR /app +COPY package.json package-lock.json* ./ +COPY --from=builder /app/node_modules ./node_modules +COPY --from=builder /app/.next ./.next +COPY --from=builder /app/public ./public +COPY --from=builder /app/messages ./messages +RUN npm prune --omit=dev +EXPOSE 80 +CMD ["npm", "start", "--", "-p", "3000", "-H", "0.0.0.0"] +HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ + CMD wget -q --spider http://localhost:3000/ || exit 1 \ No newline at end of file diff --git a/app/[type]/[slug]/archive-button.tsx b/app/[type]/[slug]/archive-button.tsx index a6f4e36..45b1357 100644 --- a/app/[type]/[slug]/archive-button.tsx +++ b/app/[type]/[slug]/archive-button.tsx @@ -1,23 +1,25 @@ "use client"; -import { useTransition } from "react"; import { Button } from "@/components/ui/button"; import { ArchiveIcon } from "lucide-react"; import { Spinner } from "@/components/ui/spinner"; import { archivePageAction } from "@/lib/pages/actions"; import { PageType } from "@/lib/pages/types"; +import { useAction } from "@/hooks/useAction"; export function ArchiveButton({ pageId, slug, type }: { pageId: string, slug: string, type: PageType }) { - const [isPending, startTransition] = useTransition(); + const { run, isPending } = useAction(archivePageAction, { + successMessage: "Page archived", + }); return ( ); -} \ No newline at end of file +} diff --git a/app/[type]/[slug]/edit/client.tsx b/app/[type]/[slug]/edit/client.tsx index 66632f4..c22cd0e 100644 --- a/app/[type]/[slug]/edit/client.tsx +++ b/app/[type]/[slug]/edit/client.tsx @@ -1,14 +1,13 @@ "use client"; -import { useRef, useState, useTransition } from "react"; +import { useRef } from "react"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import Editor, { EditorData } from "@/components/editor"; import { savePageAction } from "@/lib/pages/actions"; -import { validatePage } from "@/lib/pages/validation"; import { Block, Page, PageMeta, PageType } from "@/lib/pages/types"; -import { Loader2 } from "lucide-react"; -import { toast } from "sonner"; +import { Spinner } from "@/components/ui/spinner"; +import { useAction } from "@/hooks/useAction"; type Props = { page: Page | null; @@ -18,8 +17,10 @@ type Props = { export default function EditPageClient({ page, pageType, initialBlocks = [] }: Props) { const router = useRouter(); - const [isPending, startTransition] = useTransition(); - const [fieldErrors, setFieldErrors] = useState<{ title?: string[]; slug?: string[] }>({}); + const { run, isPending } = useAction(savePageAction, { + successMessage: "Page saved", + onSuccess: () => router.push(`/${pageType.toLowerCase()}/${dataRef.current.slug}`), + }); const dataRef = useRef({ title: page?.title ?? "", @@ -31,47 +32,20 @@ export default function EditPageClient({ page, pageType, initialBlocks = [] }: P }); function handleSave(status: "DRAFT" | "PUBLISHED") { - startTransition(async () => { - const data = dataRef.current; - const payload = { - id: page?.id ?? null, - slug: data.slug || data.title.toLowerCase().replace(/\s+/g, "-"), - title: data.title, - type: pageType, - status, - blocks: data.blocks, - meta: { location: data.location, tags: data.tags }, - }; - - const parsed = validatePage(payload); - if (!parsed.success) { - const errors: { title?: string[]; slug?: string[] } = {}; - for (const msg of parsed.errors) { - if (msg.toLowerCase().includes("title")) { - (errors.title ??= []).push(msg); - } else if (msg.toLowerCase().includes("slug")) { - (errors.slug ??= []).push(msg); - } - } - setFieldErrors(errors); - toast.error(parsed.errors.join(", ")); - return; - } - - setFieldErrors({}); - - try { - await savePageAction(payload); - toast.success("Page saved"); - router.push(`/${pageType.toLowerCase()}/${data.slug}`); - } catch (e) { - toast.error(e instanceof Error ? e.message : "Failed to save page"); - } + const data = dataRef.current; + run({ + id: page?.id ?? null, + slug: data.slug || data.title.toLowerCase().replace(/\s+/g, "-"), + title: data.title, + type: pageType, + status, + blocks: data.blocks, + meta: { location: data.location, tags: data.tags }, }); } return ( -
+

Edit page

@@ -79,7 +53,7 @@ export default function EditPageClient({ page, pageType, initialBlocks = [] }: P disabled={isPending} onClick={() => handleSave("PUBLISHED")} > - {isPending && } + {isPending && } Save
@@ -88,12 +62,10 @@ export default function EditPageClient({ page, pageType, initialBlocks = [] }: P page={page} type={pageType} initialBlocks={initialBlocks} - errors={fieldErrors} onChange={(data) => { dataRef.current = data; - setFieldErrors({}); }} />
); -} \ No newline at end of file +} diff --git a/app/[type]/[slug]/edit/page.tsx b/app/[type]/[slug]/edit/page.tsx index 2798a5c..edbacfb 100644 --- a/app/[type]/[slug]/edit/page.tsx +++ b/app/[type]/[slug]/edit/page.tsx @@ -1,5 +1,3 @@ -import Header from "@/components/header"; -import Footer from "@/components/footer"; import { getPageBySlug, getBlocksByPageId } from "@/lib/pages/repository"; import { getCurrentUser } from "@/lib/user"; import { redirect, notFound } from "next/navigation"; @@ -20,10 +18,6 @@ export default async function EditPage({ params }: { params: Promise<{ type: str const blocks = page ? await getBlocksByPageId(page.id) : []; return ( - <> -
- -