Skip to content

Commit

Permalink
Merge pull request #161 from pluginpal/feature/60-use-same-collection…
Browse files Browse the repository at this point in the history
…-with-different-patterns

Feature/60 use same collection with different patterns
  • Loading branch information
boazpoolman authored Nov 10, 2024
2 parents c32aee9 + a187578 commit d82e427
Show file tree
Hide file tree
Showing 14 changed files with 579 additions and 450 deletions.
86 changes: 43 additions & 43 deletions .github/workflows/deploy-test-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,54 +27,54 @@ jobs:
environment:
name: Test
url: https://test.pluginpal.io
steps:
- name: Checkout repository
uses: actions/checkout@v2
# steps:
# - name: Checkout repository
# uses: actions/checkout@v2

- name: Set up Docker
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'yarn'
# - name: Set up Docker
# uses: actions/setup-node@v3
# with:
# node-version: 18
# cache: 'yarn'

- name: Install plugin dependencies
run: yarn install
# - name: Install plugin dependencies
# run: yarn install

- name: Build the packages
run: yarn run build
# - name: Build the packages
# run: yarn run build

- name: Install de playground dependencies
run: yarn playground:install
# - name: Install de playground dependencies
# run: yarn playground:install

- name: Build a Docker image of the playground
run: |
cd playground
docker build \
--build-arg PUBLIC_URL=${{ secrets.TEST_URL }} \
-t strapi-playground:latest .
docker save -o strapi-playground-latest.tar strapi-playground:latest
# - name: Build a Docker image of the playground
# run: |
# cd playground
# docker build \
# --build-arg PUBLIC_URL=${{ secrets.TEST_URL }} \
# -t strapi-playground:latest .
# docker save -o strapi-playground-latest.tar strapi-playground:latest

- name: Transfer the Docker image to the Dokku server
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_CI_USERNAME }}
password: ${{ secrets.SSH_CI_PASSWORD }}
source: playground/strapi-playground-latest.tar
target: /var/lib/dokku/data/storage/strapi/docker-images
# - name: Transfer the Docker image to the Dokku server
# uses: appleboy/[email protected]
# with:
# host: ${{ secrets.SSH_HOST }}
# username: ${{ secrets.SSH_CI_USERNAME }}
# password: ${{ secrets.SSH_CI_PASSWORD }}
# source: playground/strapi-playground-latest.tar
# target: /var/lib/dokku/data/storage/strapi/docker-images

- name: Deploy the Dokku app based on the Docker image
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_CI_USERNAME }}
password: ${{ secrets.SSH_CI_PASSWORD }}
script_stop: true
script: |
sudo docker load -i /var/lib/dokku/data/storage/strapi/docker-images/playground/strapi-playground-latest.tar
STRAPI_LATEST_IMAGE=$(sudo docker images --format "{{.ID}}" strapi-playground:latest)
sudo docker tag strapi-playground:latest strapi-playground:$STRAPI_LATEST_IMAGE
dokku git:from-image strapi strapi-playground:$STRAPI_LATEST_IMAGE
sudo docker system prune --all --force
sudo rm -rf /var/lib/dokku/data/storage/strapi/docker-images/playground/strapi-playground-latest.tar
# - name: Deploy the Dokku app based on the Docker image
# uses: appleboy/[email protected]
# with:
# host: ${{ secrets.SSH_HOST }}
# username: ${{ secrets.SSH_CI_USERNAME }}
# password: ${{ secrets.SSH_CI_PASSWORD }}
# script_stop: true
# script: |
# sudo docker load -i /var/lib/dokku/data/storage/strapi/docker-images/playground/strapi-playground-latest.tar
# STRAPI_LATEST_IMAGE=$(sudo docker images --format "{{.ID}}" strapi-playground:latest)
# sudo docker tag strapi-playground:latest strapi-playground:$STRAPI_LATEST_IMAGE
# dokku git:from-image strapi strapi-playground:$STRAPI_LATEST_IMAGE
# sudo docker system prune --all --force
# sudo rm -rf /var/lib/dokku/data/storage/strapi/docker-images/playground/strapi-playground-latest.tar

11 changes: 6 additions & 5 deletions packages/core/admin/api/url-alias.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useFetchClient } from '@strapi/helper-plugin';
import { UrlAliasEntity } from '../types/url-aliases';

