Skip to content

Commit 8012de2

Browse files
authored
feat: extending schema validators and on update server errors details displayed (#576)
* feat: extending schema validators and on update server errors details displayed * fix: failing tests * chore: missing tests * chore: missing tests * fix: post rebase fixes
1 parent fb71af3 commit 8012de2

File tree

22 files changed

+432
-78
lines changed

22 files changed

+432
-78
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,26 @@ query {
653653

654654
## 🔌 Extensions
655655

656+
### Server-side schema
657+
658+
On bootstrap you can customise Zod validators. Not all schemas are available at the moment, consult `CommonService` to see all available updaters.
659+
660+
Example:
661+
662+
```ts
663+
const navigationCommonService = strapi.plugin('navigation').service('common');
664+
665+
navigationCommonService.updateUpdateNavigationSchema((schema: ZodObject) => {
666+
return schema.refine((data) => {
667+
if (!data.visible) {
668+
return false;
669+
}
670+
671+
return true;
672+
}, { message: "Hidden navigation updated." });
673+
});
674+
```
675+
656676
### Slug generation
657677

658678
Slug generation is available as a controller and service. If you have custom requirements outside of what this plugin provides you can add your own logic with [plugins extensions](https://docs.strapi.io/developer-docs/latest/development/plugins-extension.html).

admin/src/pages/HomePage/components/NavigationManager/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const NavigationManager = ({ initialState, isOpened, onClose }: Props) =>
4343

4444
const createNavigationMutation = useCreateNavigation();
4545

46-
const updateNavigationMutation = useUpdateNavigation();
46+
const updateNavigationMutation = useUpdateNavigation({});
4747

4848
const purgeNavigationsMutation = usePurgeNavigation();
4949

admin/src/pages/HomePage/hooks/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,13 @@ export const useCreateNavigation = () => {
179179
});
180180
};
181181

182-
export const useUpdateNavigation = (onSuccess?: Effect<NavigationSchema>) => {
182+
export const useUpdateNavigation = ({
183+
onError,
184+
onSuccess,
185+
}: {
186+
onSuccess?: Effect<NavigationSchema>;
187+
onError?: Effect<unknown>;
188+
}) => {
183189
const fetch = getFetchClient();
184190
const apiClient = getApiClient(fetch);
185191
// TODO: nicer cache update
@@ -194,6 +200,7 @@ export const useUpdateNavigation = (onSuccess?: Effect<NavigationSchema>) => {
194200

195201
onSuccess?.(next);
196202
},
203+
onError,
197204
});
198205
};
199206

