@campxdev/shared 0.3.3 → 0.3.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@campxdev/shared",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "main": "./exports.ts",
5
5
  "scripts": {
6
6
  "start": "react-scripts start",
@@ -1,12 +1,25 @@
1
+ import {ReactNode} from 'react'
1
2
  import {ErrorBoundary as ReactErrorBoundary} from 'react-error-boundary'
2
3
  import {QueryErrorResetBoundary} from 'react-query'
4
+ import {useLocation} from 'react-router-dom'
3
5
  import ErrorFallback from './ErrorFallback'
4
6
 
5
- export default function ErrorBoundary({children}) {
7
+ export default function ErrorBoundary({
8
+ children,
9
+ resetKey,
10
+ }: {
11
+ children: ReactNode
12
+ resetKey?: string
13
+ }) {
14
+ const location = useLocation()
6
15
  return (
7
16
  <QueryErrorResetBoundary>
8
17
  {({reset}) => (
9
- <ReactErrorBoundary fallbackRender={ErrorFallback} onReset={reset}>
18
+ <ReactErrorBoundary
19
+ key={resetKey ?? location?.pathname}
20
+ fallbackRender={ErrorFallback}
21
+ onReset={reset}
22
+ >
10
23
  {children}
11
24
  </ReactErrorBoundary>
12
25
  )}
@@ -23,7 +23,7 @@ export default function ErrorFallback({error, resetErrorBoundary}) {
23
23
  return (
24
24
  <Box sx={{marginTop: '16px', padding: '16px'}}>
25
25
  <StyledAlert severity='error'>
26
- {error?.message}
26
+ {error?.response?.data?.message ?? error?.message}
27
27
  <Button
28
28
  className='retryBtn'
29
29
  onClick={() => resetErrorBoundary()}
@@ -1,14 +1,17 @@
1
- import styled from 'styled-components'
1
+ import {Box, styled} from '@mui/material'
2
+ import ErrorBoundary from './ErrorBoundary'
2
3
 
3
- const StyledPageContent = styled.div`
4
- padding-left: 25px;
5
- padding-right: 25px;
6
- margin-top: 16px;
7
- padding-bottom: 3rem;
8
- `
4
+ const StyledPageContent = styled(Box)(() => ({
5
+ paddingLeft: '25px',
6
+ paddingRight: '25px',
7
+ marginTop: '16px',
8
+ paddingBottom: '3rem',
9
+ }))
9
10
 
10
11
  export function PageContent(props) {
11
- return (
12
- <StyledPageContent style={props.style}>{props.children}</StyledPageContent>
13
- )
12
+ return (
13
+ <StyledPageContent style={props.style}>
14
+ <ErrorBoundary>{props.children}</ErrorBoundary>
15
+ </StyledPageContent>
16
+ )
14
17
  }
@@ -1,22 +1,40 @@
1
- import { createContext, useState } from 'react'
1
+ import {createContext, ReactNode, useState} from 'react'
2
2
  import PopupConfirm from './PopupConfirm'
3
3
 
4
- export const ConfirmContext = createContext(null)
4
+ interface ConfirmContextProps {
5
+ confirm: {
6
+ prompt: ReactNode
7
+ isOpen: boolean
8
+ proceed: () => void
9
+ cancel: () => void
10
+ }
11
+ setConfirm: any
12
+ }
13
+
14
+ export const ConfirmContext = createContext<ConfirmContextProps>({
15
+ confirm: {
16
+ cancel: () => {},
17
+ proceed: () => {},
18
+ prompt: 'Are you sure?',
19
+ isOpen: false,
20
+ },
21
+ setConfirm: () => {},
22
+ })
5
23
 
6
- const ConfirmContextProvider = ({ children }) => {
7
- const [confirm, setConfirm] = useState({
8
- prompt: '',
9
- isOpen: false,
10
- proceed: null,
11
- cancel: null,
12
- })
24
+ const ConfirmContextProvider = ({children}) => {
25
+ const [confirm, setConfirm] = useState({
26
+ prompt: '',
27
+ isOpen: false,
28
+ proceed: null,
29
+ cancel: null,
30
+ })
13
31
 
14
- return (
15
- <ConfirmContext.Provider value={[confirm, setConfirm]}>
16
- {children}
17
- <PopupConfirm />
18
- </ConfirmContext.Provider>
19
- )
32
+ return (
33
+ <ConfirmContext.Provider value={{confirm, setConfirm}}>
34
+ {children}
35
+ <PopupConfirm />
36
+ </ConfirmContext.Provider>
37
+ )
20
38
  }
21
39
 
22
40
  export default ConfirmContextProvider
@@ -1,47 +1,47 @@
1
- import { useContext, useEffect, useState } from 'react'
2
- import { ConfirmContext } from './ConfirmContextProvider'
1
+ import {ReactNode, useContext, useEffect, useState} from 'react'
2
+ import {ConfirmContext} from './ConfirmContextProvider'
3
3
 
4
4
  export default function useConfirm(): {
5
- isConfirmed: (prompt?: string) => Promise<boolean>
6
- prompt?: ''
7
- isOpen?: false
8
- proceed?: null
9
- cancel?: null
5
+ isConfirmed: (prompt?: string) => Promise<boolean>
6
+ prompt?: ReactNode
7
+ isOpen?: boolean
8
+ proceed?: () => void
9
+ cancel?: () => void
10
10
  } {
11
- const [confirm, setConfirm] = useContext(ConfirmContext)
12
- const [needsCleanup, setNeedsCleanup] = useState(false)
11
+ const {confirm, setConfirm} = useContext(ConfirmContext)
12
+ const [needsCleanup, setNeedsCleanup] = useState(false)
13
13
 
14
- const isConfirmed = (prompt) => {
15
- setNeedsCleanup(true)
16
- const promise = new Promise((resolve, reject) => {
17
- setConfirm({
18
- prompt,
19
- isOpen: true,
20
- proceed: resolve,
21
- cancel: reject,
22
- })
23
- })
24
- return promise.then(
25
- () => {
26
- setConfirm({ ...confirm, isOpen: false })
27
- return true
28
- },
29
- () => {
30
- setConfirm({ ...confirm, isOpen: false })
31
- return false
32
- },
33
- )
34
- }
14
+ const isConfirmed = (prompt) => {
15
+ setNeedsCleanup(true)
16
+ const promise = new Promise((resolve, reject) => {
17
+ setConfirm({
18
+ prompt,
19
+ isOpen: true,
20
+ proceed: resolve,
21
+ cancel: reject,
22
+ })
23
+ })
24
+ return promise.then(
25
+ () => {
26
+ setConfirm({...confirm, isOpen: false})
27
+ return true
28
+ },
29
+ () => {
30
+ setConfirm({...confirm, isOpen: false})
31
+ return false
32
+ }
33
+ )
34
+ }
35
35
 
36
- useEffect(() => {
37
- return () => {
38
- if (confirm.cancel && needsCleanup) {
39
- confirm.cancel()
40
- }
41
- }
42
- }, [confirm, needsCleanup])
43
- return {
44
- ...confirm,
45
- isConfirmed,
46
- }
36
+ useEffect(() => {
37
+ return () => {
38
+ if (confirm.cancel && needsCleanup) {
39
+ confirm.cancel()
40
+ }
41
+ }
42
+ }, [confirm, needsCleanup])
43
+ return {
44
+ ...confirm,
45
+ isConfirmed,
46
+ }
47
47
  }
@@ -1,6 +1,7 @@
1
- export { default as getUrlParams } from './getUrlParams'
2
- export { default as arrayPadEnd } from './arrayPadEnd'
3
- export { default as romanize } from './romanize'
1
+ export {default as getUrlParams} from './getUrlParams'
2
+ export {default as arrayPadEnd} from './arrayPadEnd'
3
+ export {default as romanize} from './romanize'
4
4
 
5
- export { default as withRouteWrapper } from './withRouteWrapper'
6
- export { default as withSuspense } from './withSuspense'
5
+ export {default as withRouteWrapper} from './withRouteWrapper'
6
+ export {default as withSuspense} from './withSuspense'
7
+ export {default as withLocalization} from './withLocalization'
@@ -0,0 +1,11 @@
1
+ import {LocalizationProvider} from '@mui/x-date-pickers'
2
+ import {AdapterMoment} from '@mui/x-date-pickers/AdapterMoment'
3
+ import {ReactNode} from 'react'
4
+
5
+ export default function withLocalization(component: ReactNode) {
6
+ return (
7
+ <LocalizationProvider dateAdapter={AdapterMoment}>
8
+ {component}
9
+ </LocalizationProvider>
10
+ )
11
+ }