Skip to content

Commit ec29a32

Browse files
committed
K this ain't amazing, but it does the job
1 parent 3c4dcf0 commit ec29a32

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

src/components/Section.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { Button, Card, Collapse, Flex, Heading, Menu } from "@contentful/f36-com
44
import Settings from "./Settings";
55
import BlocksList from "./BlocksList";
66

7-
const Section = ({ config, field }: SectionProps) => {
7+
const Section = ({ config, field, locale }: SectionProps) => {
88
const settings = config?.config?.settings || [];
99
const blocks = config?.config?.blocks || [];
10-
const [value, setValue] = useFieldValue<any>(config.field);
10+
const [value, setValue] = useFieldValue<any>(config.field, locale);
1111
const [isExpanded, setIsExpanded] = useState(false);
1212
return (
1313
<div style={{
@@ -71,6 +71,7 @@ const Section = ({ config, field }: SectionProps) => {
7171
export interface SectionProps {
7272
config: any;
7373
field: any;
74+
locale?: any;
7475
}
7576

7677
export default Section

src/locations/Field.tsx

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,75 @@
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';
33
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';
66
const Field = () => {
77
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+
);
873
/*
974
To use the cma, inject it as follows.
1075
If it is not needed, you can remove the next line.

0 commit comments

Comments
 (0)