openadminjs 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/create-project.js +38 -6
- package/dist/create-project.test.js +69 -24
- package/dist/index.js +16 -5
- package/package.json +4 -2
- package/template/.env.example +2 -2
- package/template/README.md +24 -3
- package/template/apps/admin/app/api/auth/login/route.ts +51 -0
- package/template/apps/admin/app/api/auth/logout/route.ts +11 -0
- package/template/apps/admin/app/api/auth/refresh/route.ts +46 -0
- package/template/apps/admin/app/api-tokens/page.tsx +253 -0
- package/template/apps/admin/app/audit-logs/page.tsx +5 -0
- package/template/apps/admin/app/dashboard/page.tsx +392 -0
- package/template/apps/admin/app/files/page.tsx +5 -0
- package/template/apps/admin/app/globals.css +206 -0
- package/template/apps/admin/app/jobs/page.tsx +19 -0
- package/template/apps/admin/app/layout.tsx +57 -0
- package/template/apps/admin/app/login/page.test.tsx +18 -0
- package/template/apps/admin/app/login/page.tsx +161 -0
- package/template/apps/admin/app/login/safe-next-param.ts +4 -0
- package/template/apps/admin/app/notifications/page.tsx +146 -0
- package/template/apps/admin/app/page.tsx +5 -0
- package/template/apps/admin/app/plugins/page.tsx +407 -0
- package/template/apps/admin/app/profile/page.tsx +116 -0
- package/template/apps/admin/app/resources/[resource]/[id]/edit/page.tsx +45 -0
- package/template/apps/admin/app/resources/[resource]/[id]/page.tsx +449 -0
- package/template/apps/admin/app/resources/[resource]/create/page.tsx +34 -0
- package/template/apps/admin/app/resources/[resource]/page.tsx +100 -0
- package/template/apps/admin/app/settings/page.tsx +233 -0
- package/template/apps/admin/components/ai-assistant-widget.tsx +684 -0
- package/template/apps/admin/components/app-shell.tsx +305 -0
- package/template/apps/admin/components/auth-guard.test.tsx +55 -0
- package/template/apps/admin/components/auth-guard.tsx +39 -0
- package/template/apps/admin/components/jobs-panel.tsx +103 -0
- package/template/apps/admin/components/logo.tsx +30 -0
- package/template/apps/admin/components/resource-form.tsx +349 -0
- package/template/apps/admin/components/resource-table.tsx +277 -0
- package/template/apps/admin/lib/ai-events.ts +23 -0
- package/template/apps/admin/lib/api.test.ts +118 -0
- package/template/apps/admin/lib/api.ts +135 -0
- package/template/apps/admin/lib/auth-flow.e2e.test.ts +79 -0
- package/template/apps/admin/lib/brand.ts +14 -0
- package/template/apps/admin/lib/locale.ts +49 -0
- package/template/apps/admin/lib/tokens.ts +93 -0
- package/template/apps/admin/middleware.ts +58 -0
- package/template/apps/admin/next-env.d.ts +6 -0
- package/template/apps/admin/next.config.mjs +7 -0
- package/template/apps/admin/package.json +44 -0
- package/template/apps/admin/postcss.config.mjs +6 -0
- package/template/apps/admin/public/brand/apple-touch-icon.png +0 -0
- package/template/apps/admin/public/brand/favicon.svg +1 -0
- package/template/apps/admin/public/brand/og-image.png +0 -0
- package/template/apps/admin/public/brand/openadminjs-icon-new.png +0 -0
- package/template/apps/admin/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/template/apps/admin/public/brand/openadminjs-logo-new.png +0 -0
- package/template/apps/admin/tailwind.config.ts +37 -0
- package/template/apps/admin/tsconfig.json +23 -0
- package/template/apps/admin/vitest.config.ts +7 -0
- package/template/apps/api/package.json +56 -0
- package/template/apps/api/plugins.manifest.example.json +34 -0
- package/template/apps/api/plugins.manifest.json +4 -0
- package/template/apps/api/src/access/catalog.ts +112 -0
- package/template/apps/api/src/access/derive-permissions.test.ts +26 -0
- package/template/apps/api/src/access/derive-permissions.ts +36 -0
- package/template/apps/api/src/access/public-catalog.ts +28 -0
- package/template/apps/api/src/admin/admin-ai.controller.ts +125 -0
- package/template/apps/api/src/admin/admin-ai.events.ts +23 -0
- package/template/apps/api/src/admin/admin-ai.service.ts +1271 -0
- package/template/apps/api/src/admin/admin-analytics.service.ts +164 -0
- package/template/apps/api/src/admin/admin-plugins.controller.ts +32 -0
- package/template/apps/api/src/admin/admin-plugins.service.ts +267 -0
- package/template/apps/api/src/admin/admin-resource.service.test.ts +118 -0
- package/template/apps/api/src/admin/admin-resource.service.ts +375 -0
- package/template/apps/api/src/admin/admin.controller.ts +72 -0
- package/template/apps/api/src/admin/admin.module.ts +30 -0
- package/template/apps/api/src/admin/dto/plugins.dto.ts +55 -0
- package/template/apps/api/src/admin/validate-resource-payload.test.ts +44 -0
- package/template/apps/api/src/admin/validate-resource-payload.ts +103 -0
- package/template/apps/api/src/app.module.ts +45 -0
- package/template/apps/api/src/auth/auth.controller.ts +70 -0
- package/template/apps/api/src/auth/auth.dto.ts +49 -0
- package/template/apps/api/src/auth/auth.module.ts +30 -0
- package/template/apps/api/src/auth/auth.service.test.ts +75 -0
- package/template/apps/api/src/auth/auth.service.ts +231 -0
- package/template/apps/api/src/common/auth-realm.decorator.ts +6 -0
- package/template/apps/api/src/common/auth.guard.test.ts +61 -0
- package/template/apps/api/src/common/auth.guard.ts +78 -0
- package/template/apps/api/src/common/casl-ability.factory.test.ts +33 -0
- package/template/apps/api/src/common/casl-ability.factory.ts +41 -0
- package/template/apps/api/src/common/current-user.decorator.ts +6 -0
- package/template/apps/api/src/common/permission.decorator.ts +7 -0
- package/template/apps/api/src/common/permissions.guard.ts +31 -0
- package/template/apps/api/src/common/prisma.service.ts +13 -0
- package/template/apps/api/src/health/health.controller.ts +25 -0
- package/template/apps/api/src/mail/mail.module.ts +9 -0
- package/template/apps/api/src/mail/mail.service.ts +115 -0
- package/template/apps/api/src/main.ts +37 -0
- package/template/apps/api/src/plugins/bootstrap.ts +4 -0
- package/template/apps/api/src/plugins/bundled/admin-widget.plugin.ts +25 -0
- package/template/apps/api/src/plugins/bundled/hello.plugin.ts +19 -0
- package/template/apps/api/src/plugins/bundled/image-optimizer.plugin.ts +22 -0
- package/template/apps/api/src/plugins/bundled/registry.ts +13 -0
- package/template/apps/api/src/plugins/bundled/seo.plugin.ts +25 -0
- package/template/apps/api/src/plugins/manifest-path.ts +35 -0
- package/template/apps/api/src/plugins/manifest.schema.test.ts +36 -0
- package/template/apps/api/src/plugins/manifest.schema.ts +58 -0
- package/template/apps/api/src/plugins/plugin-api.interceptor.ts +47 -0
- package/template/apps/api/src/plugins/plugin-loader.ts +134 -0
- package/template/apps/api/src/plugins/plugin-runtime.test.ts +24 -0
- package/template/apps/api/src/plugins/plugin-runtime.ts +85 -0
- package/template/apps/api/src/queue/queue.controller.ts +28 -0
- package/template/apps/api/src/queue/queue.module.ts +23 -0
- package/template/apps/api/src/queue/queue.processor.ts +47 -0
- package/template/apps/api/src/queue/queue.service.ts +50 -0
- package/template/apps/api/src/resources/categories.resource.ts +21 -0
- package/template/apps/api/src/resources/commerce.resource.ts +157 -0
- package/template/apps/api/src/resources/operations.resource.ts +111 -0
- package/template/apps/api/src/resources/posts.resource.ts +88 -0
- package/template/apps/api/src/resources/registry.ts +27 -0
- package/template/apps/api/src/resources/resource-hooks.install.ts +18 -0
- package/template/apps/api/src/resources/resource-hooks.registry.test.ts +72 -0
- package/template/apps/api/src/resources/resource-hooks.registry.ts +66 -0
- package/template/apps/api/src/resources/resource-hooks.runner.ts +93 -0
- package/template/apps/api/src/resources/system.resource.ts +51 -0
- package/template/apps/api/src/resources/users.resource.ts +19 -0
- package/template/apps/api/src/store/payment/payment.controller.ts +98 -0
- package/template/apps/api/src/store/payment/payment.module.ts +14 -0
- package/template/apps/api/src/store/payment/payment.provider.ts +38 -0
- package/template/apps/api/src/store/payment/payment.service.ts +235 -0
- package/template/apps/api/src/store/store.controller.ts +26 -0
- package/template/apps/api/src/store/store.module.ts +12 -0
- package/template/apps/api/src/test-setup.ts +1 -0
- package/template/apps/api/tsconfig.json +11 -0
- package/template/apps/api/vitest.config.ts +21 -0
- package/template/apps/web/lib/openadmin-client.ts +11 -0
- package/template/apps/web/lib/seo.test.ts +41 -0
- package/template/apps/web/lib/seo.ts +24 -13
- package/template/apps/web/next-env.d.ts +4 -0
- package/template/apps/web/package.json +2 -1
- package/template/apps/web/test/next-stub.ts +7 -0
- package/template/apps/web/vitest.config.ts +19 -0
- package/template/package.json +18 -11
- package/template/packages/auth/package.json +17 -0
- package/template/packages/auth/src/index.test.ts +10 -0
- package/template/packages/auth/src/index.ts +17 -0
- package/template/packages/auth/tsconfig.json +8 -0
- package/template/packages/brand/apps/admin/public/brand/apple-touch-icon.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/favicon.svg +1 -0
- package/template/packages/brand/apps/admin/public/brand/og-image.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/openadminjs-icon-new.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/openadminjs-logo-new.png +0 -0
- package/template/packages/brand/apps/web/public/brand/apple-touch-icon.png +0 -0
- package/template/packages/brand/apps/web/public/brand/favicon.svg +1 -0
- package/template/packages/brand/apps/web/public/brand/og-image.png +0 -0
- package/template/packages/brand/apps/web/public/brand/openadminjs-icon-new.png +0 -0
- package/template/packages/brand/apps/web/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/template/packages/brand/apps/web/public/brand/openadminjs-logo-new.png +0 -0
- package/template/packages/brand/package.json +19 -0
- package/template/packages/brand/site/assets/brand/favicon.svg +1 -0
- package/template/packages/brand/site/assets/brand/og-image.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-icon.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo-dark.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo-horizontal.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo-monochrome.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo.svg +1 -0
- package/template/packages/brand/src/generate-assets.mjs +182 -0
- package/template/packages/brand/src/index.test.ts +9 -0
- package/template/packages/brand/src/index.ts +7 -0
- package/template/packages/brand/tsconfig.json +7 -0
- package/template/packages/core/package.json +20 -0
- package/template/packages/core/src/index.js +1 -0
- package/template/packages/core/src/index.test.ts +27 -0
- package/template/packages/core/src/index.ts +61 -0
- package/template/packages/core/tsconfig.json +8 -0
- package/template/packages/mail/package.json +18 -0
- package/template/packages/mail/src/index.test.ts +23 -0
- package/template/packages/mail/src/index.ts +246 -0
- package/template/packages/mail/tsconfig.json +5 -0
- package/template/packages/notifications/package.json +16 -0
- package/template/packages/notifications/src/index.test.ts +9 -0
- package/template/packages/notifications/src/index.ts +5 -0
- package/template/packages/notifications/tsconfig.json +5 -0
- package/template/packages/payments/package.json +15 -0
- package/template/packages/payments/src/index.ts +256 -0
- package/template/packages/payments/tsconfig.json +9 -0
- package/template/packages/permissions/package.json +17 -0
- package/template/packages/permissions/src/blueprints.js +57 -0
- package/template/packages/permissions/src/blueprints.test.ts +62 -0
- package/template/packages/permissions/src/blueprints.ts +89 -0
- package/template/packages/permissions/src/index.js +11 -0
- package/template/packages/permissions/src/index.test.ts +34 -0
- package/template/packages/permissions/src/index.ts +22 -0
- package/template/packages/permissions/tsconfig.json +8 -0
- package/template/packages/plugin-sdk/package.json +26 -0
- package/template/packages/plugin-sdk/src/hooks.ts +135 -0
- package/template/packages/plugin-sdk/src/index.ts +42 -0
- package/template/packages/plugin-sdk/src/sdk.test.ts +13 -0
- package/template/packages/plugin-sdk/tsconfig.json +8 -0
- package/template/packages/plugin-sdk/vitest.config.ts +5 -0
- package/template/packages/queue/package.json +16 -0
- package/template/packages/queue/src/index.test.ts +12 -0
- package/template/packages/queue/src/index.ts +6 -0
- package/template/packages/queue/tsconfig.json +5 -0
- package/template/packages/resource/package.json +17 -0
- package/template/packages/resource/src/index.js +177 -0
- package/template/packages/resource/src/index.test.ts +151 -0
- package/template/packages/resource/src/index.ts +344 -0
- package/template/packages/resource/tsconfig.json +8 -0
- package/template/packages/storage/package.json +16 -0
- package/template/packages/storage/src/index.test.ts +10 -0
- package/template/packages/storage/src/index.ts +23 -0
- package/template/packages/storage/tsconfig.json +5 -0
- package/template/prisma/migrations/20260505151751_baseline_init_all_tables/migration.sql +371 -0
- package/template/prisma/migrations/migration_lock.toml +3 -0
- package/template/prisma/schema.prisma +291 -0
- package/template/prisma/seed.ts +217 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { safeNextParam } from "./safe-next-param";
|
|
3
|
+
|
|
4
|
+
describe("login safeNextParam", () => {
|
|
5
|
+
it("defaults to dashboard for empty values", () => {
|
|
6
|
+
expect(safeNextParam(null)).toBe("/dashboard");
|
|
7
|
+
expect(safeNextParam("")).toBe("/dashboard");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("rejects protocol-relative values", () => {
|
|
11
|
+
expect(safeNextParam("//evil.example")).toBe("/dashboard");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("accepts internal paths", () => {
|
|
15
|
+
expect(safeNextParam("/dashboard")).toBe("/dashboard");
|
|
16
|
+
expect(safeNextParam("/resources/posts")).toBe("/resources/posts");
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState } from "react";
|
|
4
|
+
import { useRouter } from "next/navigation";
|
|
5
|
+
import { ArrowRight, Lock, Loader2, Mail } from "lucide-react";
|
|
6
|
+
import { hasUsableAccessToken } from "../../lib/api";
|
|
7
|
+
import { Logo } from "../../components/logo";
|
|
8
|
+
import { safeNextParam } from "./safe-next-param";
|
|
9
|
+
|
|
10
|
+
export default function LoginPage() {
|
|
11
|
+
const router = useRouter();
|
|
12
|
+
const [error, setError] = useState("");
|
|
13
|
+
const [loading, setLoading] = useState(false);
|
|
14
|
+
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (!hasUsableAccessToken()) return;
|
|
17
|
+
const next = safeNextParam(new URLSearchParams(window.location.search).get("next"));
|
|
18
|
+
router.replace(next);
|
|
19
|
+
}, [router]);
|
|
20
|
+
|
|
21
|
+
async function submit(formData: FormData) {
|
|
22
|
+
setError("");
|
|
23
|
+
setLoading(true);
|
|
24
|
+
try {
|
|
25
|
+
// POST to the Next.js BFF route — it proxies to the NestJS API and sets HttpOnly-friendly cookies
|
|
26
|
+
const res = await fetch("/api/auth/login", {
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers: { "content-type": "application/json" },
|
|
29
|
+
body: JSON.stringify({
|
|
30
|
+
email: formData.get("email"),
|
|
31
|
+
password: formData.get("password"),
|
|
32
|
+
realm: "admin"
|
|
33
|
+
})
|
|
34
|
+
});
|
|
35
|
+
const data = (await res.json().catch(() => ({}))) as { ok?: boolean; message?: string };
|
|
36
|
+
if (!res.ok) {
|
|
37
|
+
let msg =
|
|
38
|
+
typeof data.message === "string" && data.message.length > 0
|
|
39
|
+
? data.message
|
|
40
|
+
: "Invalid email or password. Please try again.";
|
|
41
|
+
if (res.status >= 500 && (msg === "Internal Server Error" || msg === "Internal server error")) {
|
|
42
|
+
msg =
|
|
43
|
+
"Server error while signing in. Check that the API is running, PostgreSQL is up, migrations are applied, and apps/api/.env defines JWT_SECRET (see README).";
|
|
44
|
+
}
|
|
45
|
+
setError(msg);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const next = safeNextParam(new URLSearchParams(window.location.search).get("next"));
|
|
49
|
+
router.push(next);
|
|
50
|
+
} catch (e) {
|
|
51
|
+
setError(
|
|
52
|
+
e instanceof TypeError
|
|
53
|
+
? "Cannot reach the server. Ensure the app is running and NEXT_PUBLIC_API_URL is set correctly."
|
|
54
|
+
: e instanceof Error
|
|
55
|
+
? e.message
|
|
56
|
+
: "Network error"
|
|
57
|
+
);
|
|
58
|
+
} finally {
|
|
59
|
+
setLoading(false);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<main className="relative grid min-h-screen place-items-center overflow-hidden bg-[#f8fafc] px-4">
|
|
65
|
+
{/* Background decorations */}
|
|
66
|
+
<div className="pointer-events-none absolute inset-0 overflow-hidden" aria-hidden>
|
|
67
|
+
<div className="absolute -left-48 -top-48 h-[600px] w-[600px] rounded-full bg-blue-100/70 blur-3xl" />
|
|
68
|
+
<div className="absolute -bottom-48 -right-48 h-[600px] w-[600px] rounded-full bg-teal-100/70 blur-3xl" />
|
|
69
|
+
<div className="absolute left-1/2 top-1/2 h-[400px] w-[700px] -translate-x-1/2 -translate-y-1/2 rounded-full bg-indigo-50/50 blur-3xl" />
|
|
70
|
+
</div>
|
|
71
|
+
|
|
72
|
+
<div className="relative z-10 w-full max-w-[420px] animate-in">
|
|
73
|
+
{/* Card */}
|
|
74
|
+
<div className="overflow-hidden rounded-3xl border border-slate-200/80 bg-white/95 shadow-[0_8px_40px_rgba(0,0,0,0.1)] backdrop-blur-sm">
|
|
75
|
+
{/* Top gradient bar */}
|
|
76
|
+
<div className="h-1 w-full bg-gradient-to-r from-[#2454ff] via-[#7c3aed] to-[#0ea5a4]" />
|
|
77
|
+
|
|
78
|
+
<div className="px-8 pb-8 pt-7">
|
|
79
|
+
{/* Logo */}
|
|
80
|
+
<div className="mb-7">
|
|
81
|
+
<Logo />
|
|
82
|
+
</div>
|
|
83
|
+
|
|
84
|
+
{/* Heading */}
|
|
85
|
+
<div className="mb-6">
|
|
86
|
+
<h1 className="text-[1.6rem] font-bold tracking-tight text-slate-900">
|
|
87
|
+
Welcome back
|
|
88
|
+
</h1>
|
|
89
|
+
<p className="mt-1 text-sm text-slate-500">
|
|
90
|
+
Sign in to continue to your admin panel
|
|
91
|
+
</p>
|
|
92
|
+
</div>
|
|
93
|
+
|
|
94
|
+
{/* Form */}
|
|
95
|
+
<form action={submit} className="space-y-4">
|
|
96
|
+
<label className="block space-y-1.5">
|
|
97
|
+
<span className="text-sm font-medium text-slate-700">Email address</span>
|
|
98
|
+
<div className="relative">
|
|
99
|
+
<Mail className="pointer-events-none absolute left-3.5 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-400" />
|
|
100
|
+
<input
|
|
101
|
+
name="email"
|
|
102
|
+
type="email"
|
|
103
|
+
placeholder="you@company.com"
|
|
104
|
+
className="input-base pl-10"
|
|
105
|
+
autoComplete="email"
|
|
106
|
+
/>
|
|
107
|
+
</div>
|
|
108
|
+
</label>
|
|
109
|
+
|
|
110
|
+
<label className="block space-y-1.5">
|
|
111
|
+
<div className="flex items-center justify-between">
|
|
112
|
+
<span className="text-sm font-medium text-slate-700">Password</span>
|
|
113
|
+
</div>
|
|
114
|
+
<div className="relative">
|
|
115
|
+
<Lock className="pointer-events-none absolute left-3.5 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-400" />
|
|
116
|
+
<input
|
|
117
|
+
name="password"
|
|
118
|
+
type="password"
|
|
119
|
+
placeholder="Enter your password"
|
|
120
|
+
className="input-base pl-10"
|
|
121
|
+
autoComplete="current-password"
|
|
122
|
+
/>
|
|
123
|
+
</div>
|
|
124
|
+
</label>
|
|
125
|
+
|
|
126
|
+
{error && (
|
|
127
|
+
<div className="flex items-center gap-2 rounded-xl bg-red-50 px-4 py-3 text-sm text-red-600 fade-in">
|
|
128
|
+
<div className="h-1.5 w-1.5 shrink-0 rounded-full bg-red-500" />
|
|
129
|
+
{error}
|
|
130
|
+
</div>
|
|
131
|
+
)}
|
|
132
|
+
|
|
133
|
+
<button
|
|
134
|
+
type="submit"
|
|
135
|
+
disabled={loading}
|
|
136
|
+
className="btn-primary mt-2 w-full py-3"
|
|
137
|
+
>
|
|
138
|
+
{loading ? (
|
|
139
|
+
<>
|
|
140
|
+
<Loader2 className="h-4 w-4 animate-spin" />
|
|
141
|
+
Signing in...
|
|
142
|
+
</>
|
|
143
|
+
) : (
|
|
144
|
+
<>
|
|
145
|
+
Sign in
|
|
146
|
+
<ArrowRight className="h-4 w-4" />
|
|
147
|
+
</>
|
|
148
|
+
)}
|
|
149
|
+
</button>
|
|
150
|
+
</form>
|
|
151
|
+
</div>
|
|
152
|
+
</div>
|
|
153
|
+
|
|
154
|
+
{/* Footer note */}
|
|
155
|
+
<p className="mt-5 text-center text-xs text-slate-400">
|
|
156
|
+
OpenAdminJS · Free open-source admin framework for Node.js
|
|
157
|
+
</p>
|
|
158
|
+
</div>
|
|
159
|
+
</main>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState } from "react";
|
|
4
|
+
import { AppShell } from "../../components/app-shell";
|
|
5
|
+
import { api } from "../../lib/api";
|
|
6
|
+
|
|
7
|
+
type NotificationRecord = {
|
|
8
|
+
id: string;
|
|
9
|
+
title: string;
|
|
10
|
+
body: string | null;
|
|
11
|
+
readAt: string | null;
|
|
12
|
+
createdAt: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type ListResponse<T> = {
|
|
16
|
+
data: T[];
|
|
17
|
+
meta: { total: number };
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default function NotificationsPage() {
|
|
21
|
+
const [rows, setRows] = useState<NotificationRecord[]>([]);
|
|
22
|
+
const [title, setTitle] = useState("");
|
|
23
|
+
const [body, setBody] = useState("");
|
|
24
|
+
const [loading, setLoading] = useState(false);
|
|
25
|
+
const [error, setError] = useState<string | null>(null);
|
|
26
|
+
|
|
27
|
+
async function load() {
|
|
28
|
+
setLoading(true);
|
|
29
|
+
try {
|
|
30
|
+
const response = await api<ListResponse<NotificationRecord>>("/admin/resources/notifications?sort=createdAt:desc&limit=50");
|
|
31
|
+
setRows(response.data ?? []);
|
|
32
|
+
setError(null);
|
|
33
|
+
} catch (loadError) {
|
|
34
|
+
setError(loadError instanceof Error ? loadError.message : "Failed to load notifications");
|
|
35
|
+
} finally {
|
|
36
|
+
setLoading(false);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
load();
|
|
42
|
+
}, []);
|
|
43
|
+
|
|
44
|
+
async function createNotification(event: React.FormEvent<HTMLFormElement>) {
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
if (!title.trim()) return;
|
|
47
|
+
try {
|
|
48
|
+
await api("/admin/resources/notifications", {
|
|
49
|
+
method: "POST",
|
|
50
|
+
body: JSON.stringify({ title: title.trim(), body: body.trim() || null })
|
|
51
|
+
});
|
|
52
|
+
setTitle("");
|
|
53
|
+
setBody("");
|
|
54
|
+
await load();
|
|
55
|
+
} catch (createError) {
|
|
56
|
+
setError(createError instanceof Error ? createError.message : "Failed to create notification");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function act(id: string, action: "mark-read" | "mark-unread") {
|
|
61
|
+
try {
|
|
62
|
+
await api(`/admin/resources/notifications/${id}/actions/${action}`, { method: "POST" });
|
|
63
|
+
await load();
|
|
64
|
+
} catch (actionError) {
|
|
65
|
+
setError(actionError instanceof Error ? actionError.message : "Failed to update notification");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function remove(id: string) {
|
|
70
|
+
try {
|
|
71
|
+
await api(`/admin/resources/notifications/${id}`, { method: "DELETE" });
|
|
72
|
+
await load();
|
|
73
|
+
} catch (deleteError) {
|
|
74
|
+
setError(deleteError instanceof Error ? deleteError.message : "Failed to delete notification");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<AppShell>
|
|
80
|
+
<div className="space-y-5">
|
|
81
|
+
<div>
|
|
82
|
+
<p className="text-xs font-bold uppercase tracking-wide text-[#0b8585]">Workspace communication</p>
|
|
83
|
+
<h1 className="mt-1 text-3xl font-bold tracking-normal text-[#24324b]">Notifications</h1>
|
|
84
|
+
<p className="text-sm text-[#637083]">Create, read, and manage notification lifecycle.</p>
|
|
85
|
+
</div>
|
|
86
|
+
|
|
87
|
+
{error ? <p className="rounded-xl bg-red-50 px-3 py-2 text-sm text-red-700">{error}</p> : null}
|
|
88
|
+
|
|
89
|
+
<section className="rounded-2xl border border-[#e4e8f0] bg-white p-5 shadow-[0_12px_34px_rgba(36,50,75,0.07)]">
|
|
90
|
+
<h2 className="text-lg font-bold text-[#24324b]">Create notification</h2>
|
|
91
|
+
<form onSubmit={createNotification} className="mt-4 grid gap-3">
|
|
92
|
+
<input
|
|
93
|
+
value={title}
|
|
94
|
+
onChange={(event) => setTitle(event.target.value)}
|
|
95
|
+
className="rounded-xl border border-[#e4e8f0] bg-[#f6f8fc] px-3 py-2 text-sm"
|
|
96
|
+
placeholder="Title"
|
|
97
|
+
/>
|
|
98
|
+
<textarea
|
|
99
|
+
value={body}
|
|
100
|
+
onChange={(event) => setBody(event.target.value)}
|
|
101
|
+
className="min-h-24 rounded-xl border border-[#e4e8f0] bg-[#f6f8fc] px-3 py-2 text-sm"
|
|
102
|
+
placeholder="Body"
|
|
103
|
+
/>
|
|
104
|
+
<button type="submit" className="w-fit rounded-xl bg-gradient-to-r from-[#2454ff] to-[#0ea5a4] px-4 py-2 text-sm font-bold text-white">
|
|
105
|
+
Create notification
|
|
106
|
+
</button>
|
|
107
|
+
</form>
|
|
108
|
+
</section>
|
|
109
|
+
|
|
110
|
+
<section className="rounded-2xl border border-[#e4e8f0] bg-white p-5 shadow-[0_12px_34px_rgba(36,50,75,0.07)]">
|
|
111
|
+
<h2 className="text-lg font-bold text-[#24324b]">Inbox</h2>
|
|
112
|
+
<div className="mt-4 space-y-3">
|
|
113
|
+
{loading ? <p className="text-sm text-[#637083]">Loading notifications...</p> : null}
|
|
114
|
+
{!loading && rows.length === 0 ? <p className="text-sm text-[#637083]">No notifications yet.</p> : null}
|
|
115
|
+
{rows.map((row) => (
|
|
116
|
+
<article key={row.id} className="rounded-xl border border-[#eef2fb] bg-[#f9fbff] p-3">
|
|
117
|
+
<div className="flex items-center justify-between gap-2">
|
|
118
|
+
<h3 className="font-semibold text-[#24324b]">{row.title}</h3>
|
|
119
|
+
<span className={`rounded-full px-2 py-1 text-xs font-semibold ${row.readAt ? "bg-[#e5ecff] text-[#2454ff]" : "bg-[#e9fbfa] text-[#0b8585]"}`}>
|
|
120
|
+
{row.readAt ? "Read" : "Unread"}
|
|
121
|
+
</span>
|
|
122
|
+
</div>
|
|
123
|
+
{row.body ? <p className="mt-1 text-sm text-[#637083]">{row.body}</p> : null}
|
|
124
|
+
<p className="mt-1 text-xs text-[#8a95a8]">{new Date(row.createdAt).toLocaleString()}</p>
|
|
125
|
+
<div className="mt-2 flex items-center gap-2">
|
|
126
|
+
{row.readAt ? (
|
|
127
|
+
<button type="button" onClick={() => act(row.id, "mark-unread")} className="rounded-lg border border-[#dce4f6] bg-white px-3 py-1.5 text-xs font-semibold text-[#24324b]">
|
|
128
|
+
Mark unread
|
|
129
|
+
</button>
|
|
130
|
+
) : (
|
|
131
|
+
<button type="button" onClick={() => act(row.id, "mark-read")} className="rounded-lg border border-[#dce4f6] bg-white px-3 py-1.5 text-xs font-semibold text-[#24324b]">
|
|
132
|
+
Mark read
|
|
133
|
+
</button>
|
|
134
|
+
)}
|
|
135
|
+
<button type="button" onClick={() => remove(row.id)} className="rounded-lg border border-red-200 bg-red-50 px-3 py-1.5 text-xs font-semibold text-red-700">
|
|
136
|
+
Delete
|
|
137
|
+
</button>
|
|
138
|
+
</div>
|
|
139
|
+
</article>
|
|
140
|
+
))}
|
|
141
|
+
</div>
|
|
142
|
+
</section>
|
|
143
|
+
</div>
|
|
144
|
+
</AppShell>
|
|
145
|
+
);
|
|
146
|
+
}
|