Skip to content

Commit 55bab4b

Browse files
authored
Merge pull request #1438 from Dokploy/558-cancel-button-when-editing-environment-settings-and-other-text
feat(environment): add unsaved changes handling for environment settings
2 parents 62bd8e3 + 6afd1bf commit 55bab4b

File tree

2 files changed

+85
-13
lines changed

2 files changed

+85
-13
lines changed

apps/dokploy/components/dashboard/application/environment/show-enviroment.tsx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,27 @@ export const ShowEnvironment = ({ id, type }: Props) => {
7171
resolver: zodResolver(addEnvironmentSchema),
7272
});
7373

74+
// Watch form value
75+
const currentEnvironment = form.watch("environment");
76+
const hasChanges = currentEnvironment !== (data?.env || "");
77+
7478
useEffect(() => {
7579
if (data) {
7680
form.reset({
7781
environment: data.env || "",
7882
});
7983
}
80-
}, [form.reset, data, form]);
84+
}, [data, form]);
8185

82-
const onSubmit = async (data: EnvironmentSchema) => {
86+
const onSubmit = async (formData: EnvironmentSchema) => {
8387
mutateAsync({
8488
mongoId: id || "",
8589
postgresId: id || "",
8690
redisId: id || "",
8791
mysqlId: id || "",
8892
mariadbId: id || "",
8993
composeId: id || "",
90-
env: data.environment,
94+
env: formData.environment,
9195
})
9296
.then(async () => {
9397
toast.success("Environments Added");
@@ -98,6 +102,12 @@ export const ShowEnvironment = ({ id, type }: Props) => {
98102
});
99103
};
100104

105+
const handleCancel = () => {
106+
form.reset({
107+
environment: data?.env || "",
108+
});
109+
};
110+
101111
return (
102112
<div className="flex w-full flex-col gap-5 ">
103113
<Card className="bg-background">
@@ -106,6 +116,11 @@ export const ShowEnvironment = ({ id, type }: Props) => {
106116
<CardTitle className="text-xl">Environment Settings</CardTitle>
107117
<CardDescription>
108118
You can add environment variables to your resource.
119+
{hasChanges && (
120+
<span className="text-yellow-500 ml-2">
121+
(You have unsaved changes)
122+
</span>
123+
)}
109124
</CardDescription>
110125
</div>
111126

@@ -155,8 +170,22 @@ PORT=3000
155170
)}
156171
/>
157172

158-
<div className="flex flex-row justify-end">
159-
<Button isLoading={isLoading} className="w-fit" type="submit">
173+
<div className="flex flex-row justify-end gap-2">
174+
{hasChanges && (
175+
<Button
176+
type="button"
177+
variant="outline"
178+
onClick={handleCancel}
179+
>
180+
Cancel
181+
</Button>
182+
)}
183+
<Button
184+
isLoading={isLoading}
185+
className="w-fit"
186+
type="submit"
187+
disabled={!hasChanges}
188+
>
160189
Save
161190
</Button>
162191
</div>

apps/dokploy/components/dashboard/application/environment/show.tsx

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
77
import { useForm } from "react-hook-form";
88
import { toast } from "sonner";
99
import { z } from "zod";
10+
import { useEffect } from "react";
1011

1112
const addEnvironmentSchema = z.object({
1213
env: z.string(),
@@ -34,16 +35,32 @@ export const ShowEnvironment = ({ applicationId }: Props) => {
3435

3536
const form = useForm<EnvironmentSchema>({
3637
defaultValues: {
37-
env: data?.env || "",
38-
buildArgs: data?.buildArgs || "",
38+
env: "",
39+
buildArgs: "",
3940
},
4041
resolver: zodResolver(addEnvironmentSchema),
4142
});
4243

43-
const onSubmit = async (data: EnvironmentSchema) => {
44+
// Watch form values
45+
const currentEnv = form.watch("env");
46+
const currentBuildArgs = form.watch("buildArgs");
47+
const hasChanges =
48+
currentEnv !== (data?.env || "") ||
49+
currentBuildArgs !== (data?.buildArgs || "");
50+
51+
useEffect(() => {
52+
if (data) {
53+
form.reset({
54+
env: data.env || "",
55+
buildArgs: data.buildArgs || "",
56+
});
57+
}
58+
}, [data, form]);
59+
60+
const onSubmit = async (formData: EnvironmentSchema) => {
4461
mutateAsync({
45-
env: data.env,
46-
buildArgs: data.buildArgs,
62+
env: formData.env,
63+
buildArgs: formData.buildArgs,
4764
applicationId,
4865
})
4966
.then(async () => {
@@ -55,6 +72,13 @@ export const ShowEnvironment = ({ applicationId }: Props) => {
5572
});
5673
};
5774

75+
const handleCancel = () => {
76+
form.reset({
77+
env: data?.env || "",
78+
buildArgs: data?.buildArgs || "",
79+
});
80+
};
81+
5882
return (
5983
<Card className="bg-background px-6 pb-6">
6084
<Form {...form}>
@@ -65,7 +89,16 @@ export const ShowEnvironment = ({ applicationId }: Props) => {
6589
<Secrets
6690
name="env"
6791
title="Environment Settings"
68-
description="You can add environment variables to your resource."
92+
description={
93+
<span>
94+
You can add environment variables to your resource.
95+
{hasChanges && (
96+
<span className="text-yellow-500 ml-2">
97+
(You have unsaved changes)
98+
</span>
99+
)}
100+
</span>
101+
}
69102
placeholder={["NODE_ENV=production", "PORT=3000"].join("\n")}
70103
/>
71104
{data?.buildType === "dockerfile" && (
@@ -89,8 +122,18 @@ export const ShowEnvironment = ({ applicationId }: Props) => {
89122
placeholder="NPM_TOKEN=xyz"
90123
/>
91124
)}
92-
<div className="flex flex-row justify-end">
93-
<Button isLoading={isLoading} className="w-fit" type="submit">
125+
<div className="flex flex-row justify-end gap-2">
126+
{hasChanges && (
127+
<Button type="button" variant="outline" onClick={handleCancel}>
128+
Cancel
129+
</Button>
130+
)}
131+
<Button
132+
isLoading={isLoading}
133+
className="w-fit"
134+
type="submit"
135+
disabled={!hasChanges}
136+
>
94137
Save
95138
</Button>
96139
</div>

0 commit comments

Comments
 (0)