From dd16baf234f8c08a43f586ee1a854e957a3c0348 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sat, 8 Jun 2024 16:41:38 -0600 Subject: [PATCH] refactor: use slugify function --- .../dashboard/project/add-application.tsx | 48 ++----------------- components/dashboard/project/add-compose.tsx | 41 ++++++++++++++-- components/dashboard/project/add-database.tsx | 9 ++-- 3 files changed, 46 insertions(+), 52 deletions(-) diff --git a/components/dashboard/project/add-application.tsx b/components/dashboard/project/add-application.tsx index 712db33b0..ecf2a1afb 100644 --- a/components/dashboard/project/add-application.tsx +++ b/components/dashboard/project/add-application.tsx @@ -27,6 +27,7 @@ import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; import { Textarea } from "@/components/ui/textarea"; +import { slugify } from "@/lib/slug"; const AddTemplateSchema = z.object({ name: z.string().min(1, { @@ -54,6 +55,7 @@ interface Props { export const AddApplication = ({ projectId, projectName }: Props) => { const utils = api.useUtils(); const [visible, setVisible] = useState(false); + const slug = slugify(projectName); const { mutateAsync, isLoading, error, isError } = api.application.create.useMutation(); @@ -61,7 +63,7 @@ export const AddApplication = ({ projectId, projectName }: Props) => { const form = useForm({ defaultValues: { name: "", - appName: `${projectName}-`, + appName: `${slug}-`, description: "", }, resolver: zodResolver(AddTemplateSchema), @@ -106,7 +108,6 @@ export const AddApplication = ({ projectId, projectName }: Props) => { {isError && {error?.message}} -
{ {...field} onChange={(e) => { const val = e.target.value?.trim() || ""; - form.setValue("appName", `${projectName}-${val}`); + form.setValue("appName", `${slug}-${val}`); field.onChange(val); }} /> @@ -165,47 +166,6 @@ export const AddApplication = ({ projectId, projectName }: Props) => { )} /> - - {/* ( - - Build Type - - - - - - - - Dockerfile - - - - - - - Nixpacks - - - - - - - Heroku Buildpacks - - - - - - - )} - /> */} diff --git a/components/dashboard/project/add-compose.tsx b/components/dashboard/project/add-compose.tsx index 97df55ef8..fd769c911 100644 --- a/components/dashboard/project/add-compose.tsx +++ b/components/dashboard/project/add-compose.tsx @@ -34,12 +34,22 @@ import { SelectTrigger, SelectValue, } from "@/components/ui/select"; +import { slugify } from "@/lib/slug"; const AddComposeSchema = z.object({ composeType: z.enum(["docker-compose", "stack"]).optional(), name: z.string().min(1, { message: "Name is required", }), + appName: z + .string() + .min(1, { + message: "App name is required", + }) + .regex(/^[a-z](?!.*--)([a-z0-9-]*[a-z])?$/, { + message: + "App name supports letters, numbers, '-' and can only start and end letters, and does not support continuous '-'", + }), description: z.string().optional(), }); @@ -47,11 +57,12 @@ type AddCompose = z.infer; interface Props { projectId: string; + projectName?: string; } -export const AddCompose = ({ projectId }: Props) => { +export const AddCompose = ({ projectId, projectName }: Props) => { const utils = api.useUtils(); - + const slug = slugify(projectName); const { mutateAsync, isLoading, error, isError } = api.compose.create.useMutation(); @@ -60,6 +71,7 @@ export const AddCompose = ({ projectId }: Props) => { name: "", description: "", composeType: "docker-compose", + appName: `${slug}-`, }, resolver: zodResolver(AddComposeSchema), }); @@ -74,6 +86,7 @@ export const AddCompose = ({ projectId }: Props) => { description: data.description, projectId, composeType: data.composeType, + appName: data.appName, }) .then(async () => { toast.success("Compose Created"); @@ -120,14 +133,34 @@ export const AddCompose = ({ projectId }: Props) => { Name - + { + const val = e.target.value?.trim() || ""; + form.setValue("appName", `${slug}-${val}`); + field.onChange(val); + }} + /> - )} /> + ( + + AppName + + + + + + )} + /> { const utils = api.useUtils(); const [visible, setVisible] = useState(false); - + const slug = slugify(projectName); const postgresMutation = api.postgres.create.useMutation(); const mongoMutation = api.mongo.create.useMutation(); const redisMutation = api.redis.create.useMutation(); @@ -155,7 +156,7 @@ export const AddDatabase = ({ projectId, projectName }: Props) => { type: "postgres", dockerImage: "", name: "", - appName: `${projectName}-`, + appName: `${slug}-`, databasePassword: "", description: "", databaseName: "", @@ -341,7 +342,7 @@ export const AddDatabase = ({ projectId, projectName }: Props) => { {...field} onChange={(e) => { const val = e.target.value?.trim() || ""; - form.setValue("appName", `${projectName}-${val}`); + form.setValue("appName", `${slug}-${val}`); field.onChange(val); }} />