|
1 |
| -import React from 'react'; |
2 |
| -import { Paragraph } from '@contentful/f36-components'; |
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { Flex, Paragraph, Spinner, Text } from '@contentful/f36-components'; |
3 | 3 | import { FieldExtensionSDK } from '@contentful/app-sdk';
|
4 |
| -import { /* useCMA, */ useSDK } from '@contentful/react-apps-toolkit'; |
5 |
| - |
| 4 | +import { useCMA, useSDK } from '@contentful/react-apps-toolkit'; |
| 5 | +import Section from '../components/Section'; |
6 | 6 | const Field = () => {
|
7 | 7 | const sdk = useSDK<FieldExtensionSDK>();
|
| 8 | + sdk.window.startAutoResizer(); |
| 9 | + const [config, setConfig] = useState<any[]|null>(null); |
| 10 | + const cma = useCMA(); |
| 11 | + useEffect(() => { |
| 12 | + cma.entry.getMany({ |
| 13 | + query: { |
| 14 | + content_type: "customFieldDefinitions" |
| 15 | + } |
| 16 | + }).then((entries: any) => { |
| 17 | + const currentType = sdk.entry.getSys().contentType.sys.id; |
| 18 | + const fields = { |
| 19 | + [sdk.field.id]: sdk.entry.fields[sdk.field.id] |
| 20 | + }; |
| 21 | + const matchingConfigs = (entries?.items || []).map((entry: any) => { |
| 22 | + let currentConfig = entry?.fields?.config; |
| 23 | + currentConfig = currentConfig && Object.keys(currentConfig).length > 0 ? currentConfig[Object.keys(currentConfig).shift() || ''] : null; |
| 24 | + let currentTitle = entry?.fields?.title; |
| 25 | + currentTitle = currentTitle && Object.keys(currentTitle).length > 0 ? currentTitle[Object.keys(currentTitle).shift() || ''] : null; |
| 26 | + let currentModels = entry?.fields?.models; |
| 27 | + currentModels = currentModels && Object.keys(currentModels).length > 0 ? currentModels[Object.keys(currentModels).shift() || ''] : null; |
| 28 | + return { |
| 29 | + config: currentConfig, |
| 30 | + models: currentModels, |
| 31 | + title: currentTitle |
| 32 | + } |
| 33 | + }).filter((currentConfig: any) => { |
| 34 | + return (currentConfig.models || []).find((model: string) => { |
| 35 | + const parts = model.split(':'); |
| 36 | + const type = parts.shift(); |
| 37 | + const field = parts.shift() || ''; |
| 38 | + return (type === currentType || type === '*') && fields[field]; |
| 39 | + }) |
| 40 | + }); |
| 41 | + const finalConfig:any = {} |
| 42 | + matchingConfigs.forEach((currentConfig:any) => { |
| 43 | + (currentConfig.models || []).forEach((model: string) => { |
| 44 | + const parts = model.split(':'); |
| 45 | + const type = parts.shift(); |
| 46 | + const field = parts.shift() || ''; |
| 47 | + if (type === currentType || (type === '*' && fields[field])) { |
| 48 | + finalConfig[field] = { |
| 49 | + name: currentConfig.title, |
| 50 | + ...currentConfig.config |
| 51 | + }; |
| 52 | + } |
| 53 | + }) |
| 54 | + }) |
| 55 | + const configArray:any[] = []; |
| 56 | + Object.keys(finalConfig).forEach((key:string) => { |
| 57 | + configArray.push({ |
| 58 | + field: key, |
| 59 | + config: finalConfig[key] |
| 60 | + }) |
| 61 | + }) |
| 62 | + setConfig(configArray) |
| 63 | + }); |
| 64 | + }, [cma, sdk.entry]); |
| 65 | + return config ? ( |
| 66 | + config?.length ? <>{config.map((conf) => <Section field={sdk.entry.fields[conf.field]} config={conf} key={conf.field} locale={sdk.field.locale} />)}</> : <Text>Oops, no config found for this content type</Text> |
| 67 | + ) : ( |
| 68 | + <Flex> |
| 69 | + <Text marginRight="spacingXs">Loading</Text> |
| 70 | + <Spinner /> |
| 71 | + </Flex> |
| 72 | + ); |
8 | 73 | /*
|
9 | 74 | To use the cma, inject it as follows.
|
10 | 75 | If it is not needed, you can remove the next line.
|
|
0 commit comments