Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dashboard): digest window preserve regular/scheduled tab form changes #7340

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react';
import { Tabs } from '@radix-ui/react-tabs';
import { RiCalendarScheduleFill } from 'react-icons/ri';
import { useFormContext } from 'react-hook-form';
import { FieldValues, useFormContext } from 'react-hook-form';
import { TimeUnitEnum } from '@novu/shared';

import { FormField, FormLabel, FormMessagePure } from '@/components/primitives/form/form';
Expand All @@ -18,6 +18,8 @@ const REGULAR_DIGEST_TYPE = 'regular';
const SCHEDULED_DIGEST_TYPE = 'scheduled';
const TWO_SECONDS = 2000;

type PreservedFormValuesByType = { [key: string]: FieldValues | undefined };

export const DigestWindow = () => {
const { control, getFieldState, setValue, setError, getValues, trigger } = useFormContext();
const formValues = getValues();
Expand All @@ -26,12 +28,43 @@ export const DigestWindow = () => {
const [digestType, setDigestType] = useState(
typeof amount !== 'undefined' ? REGULAR_DIGEST_TYPE : SCHEDULED_DIGEST_TYPE
);
const [preservedFormValuesByType, setPreservedFormValuesByType] = useState<PreservedFormValuesByType>({
regular: undefined,
scheduled: undefined,
});
const amountField = getFieldState(`${AMOUNT_KEY}`);
const unitField = getFieldState(`${UNIT_KEY}`);
const cronField = getFieldState(`${CRON_KEY}`);
const regularDigestError = amountField.error || unitField.error;
const scheduledDigestError = cronField.error;

const handleDigestTypeChange = async (value: string) => {
// get the latest form values
const controlValues = getValues().controlValues;

// preserve the current form values
setPreservedFormValuesByType((old) => ({ ...old, [digestType]: { ...controlValues } }));
setDigestType(value);

// restore the preserved form values
const preservedFormValues = preservedFormValuesByType[value];
if (preservedFormValues) {
setValue(AMOUNT_KEY, preservedFormValues['amount'], { shouldDirty: true });
setValue(UNIT_KEY, preservedFormValues['unit'], { shouldDirty: true });
setValue(CRON_KEY, preservedFormValues['cron'], { shouldDirty: true });
} else if (value === SCHEDULED_DIGEST_TYPE) {
setValue(AMOUNT_KEY, undefined, { shouldDirty: true });
setValue(UNIT_KEY, undefined, { shouldDirty: true });
setValue(CRON_KEY, EVERY_MINUTE_CRON, { shouldDirty: true });
} else {
setValue(AMOUNT_KEY, '', { shouldDirty: true });
setValue(UNIT_KEY, TimeUnitEnum.SECONDS, { shouldDirty: true });
setValue(CRON_KEY, undefined, { shouldDirty: true });
}
await trigger();
saveForm();
};

return (
<div className="flex flex-col gap-2">
<FormLabel>
Expand All @@ -47,20 +80,7 @@ export const DigestWindow = () => {
e.preventDefault();
e.stopPropagation();
}}
onValueChange={async (value) => {
setDigestType(value);
if (value === SCHEDULED_DIGEST_TYPE) {
setValue(AMOUNT_KEY, undefined, { shouldDirty: true });
setValue(UNIT_KEY, undefined, { shouldDirty: true });
setValue(CRON_KEY, EVERY_MINUTE_CRON, { shouldDirty: true });
} else {
setValue(AMOUNT_KEY, '', { shouldDirty: true });
setValue(UNIT_KEY, TimeUnitEnum.SECONDS, { shouldDirty: true });
setValue(CRON_KEY, undefined, { shouldDirty: true });
}
await trigger();
saveForm();
}}
onValueChange={handleDigestTypeChange}
>
<div className="bg-neutral-alpha-50 flex flex-col rounded-lg border border-solid border-neutral-100">
<div className="rounded-t-lg p-2">
Expand Down
Loading