@xyo-network/react-access-gate 4.0.2 → 4.0.3

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.
@@ -0,0 +1,2 @@
1
+ export * from './useAccessCodes.ts';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA"}
@@ -0,0 +1,7 @@
1
+ export declare const useAccessCodes: (localStorageKey: string) => {
2
+ validated: boolean;
3
+ userAccessCodes: any[] | undefined;
4
+ onAccessCodeSuccess: () => void;
5
+ onCodeInputChange: (code?: string) => boolean;
6
+ };
7
+ //# sourceMappingURL=useAccessCodes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useAccessCodes.d.ts","sourceRoot":"","sources":["../../../src/hooks/useAccessCodes.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,cAAc,oBAAqB,MAAM;;;;+BAclB,MAAM;CAezC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xyo-network/react-access-gate",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
4
4
  "description": "Common React library for all XYO projects that use React",
5
5
  "keywords": [
6
6
  "xyo",
@@ -43,20 +43,20 @@
43
43
  "lint-pkg": "npmPkgJsonLint ."
44
44
  },
45
45
  "dependencies": {
46
- "@mui/icons-material": "^6.1.1",
46
+ "@mui/icons-material": "^6.1.2",
47
47
  "@xylabs/react-button": "^5.0.1",
48
48
  "@xylabs/react-flexbox": "^5.0.1",
49
49
  "@xylabs/react-shared": "^5.0.1"
50
50
  },
51
51
  "devDependencies": {
52
- "@mui/material": "^6.1.1",
53
- "@mui/styles": "^6.1.1",
54
- "@storybook/react": "^8.3.3",
52
+ "@mui/material": "^6.1.2",
53
+ "@mui/styles": "^6.1.2",
54
+ "@storybook/react": "^8.3.5",
55
55
  "@xylabs/ts-scripts-yarn3": "^4.0.7",
56
56
  "@xylabs/tsconfig-react": "^4.0.7",
57
57
  "react": "^18.3.1",
58
58
  "react-dom": "^18.3.1",
59
- "storybook": "^8.3.3",
59
+ "storybook": "^8.3.5",
60
60
  "typescript": "^5.6.2"
61
61
  },
62
62
  "peerDependencies": {
@@ -1,10 +1,13 @@
1
1
  import { Alert, Typography } from '@mui/material'
2
2
  import type { Meta, StoryFn } from '@storybook/react'
3
3
  import { FlexCol } from '@xylabs/react-flexbox'
4
- import React, { useState } from 'react'
4
+ import React from 'react'
5
5
 
6
+ import { useAccessCodes } from '../hooks/index.ts'
6
7
  import { AccessCodeGateFlexbox } from './AccessCodeGateFlexbox.tsx'
7
8
 
9
+ const ValidAccessCodes = ['100519']
10
+
8
11
  export default {
9
12
  component: AccessCodeGateFlexbox,
10
13
  title: 'access/AccessCodeGateFlexbox',
@@ -15,12 +18,9 @@ const Template: StoryFn<typeof AccessCodeGateFlexbox> = args => (
15
18
  )
16
19
 
17
20
  const TemplateWithAccessCodes: StoryFn<typeof AccessCodeGateFlexbox> = (args) => {
18
- const validAccessCodes = ['100519']
19
- const [validated, setValidated] = useState(false)
20
- const onAccessCodeSuccess = () => {
21
- setValidated(true)
22
- }
23
- const validateFunction = (code?: string) => code?.length === 6
21
+ const {
22
+ validated, onAccessCodeSuccess, onCodeInputChange,
23
+ } = useAccessCodes('storybook-access-codes-test')
24
24
 
25
25
  return validated
26
26
  ? <Alert severity="success">Success!</Alert>
@@ -28,11 +28,14 @@ const TemplateWithAccessCodes: StoryFn<typeof AccessCodeGateFlexbox> = (args) =>
28
28
  <FlexCol gap={2}>
29
29
  <AccessCodeGateFlexbox
30
30
  onAccessCodeSuccess={onAccessCodeSuccess}
31
- validAccessCodes={validAccessCodes}
32
- validateFunction={validateFunction}
31
+ validAccessCodes={ValidAccessCodes}
32
+ validateFunction={onCodeInputChange}
33
33
  {...args}
34
34
  />
35
- <Typography variant="caption">Hint: 100519</Typography>
35
+ <Typography variant="caption">
36
+ Hint:
37
+ {ValidAccessCodes[0]}
38
+ </Typography>
36
39
  </FlexCol>
37
40
  )
38
41
  }
@@ -0,0 +1 @@
1
+ export * from './useAccessCodes.ts'
@@ -0,0 +1,32 @@
1
+ import { useMemo, useState } from 'react'
2
+
3
+ export const useAccessCodes = (localStorageKey: string) => {
4
+ const [validated, setValidated] = useState(false)
5
+ const [codeInput, setCodeInput] = useState('')
6
+
7
+ const onAccessCodeSuccess = () => {
8
+ // Save the access code to local storage
9
+ if (codeInput) {
10
+ localStorage.setItem(localStorageKey, JSON.stringify([codeInput]))
11
+ setValidated(true)
12
+ } else {
13
+ // If the codeInput is empty, but we have success, do nothing since the successful code is already saved
14
+ setValidated(true)
15
+ }
16
+ }
17
+ const onCodeInputChange = (code?: string) => {
18
+ setCodeInput(code ?? '')
19
+ return code?.length === 6
20
+ }
21
+ const userAccessCodes = useMemo(() => {
22
+ const storedCodes = localStorage.getItem(localStorageKey)
23
+ if (storedCodes) {
24
+ const parsedResult = JSON.parse(storedCodes ?? '')
25
+ if (Array.isArray(parsedResult)) return parsedResult
26
+ }
27
+ }, [])
28
+
29
+ return {
30
+ validated, userAccessCodes, onAccessCodeSuccess, onCodeInputChange,
31
+ }
32
+ }