Skip to content

Commit

Permalink
setting delete on the right field, nested
Browse files Browse the repository at this point in the history
  • Loading branch information
cowboybeer committed Dec 15, 2023
1 parent 55d6ca4 commit ba6f9dc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 29 deletions.
11 changes: 6 additions & 5 deletions cms/Schema/SchemaField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export const SchemaField = ({
setItemToEdit={setItemToEdit}
setOpenEditModal={setOpenEditModal}
setPathToEdit={setPathToEdit}
setOpenDeleteModal={setOpenDeleteModal}
/>
)
})}
Expand All @@ -182,6 +183,7 @@ const NestedSchemaField = ({
setItemToEdit,
setOpenEditModal,
setPathToEdit,
setOpenDeleteModal,
}) => {
console.log('🚑', objItem)
// console.log(ALL_FIELDS)
Expand Down Expand Up @@ -264,9 +266,6 @@ const NestedSchemaField = ({
e.stopPropagation()
}}
size="small"
// disabled={SYSTEM_FIELDS_LABELS.includes(
// item.name.toLowerCase()
// )}
ghost
icon={<IconMoreHorizontal />}
/>
Expand All @@ -284,8 +283,9 @@ const NestedSchemaField = ({
</Dropdown.Item>
<Dropdown.Item
onClick={() => {
// setItemToEdit(item.name)
// setOpenDeleteModal(true)
setItemToEdit(objItem?.name || objItem?.meta?.name)
setOpenDeleteModal(true)
setPathToEdit(path)
}}
>
Delete
Expand All @@ -310,6 +310,7 @@ const NestedSchemaField = ({
setItemToEdit={setItemToEdit}
setOpenEditModal={setOpenEditModal}
setPathToEdit={setPathToEdit}
setOpenDeleteModal={setOpenDeleteModal}
/>
)
})}
Expand Down
39 changes: 34 additions & 5 deletions cms/Schema/SchemaFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
IconStar,
Modal,
Row,
Confirmation,
Text,
Button,
IconDelete,
Expand All @@ -30,6 +29,7 @@ import {
verticalListSortingStrategy,
} from '@dnd-kit/sortable'
import { SchemaField } from './SchemaField'
import { setDeep } from './utils/setDeep'

const SYSTEM_FIELDS = [
{ label: 'type', icon: <IconExtension />, color: 'grey' },
Expand Down Expand Up @@ -131,7 +131,7 @@ export const SchemaFields = () => {
const [openEditModal, setOpenEditModal] = useState(false)
const [openDeleteModal, setOpenDeleteModal] = useState(false)
const [itemToEdit, setItemToEdit] = useState('')
const [pathToEdit, setPathToEdit] = useState()
const [pathToEdit, setPathToEdit] = useState<string[] | undefined>()

const { data: schema, loading: loadingSchema } = useQuery('db:schema')

Expand Down Expand Up @@ -247,14 +247,43 @@ export const SchemaFields = () => {
</Button>
<Button
onClick={async () => {
let nestedPathFields = {}
let editPathArr: string[] = []
if (pathToEdit) {
pathToEdit.map((item, idx) => {
editPathArr.push(item)
idx !== pathToEdit.length - 1 &&
editPathArr.push('properties')
})

nestedPathFields = {
[editPathArr[0]]: {
...schema?.types[routeType as string]?.fields[
editPathArr[0]
],
},
}

setDeep(
nestedPathFields,
editPathArr,
{ $delete: true },
true
)
}

console.log('DELETE THIS 😾🔫', nestedPathFields)

await client.call('db:set-schema', {
mutate: true,
schema: {
types: {
[routeType as string]: {
fields: {
[itemToEdit]: { $delete: true },
},
fields: pathToEdit
? nestedPathFields
: {
[itemToEdit]: { $delete: true },
},
},
},
},
Expand Down
20 changes: 1 addition & 19 deletions cms/Schema/SpecificFieldModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useRoute } from 'kabouter'
import { Modal, Button, Input, Tab, Tabs, getPluralName } from '@based/ui'
import { useClient, useQuery } from '@based/react'
import { SpecificFieldSettings } from './SpecificFieldSettings'
import { setDeep } from './utils/setDeep'

type SpecificFieldModalProps = {
field: string
Expand All @@ -14,25 +15,6 @@ type SpecificFieldModalProps = {
setPathToEdit?: (v: string | string[]) => void
}

function setDeep(obj, path, value, setrecursively = false) {
path.reduce((a, b, level) => {
if (
setrecursively &&
typeof a[b] === 'undefined' &&
level !== path.length
) {
a[b] = {}
return a[b]
}

if (level === path.length - 1) {
a[b] = value
return value
}
return a[b]
}, obj)
}

const metaReducer = (state, action) => {
if (action.type === 'prune') {
const newMeta = Object.fromEntries(
Expand Down
18 changes: 18 additions & 0 deletions cms/Schema/utils/setDeep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const setDeep = (obj, path, value, setrecursively = false) => {
path.reduce((a, b, level) => {
if (
setrecursively &&
typeof a[b] === 'undefined' &&
level !== path.length
) {
a[b] = {}
return a[b]
}

if (level === path.length - 1) {
a[b] = value
return value
}
return a[b]
}, obj)
}

0 comments on commit ba6f9dc

Please sign in to comment.