Skip to content

Commit f61b5b3

Browse files
committed
Update app create collection form
1 parent d69ab98 commit f61b5b3

File tree

8 files changed

+385
-203
lines changed

8 files changed

+385
-203
lines changed

packages/client/src/App.tsx

+1-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ export const App = () => (
2121
<StacApiProvider apiUrl={process.env.REACT_APP_STAC_API!}>
2222
<PluginConfigProvider config={config}>
2323
<Router>
24-
<Container
25-
mx='auto'
26-
p='5'
27-
bgColor='white'
28-
boxShadow='md'
29-
maxW='container.lg'
30-
>
24+
<Container mx='auto' p='5' bgColor='white' maxW='container.lg'>
3125
<Box
3226
as='header'
3327
borderBottom='1px dashed'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import React, { useMemo, useState } from 'react';
2+
import {
3+
PluginBox,
4+
useCollectionPlugins,
5+
WidgetRenderer
6+
} from '@stac-manager/data-core';
7+
import {
8+
Box,
9+
Button,
10+
Flex,
11+
Heading,
12+
Tab,
13+
TabList,
14+
TabPanel,
15+
TabPanels,
16+
Tabs,
17+
Text
18+
} from '@chakra-ui/react';
19+
import { Formik, FormikHelpers } from 'formik';
20+
import { WidgetJSON } from '@stac-manager/data-widgets';
21+
22+
type FormView = 'fields' | 'json';
23+
24+
export function EditForm(props: {
25+
initialData?: any;
26+
onSubmit: (data: any, formikHelpers: FormikHelpers<any>) => void;
27+
}) {
28+
const { initialData, onSubmit } = props;
29+
const [stacData, setStacData] = useState(initialData || {});
30+
31+
const { plugins, formData, toOutData, isLoading } =
32+
useCollectionPlugins(stacData);
33+
34+
const [view, setView] = useState<FormView>('fields');
35+
36+
const editorData = useMemo(
37+
() => (view === 'json' ? { jsonData: stacData } : formData),
38+
[view, formData]
39+
);
40+
41+
return (
42+
<Box>
43+
{isLoading ? (
44+
<Box>Loading plugins...</Box>
45+
) : (
46+
<Flex direction='column' gap={4}>
47+
<Formik
48+
validateOnChange={false}
49+
validateOnBlur={false}
50+
enableReinitialize
51+
initialValues={editorData}
52+
onSubmit={(values, actions) => {
53+
const exitData =
54+
view === 'json' ? values.jsonData : toOutData(values);
55+
return onSubmit(exitData, actions);
56+
}}
57+
>
58+
{({ handleSubmit, values, isSubmitting }) => (
59+
<Flex
60+
as='form'
61+
direction='column'
62+
gap={4}
63+
// @ts-expect-error Can't detect the as=form and throws error
64+
onSubmit={handleSubmit}
65+
>
66+
<Flex
67+
gap={4}
68+
p={4}
69+
borderRadius='lg'
70+
bg='base.50'
71+
justifyContent='space-between'
72+
position='sticky'
73+
top={0}
74+
zIndex={100}
75+
boxShadow='md'
76+
>
77+
<Box>
78+
<Text fontSize='sm'>
79+
{initialData && 'Edit '}Collection
80+
</Text>
81+
<Heading>
82+
{initialData
83+
? initialData.title || 'Untitled'
84+
: 'New Collection'}
85+
</Heading>
86+
</Box>
87+
<Button
88+
type='submit'
89+
isDisabled={isSubmitting}
90+
colorScheme='primary'
91+
size='md'
92+
alignSelf='flex-end'
93+
flexShrink={0}
94+
>
95+
{initialData ? 'Save' : 'Create'}
96+
</Button>
97+
</Flex>
98+
<Tabs
99+
isLazy
100+
variant='enclosed'
101+
index={view === 'fields' ? 0 : 1}
102+
onChange={(i) => {
103+
setView(['fields', 'json'][i] as FormView);
104+
setStacData(
105+
view === 'json' ? values.jsonData : toOutData(values)
106+
);
107+
}}
108+
>
109+
<TabList>
110+
<Tab bg='base.50a'>FORM</Tab>
111+
<Tab>JSON</Tab>
112+
</TabList>
113+
<TabPanels>
114+
<TabPanel p={0} display='flex' flexFlow='column' gap={4}>
115+
{view === 'fields' &&
116+
plugins.map((pl) => (
117+
<PluginBox key={pl.name} plugin={pl}>
118+
{({ field }) => (
119+
<Box
120+
p='16'
121+
borderRadius='lg'
122+
bg='base.50a'
123+
display='flex'
124+
flexDir='column'
125+
gap={8}
126+
>
127+
<Heading size='sm'>{pl.name}</Heading>
128+
<WidgetRenderer pointer='' field={field} />
129+
</Box>
130+
)}
131+
</PluginBox>
132+
))}
133+
</TabPanel>
134+
<TabPanel p={0} pt={4}>
135+
{view === 'json' && (
136+
<WidgetJSON
137+
field={{ type: 'json', label: 'Json Document' }}
138+
pointer='jsonData'
139+
/>
140+
)}
141+
</TabPanel>
142+
</TabPanels>
143+
</Tabs>
144+
</Flex>
145+
)}
146+
</Formik>
147+
</Flex>
148+
)}
149+
</Box>
150+
);
151+
}

0 commit comments

Comments
 (0)