@graphcommerce/ecommerce-ui 8.1.0-canary.9 → 9.0.0-canary.101

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.
@@ -1,8 +1,8 @@
1
1
  import {
2
- Controller,
3
2
  ControllerProps,
4
3
  FieldError,
5
4
  FieldValues,
5
+ useController,
6
6
  } from '@graphcommerce/react-hook-form'
7
7
  import { i18n } from '@lingui/core'
8
8
  import {
@@ -25,8 +25,6 @@ type SingleToggleButtonProps = Omit<ToggleButtonProps, 'value' | 'children'> & {
25
25
  export type ToggleButtonGroupElementProps<T extends FieldValues> = ToggleButtonGroupProps & {
26
26
  required?: boolean
27
27
  label?: string
28
- /** @deprecated Form value parsing should happen in the handleSubmit function of the form */
29
- parseError?: (error: FieldError) => string
30
28
  options: SingleToggleButtonProps[]
31
29
  formLabelProps?: FormLabelProps
32
30
  helperText?: string
@@ -39,9 +37,11 @@ export function ToggleButtonGroupElement<TFieldValues extends FieldValues = Fiel
39
37
  rules = {},
40
38
  required,
41
39
  options = [],
42
- parseError,
43
40
  helperText,
44
41
  formLabelProps,
42
+ defaultValue,
43
+ disabled,
44
+ shouldUnregister,
45
45
  ...toggleButtonGroupProps
46
46
  }: ToggleButtonGroupElementProps<TFieldValues>) {
47
47
  if (required && !rules.required) {
@@ -49,50 +49,51 @@ export function ToggleButtonGroupElement<TFieldValues extends FieldValues = Fiel
49
49
  }
50
50
 
51
51
  const isRequired = required || !!rules?.required
52
+
53
+ const {
54
+ field: { value, onChange, onBlur },
55
+ fieldState: { invalid, error },
56
+ } = useController({
57
+ name,
58
+ control,
59
+ rules,
60
+ defaultValue,
61
+ disabled,
62
+ shouldUnregister,
63
+ })
64
+
65
+ const renderHelperText = error ? error.message : helperText
66
+
52
67
  return (
53
- <Controller
54
- name={name}
55
- control={control}
56
- rules={rules}
57
- render={({ field: { value, onChange, onBlur }, fieldState: { invalid, error } }) => {
58
- const renderHelperText = error
59
- ? typeof parseError === 'function'
60
- ? parseError(error)
61
- : error.message
62
- : helperText
63
- return (
64
- <FormControl error={invalid} required={isRequired}>
65
- {label && (
66
- <FormLabel
67
- {...formLabelProps}
68
- error={invalid}
69
- required={isRequired}
70
- sx={{ mb: 1, ...formLabelProps?.sx }}
71
- >
72
- {label}
73
- </FormLabel>
74
- )}
75
- <ToggleButtonGroup
76
- {...toggleButtonGroupProps}
77
- value={value}
78
- onBlur={onBlur}
79
- onChange={(event, val) => {
80
- onChange(val)
81
- if (typeof toggleButtonGroupProps.onChange === 'function') {
82
- toggleButtonGroupProps.onChange(event, val)
83
- }
84
- }}
85
- >
86
- {options.map(({ label, id, ...toggleProps }) => (
87
- <ToggleButton value={id} {...toggleProps} key={id}>
88
- {label}
89
- </ToggleButton>
90
- ))}
91
- </ToggleButtonGroup>
92
- {renderHelperText && <FormHelperText>{renderHelperText}</FormHelperText>}
93
- </FormControl>
94
- )
95
- }}
96
- />
68
+ <FormControl error={invalid} required={isRequired}>
69
+ {label && (
70
+ <FormLabel
71
+ {...formLabelProps}
72
+ error={invalid}
73
+ required={isRequired}
74
+ sx={{ mb: 1, ...formLabelProps?.sx }}
75
+ >
76
+ {label}
77
+ </FormLabel>
78
+ )}
79
+ <ToggleButtonGroup
80
+ {...toggleButtonGroupProps}
81
+ value={value}
82
+ onBlur={onBlur}
83
+ onChange={(event, val) => {
84
+ onChange(val)
85
+ if (typeof toggleButtonGroupProps.onChange === 'function') {
86
+ toggleButtonGroupProps.onChange(event, val)
87
+ }
88
+ }}
89
+ >
90
+ {options.map(({ label, id, ...toggleProps }) => (
91
+ <ToggleButton value={id} {...toggleProps} key={id}>
92
+ {label}
93
+ </ToggleButton>
94
+ ))}
95
+ </ToggleButtonGroup>
96
+ {renderHelperText && <FormHelperText>{renderHelperText}</FormHelperText>}
97
+ </FormControl>
97
98
  )
98
99
  }
@@ -1,13 +1,16 @@
1
+ export * from './ActionCardListForm'
1
2
  export * from './AutoCompleteElement'
2
3
  export * from './CheckboxButtonGroup'
3
4
  export * from './CheckboxElement'
5
+ export * from './EmailElement'
4
6
  export * from './MultiSelectElement'
7
+ export * from './NumberFieldElement'
5
8
  export * from './PasswordElement'
6
9
  export * from './PasswordRepeatElement'
7
- export * from './NumberFieldElement'
8
- export * from './SliderElement'
9
- export * from './SwitchElement'
10
10
  export * from './RadioButtonGroup'
11
11
  export * from './SelectElement'
12
+ export * from './SliderElement'
13
+ export * from './SwitchElement'
14
+ export * from './TelephoneElement'
12
15
  export * from './TextFieldElement'
13
16
  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,151 @@
1
+ import { PreviewData } from '@graphcommerce/graphql'
2
+ import {
3
+ IconSvg,
4
+ MessageSnackbar,
5
+ iconChevronRight,
6
+ iconClose,
7
+ iconContrast,
8
+ iconInfo,
9
+ iconRefresh,
10
+ } from '@graphcommerce/next-ui'
11
+ import { FormAutoSubmit, FormPersist, FormProvider, useForm } from '@graphcommerce/react-hook-form'
12
+ import { Box, IconButton } from '@mui/material'
13
+ import { useRouter } from 'next/router'
14
+ import { TextFieldElement } from '../FormComponents'
15
+ import { LightTooltip } from './LightTooltip'
16
+ import { PreviewModeActions } from './PreviewModeActions'
17
+ import { PreviewModeToolbar } from './PreviewModeToolbar'
18
+
19
+ export function getPreviewUrl() {
20
+ const url = new URL(window.location.href)
21
+ url.pathname = '/api/preview'
22
+ ;[...url.searchParams.entries()].forEach(([key]) => url.searchParams.delete(key))
23
+ return url
24
+ }
25
+
26
+ function PreviewModeEnabled() {
27
+ const form = useForm<{ secret: string; previewData: PreviewData }>({})
28
+
29
+ const submit = form.handleSubmit((formValues) => {
30
+ const url = getPreviewUrl()
31
+ url.searchParams.append('action', 'update')
32
+
33
+ Object.entries(formValues).forEach(([key, value]) => {
34
+ url.searchParams.append(key, JSON.stringify(value))
35
+ })
36
+
37
+ window.location.href = url.toString()
38
+ })
39
+
40
+ const exitHandler = form.handleSubmit(() => {
41
+ const url = getPreviewUrl()
42
+ url.searchParams.append('action', 'exit')
43
+
44
+ window.location.href = url.toString()
45
+ })
46
+
47
+ const revalidateHandler = form.handleSubmit((formValues) => {
48
+ const url = getPreviewUrl()
49
+ Object.entries(formValues).forEach(([key, value]) => {
50
+ url.searchParams.append(key, `${value}`)
51
+ })
52
+ url.searchParams.append('action', 'revalidate')
53
+ window.location.href = url.toString()
54
+ })
55
+
56
+ return (
57
+ <FormProvider {...form}>
58
+ <MessageSnackbar
59
+ variant='pill'
60
+ severity='warning'
61
+ disableBackdropClick
62
+ disableClose
63
+ open
64
+ icon={iconContrast}
65
+ onClose={() => {}}
66
+ action={
67
+ <>
68
+ <PreviewModeActions />
69
+ <LightTooltip title='Revalidate / Regenerate Page' placement='top'>
70
+ <IconButton color='secondary' type='submit' onClick={revalidateHandler}>
71
+ <IconSvg src={iconRefresh} />
72
+ </IconButton>
73
+ </LightTooltip>
74
+ <LightTooltip title='Stop preview mode' placement='top'>
75
+ <IconButton color='secondary' type='submit' onClick={exitHandler}>
76
+ <IconSvg src={iconClose} />
77
+ </IconButton>
78
+ </LightTooltip>
79
+ </>
80
+ }
81
+ >
82
+ <Box sx={{ display: 'grid', gridAutoFlow: 'column', placeItems: 'center', gap: 4 }}>
83
+ <Box sx={{ display: 'flex', placeItems: 'center' }}>
84
+ Preview Mode{' '}
85
+ <LightTooltip title='You are currently viewing the website in Preview Mode (caches are disabled).'>
86
+ <IconButton size='small'>
87
+ <IconSvg src={iconInfo} />
88
+ </IconButton>
89
+ </LightTooltip>
90
+ </Box>
91
+ <PreviewModeToolbar />
92
+ </Box>
93
+ </MessageSnackbar>
94
+ <FormPersist form={form} name='PreviewModePreviewData' />
95
+ <FormAutoSubmit control={form.control} submit={submit} />
96
+ </FormProvider>
97
+ )
98
+ }
99
+
100
+ function PreviewModeDisabled() {
101
+ const form = useForm<{ secret: string }>({
102
+ defaultValues: {
103
+ secret:
104
+ process.env.NODE_ENV === 'development'
105
+ ? (import.meta.graphCommerce.previewSecret ?? '')
106
+ : '',
107
+ },
108
+ })
109
+
110
+ const submit = form.handleSubmit((formValues) => {
111
+ const url = getPreviewUrl()
112
+ url.searchParams.append('action', 'enable')
113
+
114
+ Object.entries(formValues).forEach(([key, value]) => {
115
+ url.searchParams.append(key, typeof value === 'string' ? value : JSON.stringify(value))
116
+ })
117
+
118
+ window.location.href = url.toString()
119
+ })
120
+
121
+ return (
122
+ <FormProvider {...form}>
123
+ <MessageSnackbar
124
+ variant='pill'
125
+ severity='warning'
126
+ disableBackdropClick
127
+ disableClose
128
+ open
129
+ icon={iconContrast}
130
+ onClose={() => {}}
131
+ action={
132
+ <IconButton color='secondary' type='submit' onClick={submit}>
133
+ <IconSvg src={iconChevronRight} />
134
+ </IconButton>
135
+ }
136
+ >
137
+ <Box sx={{ display: 'grid', gridAutoFlow: 'column', placeItems: 'center', gap: 4 }}>
138
+ <Box sx={{ display: 'flex', placeItems: 'center' }}>Preview Mode</Box>
139
+ <TextFieldElement control={form.control} name='secret' label='Secret' />
140
+ </Box>
141
+ </MessageSnackbar>
142
+ <FormPersist form={form} name='PreviewModePreviewData' />
143
+ </FormProvider>
144
+ )
145
+ }
146
+
147
+ export function PreviewMode() {
148
+ const router = useRouter()
149
+
150
+ return router.isPreview ? <PreviewModeEnabled /> : <PreviewModeDisabled />
151
+ }
@@ -0,0 +1,6 @@
1
+ export type PreviewModeActionsProps = Record<string, unknown>
2
+
3
+ export function PreviewModeActions(props: PreviewModeActionsProps) {
4
+ const {} = props
5
+ return <></>
6
+ }
@@ -0,0 +1,6 @@
1
+ export type PreviewModeToolbarProps = Record<string, unknown>
2
+
3
+ export function PreviewModeToolbar(props: PreviewModeToolbarProps) {
4
+ const {} = props
5
+ return null
6
+ }
@@ -0,0 +1,5 @@
1
+ export * from './PreviewMode'
2
+ export * from './PreviewModeActions'
3
+ export * from './PreviewModeToolbar'
4
+ export * from './usePreviewModeForm'
5
+ export * from './previewModeDefaults'
@@ -0,0 +1,5 @@
1
+ import { PreviewData } from '@graphcommerce/graphql'
2
+
3
+ export function previewModeDefaults(): PreviewData {
4
+ return {}
5
+ }
@@ -0,0 +1,6 @@
1
+ import { PreviewData } from '@graphcommerce/graphql'
2
+ import { useFormContext } from '@graphcommerce/react-hook-form'
3
+
4
+ export function usePreviewModeForm() {
5
+ return useFormContext<{ previewData: PreviewData }>()
6
+ }
@@ -2,3 +2,4 @@ export * from './ComposedSubmitButton'
2
2
  export * from './ApolloError'
3
3
  export * from './WaitForQueries'
4
4
  export * from './FormComponents'
5
+ export * from './PreviewMode'
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.9",
5
+ "version": "9.0.0-canary.101",
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.9",
16
- "@graphcommerce/graphql": "^8.1.0-canary.9",
17
- "@graphcommerce/next-ui": "^8.1.0-canary.9",
18
- "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.9",
19
- "@graphcommerce/react-hook-form": "^8.1.0-canary.9",
20
- "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.9",
15
+ "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.101",
16
+ "@graphcommerce/graphql": "^9.0.0-canary.101",
17
+ "@graphcommerce/next-ui": "^9.0.0-canary.101",
18
+ "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.101",
19
+ "@graphcommerce/react-hook-form": "^9.0.0-canary.101",
20
+ "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.101",
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
+ }
@@ -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
+ }