|  | @@ -3,17 +3,18 @@ import arrayMove from 'array-move'
 | 
	
		
			
				|  |  |  import BlockInputs from './BlockInputs'
 | 
	
		
			
				|  |  |  import { SortableList } from '../../sortable'
 | 
	
		
			
				|  |  |  import { SubBlockFragment } from '../../gql'
 | 
	
		
			
				|  |  | +import { TextInput } from '../../form'
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  const BlockInstanceInputs = ({
 | 
	
		
			
				|  |  | +  value = [],
 | 
	
		
			
				|  |  |    name,
 | 
	
		
			
				|  |  | -  value,
 | 
	
		
			
				|  |  |    onChange
 | 
	
		
			
				|  |  |  }: {
 | 
	
		
			
				|  |  | -  name: string
 | 
	
		
			
				|  |  |    value?: Partial<SubBlockFragment>[]
 | 
	
		
			
				|  |  | +  name: string
 | 
	
		
			
				|  |  |    onChange: GenericEventHandler
 | 
	
		
			
				|  |  |  }) => {
 | 
	
		
			
				|  |  | -  const [state, setState] = useState(value.map((item, index) => index))
 | 
	
		
			
				|  |  | +  const [state, setState] = useState(value.map(item => item.id))
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    function onSortEnd({
 | 
	
		
			
				|  |  |      oldIndex,
 | 
	
	
		
			
				|  | @@ -24,28 +25,70 @@ const BlockInstanceInputs = ({
 | 
	
		
			
				|  |  |    }) {
 | 
	
		
			
				|  |  |      const newOrder = arrayMove(state, oldIndex, newIndex)
 | 
	
		
			
				|  |  |      setState(newOrder)
 | 
	
		
			
				|  |  | +    const updatedValues = value.map(item => {
 | 
	
		
			
				|  |  | +      const order = newOrder.findIndex(orderedId => orderedId === item.id)
 | 
	
		
			
				|  |  | +      return { ...item, order }
 | 
	
		
			
				|  |  | +    })
 | 
	
		
			
				|  |  | +    onChange({
 | 
	
		
			
				|  |  | +      target: {
 | 
	
		
			
				|  |  | +        type: 'custom',
 | 
	
		
			
				|  |  | +        name,
 | 
	
		
			
				|  |  | +        value: updatedValues
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +    })
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    useEffect(() => {
 | 
	
		
			
				|  |  | -    const missingIndices = []
 | 
	
		
			
				|  |  | -    for (let idx = 0; idx < value.length - 0; idx++) {
 | 
	
		
			
				|  |  | -      if (!state.includes(idx)) missingIndices.push(idx)
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -    setState([...state, ...missingIndices])
 | 
	
		
			
				|  |  | +    const missingIds = value
 | 
	
		
			
				|  |  | +      .filter(item => !state.includes(item.id))
 | 
	
		
			
				|  |  | +      .map(item => item.id)
 | 
	
		
			
				|  |  | +    const stateWithoutRemovedItems = state.filter(stateId =>
 | 
	
		
			
				|  |  | +      value.find(item => stateId === item.id)
 | 
	
		
			
				|  |  | +    )
 | 
	
		
			
				|  |  | +    console.log('filtered!', {
 | 
	
		
			
				|  |  | +      value,
 | 
	
		
			
				|  |  | +      state,
 | 
	
		
			
				|  |  | +      missingIds,
 | 
	
		
			
				|  |  | +      stateWithoutRemovedItems
 | 
	
		
			
				|  |  | +    })
 | 
	
		
			
				|  |  | +    setState([...stateWithoutRemovedItems, ...missingIds])
 | 
	
		
			
				|  |  |    }, [value])
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  const items = state.map((order, index) => {
 | 
	
		
			
				|  |  | -    const item = value[order]
 | 
	
		
			
				|  |  | +  const items = state.map(stateId => {
 | 
	
		
			
				|  |  | +    const itemIndex = value.findIndex(item => item.id === stateId)
 | 
	
		
			
				|  |  | +    if (itemIndex < 0) return null
 | 
	
		
			
				|  |  | +    const item = value[itemIndex]
 | 
	
		
			
				|  |  |      return (
 | 
	
		
			
				|  |  | -      <BlockInputs
 | 
	
		
			
				|  |  | -        key={item.id}
 | 
	
		
			
				|  |  | -        name={`${name}.${order}.block`}
 | 
	
		
			
				|  |  | -        value={item.block}
 | 
	
		
			
				|  |  | -        onChange={onChange}
 | 
	
		
			
				|  |  | -      />
 | 
	
		
			
				|  |  | +      <div key={item.id}>
 | 
	
		
			
				|  |  | +        <p>
 | 
	
		
			
				|  |  | +          {item.order} {item.id}
 | 
	
		
			
				|  |  | +        </p>
 | 
	
		
			
				|  |  | +        <TextInput
 | 
	
		
			
				|  |  | +          name='rounds'
 | 
	
		
			
				|  |  | +          label='Rounds'
 | 
	
		
			
				|  |  | +          value={item.rounds}
 | 
	
		
			
				|  |  | +          type='number'
 | 
	
		
			
				|  |  | +          onChange={onChange}
 | 
	
		
			
				|  |  | +        />
 | 
	
		
			
				|  |  | +        <TextInput
 | 
	
		
			
				|  |  | +          name='variation'
 | 
	
		
			
				|  |  | +          label='Variation'
 | 
	
		
			
				|  |  | +          value={item.variation}
 | 
	
		
			
				|  |  | +          onChange={onChange}
 | 
	
		
			
				|  |  | +        />
 | 
	
		
			
				|  |  | +        {item.block && (
 | 
	
		
			
				|  |  | +          <BlockInputs
 | 
	
		
			
				|  |  | +            name={`${name}.${itemIndex}.block`}
 | 
	
		
			
				|  |  | +            value={item.block}
 | 
	
		
			
				|  |  | +            onChange={onChange}
 | 
	
		
			
				|  |  | +          />
 | 
	
		
			
				|  |  | +        )}
 | 
	
		
			
				|  |  | +        <button type='button'>Add block</button>
 | 
	
		
			
				|  |  | +      </div>
 | 
	
		
			
				|  |  |      )
 | 
	
		
			
				|  |  |    })
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +  console.log({ name, items, value, state })
 | 
	
		
			
				|  |  |    return (
 | 
	
		
			
				|  |  |      <SortableList
 | 
	
		
			
				|  |  |        items={items}
 |