Skip to content

Commit

Permalink
JNG-5962 aggregation validation
Browse files Browse the repository at this point in the history
  • Loading branch information
noherczeg committed Oct 14, 2024
1 parent a73122e commit 6134909
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ import {
{{/ unless }}
type ServerError,
validateWithNestedErrors,
isErrorNestedValidationError,
extractRelationErrorList,
} from '~/utilities';
import type { SidekickComponentProps, DialogResult, TableRowAction, ToolBarActionProps, ColumnCustomizerHook, FiltersSerializer{{# if isMUILicensePlanPro }}, PersistedColumnInfo{{/ if }} } from '~/utilities';
import { OBJECTCLASS } from '@pandino/pandino-api';
Expand Down Expand Up @@ -156,7 +158,11 @@ export function {{ componentName table }}(props: {{ componentName table }}Props)
} catch(e: any) {
if (e?.response?.status === 400) {
const errorMap: Map<keyof {{ classDataName (getReferenceClassType table) 'Stored' }}, string> = new Map<keyof {{ classDataName (getReferenceClassType table) 'Stored' }}, string>();
const errorList = e.response.data as ServerError<{{ classDataName (getReferenceClassType table) 'Stored' }}>[];
{{# if table.isEager }}
// can be -1 if row is not yet written into ownerData
const rowIdx = (ownerData?.{{ table.dataElement.name }} || []).findIndex(r => r.__identifier === rowData.__identifier);
{{/ if }}
const errorList = isErrorNestedValidationError(e, dataPath) ? extractRelationErrorList<any, {{ classDataName (getReferenceClassType table) 'Stored' }}>(e.response.data, dataPath!{{# if table.isEager }}, rowIdx === -1 ? 0 : rowIdx{{/ if }}) : e.response.data as ServerError<{{ classDataName (getReferenceClassType table) 'Stored' }}>[];
if (errorList && errorList.length && errorList[0].location) {
validateWithNestedErrors(errorList, errorMap, t);
}
Expand Down
11 changes: 10 additions & 1 deletion judo-ui-react/src/main/resources/actor/src/dialogs/index.tsx.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import { useRef, useCallback, useEffect, useState, useMemo, lazy, Suspense } fro
simpleCloneDeep,
getValue,
setValue,
{{# if page.container.form }}
getErrorRelationKeyFromPayload,
{{/ if }}
} from '~/utilities';
import type {
DialogResult,
Expand Down Expand Up @@ -244,6 +247,9 @@ export default function {{ pageName page }}(props: {{ pageName page }}Props) {
{{# if table.isEager }}
if (rowData.__identifier!.startsWith(draftIdentifierPrefix)) {
const updatedList = [rowData, ...(data.{{ table.relationName }} || [])];
{{# if page.container.form }}
await validate({...data, {{ table.relationName }}: updatedList});
{{/ if }}
storeDiff('{{ table.dataElement.name }}', updatedList);
} else {
// we need to create a copy to keep order, and because mui datagrid freezes elements, and crashes on reference updates
Expand All @@ -252,6 +258,9 @@ export default function {{ pageName page }}(props: {{ pageName page }}Props) {
updatedList[existingIndex] = {
...rowData,
};
{{# if page.container.form }}
await validate({...data, {{ table.relationName }}: updatedList});
{{/ if }}
storeDiff('{{ table.dataElement.name }}', updatedList);
}
{{ else }}
Expand All @@ -268,7 +277,7 @@ export default function {{ pageName page }}(props: {{ pageName page }}Props) {
}
await refresh();
{{/ if }}
} catch(error) {
} catch(error: any) {
return Promise.reject(error);
}{{# unless table.isEager }} finally {
setIsLoading(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,12 @@ export default function {{ pageName page }}() {
const apply{{ firstToUpper table.relationName }}RowEdit = async (rowData: {{ classDataName (getReferenceClassType table) 'Stored' }}): Promise<any> => {
try {
{{# if table.isEager }}
{{# if page.container.view }}
await {{ getServiceImplForPage page }}.validateUpdate{{ firstToUpper table.relationName }}(
{ __signedIdentifier: signedIdentifier } as any,
rowData,
);
{{/ if }}
if (rowData.__identifier!.startsWith(draftIdentifierPrefix)) {
const updatedList = [rowData, ...(data.{{ table.relationName }} || [])];
storeDiff('{{ table.dataElement.name }}', updatedList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ export interface ErrorProcessResult<T> {
validation?: Map<keyof T, string>;
}

export const getErrorRelationKeyFromPayload = <T,>(error: ServerError<T>): keyof T | undefined => {
const split = error.location.split(/\[|\./).filter((e) => e.length);
return split.length > 1 ? (split[0] as keyof T) : undefined;
};

export const extractRelationErrorList = <T,R>(errorList: ServerError<T>[], relationKey: keyof T, idx = 0): ServerError<R>[] => {
const reg = new RegExp(`^${relationKey as string}\\[${idx}\\]\\.`);
return errorList.filter(e => e.location.match(reg)).map(e => ({
...e,
location: e.location.replace(reg, ''),
}));
}

export const useErrorHandler = () => {
const { t } = useTranslation();
const { showErrorSnack } = useSnacks();
Expand Down Expand Up @@ -163,8 +176,7 @@ export function validateWithNestedErrors<T>(errorList: ServerError<T>[], validat
const errorRelations: Record<string, Array<string>> = {};

errorList.forEach((error) => {
const split = error.location.split(/\[|\./).filter((e) => e.length);
const relationKey: keyof T | undefined = split.length > 1 ? (split[0] as keyof T) : undefined;
const relationKey: keyof T | undefined = getErrorRelationKeyFromPayload<T>(error);
if (relationKey) {
if(!Array.isArray(errorRelations[relationKey as string])) {
errorRelations[relationKey as string] = [];
Expand Down

0 comments on commit 6134909

Please sign in to comment.