@graphcommerce/ecommerce-ui 8.1.0-canary.5 → 8.1.0-canary.50
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 +100 -0
- package/components/FormComponents/EmailElement.tsx +27 -0
- package/components/FormComponents/NumberFieldElement.tsx +5 -1
- package/components/FormComponents/TelephoneElement.tsx +23 -0
- package/components/FormComponents/TextFieldElement.tsx +7 -4
- package/components/FormComponents/index.ts +5 -3
- package/components/PreviewMode/LightTooltip.tsx +11 -0
- package/components/PreviewMode/PreviewMode.tsx +148 -0
- package/components/PreviewMode/PreviewModeActions.tsx +6 -0
- package/components/PreviewMode/PreviewModeToolbar.tsx +6 -0
- package/components/PreviewMode/index.ts +5 -0
- package/components/PreviewMode/previewModeDefaults.ts +5 -0
- package/components/PreviewMode/usePreviewModeForm.ts +6 -0
- package/components/index.ts +1 -0
- package/package.json +7 -7
- package/plugins/PreviewModeFramerNextPages.tsx +38 -0
- package/route/preview.ts +60 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,105 @@
|
|
|
1
1
|
# @graphcommerce/ecommerce-ui
|
|
2
2
|
|
|
3
|
+
## 8.1.0-canary.50
|
|
4
|
+
|
|
5
|
+
## 8.1.0-canary.49
|
|
6
|
+
|
|
7
|
+
## 8.1.0-canary.48
|
|
8
|
+
|
|
9
|
+
### Minor Changes
|
|
10
|
+
|
|
11
|
+
- [#2319](https://github.com/graphcommerce-org/graphcommerce/pull/2319) [`a3409e8`](https://github.com/graphcommerce-org/graphcommerce/commit/a3409e8a629ee95413da6547cbdcf48aa2502c23) - Created a new TelephoneElement component to make re-use easier
|
|
12
|
+
([@Giovanni-Schroevers](https://github.com/Giovanni-Schroevers))
|
|
13
|
+
|
|
14
|
+
## 8.1.0-canary.47
|
|
15
|
+
|
|
16
|
+
## 8.1.0-canary.46
|
|
17
|
+
|
|
18
|
+
## 8.1.0-canary.45
|
|
19
|
+
|
|
20
|
+
## 8.1.0-canary.44
|
|
21
|
+
|
|
22
|
+
## 8.1.0-canary.43
|
|
23
|
+
|
|
24
|
+
## 8.1.0-canary.42
|
|
25
|
+
|
|
26
|
+
## 8.1.0-canary.41
|
|
27
|
+
|
|
28
|
+
## 8.1.0-canary.40
|
|
29
|
+
|
|
30
|
+
## 8.1.0-canary.39
|
|
31
|
+
|
|
32
|
+
## 8.1.0-canary.38
|
|
33
|
+
|
|
34
|
+
## 8.1.0-canary.37
|
|
35
|
+
|
|
36
|
+
## 8.1.0-canary.36
|
|
37
|
+
|
|
38
|
+
## 8.1.0-canary.35
|
|
39
|
+
|
|
40
|
+
### Patch Changes
|
|
41
|
+
|
|
42
|
+
- [#2301](https://github.com/graphcommerce-org/graphcommerce/pull/2301) [`13d0649`](https://github.com/graphcommerce-org/graphcommerce/commit/13d06498d121f93b52c25930e50aa3b0bd12a818) - Created a new EmailElement component to make re-use easier
|
|
43
|
+
([@Giovanni-Schroevers](https://github.com/Giovanni-Schroevers))
|
|
44
|
+
|
|
45
|
+
## 8.1.0-canary.34
|
|
46
|
+
|
|
47
|
+
## 8.1.0-canary.33
|
|
48
|
+
|
|
49
|
+
## 8.1.0-canary.32
|
|
50
|
+
|
|
51
|
+
## 8.1.0-canary.31
|
|
52
|
+
|
|
53
|
+
## 8.1.0-canary.30
|
|
54
|
+
|
|
55
|
+
## 8.1.0-canary.29
|
|
56
|
+
|
|
57
|
+
## 8.1.0-canary.28
|
|
58
|
+
|
|
59
|
+
## 8.1.0-canary.27
|
|
60
|
+
|
|
61
|
+
## 8.1.0-canary.26
|
|
62
|
+
|
|
63
|
+
## 8.1.0-canary.25
|
|
64
|
+
|
|
65
|
+
## 8.1.0-canary.24
|
|
66
|
+
|
|
67
|
+
## 8.1.0-canary.23
|
|
68
|
+
|
|
69
|
+
## 8.1.0-canary.22
|
|
70
|
+
|
|
71
|
+
## 8.1.0-canary.21
|
|
72
|
+
|
|
73
|
+
## 8.1.0-canary.20
|
|
74
|
+
|
|
75
|
+
## 8.1.0-canary.19
|
|
76
|
+
|
|
77
|
+
## 8.1.0-canary.18
|
|
78
|
+
|
|
79
|
+
## 8.1.0-canary.17
|
|
80
|
+
|
|
81
|
+
## 8.1.0-canary.16
|
|
82
|
+
|
|
83
|
+
## 8.1.0-canary.15
|
|
84
|
+
|
|
85
|
+
## 8.1.0-canary.14
|
|
86
|
+
|
|
87
|
+
## 8.1.0-canary.13
|
|
88
|
+
|
|
89
|
+
## 8.1.0-canary.12
|
|
90
|
+
|
|
91
|
+
## 8.1.0-canary.11
|
|
92
|
+
|
|
93
|
+
## 8.1.0-canary.10
|
|
94
|
+
|
|
95
|
+
## 8.1.0-canary.9
|
|
96
|
+
|
|
97
|
+
## 8.1.0-canary.8
|
|
98
|
+
|
|
99
|
+
## 8.1.0-canary.7
|
|
100
|
+
|
|
101
|
+
## 8.1.0-canary.6
|
|
102
|
+
|
|
3
103
|
## 8.1.0-canary.5
|
|
4
104
|
|
|
5
105
|
## 8.0.6-canary.4
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { emailPattern, FieldValues } from '@graphcommerce/react-hook-form'
|
|
2
|
+
import { i18n } from '@lingui/core'
|
|
3
|
+
import { Trans } from '@lingui/react'
|
|
4
|
+
import { TextFieldElement, TextFieldElementProps } from './TextFieldElement'
|
|
5
|
+
|
|
6
|
+
export type EmailElementProps<T extends FieldValues> = TextFieldElementProps<T>
|
|
7
|
+
|
|
8
|
+
export function EmailElement<TFieldValues extends FieldValues>(
|
|
9
|
+
props: EmailElementProps<TFieldValues>,
|
|
10
|
+
): JSX.Element {
|
|
11
|
+
const { rules, ...rest } = props
|
|
12
|
+
return (
|
|
13
|
+
<TextFieldElement
|
|
14
|
+
type='email'
|
|
15
|
+
label={<Trans id='Email address' />}
|
|
16
|
+
autoComplete='email'
|
|
17
|
+
rules={{
|
|
18
|
+
pattern: {
|
|
19
|
+
value: emailPattern,
|
|
20
|
+
message: i18n._(/* i18n */ 'Please enter a valid email address'),
|
|
21
|
+
},
|
|
22
|
+
...rules,
|
|
23
|
+
}}
|
|
24
|
+
{...rest}
|
|
25
|
+
/>
|
|
26
|
+
)
|
|
27
|
+
}
|
|
@@ -115,7 +115,11 @@ export function NumberFieldElement<T extends FieldValues>(props: NumberFieldElem
|
|
|
115
115
|
aria-label={i18n._(/* i18n */ 'Decrease')}
|
|
116
116
|
size='smaller'
|
|
117
117
|
onClick={() => {
|
|
118
|
-
if (
|
|
118
|
+
if (
|
|
119
|
+
(valueAsNumber || Infinity) <= inputProps.min ||
|
|
120
|
+
(inputProps.min === 0 && valueAsNumber <= inputProps.min)
|
|
121
|
+
)
|
|
122
|
+
return
|
|
119
123
|
onChange(value - 1)
|
|
120
124
|
}}
|
|
121
125
|
sx={{
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { FieldValues, phonePattern } from '@graphcommerce/react-hook-form'
|
|
2
|
+
import { Trans, t } from '@lingui/macro'
|
|
3
|
+
import { TextFieldElement, TextFieldElementProps } from './TextFieldElement'
|
|
4
|
+
|
|
5
|
+
export type TelephoneElementProps<T extends FieldValues> = TextFieldElementProps<T>
|
|
6
|
+
|
|
7
|
+
export function TelephoneElement<TFieldValues extends FieldValues>(
|
|
8
|
+
props: TelephoneElementProps<TFieldValues>,
|
|
9
|
+
): JSX.Element {
|
|
10
|
+
const { rules, ...rest } = props
|
|
11
|
+
return (
|
|
12
|
+
<TextFieldElement
|
|
13
|
+
type='text'
|
|
14
|
+
label={<Trans>Telephone</Trans>}
|
|
15
|
+
autoComplete='tel'
|
|
16
|
+
rules={{
|
|
17
|
+
pattern: { value: phonePattern, message: t`Invalid phone number` },
|
|
18
|
+
...rules,
|
|
19
|
+
}}
|
|
20
|
+
{...rest}
|
|
21
|
+
/>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
/* eslint-disable no-nested-ternary */
|
|
2
2
|
import { InputCheckmark } from '@graphcommerce/next-ui'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
FieldValues,
|
|
5
|
+
UseControllerProps,
|
|
6
|
+
emailPattern,
|
|
7
|
+
useController,
|
|
8
|
+
} from '@graphcommerce/react-hook-form'
|
|
4
9
|
import { i18n } from '@lingui/core'
|
|
5
10
|
import { TextField, TextFieldProps } from '@mui/material'
|
|
6
11
|
|
|
@@ -31,9 +36,7 @@ export function TextFieldElement<TFieldValues extends FieldValues>({
|
|
|
31
36
|
|
|
32
37
|
if (type === 'email' && !rules.pattern) {
|
|
33
38
|
rules.pattern = {
|
|
34
|
-
|
|
35
|
-
value:
|
|
36
|
-
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
|
|
39
|
+
value: emailPattern,
|
|
37
40
|
message: i18n._(/* i18n */ 'Please enter a valid email address'),
|
|
38
41
|
}
|
|
39
42
|
}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
export * from './AutoCompleteElement'
|
|
2
2
|
export * from './CheckboxButtonGroup'
|
|
3
3
|
export * from './CheckboxElement'
|
|
4
|
+
export * from './EmailElement'
|
|
4
5
|
export * from './MultiSelectElement'
|
|
6
|
+
export * from './NumberFieldElement'
|
|
5
7
|
export * from './PasswordElement'
|
|
6
8
|
export * from './PasswordRepeatElement'
|
|
7
|
-
export * from './NumberFieldElement'
|
|
8
|
-
export * from './SliderElement'
|
|
9
|
-
export * from './SwitchElement'
|
|
10
9
|
export * from './RadioButtonGroup'
|
|
11
10
|
export * from './SelectElement'
|
|
11
|
+
export * from './SliderElement'
|
|
12
|
+
export * from './SwitchElement'
|
|
13
|
+
export * from './TelephoneElement'
|
|
12
14
|
export * from './TextFieldElement'
|
|
13
15
|
export * from './ToggleButtonGroup'
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { styled, Tooltip, tooltipClasses } from '@mui/material'
|
|
2
|
+
|
|
3
|
+
export const LightTooltip = styled<typeof Tooltip>(({ className, ...props }) => (
|
|
4
|
+
<Tooltip {...props} classes={{ popper: className }} />
|
|
5
|
+
))(({ theme }) => ({
|
|
6
|
+
[`& .${tooltipClasses.tooltip}`]: {
|
|
7
|
+
backgroundColor: theme.palette.common.white,
|
|
8
|
+
color: theme.palette.text.primary,
|
|
9
|
+
boxShadow: theme.shadows[1],
|
|
10
|
+
},
|
|
11
|
+
}))
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { PreviewData } from '@graphcommerce/graphql'
|
|
2
|
+
import {
|
|
3
|
+
Button,
|
|
4
|
+
IconSvg,
|
|
5
|
+
MessageSnackbar,
|
|
6
|
+
iconChevronRight,
|
|
7
|
+
iconClose,
|
|
8
|
+
iconContrast,
|
|
9
|
+
iconInfo,
|
|
10
|
+
iconRefresh,
|
|
11
|
+
} from '@graphcommerce/next-ui'
|
|
12
|
+
import { FormAutoSubmit, FormPersist, FormProvider, useForm } from '@graphcommerce/react-hook-form'
|
|
13
|
+
import { Box, IconButton } from '@mui/material'
|
|
14
|
+
import { useRouter } from 'next/router'
|
|
15
|
+
import { TextFieldElement } from '../FormComponents'
|
|
16
|
+
import { LightTooltip } from './LightTooltip'
|
|
17
|
+
import { PreviewModeActions } from './PreviewModeActions'
|
|
18
|
+
import { PreviewModeToolbar } from './PreviewModeToolbar'
|
|
19
|
+
import { previewModeDefaults } from './previewModeDefaults'
|
|
20
|
+
|
|
21
|
+
export function getPreviewUrl() {
|
|
22
|
+
const url = new URL(window.location.href)
|
|
23
|
+
url.pathname = '/api/preview'
|
|
24
|
+
;[...url.searchParams.entries()].forEach(([key]) => url.searchParams.delete(key))
|
|
25
|
+
return url
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function PreviewModeEnabled() {
|
|
29
|
+
const form = useForm<{ secret: string; previewData: PreviewData }>({
|
|
30
|
+
defaultValues: { previewData: previewModeDefaults() },
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const submit = form.handleSubmit((formValues) => {
|
|
34
|
+
const url = getPreviewUrl()
|
|
35
|
+
url.searchParams.append('action', 'update')
|
|
36
|
+
|
|
37
|
+
Object.entries(formValues).forEach(([key, value]) => {
|
|
38
|
+
url.searchParams.append(key, JSON.stringify(value))
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
window.location.href = url.toString()
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const exitHandler = form.handleSubmit(() => {
|
|
45
|
+
const url = getPreviewUrl()
|
|
46
|
+
url.searchParams.append('action', 'exit')
|
|
47
|
+
|
|
48
|
+
window.location.href = url.toString()
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const revalidateHandler = form.handleSubmit((formValues) => {
|
|
52
|
+
const url = getPreviewUrl()
|
|
53
|
+
Object.entries(formValues).forEach(([key, value]) => {
|
|
54
|
+
url.searchParams.append(key, `${value}`)
|
|
55
|
+
})
|
|
56
|
+
url.searchParams.append('action', 'revalidate')
|
|
57
|
+
window.location.href = url.toString()
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<FormProvider {...form}>
|
|
62
|
+
<MessageSnackbar
|
|
63
|
+
variant='pill'
|
|
64
|
+
severity='warning'
|
|
65
|
+
disableBackdropClick
|
|
66
|
+
disableClose
|
|
67
|
+
open
|
|
68
|
+
icon={iconContrast}
|
|
69
|
+
onClose={() => {}}
|
|
70
|
+
action={
|
|
71
|
+
<>
|
|
72
|
+
<PreviewModeActions />
|
|
73
|
+
<LightTooltip title='Revalidate / Regenerate Page' placement='top'>
|
|
74
|
+
<IconButton color='secondary' type='submit' onClick={revalidateHandler}>
|
|
75
|
+
<IconSvg src={iconRefresh} />
|
|
76
|
+
</IconButton>
|
|
77
|
+
</LightTooltip>
|
|
78
|
+
<LightTooltip title='Stop preview mode' placement='top'>
|
|
79
|
+
<IconButton color='secondary' type='submit' onClick={exitHandler}>
|
|
80
|
+
<IconSvg src={iconClose} />
|
|
81
|
+
</IconButton>
|
|
82
|
+
</LightTooltip>
|
|
83
|
+
</>
|
|
84
|
+
}
|
|
85
|
+
>
|
|
86
|
+
<Box sx={{ display: 'grid', gridAutoFlow: 'column', placeItems: 'center', gap: 4 }}>
|
|
87
|
+
<Box sx={{ display: 'flex', placeItems: 'center' }}>
|
|
88
|
+
Preview Mode{' '}
|
|
89
|
+
<LightTooltip title='You are currently viewing the website in Preview Mode (caches are disabled).'>
|
|
90
|
+
<IconButton size='small'>
|
|
91
|
+
<IconSvg src={iconInfo} />
|
|
92
|
+
</IconButton>
|
|
93
|
+
</LightTooltip>
|
|
94
|
+
</Box>
|
|
95
|
+
<PreviewModeToolbar />
|
|
96
|
+
</Box>
|
|
97
|
+
</MessageSnackbar>
|
|
98
|
+
<FormPersist form={form} name='PreviewModePreviewData' />
|
|
99
|
+
<FormAutoSubmit control={form.control} submit={submit} />
|
|
100
|
+
</FormProvider>
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function PreviewModeDisabled() {
|
|
105
|
+
const form = useForm<{ secret: string }>({})
|
|
106
|
+
|
|
107
|
+
const submit = form.handleSubmit((formValues) => {
|
|
108
|
+
const url = getPreviewUrl()
|
|
109
|
+
url.searchParams.append('action', 'enable')
|
|
110
|
+
|
|
111
|
+
Object.entries(formValues).forEach(([key, value]) => {
|
|
112
|
+
url.searchParams.append(key, typeof value === 'string' ? value : JSON.stringify(value))
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
window.location.href = url.toString()
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
return (
|
|
119
|
+
<FormProvider {...form}>
|
|
120
|
+
<MessageSnackbar
|
|
121
|
+
variant='pill'
|
|
122
|
+
severity='warning'
|
|
123
|
+
disableBackdropClick
|
|
124
|
+
disableClose
|
|
125
|
+
open
|
|
126
|
+
icon={iconContrast}
|
|
127
|
+
onClose={() => {}}
|
|
128
|
+
action={
|
|
129
|
+
<IconButton color='secondary' type='submit' onClick={submit}>
|
|
130
|
+
<IconSvg src={iconChevronRight} />
|
|
131
|
+
</IconButton>
|
|
132
|
+
}
|
|
133
|
+
>
|
|
134
|
+
<Box sx={{ display: 'grid', gridAutoFlow: 'column', placeItems: 'center', gap: 4 }}>
|
|
135
|
+
<Box sx={{ display: 'flex', placeItems: 'center' }}>Preview Mode</Box>
|
|
136
|
+
<TextFieldElement control={form.control} name='secret' label='Secret' />
|
|
137
|
+
</Box>
|
|
138
|
+
</MessageSnackbar>
|
|
139
|
+
<FormPersist form={form} name='PreviewModePreviewData' />
|
|
140
|
+
</FormProvider>
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function PreviewMode() {
|
|
145
|
+
const router = useRouter()
|
|
146
|
+
|
|
147
|
+
return router.isPreview ? <PreviewModeEnabled /> : <PreviewModeDisabled />
|
|
148
|
+
}
|
package/components/index.ts
CHANGED
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": "8.1.0-canary.
|
|
5
|
+
"version": "8.1.0-canary.50",
|
|
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": "^8.1.0-canary.
|
|
16
|
-
"@graphcommerce/graphql": "^8.1.0-canary.
|
|
17
|
-
"@graphcommerce/next-ui": "^8.1.0-canary.
|
|
18
|
-
"@graphcommerce/prettier-config-pwa": "^8.1.0-canary.
|
|
19
|
-
"@graphcommerce/react-hook-form": "^8.1.0-canary.
|
|
20
|
-
"@graphcommerce/typescript-config-pwa": "^8.1.0-canary.
|
|
15
|
+
"@graphcommerce/eslint-config-pwa": "^8.1.0-canary.50",
|
|
16
|
+
"@graphcommerce/graphql": "^8.1.0-canary.50",
|
|
17
|
+
"@graphcommerce/next-ui": "^8.1.0-canary.50",
|
|
18
|
+
"@graphcommerce/prettier-config-pwa": "^8.1.0-canary.50",
|
|
19
|
+
"@graphcommerce/react-hook-form": "^8.1.0-canary.50",
|
|
20
|
+
"@graphcommerce/typescript-config-pwa": "^8.1.0-canary.50",
|
|
21
21
|
"@lingui/core": "^4.2.1",
|
|
22
22
|
"@lingui/macro": "^4.2.1",
|
|
23
23
|
"@lingui/react": "^4.2.1",
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { PagesProps } from '@graphcommerce/framer-next-pages'
|
|
2
|
+
import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
|
|
3
|
+
import dynamic from 'next/dynamic'
|
|
4
|
+
import { useEffect, useState } from 'react'
|
|
5
|
+
|
|
6
|
+
const PreviewMode = dynamic(
|
|
7
|
+
async () => (await import('../components/PreviewMode/PreviewMode')).PreviewMode,
|
|
8
|
+
{},
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
export const config: PluginConfig = {
|
|
12
|
+
type: 'component',
|
|
13
|
+
module: '@graphcommerce/framer-next-pages',
|
|
14
|
+
ifConfig: 'previewSecret',
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function FramerNextPages(props: PluginProps<PagesProps>) {
|
|
18
|
+
const { Prev, router, ...rest } = props
|
|
19
|
+
const [enabled, setEnabled] = useState(router.isPreview)
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
const handler = (e: KeyboardEvent) => {
|
|
23
|
+
if (e.altKey && e.code === 'Backquote') {
|
|
24
|
+
setEnabled((prev) => !prev)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
window.addEventListener('keydown', handler, false)
|
|
29
|
+
return () => window.removeEventListener('keydown', handler)
|
|
30
|
+
}, [])
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<>
|
|
34
|
+
<Prev {...rest} router={router} />
|
|
35
|
+
{enabled && <PreviewMode />}
|
|
36
|
+
</>
|
|
37
|
+
)
|
|
38
|
+
}
|
package/route/preview.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { PreviewData } from '@graphcommerce/graphql'
|
|
2
|
+
import { NextApiRequest, NextApiResponse } from 'next'
|
|
3
|
+
import { previewModeDefaults } from '../components/PreviewMode/previewModeDefaults'
|
|
4
|
+
|
|
5
|
+
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
6
|
+
const { action } = req.query
|
|
7
|
+
|
|
8
|
+
// const domain = req.url ? new URL(req.url) : undefined
|
|
9
|
+
const referer = req.headers.referer ? new URL(req.headers.referer) : undefined
|
|
10
|
+
const redirectTo =
|
|
11
|
+
req.headers.redirectTo ??
|
|
12
|
+
(referer && req.headers.host === referer.host ? referer.pathname : '/')
|
|
13
|
+
|
|
14
|
+
if (!action) {
|
|
15
|
+
res.status(400).json({ message: 'No action provided' })
|
|
16
|
+
res.end()
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (action === 'enable' && req.query.secret) {
|
|
21
|
+
if (req.query.secret !== import.meta.graphCommerce.previewSecret) {
|
|
22
|
+
// This secret should only be known to this API route and the CMS
|
|
23
|
+
res.status(401).json({ message: 'Invalid token' })
|
|
24
|
+
res.end()
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
res.setDraftMode({ enable: true })
|
|
29
|
+
const previewData = req.query.previewDat
|
|
30
|
+
? (JSON.parse(`${req.query.previewData}`) as PreviewData)
|
|
31
|
+
: previewModeDefaults()
|
|
32
|
+
|
|
33
|
+
res.setPreviewData(previewData)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (action === 'exit') {
|
|
37
|
+
res.setDraftMode({ enable: false })
|
|
38
|
+
res.clearPreviewData()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (action === 'revalidate') {
|
|
42
|
+
const url = referer ? new URL(referer) : undefined
|
|
43
|
+
|
|
44
|
+
if (!url) {
|
|
45
|
+
res.status(401).json({ message: 'No referer header found' })
|
|
46
|
+
res.end()
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await res.revalidate(url.pathname)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (action === 'update' && req.preview && req.query.previewData) {
|
|
54
|
+
// todo we should probabaly validate this.
|
|
55
|
+
res.setPreviewData(JSON.parse(`${req.query.previewData}`) as PreviewData)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
res.writeHead(307, { Location: redirectTo })
|
|
59
|
+
res.end()
|
|
60
|
+
}
|