-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.tsx
78 lines (72 loc) · 2.93 KB
/
schema.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { AnimatePresence } from 'framer-motion'
import Modal from '@components/generate/modal'
import Field from '@components/generate/field'
import AddIcon from '@icons/add.svg'
import { useState } from 'react'
import generateId from '@lib/generate-id'
const Schema = ({ generate, fields, setFields }: any) => {
const [modalOpen, setModalOpen] = useState(-1)
const addField = () => {
setFields((prev: any) => [...prev, { id: generateId(), name: '', type: '' }])
}
const removeField = (index: number) => {
setFields((prev: any[]) => prev.filter((_, i) => i !== index))
}
const updateField = (index: number, field: { id: string, name: string, type: string }) => {
setFields((prev: any[]) => prev.map((f, i) => i === index ? field : f))
}
return (
<div className="flex flex-col justify-between gap-14 grow min-h-full">
<AnimatePresence mode="wait">
{modalOpen !== -1 && (
<Modal
key={modalOpen}
hideModal={() => setModalOpen(-1)}
updateType={(type: string) => {
updateField(modalOpen, { ...fields[modalOpen], type })
setModalOpen(-1)
}}
/>
)}
</AnimatePresence>
<div className="flex flex-col gap-10 grow">
<div className="flex flex-col gap-4">
<h1 className="italic font-mono font-medium text-3xl inline-flex gap-2 before:font-mono before:content-['{'] before:text-primary after:font-mono after:content-['}'] after:text-primary">
Generate
</h1>
<p className="text-gray-400 text-sm lg:max-w-[30vw]">
Pick a name for your attribute along with its data type. Once you{'\''}re done, click the generate button to
generate your test data.
</p>
</div>
<div className="flex flex-col gap-4 py-5">
{fields.map((field: any, i: number) => (
<Field
key={field.id}
field={field}
removeField={() => removeField(i)}
updateField={(field: { id: string, name: string, type: string }) => updateField(i, field)}
setModalOpen={() => setModalOpen(i)}
/>
))}
</div>
</div>
<div className="flex flex-col gap-2">
<button
onClick={addField}
className="flex items-center justify-center gap-1 text-gray-400 py-2 hover:text-white"
>
<AddIcon className="w-5 aspect-square fill-current" />
<span>Add Field</span>
</button>
<button
onClick={() => generate(fields)}
className="w-full bg-gray-50 text-gray-900 py-2 px-4 rounded-md font-semibold uppercase tracking-widest hover:tracking-[.5rem] hover:bg-primary hover:shadow-lg hover:shadow-primary/20 border border-transparent hover:border-primary-900 transition-all duration-200 ease-in-out"
>
Generate
</button>
</div>
</div>
)
}
export default Schema