@sanity/personalization-plugin 2.1.0-field-names.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/personalization-plugin",
3
- "version": "2.1.0-field-names.1",
3
+ "version": "2.1.0",
4
4
  "description": "Plugin to help with personalization, a/b testing when using Sanity",
5
5
  "keywords": [
6
6
  "sanity",
@@ -19,7 +19,6 @@ const useAddExperimentAction = (
19
19
  const {onChange, active, experimentNameOverride} = props
20
20
 
21
21
  const handleAddAction = () => {
22
- // console.log('showing experiment', patchActiveTrueEvent)
23
22
  onChange([set(!active, ['active'])])
24
23
  }
25
24
 
@@ -1,71 +1,18 @@
1
- import {FormEvent, useCallback} from 'react'
2
- import {
3
- FormPatch,
4
- PatchEvent,
5
- set,
6
- StringInputProps,
7
- unset,
8
- useDocumentOperation,
9
- useFormValue,
10
- } from 'sanity'
11
-
12
- import {VariantType} from '../types'
13
- import {useExperimentContext} from './ExperimentContext'
14
- import {Select} from './Select'
15
-
16
- const formatlistOptions = (varants: VariantType[]) =>
17
- varants.map((variant) => ({
18
- title: variant.label,
19
- value: variant.id,
20
- }))
21
-
22
- export const VariantInput = (props: StringInputProps) => {
23
- const experimentPath = props.path.slice(0, -3)
24
-
25
- const experimentValue = useFormValue([...experimentPath, 'experimentValue'])
26
-
27
- const id = useFormValue(['_id']) as string
28
-
29
- const {patch} = useDocumentOperation(id.replace('drafts.', ''), props.schemaType.name)
30
-
31
- const {experiments} = useExperimentContext()
32
-
33
- const handleChange = useCallback(
34
- (
35
- event: FormEvent<Element>,
36
- onChange: (patchchange: FormPatch | FormPatch[] | PatchEvent) => void,
37
- ) => {
38
- const target = event.currentTarget as HTMLSelectElement
39
- const inputValue = target.value
40
- const variantExperimentId = props.id.replace('variantId', 'experimentId')
41
-
42
- if (inputValue) {
43
- onChange(set(inputValue))
44
- const patchEvent = {
45
- set: {[variantExperimentId]: experimentValue},
46
- }
47
- patch.execute([patchEvent])
48
- } else {
49
- onChange(unset())
50
- const patchEvent = {
51
- unset: [variantExperimentId],
52
- }
53
- patch.execute([patchEvent])
54
- }
55
- },
56
- [experimentValue, patch, props.id],
57
- )
58
-
59
- const filteredVariants =
60
- experiments.find((option) => {
61
- return option.id === experimentValue
62
- })?.variants || []
63
-
1
+ import {Button, Inline, Stack} from '@sanity/ui'
2
+ import {ObjectInputProps, set, useFormValue} from 'sanity'
3
+
4
+ export const VariantInput = (props: ObjectInputProps) => {
5
+ const defaultValue = useFormValue([props.path[0], 'default'])
6
+ const handleClick = () => {
7
+ props.onChange(set(defaultValue, ['value']))
8
+ }
64
9
  return (
65
- <Select
66
- {...props}
67
- listOptions={formatlistOptions(filteredVariants)}
68
- handleChange={handleChange}
69
- />
10
+ <Stack space={3}>
11
+ {props.renderDefault(props)}
12
+
13
+ <Inline space={1}>
14
+ <Button text="Copy default" mode="ghost" onClick={() => handleClick()} />
15
+ </Inline>
16
+ </Stack>
70
17
  )
71
18
  }
@@ -11,6 +11,7 @@ import {ArrayInput} from './components/Array'
11
11
  import {CONFIG_DEFAULT, ExperimentProvider} from './components/ExperimentContext'
12
12
  import {ExperimentField} from './components/ExperimentField'
13
13
  import {ExperimentInput} from './components/ExperimentInput'
14
+ import {VariantInput} from './components/VariantInput'
14
15
  import {VariantPreview} from './components/VariantPreview'
15
16
  import {FieldPluginConfig} from './types'
16
17
  import {flattenSchemaType} from './utils/flattenSchemaType'
@@ -121,6 +122,7 @@ const createVariantType = ({
121
122
  type: 'object',
122
123
  components: {
123
124
  preview: VariantPreview,
125
+ input: VariantInput,
124
126
  },
125
127
  fields: [
126
128
  {