diff --git a/src/plugins/strapi-plugin-internal-links/README.md b/src/plugins/strapi-plugin-internal-links/README.md index 797b72f..1da4faa 100644 --- a/src/plugins/strapi-plugin-internal-links/README.md +++ b/src/plugins/strapi-plugin-internal-links/README.md @@ -70,7 +70,12 @@ Add the custom field with the content-type builder or directly to the JSON. "pageSearchOptions": { "searchableFields": string[], "subTitlePath": string - } + }, + "externalApi": { + "enabled": boolean, + "apiUrl": string, + "labelPath": string, + "valuePath": string }, } // ... @@ -90,19 +95,6 @@ npm run build The options of the internal link has a option in the contet-type builder to add an external source to the link module. -```json -// ... -"options": { - "externalApi": { - "enabled": boolean, - "apiUrl": string, - "labelPath": string, - "valuePath": string - }, -}, -// ... -``` - If the external API is enabled, all fields are required (you can't make if statement in the option thing to make it required) so fill it in. diff --git a/src/plugins/strapi-plugin-internal-links/admin/src/api/fetch-source.ts b/src/plugins/strapi-plugin-internal-links/admin/src/api/fetch-source.ts index 82c8a20..adcd2ca 100644 --- a/src/plugins/strapi-plugin-internal-links/admin/src/api/fetch-source.ts +++ b/src/plugins/strapi-plugin-internal-links/admin/src/api/fetch-source.ts @@ -1,15 +1,24 @@ import getRequestUrl from '../utils/get-request-url'; -export type externalApiResult = { +type FetchSourceParams = { + fetchClient: any; + externalApiUrl: string; + inputValue: string; +}; + +export type ExternalApiResult = { data: Record; }; -export const fetchSource = async ( - { fetchClient }: Record, - externalApiUrl: string, - inputValue: string -): Promise => { +export const fetchSource = async ({ + fetchClient, + externalApiUrl, + inputValue +}: FetchSourceParams): Promise => { try { + if (!externalApiUrl) { + throw new Error('No URL field set in settings'); + } const { post } = fetchClient; const result = await post(getRequestUrl('source'), { data: { diff --git a/src/plugins/strapi-plugin-internal-links/admin/src/components/form/index.tsx b/src/plugins/strapi-plugin-internal-links/admin/src/components/form/index.tsx index 9aa0581..877eb77 100644 --- a/src/plugins/strapi-plugin-internal-links/admin/src/components/form/index.tsx +++ b/src/plugins/strapi-plugin-internal-links/admin/src/components/form/index.tsx @@ -39,10 +39,9 @@ const InternalLinkForm = ({ /(^https?:\/\/(www.)?[a-zA-Z0-9]{1,}.[^s]{2,}((\/[a-zA-Z0-9\-\_\=\?\%\&\#]{1,}){1,})?)\/?$|^mailto:[\w-\. +]+@([\w-]+\.)+[\w-]{2,4}$|^tel:((\+|00(\s|\s?\-\s?)?)[0-9]{2}(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)[0-9](((\s|\s?\-\s?)?[0-9]){1,})|^#[a-zA-Z0-9\,\[\]\-\_\=\?\%\&\#]{1,}$/ ); const { contentType, setContentTypeUid } = useContentTypeOptions(link.targetContentTypeUid); - const { page, setPageId, pageOptionsIsLoading } = usePageOptions(contentType, link.targetContentTypeId); + const { page, pageId, setPageId, pageOptionsIsLoading } = usePageOptions(contentType, link.targetContentTypeId); const { setPlatformId } = usePlatformOptions({ page, pageOptionsIsLoading }); - const [isExternalTab, setIsExternalTab] = useState(link.type === 'external'); - const [isSourceTab, setIsSourceTab] = useState(link.type === 'source'); + const [tabType, setTabType] = useState<'internal' | 'external' | 'source'>(link.type || INTERNAL_LINK_TYPE.INTERNAL); useEffect(() => { if (pluginConfig && useSinglePageType) { @@ -51,14 +50,13 @@ const InternalLinkForm = ({ }, [pluginConfig]); const checkTab = () => { - if (isExternalTab) { + if (tabType === INTERNAL_LINK_TYPE.EXTERNAL) { return 1; } - if (isSourceTab) { + if (tabType === INTERNAL_LINK_TYPE.SOURCE) { return 2; - } else { - return 0; } + return 0; }; const onContentTypeChange = (value: IContentTypeOption) => { @@ -236,16 +234,13 @@ const InternalLinkForm = ({ url: undefined })); if (selected === 0) { - setIsExternalTab(false); - setIsSourceTab(false); + setTabType(INTERNAL_LINK_TYPE.INTERNAL); setLink((value) => ({ ...value, type: INTERNAL_LINK_TYPE.INTERNAL })); } else if (selected === 1) { - setIsExternalTab(true); - setIsSourceTab(false); + setTabType(INTERNAL_LINK_TYPE.EXTERNAL); setLink((value) => ({ ...value, type: INTERNAL_LINK_TYPE.EXTERNAL })); } else if (selected === 2) { - setIsExternalTab(false); - setIsSourceTab(true); + setTabType(INTERNAL_LINK_TYPE.SOURCE); setLink((value) => ({ ...value, type: INTERNAL_LINK_TYPE.SOURCE })); } }; @@ -259,7 +254,7 @@ const InternalLinkForm = ({ }; useLayoutEffect(() => { - setIsExternalTab(link.type === 'external'); + setTabType(link.type); }, []); useEffect(() => { @@ -302,6 +297,8 @@ const InternalLinkForm = ({ => { - const externalItems = await fetchSource({ fetchClient }, externalApiUrl, inputValue); + if (!externalApiUrl) { + throw new Error('No URL field set in settings'); + } + + const externalItems = await fetchSource({ fetchClient, externalApiUrl, inputValue }); if (!externalItems || !externalApiLabelpath || !externalApiValuepath) { return []; } @@ -61,7 +65,7 @@ export const ExternalApiSearch = ({ diff --git a/src/plugins/strapi-plugin-internal-links/admin/src/components/form/tabs/external-tab.tsx b/src/plugins/strapi-plugin-internal-links/admin/src/components/form/tabs/external-tab.tsx index 6c147bc..def8a84 100644 --- a/src/plugins/strapi-plugin-internal-links/admin/src/components/form/tabs/external-tab.tsx +++ b/src/plugins/strapi-plugin-internal-links/admin/src/components/form/tabs/external-tab.tsx @@ -20,7 +20,6 @@ interface Props { export const ExternalTab = ({ link, shouldShowTitle, - isExternalTab, errors, onTextBlur, onTextChange, @@ -28,7 +27,6 @@ export const ExternalTab = ({ onLinkChange }: Props) => { const { formatMessage } = useIntl(); - const translationLinkKey = !isExternalTab ? 'generated-link' : 'link'; return ( @@ -51,7 +49,7 @@ export const ExternalTab = ({ @@ -61,9 +59,8 @@ export const ExternalTab = ({ onChange={onLinkChange} onBlur={onLinkBlur} required - disabled={!isExternalTab} placeholder={formatMessage({ - id: getTrad(`internal-link.form.${translationLinkKey}.placeholder`) + id: getTrad('internal-link.form.link.placeholder') })} /> diff --git a/src/plugins/strapi-plugin-internal-links/admin/src/components/form/tabs/internal-tab.tsx b/src/plugins/strapi-plugin-internal-links/admin/src/components/form/tabs/internal-tab.tsx index 6ceda03..8642f9e 100644 --- a/src/plugins/strapi-plugin-internal-links/admin/src/components/form/tabs/internal-tab.tsx +++ b/src/plugins/strapi-plugin-internal-links/admin/src/components/form/tabs/internal-tab.tsx @@ -20,6 +20,8 @@ interface Props { isExternalTab?: boolean; errors: IInternalLinkErrors; attributeOptions?: IInternalLinkAttribute['options']; + contentType?: IContentTypeOption; + pageId?: number; onTextChange: (e: React.ChangeEvent) => void; onTextBlur: (e: React.FocusEvent) => void; onPageChange: (id?: number, path?: string, domain?: string) => void; @@ -32,9 +34,10 @@ interface Props { export const InternalTab = ({ link, shouldShowTitle, - isExternalTab, + pageId, errors, attributeOptions, + contentType, onTextBlur, onTextChange, onPageChange, @@ -44,9 +47,10 @@ export const InternalTab = ({ onPlatformChange }: Props) => { const { formatMessage } = useIntl(); - const { contentType, contentTypeOptions, contentTypeOptionsIsLoading, contentTypeOptionsIsFetching } = - useContentTypeOptions(link.targetContentTypeUid); - const { page, pageId, pageOptionsIsLoading } = usePageOptions(contentType, link.targetContentTypeId); + const { contentTypeOptions, contentTypeOptionsIsLoading, contentTypeOptionsIsFetching } = useContentTypeOptions( + link.targetContentTypeUid + ); + const { page, pageOptionsIsLoading } = usePageOptions(contentType, link.targetContentTypeId); const { data: pluginConfig, isLoading: isLoadingConfig } = useGetConfig({}); const useSinglePageType = !!pluginConfig?.useSinglePageType || pluginConfig?.pageBuilder?.enabled; const pageBuilderEnabled = pluginConfig?.pageBuilder?.enabled; @@ -84,7 +88,7 @@ export const InternalTab = ({ )} - {!isExternalTab && pageBuilderEnabled && platformOptions.length > 1 && ( + {pageBuilderEnabled && platformOptions.length > 1 && ( )} - {!isExternalTab && !isLoadingConfig && !useSinglePageType && ( + {!isLoadingConfig && !useSinglePageType && (