@graphcommerce/ecommerce-ui 11.0.0-canary.60 → 11.0.0-canary.62

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @graphcommerce/ecommerce-ui
2
2
 
3
+ ## 11.0.0-canary.62
4
+
5
+ ## 11.0.0-canary.61
6
+
7
+ ### Patch Changes
8
+
9
+ - [#2721](https://github.com/graphcommerce-org/graphcommerce/pull/2721) [`c50f4fa`](https://github.com/graphcommerce-org/graphcommerce/commit/c50f4faa52f3411e86e22bf8bdd038f1bfa3aafe) - The preview mode toolbar sends `previewData` only after a change by the user. `FormPersist` restores `previewData` after each page load, and `FormAutoSubmit` sent that restore as an `update` again: the page reloaded in a loop after a change of the Hygraph stage. ([@bramvanderholst](https://github.com/bramvanderholst))
10
+
3
11
  ## 11.0.0-canary.60
4
12
 
5
13
  ## 11.0.0-canary.59
@@ -0,0 +1,129 @@
1
+ // @vitest-environment happy-dom
2
+
3
+ import { createTheme, ThemeProvider } from '@mui/material'
4
+ import type React from 'react'
5
+ import { act } from 'react'
6
+ import { createRoot } from 'react-dom/client'
7
+ import { afterAll, afterEach, beforeAll, beforeEach, expect, it, vi } from 'vitest'
8
+ import { PreviewModeEnabled } from './PreviewModeEnabled'
9
+
10
+ vi.mock('@graphcommerce/next-ui', () => ({
11
+ iconClose: '',
12
+ iconContrast: '',
13
+ iconInfo: '',
14
+ iconRefresh: '',
15
+ IconSvg: () => null,
16
+ MessageSnackbar: (props: { children: React.ReactNode }) => props.children,
17
+ }))
18
+
19
+ // A toolbar with one `previewData` field, such as the Hygraph stage select.
20
+ vi.mock('./PreviewModeToolbar', async () => {
21
+ const { useController } = await import('@graphcommerce/react-hook-form')
22
+ const { usePreviewModeForm } = await import('./usePreviewModeForm')
23
+
24
+ return {
25
+ PreviewModeToolbar: () => {
26
+ const { control } = usePreviewModeForm()
27
+ const { field } = useController({
28
+ control,
29
+ name: 'previewData.stage' as 'previewData',
30
+ defaultValue: 'DRAFT' as never,
31
+ })
32
+ return (
33
+ <input
34
+ aria-label='stage'
35
+ value={`${field.value}`}
36
+ onChange={(event) => field.onChange(event.target.value)}
37
+ />
38
+ )
39
+ },
40
+ }
41
+ })
42
+
43
+ const pageUrl = 'https://example.com/page'
44
+ const originalLocation = window.location
45
+
46
+ async function renderPreviewMode() {
47
+ const container = document.createElement('div')
48
+ document.body.appendChild(container)
49
+ const root = createRoot(container)
50
+
51
+ await act(async () => {
52
+ root.render(
53
+ <ThemeProvider theme={createTheme({ cssVariables: true })}>
54
+ <PreviewModeEnabled />
55
+ </ThemeProvider>,
56
+ )
57
+ })
58
+ await waitForAutoSubmit()
59
+
60
+ return { container, unmount: () => act(() => root.unmount()) }
61
+ }
62
+
63
+ /** FormAutoSubmit submits after a debounce. */
64
+ function waitForAutoSubmit() {
65
+ return act(async () => {
66
+ await vi.advanceTimersByTimeAsync(1000)
67
+ })
68
+ }
69
+
70
+ beforeAll(() => {
71
+ Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true })
72
+ })
73
+
74
+ afterAll(() => {
75
+ Reflect.deleteProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT')
76
+ })
77
+
78
+ beforeEach(() => {
79
+ vi.useFakeTimers()
80
+ Object.defineProperty(window, 'location', {
81
+ configurable: true,
82
+ writable: true,
83
+ value: { href: pageUrl },
84
+ })
85
+ })
86
+
87
+ afterEach(() => {
88
+ vi.useRealTimers()
89
+ window.sessionStorage.clear()
90
+ Object.defineProperty(window, 'location', {
91
+ configurable: true,
92
+ writable: true,
93
+ value: originalLocation,
94
+ })
95
+ })
96
+
97
+ it('sends no update when FormPersist restores previewData after a page load', async () => {
98
+ window.sessionStorage.PreviewModePreviewData = JSON.stringify({
99
+ previewData: { stage: 'PUBLISHED' },
100
+ })
101
+
102
+ const { container, unmount } = await renderPreviewMode()
103
+
104
+ expect(container.querySelector('input')?.value).toBe('PUBLISHED')
105
+ expect(window.location.href).toBe(pageUrl)
106
+
107
+ unmount()
108
+ })
109
+
110
+ it('sends the update when the user changes previewData', async () => {
111
+ const { container, unmount } = await renderPreviewMode()
112
+ const input = container.querySelector('input') as HTMLInputElement
113
+
114
+ await act(async () => {
115
+ Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set?.call(
116
+ input,
117
+ 'PUBLISHED',
118
+ )
119
+ input.dispatchEvent(new window.Event('input', { bubbles: true }))
120
+ })
121
+ await waitForAutoSubmit()
122
+
123
+ const url = new URL(window.location.href)
124
+ expect(url.pathname).toBe('/api/preview')
125
+ expect(url.searchParams.get('action')).toBe('update')
126
+ expect(url.searchParams.get('previewData')).toBe(JSON.stringify({ stage: 'PUBLISHED' }))
127
+
128
+ unmount()
129
+ })
@@ -9,6 +9,7 @@ import {
9
9
  } from '@graphcommerce/next-ui'
