Skip to content

Commit e3612a1

Browse files
authored
Merge pull request #302 from jorrit/fixnoni18nupdate
Fixes for installations/content types without i18n
2 parents 6843cd7 + 6f1ca00 commit e3612a1

File tree

12 files changed

+76
-40
lines changed

12 files changed

+76
-40
lines changed

.changeset/better-symbols-smoke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"strapi-plugin-webtools": patch
3+
---
4+
5+
fix: alias gets -0 suffix when updating non-i18n document

packages/core/admin/containers/App/index.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Link,
77
} from 'react-router-dom';
88

9+
import { useIntl } from 'react-intl';
910
import {
1011
SubNav,
1112
SubNavHeader,
@@ -34,6 +35,7 @@ const App = () => {
3435
const {
3536
allowedActions: { canList, canPatterns, canOverview },
3637
} = useRBAC(pluginPermissions);
38+
const { formatMessage } = useIntl();
3739

3840
const plugin = getPlugin(pluginId);
3941

@@ -50,18 +52,18 @@ const App = () => {
5052
<SubNavSections>
5153
<SubNavSection label="Core">
5254
{canOverview && (
53-
<SubNavLink tag={Link} to="/plugins/webtools" key="test" className={currentPath === '/plugins/webtools' ? 'active' : ''}>
54-
Overview
55+
<SubNavLink tag={Link} to="/plugins/webtools" key="overview" className={currentPath === '/plugins/webtools' ? 'active' : ''}>
56+
{formatMessage({ id: 'webtools.settings.page.overview.title', defaultMessage: 'Overview' })}
5557
</SubNavLink>
5658
)}
5759
{canList && (
58-
<SubNavLink tag={Link} to="/plugins/webtools/urls" key="test" className={currentPath.startsWith('/plugins/webtools/urls') ? 'active' : ''}>
59-
All URLs
60+
<SubNavLink tag={Link} to="/plugins/webtools/urls" key="list" className={currentPath.startsWith('/plugins/webtools/urls') ? 'active' : ''}>
61+
{formatMessage({ id: 'webtools.settings.page.list.title', defaultMessage: 'All URLs' })}
6062
</SubNavLink>
6163
)}
6264
{canPatterns && (
63-
<SubNavLink tag={Link} to="/plugins/webtools/patterns" key="test" className={currentPath.startsWith('/plugins/webtools/patterns') ? 'active' : ''}>
64-
Url Patterns
65+
<SubNavLink tag={Link} to="/plugins/webtools/patterns" key="patterns" className={currentPath.startsWith('/plugins/webtools/patterns') ? 'active' : ''}>
66+
{formatMessage({ id: 'webtools.settings.page.patterns.title', defaultMessage: 'URL patterns' })}
6567
</SubNavLink>
6668
)}
6769
</SubNavSection>

packages/core/admin/screens/List/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const List = () => {
7474
return (
7575
<Page.Protect permissions={pluginPermissions['settings.list']}>
7676
<Layouts.Header
77-
title={formatMessage({ id: 'webtools.settings.page.list.title', defaultMessage: 'URLs' })}
77+
title={formatMessage({ id: 'webtools.settings.page.list.title', defaultMessage: 'All URLs' })}
7878
subtitle={formatMessage({ id: 'webtools.settings.page.list.description', defaultMessage: 'A list of all the known URL aliases.' })}
7979
primaryAction={(
8080
<GeneratePathsModal

packages/core/admin/screens/Patterns/ListPage/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const ListPatternPage = () => {
4141
<Page.Protect permissions={pluginPermissions['settings.patterns']}>
4242
<Box>
4343
<Layouts.Header
44-
title={formatMessage({ id: 'webtools.settings.page.patterns.title', defaultMessage: 'Patterns' })}
44+
title={formatMessage({ id: 'webtools.settings.page.patterns.title', defaultMessage: 'URL patterns' })}
4545
subtitle={formatMessage({ id: 'webtools.settings.page.patterns.description', defaultMessage: 'A list of all the known URL alias patterns.' })}
4646
primaryAction={(
4747
<Button onClick={() => navigate(`/plugins/${pluginId}/patterns/new`)} startIcon={<Plus />}>

packages/core/admin/translations/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"settings.form.pattern.description_3": "or",
2323
"settings.form.contenttype.label": "Content type",
2424

25+
"settings.page.overview.title": "Overview",
26+
"settings.page.overview.description": "Webtools global information",
27+
2528
"settings.page.list.title": "All URLs",
2629
"settings.page.list.description": "A list of all the known URL aliases.",
2730
"settings.page.list.body": "List all URL aliases",
@@ -65,5 +68,5 @@
6568
"settings.page.patterns.create.subtitle": "Pattern details",
6669
"settings.page.patterns.create.description": "Add a pattern for automatic URL alias generation.",
6770

68-
"notification.success.permalink_copied": "Permalinkk copied to the clipboard"
71+
"notification.success.permalink_copied": "Permalink copied to the clipboard"
6972
}

packages/core/admin/translations/nl.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"settings.form.pattern.description_3": "of",
2121
"settings.form.contenttype.label": "Inhoudstype",
2222

23+
"settings.page.overview.title": "Overzicht",
24+
"settings.page.overview.description": "Algemene Webtools informatie",
25+
2326
"settings.page.list.title": "Alle URL's",
2427
"settings.page.list.description": "Een lijst met alle bekende URL-aliassen.",
2528
"settings.page.list.body": "Laat alle URL-aliassen zien",

packages/core/server/middlewares/__tests__/middlewares.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,28 @@ describe('Query layer decorator', () => {
296296
expect(updatedPage).toHaveProperty('url_alias[0].url_path', url);
297297
});
298298

299+
it('Update - Should not duplicate check the same entry when updated - no i18n', async () => {
300+
const category = await strapi.documents('api::category.category').create({
301+
data: {
302+
title: 'A category',
303+
},
304+
populate: ['url_alias'],
305+
});
306+
307+
console.log(category);
308+
expect(category).toHaveProperty('url_alias[0].url_path', '/category/a-category');
309+
310+
const updatedCategory = await strapi.documents('api::category.category').update({
311+
documentId: category.documentId,
312+
data: {
313+
published_at: new Date(),
314+
},
315+
populate: ['url_alias'],
316+
});
317+
318+
expect(updatedCategory).toHaveProperty('url_alias[0].url_path', '/category/a-category');
319+
});
320+
299321
it('Delete - Should delete the corresponding URL alias as well', async () => {
300322
const page = await strapi.documents('api::test.test').create({
301323
data: {

packages/core/server/middlewares/generate-url-alias.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ const generateUrlAliasMiddleware: Modules.Documents.Middleware.Middleware = asyn
2121

2222
// Fetch the URL pattern for this content type.
2323
let relations: string[] = [];
24-
let languages: string[] = [undefined];
2524
let urlAliasEntity: Data.ContentType<'plugin::webtools.url-alias'> | undefined;
2625

27-
languages = [];
28-
const locales = await strapi.entityService.findMany('plugin::i18n.locale', {});
29-
languages = locales.map((locale) => locale.code);
26+
let languages: (string | undefined)[] = [undefined];
27+
if (params.locale) {
28+
const locales = await strapi.entityService.findMany('plugin::i18n.locale', {});
29+
languages = locales.map((locale) => locale.code);
30+
}
3031

3132
await Promise.all(languages.map(async (lang) => {
3233
const urlPatterns = await getPluginService('url-pattern').findByUid(uid, lang);

packages/core/server/services/bulk-generate.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,12 @@ const generateUrlAliases = async (params: GenerateParams): Promise<number> => {
3636

3737
let relations: string[] = [];
3838

39-
const locales = await strapi.documents('plugin::i18n.locale').findMany({});
40-
const languages = locales.map((locale) => locale.code);
41-
42-
// Get all relations for the type
43-
await Promise.all(languages.map(async (lang) => {
44-
const urlPatterns = await getPluginService('url-pattern').findByUid(type, lang);
45-
urlPatterns.forEach((urlPattern) => {
46-
const languageRelations = getPluginService('url-pattern').getRelationsFromPattern(urlPattern);
47-
relations = [...relations, ...languageRelations];
48-
});
49-
}));
39+
// Get all relations for the type from all patterns for all languages.
40+
const urlPatterns = await getPluginService('url-pattern').findByUid(type);
41+
urlPatterns.forEach((urlPattern) => {
42+
const languageRelations = getPluginService('url-pattern').getRelationsFromPattern(urlPattern);
43+
relations = [...relations, ...languageRelations];
44+
});
5045

5146
// Query all the entities of the type that do not have a corresponding URL alias.
5247
const entities = await strapi.documents(type as 'api::test.test').findMany({
@@ -70,9 +65,10 @@ const generateUrlAliases = async (params: GenerateParams): Promise<number> => {
7065
*/
7166
// eslint-disable-next-line no-restricted-syntax
7267
for (const entity of entities) {
68+
// FIXME: just filter the `urlPatterns` we already have.
7369
// eslint-disable-next-line no-await-in-loop
74-
const urlPatterns = await getPluginService('url-pattern').findByUid(type, entity.locale);
75-
const resolvedPath = getPluginService('url-pattern').resolvePattern(type, entity, urlPatterns[0]);
70+
const entityUrlPatterns = await getPluginService('url-pattern').findByUid(type, entity.locale);
71+
const resolvedPath = getPluginService('url-pattern').resolvePattern(type, entity, entityUrlPatterns[0]);
7672

7773
// eslint-disable-next-line no-await-in-loop
7874
const newUrlAlias = await strapi.documents('plugin::webtools.url-alias').create({

packages/core/server/services/url-alias.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const customServices = () => ({
7575
*
7676
* @param originalPath The path as generated from the pattern and document
7777
* @param currentDocumentId If generating for an existing document, its document id
78-
* @param currentLocale If generating for an existing document, its locale code
78+
* @param currentLocale If generating for an existing i18n document, its locale code
7979
*/
8080
makeUniquePath: async (
8181
originalPath: string,
@@ -98,16 +98,19 @@ const customServices = () => ({
9898
};
9999

100100
if (currentDocumentId) {
101+
const documentFilters: unknown[] = [{
102+
documentId: { $eq: currentDocumentId },
103+
}];
104+
105+
if (currentLocale) {
106+
documentFilters.push({
107+
locale: { $eq: currentLocale },
108+
});
109+
}
110+
101111
filters.$and.push({
102112
$not: {
103-
$and: [
104-
{
105-
documentId: { $eq: currentDocumentId },
106-
},
107-
{
108-
locale: { $eq: currentLocale },
109-
},
110-
],
113+
$and: documentFilters,
111114
},
112115
});
113116
}

0 commit comments

Comments
 (0)