admin/src/pages/HomePage/index.tsx

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
Typography,
99
} from '@strapi/design-system';
1010
import { Data } from '@strapi/strapi';
11-
import { Layouts, Page, useRBAC } from '@strapi/strapi/admin';
12-
import { QueryClient, QueryClientProvider, useQueryClient } from '@tanstack/react-query';
11+
import { Layouts, Page, useNotification, useRBAC } from '@strapi/strapi/admin';
12+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
1313
import { first, isEmpty } from 'lodash';
1414
import { SyntheticEvent, useCallback, useEffect, useMemo, useState } from 'react';
1515
import { useIntl } from 'react-intl';
@@ -79,6 +79,8 @@ const makeAction = <T,>({ cancel, perform, trigger }: MakeActionInput<T>) => {
7979
const Inner = () => {
8080
const { formatMessage } = useIntl();
8181

82+
const { toggleNotification } = useNotification();
83+
8284
const localeQuery = useLocale();
8385

8486
const [recentNavigation, setRecentNavigation] = useState<{ documentId?: string; id?: Data.ID }>();
@@ -268,18 +270,31 @@ const Inner = () => {
268270
i18nCopySourceLocale && setI18nCopyModalOpened(true);
269271
}, [setI18nCopyModalOpened, i18nCopySourceLocale]);
270272

271-
const updateNavigationMutation = useUpdateNavigation((next) => {
272-
setCurrentNavigation({
273-
...next,
274-
items: next.items.map(appendViewId),
275-
});
273+
const updateNavigationMutation = useUpdateNavigation({
274+
onError: (error: any) => {
275+
toggleNotification({
276+
type: 'danger',
277+
message: formatMessage(getTrad('notification.navigation.update.error')),
278+
});
276279

277-
setRecentNavigation({
278-
documentId: next.documentId,
279-
id: next.id,
280-
});
280+
try {
281+
console.error(error);
282+
console.log(error.response.data.error);
283+
} catch (e) {}
284+
},
285+
onSuccess: (next) => {
286+
setCurrentNavigation({
287+
...next,
288+
items: next.items.map(appendViewId),
289+
});
290+
291+
setRecentNavigation({
292+
documentId: next.documentId,
293+
id: next.id,
294+
});
281295

282-
setStructureChanged(false);
296+
setStructureChanged(false);
297+
},
283298
});
284299

285300
const submit = () => {

admin/src/translations/ca.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ const ca = {
200200
published: 'publié',
201201
},
202202
},
203+
update: {
204+
error: "No s'ha pogut desar la navegació. Comproveu les eines de desenvolupador.",
205+
},
203206
},
204207
error: {
205208
common: 'Erreur lors du traitement de la demande.',

admin/src/translations/en.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ const en = {
203203
published: 'published',
204204
},
205205
},
206+
update: {
207+
error: 'Unable to save a navigation. Check developer tools.',
208+
},
206209
},
207210
error: {
208211
common: 'Error while processing request.',

admin/src/translations/es.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ const en = {
204204
published: 'publicado',
205205
},
206206
},
207+
update: {
208+
error: 'No se ha podido guardar la navegación. Revisa las herramientas de desarrollador.',
209+
},
207210
},
208211
error: {
209212
common: 'Error al procesar la solicitud.',

admin/src/translations/fr.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ const fr = {
204204
published: 'publié',
205205
},
206206
},
207+
update: {
208+
error: 'Impossible d\'enregistrer la navigation. Vérifiez les outils de développement.',
209+
},
207210
},
208211
error: {
209212
common: 'Erreur lors du traitement de la demande.',

admin/src/translations/tr.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ const tr = {
203203
published: 'yayınlandı',
204204
},
205205
},
206+
update: {
207+
error: 'Navigasyon kaydedilemedi. Geliştirici araçlarını kontrol edin.',
208+
},
206209
},
207210
error: {
208211
common: 'İstek işlenirken hata oluştu.',

server/src/config/setup.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Core } from '@strapi/strapi';
22

3+
import { isEmpty } from 'lodash';
34
import configBase from '.';
45
import {
6+
ConfigSchema,
57
NavigationItemAdditionalField,
68
NavigationPluginConfigDBSchema,
79
PluginConfigKeys,
8-
configSchema,
10+
DynamicSchemas,
911
} from '../schemas';
1012
import {
1113
PluginConfigGraphQL,
@@ -14,7 +16,6 @@ import {
1416
PluginConfigPopulate,
1517
} from '../types';
1618
import { assertNotEmpty, resolveGlobalLikeId, validateAdditionalFields } from '../utils';
17-
import { isEmpty } from 'lodash';
1819

1920
type PluginDefaultConfigGetter = (
2021
key: PluginConfigKeys
@@ -41,7 +42,7 @@ export const configSetup = async ({
4142
})) ?? configBase.default),
4243
};
4344

44-
let config = isEmpty(configRaw) ? configRaw : configSchema.parse(configRaw);
45+
let config = isEmpty(configRaw) ? configRaw : DynamicSchemas.configSchema.parse(configRaw) as unknown as ConfigSchema;
4546

4647
const getWithFallback = getWithFallbackFactory(config, getFromPluginDefaults);
4748

0 commit comments

Comments
 (0)