From 8165a090d153c906fbb2fba1ad156869c96ad49c Mon Sep 17 00:00:00 2001 From: Lucas Smith Date: Fri, 23 Feb 2024 10:47:01 +0000 Subject: [PATCH] feat: migrate to site-settings --- apps/web/package.json | 1 + .../(dashboard)/admin/banner/banner-form.tsx | 119 ----------- .../src/app/(dashboard)/admin/banner/page.tsx | 15 -- apps/web/src/app/(dashboard)/admin/nav.tsx | 8 +- .../admin/site-settings/banner-form.tsx | 202 ++++++++++++++++++ .../(dashboard)/admin/site-settings/page.tsx | 24 +++ .../components/(dashboard)/layout/banner.tsx | 20 +- package-lock.json | 11 + packages/lib/server-only/banner/get-banner.ts | 11 - .../lib/server-only/banner/upsert-banner.ts | 28 --- .../site-settings/get-site-settings.ts | 9 + .../lib/server-only/site-settings/schema.ts | 12 ++ .../site-settings/schemas/_base.ts | 9 + .../site-settings/schemas/banner.ts | 23 ++ .../site-settings/upsert-site-setting.ts | 31 +++ .../migration.sql | 25 +++ .../migration.sql | 13 ++ packages/prisma/schema.prisma | 15 +- packages/trpc/server/admin-router/router.ts | 23 +- packages/trpc/server/admin-router/schema.ts | 6 + packages/trpc/server/banner-router/router.ts | 27 --- packages/trpc/server/banner-router/schema.ts | 8 - packages/trpc/server/router.ts | 2 - packages/ui/package.json | 1 + packages/ui/primitives/color-picker.tsx | 82 +++++++ 25 files changed, 497 insertions(+), 228 deletions(-) delete mode 100644 apps/web/src/app/(dashboard)/admin/banner/banner-form.tsx delete mode 100644 apps/web/src/app/(dashboard)/admin/banner/page.tsx create mode 100644 apps/web/src/app/(dashboard)/admin/site-settings/banner-form.tsx create mode 100644 apps/web/src/app/(dashboard)/admin/site-settings/page.tsx delete mode 100644 packages/lib/server-only/banner/get-banner.ts delete mode 100644 packages/lib/server-only/banner/upsert-banner.ts create mode 100644 packages/lib/server-only/site-settings/get-site-settings.ts create mode 100644 packages/lib/server-only/site-settings/schema.ts create mode 100644 packages/lib/server-only/site-settings/schemas/_base.ts create mode 100644 packages/lib/server-only/site-settings/schemas/banner.ts create mode 100644 packages/lib/server-only/site-settings/upsert-site-setting.ts create mode 100644 packages/prisma/migrations/20240222230527_change_banner_to_site_settings_model/migration.sql create mode 100644 packages/prisma/migrations/20240222230604_add_site_banner_to_site_settings/migration.sql delete mode 100644 packages/trpc/server/banner-router/router.ts delete mode 100644 packages/trpc/server/banner-router/schema.ts create mode 100644 packages/ui/primitives/color-picker.tsx diff --git a/apps/web/package.json b/apps/web/package.json index fd4faa0c1..3f485d9bd 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -42,6 +42,7 @@ "react-hotkeys-hook": "^4.4.1", "react-icons": "^4.11.0", "react-rnd": "^10.4.1", + "remeda": "^1.27.1", "sharp": "0.33.1", "ts-pattern": "^5.0.5", "typescript": "5.2.2", diff --git a/apps/web/src/app/(dashboard)/admin/banner/banner-form.tsx b/apps/web/src/app/(dashboard)/admin/banner/banner-form.tsx deleted file mode 100644 index ce7e3e181..000000000 --- a/apps/web/src/app/(dashboard)/admin/banner/banner-form.tsx +++ /dev/null @@ -1,119 +0,0 @@ -'use client'; - -import { useRouter } from 'next/navigation'; - -import { zodResolver } from '@hookform/resolvers/zod'; -import { useForm } from 'react-hook-form'; -import { z } from 'zod'; - -import { TRPCClientError } from '@documenso/trpc/client'; -import { trpc as trpcReact } from '@documenso/trpc/react'; -import { Button } from '@documenso/ui/primitives/button'; -import { - Form, - FormControl, - FormDescription, - FormField, - FormItem, - FormLabel, - FormMessage, -} from '@documenso/ui/primitives/form/form'; -import { Switch } from '@documenso/ui/primitives/switch'; -import { Textarea } from '@documenso/ui/primitives/textarea'; -import { useToast } from '@documenso/ui/primitives/use-toast'; - -const ZBannerSchema = z.object({ - text: z.string().optional(), - show: z.boolean().optional(), -}); - -type TBannerSchema = z.infer; - -export function BannerForm({ show, text }: TBannerSchema) { - const router = useRouter(); - const { toast } = useToast(); - - const form = useForm({ - resolver: zodResolver(ZBannerSchema), - defaultValues: { - show, - text, - }, - }); - - const { mutateAsync: updateBanner, isLoading: isUpdatingBanner } = - trpcReact.banner.updateBanner.useMutation(); - - const onBannerUpdate = async ({ show, text }: TBannerSchema) => { - try { - await updateBanner({ - show, - text, - }); - - toast({ - title: 'Banner Updated', - description: 'Your banner has been updated successfully.', - duration: 5000, - }); - - router.refresh(); - } catch (err) { - if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') { - toast({ - title: 'An error occurred', - description: err.message, - variant: 'destructive', - }); - } else { - toast({ - title: 'An unknown error occurred', - variant: 'destructive', - description: - 'We encountered an unknown error while attempting to reset your password. Please try again later.', - }); - } - } - }; - - return ( -
- - ( - -
- Show Banner - Show a banner to the users by the admin -
- - - -
- )} - /> - - ( - - Banner Text - -