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

fix(dashboard): tags client side validation #7325

Merged
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion apps/dashboard/src/components/create-workflow-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,15 @@ export const CreateWorkflowButton = (props: CreateWorkflowButtonProps) => {
<FormLabel hint={`(max. ${MAX_TAG_ELEMENTS})`}>Add tags</FormLabel>
</div>
<FormControl>
<TagInput suggestions={tags.map((tag) => tag.name)} {...field} value={field.value ?? []} />
<TagInput
suggestions={tags.map((tag) => tag.name)}
{...field}
value={field.value ?? []}
onChange={(tags) => {
field.onChange(tags);
form.setValue('tags', tags, { shouldValidate: true });
}}
/>
</FormControl>
<FormMessage />
</FormItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const ConfigureWorkflowForm = (props: ConfigureWorkflowFormProps) => {
form,
isReadOnly,
save: update,
shouldClientValidate: true,
});

const onPauseWorkflow = (active: boolean) => {
Expand Down
13 changes: 9 additions & 4 deletions apps/dashboard/src/hooks/use-form-autosave.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// useFormAutosave.ts
import { useCallback, useEffect } from 'react';
import { UseFormReturn, FieldValues } from 'react-hook-form';
import { useDataRef } from '@/hooks/use-data-ref';
import { useDebounce } from '@/hooks/use-debounce';
import { useCallback, useEffect } from 'react';
import { FieldValues, UseFormReturn } from 'react-hook-form';

const TEN_SECONDS = 10 * 1000;

export function useFormAutosave<U extends Record<string, unknown>, T extends FieldValues = FieldValues>({
previousData,
form: propsForm,
isReadOnly,
shouldClientValidate = false,
save,
}: {
previousData: U;
form: UseFormReturn<T>;
isReadOnly?: boolean;
shouldClientValidate?: boolean;
save: (data: U) => void;
}) {
const formRef = useDataRef(propsForm);
Expand All @@ -33,7 +35,10 @@ export function useFormAutosave<U extends Record<string, unknown>, T extends Fie
return;
}
// manually trigger the validation of the form
await form.trigger();
const isValid = await form.trigger();
if (!isValid && shouldClientValidate) {
return;
}

const values = { ...previousData, ...data };
// reset the dirty fields right away because on slow networks the patch request might take a while
Expand All @@ -42,7 +47,7 @@ export function useFormAutosave<U extends Record<string, unknown>, T extends Fie
form.reset(values, { keepErrors: true });
save(values);
},
[formRef, previousData, isReadOnly, save]
[formRef, previousData, isReadOnly, save, shouldClientValidate]
);

const debouncedOnSave = useDebounce(onSave, TEN_SECONDS);
Expand Down
Loading