Skip to content

Commit

Permalink
feat: automatic update of slug field appearance settings to use the b…
Browse files Browse the repository at this point in the history
…etter slugs widget
  • Loading branch information
pauloamgomes committed Apr 3, 2024
1 parent e02cb07 commit e58069f
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 225 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Install the App using by doing the below steps:

1. Create a new Contentful custom App and define the Application Name (e.g. Better Slugs)

2. Download [dist.zip](https://github.com/pauloamgomes/contentful-better-slugs-app/releases/download/V1.0.2/dist.zip), unzip and add the resulting folder into the Bundles upload zone:
2. Download [dist.zip](https://github.com/pauloamgomes/contentful-better-slugs-app/releases/download/V1.0.3/dist.zip), unzip and add the resulting folder into the Bundles upload zone:
(or download/clone this repo and use the dist folder):

![App Bundles Upload](./docs/app-bundles.png)
Expand All @@ -44,7 +44,7 @@ exactly as per below screenshot:

Configure the options you want (check the configuration section further below in this document)

6. Go to the Content Model definitions and set the Better Slugs app in the Appearance tab:
6. Go to the Content Model definitions, edit the slug field settings, and confirm that the Better Slugs app is selected inside the Appearance section:

![Apps Menu](./docs/app-field-config.png)

Expand Down
66 changes: 33 additions & 33 deletions dist/assets/index.11c8f708.js → dist/assets/index.d728fac3.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script type="module" crossorigin src="./assets/index.11c8f708.js"></script>
<script type="module" crossorigin src="./assets/index.d728fac3.js"></script>
</head>

<body>
Expand Down
Binary file modified docs/app-field-config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 0 additions & 10 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@ import { locations } from '@contentful/app-sdk';
import { useSDK } from '@contentful/react-apps-toolkit';
import { useMemo } from 'react';
import ConfigScreen from './locations/ConfigScreen';
import Dialog from './locations/Dialog';
import EntryEditor from './locations/EntryEditor';
import Field from './locations/Field';
import Page from './locations/Page';
import Sidebar from './locations/Sidebar';
import Home from './locations/Home';

const ComponentLocationSettings = {
[locations.LOCATION_APP_CONFIG]: ConfigScreen,
[locations.LOCATION_ENTRY_FIELD]: Field,
[locations.LOCATION_ENTRY_EDITOR]: EntryEditor,
[locations.LOCATION_DIALOG]: Dialog,
[locations.LOCATION_ENTRY_SIDEBAR]: Sidebar,
[locations.LOCATION_PAGE]: Page,
[locations.LOCATION_HOME]: Home,
};

const App = () => {
Expand Down
9 changes: 5 additions & 4 deletions src/components/AvailableTokens.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FieldAppSDK } from "@contentful/app-sdk";
import {
Flex,
IconButton,
Expand All @@ -6,7 +7,7 @@ import {
Text,
} from "@contentful/f36-components";
import { CopyIcon } from "@contentful/f36-icons";
import { useCMA } from "@contentful/react-apps-toolkit";
import { useSDK } from "@contentful/react-apps-toolkit";

import { css } from "emotion";
import { useEffect, useState } from "react";
Expand All @@ -28,13 +29,13 @@ const AvailableTokens = ({
activeSlugField: string;
onClick: (token: string) => void;
}) => {
const cma = useCMA();
const sdk = useSDK<FieldAppSDK>();
const [tokens, setTokens] = useState<Record<string, string>>({});
const [loading, setLoading] = useState<boolean>(true);

const getLinkModelValidTokenFields = async (linkModelName: string) => {
const validFields: string[] = [];
const contentModel = await cma.contentType.get({
const contentModel = await sdk.cma.contentType.get({
contentTypeId: linkModelName,
});

Expand All @@ -52,7 +53,7 @@ const AvailableTokens = ({
useEffect(() => {
const fetchModel = async () => {
const newTokens: Record<string, string> = {};
const contentModel = await cma.contentType.get({ contentTypeId: name });
const contentModel = await sdk.cma.contentType.get({ contentTypeId: name });
const fields = contentModel.fields.filter(
(field) => field.id !== activeSlugField
);
Expand Down
7 changes: 4 additions & 3 deletions src/components/ContentModelsDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ConfigAppSDK } from "@contentful/app-sdk";
import { FormControl, Select } from "@contentful/f36-components";
import { useCMA } from "@contentful/react-apps-toolkit";
import { useSDK } from "@contentful/react-apps-toolkit";
import { useEffect, useState } from "react";

interface IContentModelsDropdown {
Expand All @@ -13,12 +14,12 @@ export default function ContentModelsDropdown({
configuredModels = {},
onChange,
}: IContentModelsDropdown) {
const cma = useCMA();
const sdk = useSDK<ConfigAppSDK>();
const [models, setModels] = useState<Record<string, string>>({});

useEffect(() => {
const fetchModels = async () => {
const contentModels = await cma.contentType.getMany({ limit: 1000 });
const contentModels = await sdk.cma.contentType.getMany({ limit: 1000 });
const contentModelsDefaults = contentModels.items.reduce(
(acc: Record<string, string>, item) => {
acc[item.sys.id] = item.name;
Expand Down
5 changes: 2 additions & 3 deletions src/components/SlugFieldsDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConfigAppSDK } from "@contentful/app-sdk";
import { FormControl, Select, TextLink } from "@contentful/f36-components";
import { useCMA, useSDK } from "@contentful/react-apps-toolkit";
import { useSDK } from "@contentful/react-apps-toolkit";
import { useEffect, useState } from "react";

interface ISlugFieldsDropdownProps {
Expand All @@ -15,13 +15,12 @@ export default function SlugFieldsDropdown({
onChange,
}: ISlugFieldsDropdownProps) {
const sdk = useSDK<ConfigAppSDK>();
const cma = useCMA();
const [loading, setLoading] = useState<boolean>(true);
const [fields, setFields] = useState<Record<string, string>>({});

useEffect(() => {
const fetchFields = async () => {
const contentModel = await cma.contentType.get({ contentTypeId: model });
const contentModel = await sdk.cma.contentType.get({ contentTypeId: model });
const fields = contentModel.fields
.filter((field) => field.type === "Symbol" || field.type === "Text")
.reduce((acc: Record<string, string>, field) => {
Expand Down
28 changes: 24 additions & 4 deletions src/locations/ConfigScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
Pill,
} from "@contentful/f36-components";
import { PlusIcon } from "@contentful/f36-icons";
import { useCMA, useSDK } from "@contentful/react-apps-toolkit";
import { useSDK } from "@contentful/react-apps-toolkit";
import { css } from "emotion";
import { useCallback, useEffect, useReducer, useState } from "react";
import SlugFieldsDropdown from "../components/SlugFieldsDropdown";
Expand Down Expand Up @@ -57,7 +57,6 @@ interface IState {
const ConfigScreen = () => {
const [parameters, setParameters] = useState<AppInstallationParameters>({});
const sdk = useSDK<ConfigAppSDK>();
const cma = useCMA();

const [state, setState] = useReducer(
(state: IState, newState: Partial<IState>) => ({
Expand All @@ -82,6 +81,24 @@ const ConfigScreen = () => {
}
});

const updateModels = async () => {
for (const key of Object.keys(parameters.models || {})) {
const editorInterface = await sdk.cma.editorInterface.get({ contentTypeId: key });
const controlIdx = editorInterface?.controls?.findIndex(control => control.fieldId === parameters.models?.[key].slugField);
if (controlIdx) {
const fieldControl = editorInterface?.controls?.[controlIdx];
if (editorInterface.controls && controlIdx && fieldControl?.fieldId === parameters.models?.[key].slugField && fieldControl?.widgetId !== sdk.ids.app) {
editorInterface.controls[controlIdx].widgetId = sdk.ids.app;
editorInterface.controls[controlIdx].widgetNamespace = "app";
await sdk.cma.editorInterface.update({ contentTypeId: key }, { sys: editorInterface.sys, controls: editorInterface.controls });
sdk.notifier.success(
`Updated slug field appearance settings in ${key} to use Better Slugs.`
);
}
}
}
}

const onConfigure = useCallback(async () => {
// This method will be called when a user clicks on "Install"
// or "Save" in the configuration screen.
Expand All @@ -104,12 +121,15 @@ const ConfigScreen = () => {
// `onConfigure` allows to configure a callback to be
// invoked when a user attempts to install the app or update
// its configuration.
sdk.app.onConfigure(() => onConfigure());
sdk.app.onConfigure(async () => {
await updateModels();
return onConfigure();
});
}, [sdk, onConfigure]);

useEffect(() => {
(async () => {
const contentModels = await cma.contentType.getMany({ limit: 1000 });
const contentModels = await sdk.cma.contentType.getMany({ limit: 1000 });
const contentModelsDefaults = contentModels.items.reduce(
(acc: Record<string, string>, item) => {
acc[item.sys.id] = item.name;
Expand Down
17 changes: 0 additions & 17 deletions src/locations/Dialog.spec.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions src/locations/Dialog.tsx

This file was deleted.

17 changes: 0 additions & 17 deletions src/locations/EntryEditor.spec.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions src/locations/EntryEditor.tsx

This file was deleted.

17 changes: 0 additions & 17 deletions src/locations/Home.spec.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions src/locations/Home.tsx

This file was deleted.

17 changes: 0 additions & 17 deletions src/locations/Page.spec.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions src/locations/Page.tsx

This file was deleted.

17 changes: 0 additions & 17 deletions src/locations/Sidebar.spec.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions src/locations/Sidebar.tsx

This file was deleted.

0 comments on commit e58069f

Please sign in to comment.