mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
refactor: single form
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Card, CardContent } from "@/components/ui/card";
|
||||||
import { Form } from "@/components/ui/form";
|
import { Form } from "@/components/ui/form";
|
||||||
import { Secrets } from "@/components/ui/secrets";
|
import { Secrets } from "@/components/ui/secrets";
|
||||||
import { api } from "@/utils/api";
|
import { api } from "@/utils/api";
|
||||||
@@ -8,15 +10,10 @@ import { z } from "zod";
|
|||||||
|
|
||||||
const addEnvironmentSchema = z.object({
|
const addEnvironmentSchema = z.object({
|
||||||
env: z.string(),
|
env: z.string(),
|
||||||
});
|
|
||||||
|
|
||||||
type EnvironmentSchema = z.infer<typeof addEnvironmentSchema>;
|
|
||||||
|
|
||||||
const addBuildArgsSchema = z.object({
|
|
||||||
buildArgs: z.string(),
|
buildArgs: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
type BuildArgsSchema = z.infer<typeof addBuildArgsSchema>;
|
type EnvironmentSchema = z.infer<typeof addEnvironmentSchema>;
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
applicationId: string;
|
applicationId: string;
|
||||||
@@ -24,7 +21,6 @@ interface Props {
|
|||||||
|
|
||||||
export const ShowEnvironment = ({ applicationId }: Props) => {
|
export const ShowEnvironment = ({ applicationId }: Props) => {
|
||||||
const saveEnvironmentMutation = api.application.saveEnvironment.useMutation();
|
const saveEnvironmentMutation = api.application.saveEnvironment.useMutation();
|
||||||
const saveBuildArgsMutation = api.application.saveBuildArgs.useMutation();
|
|
||||||
|
|
||||||
const { data, refetch } = api.application.one.useQuery(
|
const { data, refetch } = api.application.one.useQuery(
|
||||||
{
|
{
|
||||||
@@ -38,21 +34,16 @@ export const ShowEnvironment = ({ applicationId }: Props) => {
|
|||||||
const envForm = useForm<EnvironmentSchema>({
|
const envForm = useForm<EnvironmentSchema>({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
env: data?.env || "",
|
env: data?.env || "",
|
||||||
},
|
|
||||||
resolver: zodResolver(addEnvironmentSchema),
|
|
||||||
});
|
|
||||||
|
|
||||||
const buildArgsForm = useForm<BuildArgsSchema>({
|
|
||||||
defaultValues: {
|
|
||||||
buildArgs: data?.buildArgs || "",
|
buildArgs: data?.buildArgs || "",
|
||||||
},
|
},
|
||||||
resolver: zodResolver(addBuildArgsSchema),
|
resolver: zodResolver(addEnvironmentSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
const onEnvSubmit = async (data: EnvironmentSchema) => {
|
const onEnvSubmit = async (data: EnvironmentSchema) => {
|
||||||
saveEnvironmentMutation
|
saveEnvironmentMutation
|
||||||
.mutateAsync({
|
.mutateAsync({
|
||||||
env: data.env,
|
env: data.env,
|
||||||
|
buildArgs: data.buildArgs,
|
||||||
applicationId,
|
applicationId,
|
||||||
})
|
})
|
||||||
.then(async () => {
|
.then(async () => {
|
||||||
@@ -64,40 +55,22 @@ export const ShowEnvironment = ({ applicationId }: Props) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const onBuildArgsSubmit = async (data: BuildArgsSchema) => {
|
|
||||||
saveBuildArgsMutation
|
|
||||||
.mutateAsync({
|
|
||||||
buildArgs: data.buildArgs,
|
|
||||||
applicationId,
|
|
||||||
})
|
|
||||||
.then(async () => {
|
|
||||||
toast.success("Buildargs Added");
|
|
||||||
await refetch();
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
toast.error("Error to add build-args");
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex w-full flex-col gap-5 ">
|
<Form {...envForm}>
|
||||||
<Form {...envForm}>
|
<form
|
||||||
<form onSubmit={envForm.handleSubmit(onEnvSubmit)}>
|
onSubmit={envForm.handleSubmit(onEnvSubmit)}
|
||||||
|
className="flex w-full flex-col gap-5 "
|
||||||
|
>
|
||||||
|
<Card className="bg-background">
|
||||||
<Secrets
|
<Secrets
|
||||||
name="env"
|
name="env"
|
||||||
isLoading={saveEnvironmentMutation.isLoading}
|
|
||||||
title="Environment Settings"
|
title="Environment Settings"
|
||||||
description="You can add environment variables to your resource."
|
description="You can add environment variables to your resource."
|
||||||
placeholder={["NODE_ENV=production", "PORT=3000"].join("\n")}
|
placeholder={["NODE_ENV=production", "PORT=3000"].join("\n")}
|
||||||
/>
|
/>
|
||||||
</form>
|
{data?.buildType === "dockerfile" && (
|
||||||
</Form>
|
|
||||||
{data?.buildType === "dockerfile" && (
|
|
||||||
<Form {...buildArgsForm}>
|
|
||||||
<form onSubmit={buildArgsForm.handleSubmit(onBuildArgsSubmit)}>
|
|
||||||
<Secrets
|
<Secrets
|
||||||
name="buildArgs"
|
name="buildArgs"
|
||||||
isLoading={saveBuildArgsMutation.isLoading}
|
|
||||||
title="Build-time Variables"
|
title="Build-time Variables"
|
||||||
description={
|
description={
|
||||||
<span>
|
<span>
|
||||||
@@ -115,9 +88,20 @@ export const ShowEnvironment = ({ applicationId }: Props) => {
|
|||||||
}
|
}
|
||||||
placeholder="NPM_TOKEN=xyz"
|
placeholder="NPM_TOKEN=xyz"
|
||||||
/>
|
/>
|
||||||
</form>
|
)}
|
||||||
</Form>
|
<CardContent>
|
||||||
)}
|
<div className="flex flex-row justify-end">
|
||||||
</div>
|
<Button
|
||||||
|
isLoading={saveEnvironmentMutation.isLoading}
|
||||||
|
className="w-fit"
|
||||||
|
type="submit"
|
||||||
|
>
|
||||||
|
Save
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ interface Props {
|
|||||||
title: string;
|
title: string;
|
||||||
description: ReactNode;
|
description: ReactNode;
|
||||||
placeholder: string;
|
placeholder: string;
|
||||||
isLoading: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Secrets = (props: Props) => {
|
export const Secrets = (props: Props) => {
|
||||||
@@ -31,7 +30,7 @@ export const Secrets = (props: Props) => {
|
|||||||
const form = useFormContext<Record<string, string>>();
|
const form = useFormContext<Record<string, string>>();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card className="bg-background">
|
<>
|
||||||
<CardHeader className="flex flex-row w-full items-center justify-between">
|
<CardHeader className="flex flex-row w-full items-center justify-between">
|
||||||
<div>
|
<div>
|
||||||
<CardTitle className="text-xl">{props.title}</CardTitle>
|
<CardTitle className="text-xl">{props.title}</CardTitle>
|
||||||
@@ -75,18 +74,7 @@ export const Secrets = (props: Props) => {
|
|||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="flex flex-row justify-end">
|
|
||||||
<Button
|
|
||||||
disabled={isVisible}
|
|
||||||
isLoading={props.isLoading}
|
|
||||||
className="w-fit"
|
|
||||||
type="submit"
|
|
||||||
>
|
|
||||||
Save
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import {
|
|||||||
apiFindMonitoringStats,
|
apiFindMonitoringStats,
|
||||||
apiFindOneApplication,
|
apiFindOneApplication,
|
||||||
apiReloadApplication,
|
apiReloadApplication,
|
||||||
apiSaveBuildArgs,
|
|
||||||
apiSaveBuildType,
|
apiSaveBuildType,
|
||||||
apiSaveDockerProvider,
|
apiSaveDockerProvider,
|
||||||
apiSaveEnvironmentVariables,
|
apiSaveEnvironmentVariables,
|
||||||
@@ -188,13 +187,6 @@ export const applicationRouter = createTRPCRouter({
|
|||||||
.mutation(async ({ input }) => {
|
.mutation(async ({ input }) => {
|
||||||
await updateApplication(input.applicationId, {
|
await updateApplication(input.applicationId, {
|
||||||
env: input.env,
|
env: input.env,
|
||||||
});
|
|
||||||
return true;
|
|
||||||
}),
|
|
||||||
saveBuildArgs: protectedProcedure
|
|
||||||
.input(apiSaveBuildArgs)
|
|
||||||
.mutation(async ({ input }) => {
|
|
||||||
await updateApplication(input.applicationId, {
|
|
||||||
buildArgs: input.buildArgs,
|
buildArgs: input.buildArgs,
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -276,6 +276,7 @@ const createSchema = createInsertSchema(applications, {
|
|||||||
applicationId: z.string(),
|
applicationId: z.string(),
|
||||||
autoDeploy: z.boolean(),
|
autoDeploy: z.boolean(),
|
||||||
env: z.string().optional(),
|
env: z.string().optional(),
|
||||||
|
buildArgs: z.string().optional(),
|
||||||
name: z.string().min(1),
|
name: z.string().min(1),
|
||||||
description: z.string().optional(),
|
description: z.string().optional(),
|
||||||
memoryReservation: z.number().optional(),
|
memoryReservation: z.number().optional(),
|
||||||
@@ -376,12 +377,6 @@ export const apiSaveEnvironmentVariables = createSchema
|
|||||||
.pick({
|
.pick({
|
||||||
applicationId: true,
|
applicationId: true,
|
||||||
env: true,
|
env: true,
|
||||||
})
|
|
||||||
.required();
|
|
||||||
|
|
||||||
export const apiSaveBuildArgs = createSchema
|
|
||||||
.pick({
|
|
||||||
applicationId: true,
|
|
||||||
buildArgs: true,
|
buildArgs: true,
|
||||||
})
|
})
|
||||||
.required();
|
.required();
|
||||||
|
|||||||
Reference in New Issue
Block a user