Skip to content

Commit

Permalink
fix: on removal of experiment clear additional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jjburbridge committed Nov 20, 2024
1 parent f4cdf44 commit d2613a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
2 changes: 0 additions & 2 deletions src/components/Array.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export const ArrayInput = (props: ArrayInputProps) => {

const {onItemAppend, objectName} = props

console.log(props)

const handleClick = useCallback(
async (variant: VariantType) => {
const item = {
Expand Down
38 changes: 24 additions & 14 deletions src/components/ExperimentField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import {
useDocumentOperation,
useFormValue,
} from 'sanity'
type PatchStuff = {patch: Operation<[patches: any][]>; activeId: string}
type PatchStuff = {patch: Operation<[patches: any[]]>; inputId: string}

const useAddExperimentAction = (
props: DocumentFieldActionProps & PatchStuff,
): DocumentFieldActionItem => {
const patchEvent = useMemo(() => {
const patchActiveEvent = useMemo(() => {
const activeId = `${props.inputId}.active`
return {
set: {[props.activeId]: true},
set: {[activeId]: true},
}
}, [props])
const handleAction = useCallback(() => {
props.patch.execute([patchEvent])
}, [patchEvent, props.patch])
props.patch.execute([patchActiveEvent])
}, [patchActiveEvent, props.patch])

return {
title: 'Add experiment',
Expand All @@ -35,14 +36,23 @@ const useAddExperimentAction = (
const useRemoveExperimentAction = (
props: DocumentFieldActionProps & PatchStuff,
): DocumentFieldActionItem => {
const patchEvent = useMemo(() => {
const patchActiveEvent = useMemo(() => {
const activeId = `${props.inputId}.active`
return {
set: {[props.activeId]: false},
set: {[activeId]: false},
}
}, [props])

const patchClearEvent = useMemo(() => {
const experimentId = `${props.inputId}.experimentId`
const variants = `${props.inputId}.variants`
return {
unset: [experimentId, variants],
}
}, [props])
const handleAction = useCallback(() => {
props.patch.execute([patchEvent])
}, [patchEvent, props.patch])
props.patch.execute([patchActiveEvent, patchClearEvent])
}, [patchActiveEvent, patchClearEvent, props.patch])

return {
title: 'Remove experiment',
Expand All @@ -53,28 +63,28 @@ const useRemoveExperimentAction = (
}
}

const newActions = ({patch, activeId, active}: PatchStuff & {active?: boolean}) =>
const newActions = ({patch, inputId, active}: PatchStuff & {active?: boolean}) =>
active
? defineDocumentFieldAction({
name: 'Experiment',
useAction: (props) => useRemoveExperimentAction({...props, patch, activeId}),
useAction: (props) => useRemoveExperimentAction({...props, patch, inputId}),
})
: defineDocumentFieldAction({
name: 'Experiment',
useAction: (props) => useAddExperimentAction({...props, patch, activeId}),
useAction: (props) => useAddExperimentAction({...props, patch, inputId}),
})

export const Experimentfield = (props: ObjectFieldProps) => {
const id = useFormValue(['_id']) as string
const {patch} = useDocumentOperation(id.replace('drafts.', ''), props.schemaType.name)
const activeId = `${props.inputId}.active`
const {inputId} = props
const active = props.value?.active as boolean | undefined

const oldActions = props.actions || []

const wihtActionProps = {
...props,
actions: [newActions({patch, activeId, active}), ...oldActions],
actions: [newActions({patch, inputId, active}), ...oldActions],
}
return props.renderDefault(wihtActionProps)
}

0 comments on commit d2613a3

Please sign in to comment.