export const useCreateUrlAlias = () => {
const { post } = useFetchClient();

const createUrlAlias = async (body: { id: number }, slug: string) => {
return post('/webtools/url-alias/create', {
return post<UrlAliasEntity>('/webtools/url-alias/create', {
data: {
...body,
contenttype: slug,
Expand All @@ -17,19 +18,19 @@ export const useCreateUrlAlias = () => {

export const useUpdateUrlAlias = () => {
const { put } = useFetchClient();

const updateUrlAlias = async (body: { id: number }, slug: string) => {
return put(`/webtools/url-alias/update/${body.id}`, {
const updateUrlAliases = async (body: { id: number }, slug: string) => {
return put<UrlAliasEntity>(`/webtools/url-alias/update/${body.id}`, {
data: {
...body,
contenttype: slug,
},
});
};

return { updateUrlAlias };
return { updateUrlAliases };
};


export const useDeleteUrlAlias = () => {
const { post } = useFetchClient();

Expand Down
112 changes: 68 additions & 44 deletions packages/core/admin/components/EditForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,88 @@ import {
TextInput,
Checkbox,
Link,
Accordion,
AccordionToggle,
AccordionContent,
} from '@strapi/design-system';

import getTrad from '../../helpers/getTrad';
import { UrlAliasEntity } from '../../types/url-aliases';

const EditForm = () => {
const { modifiedData, onChange } = useCMEditViewDataManager();
const modifiedDataUrlAlias = modifiedData.url_alias as UrlAliasEntity;
const modifiedDataUrlAliases =
(modifiedData.url_alias as UrlAliasEntity[])?.length
? modifiedData.url_alias as UrlAliasEntity[]
: [{
generated: true,
}] as UrlAliasEntity[];
const { formatMessage } = useIntl();

const updateValue = (name: string, value: string | number) => {
onChange({ target: { name: `url_alias.${name}`, value, type: 'text' } });
const updateValue = (index: number, name: string, value: string | number) => {
const updatedUrlAliases = [...modifiedDataUrlAliases];

updatedUrlAliases[index] = {
...updatedUrlAliases[index],
[name]: value,
};
onChange({ target: { name: 'url_alias', value: updatedUrlAliases, type: 'array' } });
};

const [expanded, setExpanded] = React.useState<number>(0);
// eslint-disable-next-line max-len
const toggle = (index: number) => setExpanded((prevExpanded) => (prevExpanded === index ? null : index));

return (
<Stack size={2}>
<Box>
<Box>
<Checkbox
onValueChange={(value: string) => {
updateValue('generated', value);
}}
value={
modifiedDataUrlAlias?.generated !== undefined
? modifiedDataUrlAlias?.generated
: true
}
name="generated"
hint="Uncheck this to create a custom alias below."
{modifiedDataUrlAliases?.map((alias, index) => (
<Accordion
key={alias.id}
value={`acc-${alias.id}`}
expanded={expanded === index}
onToggle={() => toggle(index)}
variant="secondary"
size="S"
>
<AccordionToggle
description={alias.url_path ? alias.url_path : 'Initial URL alias'}
>
{formatMessage({
id: getTrad('EditView.ExcludeFromSitemap'),
defaultMessage: ' Generate automatic URL alias',
})}
</Checkbox>
<Link to="/plugins/webtools/patterns">
Configure URL alias patterns.
</Link>
</Box>
<Box paddingTop={4}>
<TextInput
label="URL alias"
name="path"
hint='Specify a path by which this data can be accessed in the browser. For example, type "/about" when writing an about page.'
disabled={
modifiedDataUrlAlias?.generated !== undefined
? modifiedDataUrlAlias?.generated
: true
}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.value.match(/^[A-Za-z0-9-_.~[\]/]*$/)) {
updateValue('url_path', e.target.value);
}
}}
value={modifiedDataUrlAlias?.url_path}
/>
</Box>
</Box>
{`Alias #${index + 1}`}
</AccordionToggle>
<AccordionContent padding="12px">
<Box>
<Checkbox
onValueChange={(value: string) => {
updateValue(index, 'generated', value);
}}
value={alias.generated !== undefined ? alias.generated : true}
name={`generated-${index}`}
hint="Uncheck this to create a custom alias below."
>
{formatMessage({
id: getTrad('EditView.ExcludeFromSitemap'),
defaultMessage: ' Generate automatic URL alias',
})}
</Checkbox>
<Link to="/plugins/webtools/patterns">Configure URL alias patterns.</Link>
</Box>
<Box paddingTop={4}>
<TextInput
label="URL alias"
name={`path-${index}`}
hint='Specify a path by which this data can be accessed in the browser. For example, type "/about" when writing an about page.'
disabled={alias.generated !== undefined ? alias.generated : true}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.value.match(/^[A-Za-z0-9-_.~[\]/]*$/)) {
updateValue(index, 'url_path', e.target.value);
}
}}
value={alias.url_path}
/>
</Box>
</AccordionContent>
</Accordion>
))}
</Stack>
);
};
Expand Down
50 changes: 35 additions & 15 deletions packages/core/admin/components/EditView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,54 @@ const EditView = () => {
slug,
} = useCMEditViewDataManager();

const modifiedDataUrlAlias = modifiedData.url_alias as UrlAliasEntity;

const modifiedDataUrlAliases = modifiedData.url_alias as UrlAliasEntity[];
const i18nLang = new URLSearchParams(window.location.search).get('plugins[i18n][locale]');

useEffect(() => {
// Early return for when the i18n plugin is not enabled.
if (!i18nLang) return;

// Map through all url_aliases and clear those that do not match the current i18n language
// If the URL alias is not the same language as the entity,
// we should clear it. This happens when you're copying content
// from a different locale.
if (modifiedDataUrlAlias?.locale !== i18nLang) {
onChange({ target: { name: 'url_alias', value: null, type: 'text' } });
const updatedUrlAliases = modifiedDataUrlAliases?.map((alias) => {
if (alias?.locale !== i18nLang) {
return { ...alias, locale: null }; // Clear the alias if the locale doesn't match
}
return alias;
});

// If the URL aliases have changed, update the form data
// We fire the onChange here because we don't want unnecessary re-renders
if (JSON.stringify(updatedUrlAliases) !== JSON.stringify(modifiedDataUrlAliases)) {
onChange({ target: { name: 'url_alias', value: updatedUrlAliases, type: 'array' } });
}
}, [modifiedDataUrlAlias, onChange, i18nLang]);
}, [modifiedDataUrlAliases, onChange, i18nLang]);

const { createUrlAlias } = useCreateUrlAlias();
const { updateUrlAlias } = useUpdateUrlAlias();
const { updateUrlAliases } = useUpdateUrlAlias();

if (!allLayoutData.contentType) return null;

if (!isContentTypeEnabled(allLayoutData.contentType)) return null;
const modifiedUrlAlias = modifiedData.url_alias as UrlAliasEntity;
const initialUrlAlias = initialData.url_alias as UrlAliasEntity;
const modifiedUrlAliases = modifiedData.url_alias as UrlAliasEntity[];
const initialUrlAliases = initialData.url_alias as UrlAliasEntity[];

const onSubmit = async () => {
if (!initialUrlAlias) {
const urlAlias = await createUrlAlias(modifiedUrlAlias, slug);
onChange({ target: { name: 'url_alias', value: urlAlias, type: 'text' } });
if (!initialUrlAliases || initialUrlAliases?.length === 0) {
// Create new URL aliases
const newAliases = await Promise.all(
modifiedUrlAliases.map(async (alias) => (await createUrlAlias(alias, slug)).data),
);

onChange({ target: { name: 'url_alias', value: newAliases, type: 'array' } });
} else {
await updateUrlAlias(modifiedUrlAlias, slug);
// Update existing URL aliases
await Promise.all(
modifiedUrlAliases.map((alias) => updateUrlAliases(alias, slug)),
);
}
};

Expand All @@ -62,17 +80,19 @@ const EditView = () => {
})}
onSubmit={onSubmit}
onCancel={() => {
if (initialUrlAlias) {
onChange({ target: { name: 'url_alias', value: initialUrlAlias, type: 'text' } });
if (modifiedUrlAliases?.length > 0) {
onChange({ target: { name: 'url_alias', value: modifiedUrlAliases, type: 'array' } });
} else if (initialUrlAliases?.length > 0) {
onChange({ target: { name: 'url_alias', value: initialUrlAliases, type: 'array' } });
} else {
onChange({ target: { name: 'url_alias', value: null, type: 'text' } });
onChange({ target: { name: 'url_alias', value: null, type: 'array' } });
}
}}
>
<EditForm />
</SidebarModal>
<Permalink
path={modifiedUrlAlias?.url_path}
path={modifiedUrlAliases?.length > 0 ? modifiedUrlAliases[0].url_path : ''}
/>
</CheckPermissions>
);
Expand Down
Loading

0 comments on commit d82e427

Please sign in to comment.