From bad99e3acdba58810923deb8c9f242da750b48d7 Mon Sep 17 00:00:00 2001 From: George Desipris Date: Wed, 23 Oct 2024 15:40:15 +0300 Subject: [PATCH] feat(dashboard): Add handleValidationIssues util --- apps/dashboard/src/hooks/use-tags-query.ts | 2 +- .../src/utils/handleValidationIssues.ts | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 apps/dashboard/src/utils/handleValidationIssues.ts diff --git a/apps/dashboard/src/hooks/use-tags-query.ts b/apps/dashboard/src/hooks/use-tags-query.ts index c837464a05f..9fe17943cfe 100644 --- a/apps/dashboard/src/hooks/use-tags-query.ts +++ b/apps/dashboard/src/hooks/use-tags-query.ts @@ -6,7 +6,7 @@ import { getV2 } from '@/api/api.client'; export const useTagsQuery = () => { const { currentEnvironment } = useEnvironment(); const query = useQuery<{ data: { name: string }[] }>({ - queryKey: [QueryKeys.fetchWorkflow, currentEnvironment?._id], + queryKey: [QueryKeys.fetchTags, currentEnvironment?._id], queryFn: async () => await getV2(`/environments/${currentEnvironment!._id}/tags`), enabled: !!currentEnvironment?._id, }); diff --git a/apps/dashboard/src/utils/handleValidationIssues.ts b/apps/dashboard/src/utils/handleValidationIssues.ts new file mode 100644 index 00000000000..103d172ffed --- /dev/null +++ b/apps/dashboard/src/utils/handleValidationIssues.ts @@ -0,0 +1,27 @@ +import { FieldValues, UseFormSetError } from 'react-hook-form'; + +type ValidationIssues = Record< + string, + { + issueType: string; + message: string; + variableName: string; + }[] +>; + +type HandleValidationIssuesProps = { + fields: T; + issues: ValidationIssues; + setError: UseFormSetError; +}; +export const handleValidationIssues = (props: HandleValidationIssuesProps) => { + const { fields, issues, setError } = props; + + (Object.keys(issues) as Array).map((issueKey) => { + if (issueKey in fields) { + setError(issueKey as any, { message: issues[issueKey][0]?.message || 'Unknown error' }); + } else { + console.log(`Issue for ${issueKey} found and does not correspond to a field`); + } + }); +};