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

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,107 @@
1
1
  # @graphcommerce/ecommerce-ui
2
2
 
3
+ ## 9.0.0-canary.55
4
+
5
+ ## 9.0.0-canary.54
6
+
7
+ ## 8.1.0-canary.53
8
+
9
+ ## 8.1.0-canary.52
10
+
11
+ ## 8.1.0-canary.51
12
+
13
+ ## 8.1.0-canary.50
14
+
15
+ ## 8.1.0-canary.49
16
+
17
+ ## 8.1.0-canary.48
18
+
19
+ ### Minor Changes
20
+
21
+ - [#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
22
+ ([@Giovanni-Schroevers](https://github.com/Giovanni-Schroevers))
23
+
24
+ ## 8.1.0-canary.47
25
+
26
+ ## 8.1.0-canary.46
27
+
28
+ ## 8.1.0-canary.45
29
+
30
+ ## 8.1.0-canary.44
31
+
32
+ ## 8.1.0-canary.43
33
+
34
+ ## 8.1.0-canary.42
35
+
36
+ ## 8.1.0-canary.41
37
+
38
+ ## 8.1.0-canary.40
39
+
40
+ ## 8.1.0-canary.39
41
+
42
+ ## 8.1.0-canary.38
43
+
44
+ ## 8.1.0-canary.37
45
+
46
+ ## 8.1.0-canary.36
47
+
48
+ ## 8.1.0-canary.35
49
+
50
+ ### Patch Changes
51
+
52
+ - [#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
53
+ ([@Giovanni-Schroevers](https://github.com/Giovanni-Schroevers))
54
+
55
+ ## 8.1.0-canary.34
56
+
57
+ ## 8.1.0-canary.33
58
+
59
+ ## 8.1.0-canary.32
60
+
61
+ ## 8.1.0-canary.31
62
+
63
+ ## 8.1.0-canary.30
64
+
65
+ ## 8.1.0-canary.29
66
+
67
+ ## 8.1.0-canary.28
68
+
69
+ ## 8.1.0-canary.27
70
+
71
+ ## 8.1.0-canary.26
72
+
73
+ ## 8.1.0-canary.25
74
+
75
+ ## 8.1.0-canary.24
76
+
77
+ ## 8.1.0-canary.23
78
+
79
+ ## 8.1.0-canary.22
80
+
81
+ ## 8.1.0-canary.21
82
+
83
+ ## 8.1.0-canary.20
84
+
85
+ ## 8.1.0-canary.19
86
+
87
+ ## 8.1.0-canary.18
88
+
89
+ ## 8.1.0-canary.17
90
+
91
+ ## 8.1.0-canary.16
92
+
93
+ ## 8.1.0-canary.15
94
+
95
+ ## 8.1.0-canary.14
96
+
97
+ ## 8.1.0-canary.13
98
+
99
+ ## 8.1.0-canary.12
100
+
101
+ ## 8.1.0-canary.11
102
+
103
+ ## 8.1.0-canary.10
104
+
3
105
  ## 8.1.0-canary.9
4
106
 
5
107
  ## 8.1.0-canary.8
@@ -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 ((valueAsNumber || Infinity) <= inputProps.min) return
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 { FieldValues, UseControllerProps, useController } from '@graphcommerce/react-hook-form'
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
- // eslint-disable-next-line no-useless-escape
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
+ }
@@ -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.55",
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.55",
16
+ "@graphcommerce/graphql": "^9.0.0-canary.55",
17
+ "@graphcommerce/next-ui": "^9.0.0-canary.55",
18
+ "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.55",
19
+ "@graphcommerce/react-hook-form": "^9.0.0-canary.55",
20
+ "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.55",
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
+ }