10
10
  import { FormAutoSubmit, FormPersist, FormProvider, useForm } from '@graphcommerce/react-hook-form'
11
11
  import { Box, IconButton } from '@mui/material'
12
+ import { useEffect, useRef } from 'react'
12
13
  import { LightTooltip } from './LightTooltip'
13
14
  import { PreviewModeActions } from './PreviewModeActions'
14
15
  import { PreviewModeToolbar } from './PreviewModeToolbar'
@@ -25,6 +26,15 @@ type FormValues = { secret: string; previewData: PreviewData }
25
26
  export function PreviewModeEnabled() {
26
27
  const form = useForm<FormValues>({})
27
28
 
29
+ // FormPersist restores `previewData` after each page load. Only a change by the user submits.
30
+ const changedByUser = useRef(false)
31
+ useEffect(() => {
32
+ const subscription = form.watch((_values, { type }) => {
33
+ if (type === 'change') changedByUser.current = true
34
+ })
35
+ return () => subscription.unsubscribe()
36
+ }, [form])
37
+
28
38
  const submit = form.handleSubmit((formValues) => {
29
39
  const url = getPreviewUrl()
30
40
  url.searchParams.append('action', 'update')
@@ -97,7 +107,13 @@ export function PreviewModeEnabled() {
97
107
  {/* Only auto-submit on `previewData` edits. Watching every field made the restore of a
98
108
  persisted `secret` (via `setValue(shouldDirty)`) look like a user change, which fired the
99
109
  `update` submit → 307 redirect → reload → restore → … infinite loop in production. */}
100
- <FormAutoSubmit control={form.control} name={['previewData']} submit={submit} />
110
+ <FormAutoSubmit
111
+ control={form.control}
112
+ name={['previewData']}
113
+ submit={async () => {
114
+ if (changedByUser.current) await submit()
115
+ }}
116
+ />
101
117
  </FormProvider>
102
118
  )
103
119
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/ecommerce-ui",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "11.0.0-canary.60",
5
+ "version": "11.0.0-canary.62",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -12,12 +12,12 @@
12
12
  }
13
13
  },
14
14
  "peerDependencies": {
15
- "@graphcommerce/eslint-config-pwa": "^11.0.0-canary.60",
16
- "@graphcommerce/graphql": "^11.0.0-canary.60",
17
- "@graphcommerce/next-ui": "^11.0.0-canary.60",
18
- "@graphcommerce/prettier-config-pwa": "^11.0.0-canary.60",
19
- "@graphcommerce/react-hook-form": "^11.0.0-canary.60",
20
- "@graphcommerce/typescript-config-pwa": "^11.0.0-canary.60",
15
+ "@graphcommerce/eslint-config-pwa": "^11.0.0-canary.62",
16
+ "@graphcommerce/graphql": "^11.0.0-canary.62",
17
+ "@graphcommerce/next-ui": "^11.0.0-canary.62",
18
+ "@graphcommerce/prettier-config-pwa": "^11.0.0-canary.62",
19
+ "@graphcommerce/react-hook-form": "^11.0.0-canary.62",
20
+ "@graphcommerce/typescript-config-pwa": "^11.0.0-canary.62",
21
21
  "@lingui/core": "^6",
22
22
  "@lingui/react": "^6",
23
23
  "@mui/material": "^7.0.0",