|
@@ -1,140 +1,86 @@
|
|
-import { useState, useEffect } from 'react'
|
|
|
|
-import arrayMove from 'array-move'
|
|
|
|
import BlockInputs from './BlockInputs'
|
|
import BlockInputs from './BlockInputs'
|
|
-import { SortableList } from '../../sortable'
|
|
|
|
import { TextInput } from '../../form'
|
|
import { TextInput } from '../../form'
|
|
import { TBlockInstance } from '../types'
|
|
import { TBlockInstance } from '../types'
|
|
|
|
|
|
const BlockInstanceInputs = ({
|
|
const BlockInstanceInputs = ({
|
|
- value = [],
|
|
|
|
|
|
+ value,
|
|
name,
|
|
name,
|
|
onChange,
|
|
onChange,
|
|
|
|
+ className,
|
|
}: {
|
|
}: {
|
|
- value?: TBlockInstance[]
|
|
|
|
|
|
+ value: TBlockInstance
|
|
name: string
|
|
name: string
|
|
onChange: GenericEventHandler
|
|
onChange: GenericEventHandler
|
|
|
|
+ className?: string
|
|
}) => {
|
|
}) => {
|
|
- const [state, setState] = useState(value.map((item) => item.id))
|
|
|
|
-
|
|
|
|
- function updateOrderProperty<T extends { id: U }, U>(values: T[], orderList: U[]) {
|
|
|
|
- const orderedValues = values.map((value) => {
|
|
|
|
- const order = orderList.findIndex((orderedId) => orderedId === value.id)
|
|
|
|
- return { ...value, order }
|
|
|
|
- })
|
|
|
|
- onChange({ target: { type: 'custom', name, value: orderedValues } })
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function onSortEnd({ oldIndex, newIndex }: { oldIndex: number; newIndex: number }) {
|
|
|
|
- const newOrder = arrayMove(state, oldIndex, newIndex)
|
|
|
|
- setState(newOrder)
|
|
|
|
- updateOrderProperty(value, newOrder)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- useEffect(() => {
|
|
|
|
- const missingIds = value
|
|
|
|
- .filter((item) => !state.includes(item.id))
|
|
|
|
- .filter((item) => !item.id.startsWith('--'))
|
|
|
|
- .map((item) => item.id)
|
|
|
|
- const stateWithoutRemovedItems = state.filter((stateId) =>
|
|
|
|
- value.find((item) => stateId === item.id)
|
|
|
|
- )
|
|
|
|
- setState([...stateWithoutRemovedItems, ...missingIds])
|
|
|
|
- }, [value])
|
|
|
|
-
|
|
|
|
- const items = state
|
|
|
|
- .map((stateId) => {
|
|
|
|
- const itemIndex = value.findIndex((item) => item.id === stateId)
|
|
|
|
- if (itemIndex < 0) return null
|
|
|
|
- const item = value[itemIndex]
|
|
|
|
- return (
|
|
|
|
- <div key={item.id}>
|
|
|
|
- <p>{item.id}</p>
|
|
|
|
- <TextInput
|
|
|
|
- name={`${name}.${itemIndex}.order`}
|
|
|
|
- label='Order'
|
|
|
|
- value={item.order}
|
|
|
|
- type='number'
|
|
|
|
- onChange={onChange}
|
|
|
|
- className='bi-order'
|
|
|
|
- />
|
|
|
|
- <TextInput
|
|
|
|
- name={`${name}.${itemIndex}.rounds`}
|
|
|
|
- label='Rounds'
|
|
|
|
- value={item.rounds}
|
|
|
|
- type='number'
|
|
|
|
- onChange={onChange}
|
|
|
|
- className='bi-rounds'
|
|
|
|
- />
|
|
|
|
- <TextInput
|
|
|
|
- name={`${name}.${itemIndex}.variation`}
|
|
|
|
- label='Variation'
|
|
|
|
- value={item.variation}
|
|
|
|
- onChange={onChange}
|
|
|
|
- className='bi-variation'
|
|
|
|
- />
|
|
|
|
- <div className='bi-block'>
|
|
|
|
- {item.block && (
|
|
|
|
- <BlockInputs
|
|
|
|
- name={`${name}.${itemIndex}.block`}
|
|
|
|
- value={item.block}
|
|
|
|
- onChange={onChange}
|
|
|
|
- />
|
|
|
|
- )}
|
|
|
|
- </div>
|
|
|
|
- <button
|
|
|
|
- type='button'
|
|
|
|
- onClick={(ev) => {
|
|
|
|
- const newOrder = state.filter((orderedId) => item.id !== orderedId)
|
|
|
|
- setState(newOrder)
|
|
|
|
- const newValues = item.id?.startsWith('++')
|
|
|
|
- ? [...value.slice(0, itemIndex), ...value.slice(itemIndex + 1)]
|
|
|
|
- : [
|
|
|
|
- ...value.slice(0, itemIndex),
|
|
|
|
- { ...item, id: `--${item.id}` },
|
|
|
|
- ...value.slice(itemIndex + 1),
|
|
|
|
- ]
|
|
|
|
- updateOrderProperty(newValues, newOrder)
|
|
|
|
- }}
|
|
|
|
- className='bi-button'
|
|
|
|
- >
|
|
|
|
- Delete block
|
|
|
|
- </button>
|
|
|
|
-
|
|
|
|
- <style jsx>{`
|
|
|
|
- @media (min-width: 768px) {
|
|
|
|
- div {
|
|
|
|
- display: grid;
|
|
|
|
- grid-template-areas:
|
|
|
|
- 'order variation'
|
|
|
|
- 'rounds variation'
|
|
|
|
- 'block block'
|
|
|
|
- 'button button';
|
|
|
|
- grid-template-columns: 1fr 2fr;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- div :global(.bi-order) {
|
|
|
|
- grid-area: order;
|
|
|
|
- }
|
|
|
|
- div :global(.bi-rounds) {
|
|
|
|
- grid-area: rounds;
|
|
|
|
- }
|
|
|
|
- div :global(.bi-variation) {
|
|
|
|
- grid-area: variation;
|
|
|
|
- }
|
|
|
|
- div :global(.bi-block) {
|
|
|
|
- grid-area: block;
|
|
|
|
- }
|
|
|
|
- div :global(.bi-button) {
|
|
|
|
- grid-area: button;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- `}</style>
|
|
|
|
|
|
+ return (
|
|
|
|
+ <div className={className}>
|
|
|
|
+ {!value.id.startsWith('++') && (
|
|
|
|
+ <div className='bi-info'>
|
|
|
|
+ <div>Block Instance ID{value.id}</div>
|
|
|
|
+ <div>Created at: {value}</div>
|
|
</div>
|
|
</div>
|
|
- )
|
|
|
|
- })
|
|
|
|
- .filter((block) => block !== null)
|
|
|
|
|
|
+ )}
|
|
|
|
+ <TextInput
|
|
|
|
+ name={`${name}.order`}
|
|
|
|
+ label='Order'
|
|
|
|
+ value={value.order}
|
|
|
|
+ type='number'
|
|
|
|
+ onChange={onChange}
|
|
|
|
+ className='bi-order'
|
|
|
|
+ />
|
|
|
|
+ <TextInput
|
|
|
|
+ name={`${name}.rounds`}
|
|
|
|
+ label='Rounds'
|
|
|
|
+ value={value.rounds}
|
|
|
|
+ type='number'
|
|
|
|
+ onChange={onChange}
|
|
|
|
+ className='bi-rounds'
|
|
|
|
+ />
|
|
|
|
+ <TextInput
|
|
|
|
+ name={`${name}.variation`}
|
|
|
|
+ label='Variation'
|
|
|
|
+ value={value.variation}
|
|
|
|
+ onChange={onChange}
|
|
|
|
+ className='bi-variation'
|
|
|
|
+ />
|
|
|
|
+ <BlockInputs
|
|
|
|
+ name={`${name}.block`}
|
|
|
|
+ value={value.block}
|
|
|
|
+ onChange={onChange}
|
|
|
|
+ className='bi-block'
|
|
|
|
+ />
|
|
|
|
+
|
|
|
|
+ <style jsx>{`
|
|
|
|
+ @media (min-width: 768px) {
|
|
|
|
+ div {
|
|
|
|
+ display: grid;
|
|
|
|
+ grid-template-areas:
|
|
|
|
+ 'order rounds variation'
|
|
|
|
+ 'block block block'
|
|
|
|
+ 'button button button';
|
|
|
|
+ grid-template-columns: 1fr 1fr 2fr;
|
|
|
|
+ }
|
|
|
|
|
|
- return <SortableList items={items} onSortEnd={onSortEnd} useDragHandle lockAxis={'y'} />
|
|
|
|
|
|
+ div :global(.bi-order) {
|
|
|
|
+ grid-area: order;
|
|
|
|
+ }
|
|
|
|
+ div :global(.bi-rounds) {
|
|
|
|
+ grid-area: rounds;
|
|
|
|
+ }
|
|
|
|
+ div :global(.bi-variation) {
|
|
|
|
+ grid-area: variation;
|
|
|
|
+ }
|
|
|
|
+ div :global(.bi-block) {
|
|
|
|
+ grid-area: block;
|
|
|
|
+ }
|
|
|
|
+ div :global(.bi-button) {
|
|
|
|
+ grid-area: button;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ `}</style>
|
|
|
|
+ </div>
|
|
|
|
+ )
|
|
}
|
|
}
|
|
|
|
|
|
export default BlockInstanceInputs
|
|
export default BlockInstanceInputs
|