Skip to content

Commit

Permalink
fix: use onchagne from props rather than document operation for patch
Browse files Browse the repository at this point in the history
  • Loading branch information
jjburbridge committed Dec 9, 2024
1 parent fd075dd commit b61cdce
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions src/components/ExperimentField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@ import {
defineDocumentFieldAction,
DocumentFieldActionItem,
DocumentFieldActionProps,
FormPatch,
ObjectFieldProps,
Operation,
useDocumentOperation,
useFormValue,
PatchEvent,
set,
unset,
} from 'sanity'
type PatchStuff = {patch: Operation<[patches: unknown[]]>; inputId: string}
type PatchStuff = {onChange: (patch: FormPatch | FormPatch[] | PatchEvent) => void; inputId: string}

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

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

Check warning on line 40 in src/components/ExperimentField.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

React Hook useMemo has an unnecessary dependency: 'props'. Either exclude it or remove the dependency array

const patchClearEvent = useMemo(() => {
const experimentId = `${props.inputId}.experimentId`
const variants = `${props.inputId}.variants`
return {
unset: [experimentId, variants],
}
const experimentId = ['experimentId'] // `${props.inputId}.experimentId`
const variants = ['variants'] //`${props.inputId}.variants`
return [unset(experimentId), unset(variants)]
}, [props])

Check warning on line 46 in src/components/ExperimentField.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

React Hook useMemo has an unnecessary dependency: 'props'. Either exclude it or remove the dependency array
const handleAction = useCallback(() => {
props.patch.execute([patchActiveEvent, patchClearEvent])
}, [patchActiveEvent, patchClearEvent, props.patch])
props.onChange([patchActiveEvent, ...patchClearEvent])
}, [patchActiveEvent, patchClearEvent, props])

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

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

export const Experimentfield = (props: ObjectFieldProps) => {

Check warning on line 71 in src/components/ExperimentField.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Missing return type on function
const id = useFormValue(['_id']) as string
const {patch} = useDocumentOperation(id.replace('drafts.', ''), props.schemaType.name)
const {onChange} = props.inputProps
const {inputId} = props
const active = props.value?.active as boolean | undefined

const oldActions = props.actions || []

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

0 comments on commit b61cdce

Please sign in to comment.