Skip to content

Commit

Permalink
fix: change dirty strategy in check form
Browse files Browse the repository at this point in the history
  • Loading branch information
ckbedwell committed Jan 6, 2025
1 parent e287f10 commit 4ad984d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/components/CheckForm/CheckForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { toFormValues } from 'components/CheckEditor/checkFormTransformations';
import { CheckJobName } from 'components/CheckEditor/FormComponents/CheckJobName';
import { ChooseCheckType } from 'components/CheckEditor/FormComponents/ChooseCheckType';
import { ProbeOptions } from 'components/CheckEditor/ProbeOptions';
import { checkHasChanges } from 'components/CheckForm/checkForm.utils';
import { DNSCheckLayout } from 'components/CheckForm/FormLayouts/CheckDNSLayout';
import { GRPCCheckLayout } from 'components/CheckForm/FormLayouts/CheckGrpcLayout';
import { HttpCheckLayout } from 'components/CheckForm/FormLayouts/CheckHttpLayout';
Expand Down Expand Up @@ -93,9 +94,10 @@ export const CheckForm = ({ check, disabled }: CheckFormProps) => {
(checkType === CheckType.Browser && isOverBrowserLimit) ||
([CheckType.MULTI_HTTP, CheckType.Scripted].includes(checkType) && isOverScriptedLimit);
const isDisabled = disabled || !canWriteChecks || getLimitDisabled({ isExistingCheck, isLoading, overLimit });
const defaultValues = toFormValues(initialCheck, checkType);

const formMethods = useForm<CheckFormValues>({
defaultValues: toFormValues(initialCheck, checkType),
defaultValues,
shouldFocusError: false, // we manage focus manually
resolver: zodResolver(schema),
});
Expand Down Expand Up @@ -158,9 +160,7 @@ export const CheckForm = ({ check, disabled }: CheckFormProps) => {
</Stack>
);

const { isDirty, isSubmitSuccessful } = formMethods.formState;
// since we navigate on submit, we need this to not trigger the confirmation modal
const hasUnsavedChanges = isDirty && !isSubmitSuccessful;
const hasUnsavedChanges = checkHasChanges(defaultValues, formMethods.getValues());
const navModel = useMemo(() => {
return isExistingCheck
? createNavModel(
Expand Down
4 changes: 4 additions & 0 deletions src/components/CheckForm/checkForm.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { PROBES_FILTER_ID } from 'components/CheckEditor/CheckProbes/ProbesFilte
import { SCRIPT_TEXTAREA_ID } from 'components/CheckEditor/FormComponents/ScriptedCheckScript';
import { CHECK_FORM_ERROR_EVENT } from 'components/constants';

export function checkHasChanges(existing: CheckFormValues, incoming: CheckFormValues) {
return JSON.stringify(existing) !== JSON.stringify(incoming);
}

export function flattenKeys(errs: FieldErrors<CheckFormValues>) {
const build: string[] = [];

Expand Down

0 comments on commit 4ad984d

Please sign in to comment.