feat(environment): add unsaved changes handling for environment settings

- Implement form change tracking for environment variables
- Add cancel and save buttons with conditional rendering
- Disable save button when no changes are detected
- Show unsaved changes warning in description
- Improve user experience with form state management
This commit is contained in:
Mauricio Siu
2025-03-08 19:17:59 -06:00
parent 62bd8e3c95
commit 6afd1bf531
2 changed files with 85 additions and 13 deletions

View File

@@ -71,15 +71,19 @@ export const ShowEnvironment = ({ id, type }: Props) => {
resolver: zodResolver(addEnvironmentSchema), resolver: zodResolver(addEnvironmentSchema),
}); });
// Watch form value
const currentEnvironment = form.watch("environment");
const hasChanges = currentEnvironment !== (data?.env || "");
useEffect(() => { useEffect(() => {
if (data) { if (data) {
form.reset({ form.reset({
environment: data.env || "", environment: data.env || "",
}); });
} }
}, [form.reset, data, form]); }, [data, form]);
const onSubmit = async (data: EnvironmentSchema) => { const onSubmit = async (formData: EnvironmentSchema) => {
mutateAsync({ mutateAsync({
mongoId: id || "", mongoId: id || "",
postgresId: id || "", postgresId: id || "",
@@ -87,7 +91,7 @@ export const ShowEnvironment = ({ id, type }: Props) => {
mysqlId: id || "", mysqlId: id || "",
mariadbId: id || "", mariadbId: id || "",
composeId: id || "", composeId: id || "",
env: data.environment, env: formData.environment,
}) })
.then(async () => { .then(async () => {
toast.success("Environments Added"); toast.success("Environments Added");
@@ -98,6 +102,12 @@ export const ShowEnvironment = ({ id, type }: Props) => {
}); });
}; };
const handleCancel = () => {
form.reset({
environment: data?.env || "",
});
};
return ( return (
<div className="flex w-full flex-col gap-5 "> <div className="flex w-full flex-col gap-5 ">
<Card className="bg-background"> <Card className="bg-background">
@@ -106,6 +116,11 @@ export const ShowEnvironment = ({ id, type }: Props) => {
<CardTitle className="text-xl">Environment Settings</CardTitle> <CardTitle className="text-xl">Environment Settings</CardTitle>
<CardDescription> <CardDescription>
You can add environment variables to your resource. You can add environment variables to your resource.
{hasChanges && (
<span className="text-yellow-500 ml-2">
(You have unsaved changes)
</span>
)}
</CardDescription> </CardDescription>
</div> </div>
@@ -155,8 +170,22 @@ PORT=3000
)} )}
/> />
<div className="flex flex-row justify-end"> <div className="flex flex-row justify-end gap-2">
<Button isLoading={isLoading} className="w-fit" type="submit"> {hasChanges && (
<Button
type="button"
variant="outline"
onClick={handleCancel}
>
Cancel
</Button>
)}
<Button
isLoading={isLoading}
className="w-fit"
type="submit"
disabled={!hasChanges}
>
Save Save
</Button> </Button>
</div> </div>

View File

@@ -7,6 +7,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { toast } from "sonner"; import { toast } from "sonner";
import { z } from "zod"; import { z } from "zod";
import { useEffect } from "react";
const addEnvironmentSchema = z.object({ const addEnvironmentSchema = z.object({
env: z.string(), env: z.string(),
@@ -34,16 +35,32 @@ export const ShowEnvironment = ({ applicationId }: Props) => {
const form = useForm<EnvironmentSchema>({ const form = useForm<EnvironmentSchema>({
defaultValues: { defaultValues: {
env: data?.env || "", env: "",
buildArgs: data?.buildArgs || "", buildArgs: "",
}, },
resolver: zodResolver(addEnvironmentSchema), resolver: zodResolver(addEnvironmentSchema),
}); });
const onSubmit = async (data: EnvironmentSchema) => { // Watch form values
const currentEnv = form.watch("env");
const currentBuildArgs = form.watch("buildArgs");
const hasChanges =
currentEnv !== (data?.env || "") ||
currentBuildArgs !== (data?.buildArgs || "");
useEffect(() => {
if (data) {
form.reset({
env: data.env || "",
buildArgs: data.buildArgs || "",
});
}
}, [data, form]);
const onSubmit = async (formData: EnvironmentSchema) => {
mutateAsync({ mutateAsync({
env: data.env, env: formData.env,
buildArgs: data.buildArgs, buildArgs: formData.buildArgs,
applicationId, applicationId,
}) })
.then(async () => { .then(async () => {
@@ -55,6 +72,13 @@ export const ShowEnvironment = ({ applicationId }: Props) => {
}); });
}; };
const handleCancel = () => {
form.reset({
env: data?.env || "",
buildArgs: data?.buildArgs || "",
});
};
return ( return (
<Card className="bg-background px-6 pb-6"> <Card className="bg-background px-6 pb-6">
<Form {...form}> <Form {...form}>
@@ -65,7 +89,16 @@ export const ShowEnvironment = ({ applicationId }: Props) => {
<Secrets <Secrets
name="env" name="env"
title="Environment Settings" title="Environment Settings"
description="You can add environment variables to your resource." description={
<span>
You can add environment variables to your resource.
{hasChanges && (
<span className="text-yellow-500 ml-2">
(You have unsaved changes)
</span>
)}
</span>
}
placeholder={["NODE_ENV=production", "PORT=3000"].join("\n")} placeholder={["NODE_ENV=production", "PORT=3000"].join("\n")}
/> />
{data?.buildType === "dockerfile" && ( {data?.buildType === "dockerfile" && (
@@ -89,8 +122,18 @@ export const ShowEnvironment = ({ applicationId }: Props) => {
placeholder="NPM_TOKEN=xyz" placeholder="NPM_TOKEN=xyz"
/> />
)} )}
<div className="flex flex-row justify-end"> <div className="flex flex-row justify-end gap-2">
<Button isLoading={isLoading} className="w-fit" type="submit"> {hasChanges && (
<Button type="button" variant="outline" onClick={handleCancel}>
Cancel
</Button>
)}
<Button
isLoading={isLoading}
className="w-fit"
type="submit"
disabled={!hasChanges}
>
Save Save
</Button> </Button>
</div> </div>