@campxdev/shared 1.6.11 → 1.7.0

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/exports.ts CHANGED
@@ -13,6 +13,7 @@ export { default as ConfirmContextProvider } from './src/components/PopupConfirm
13
13
  export { default as DialogProvider } from './src/components/DrawerWrapper/DrawerWrapper'
14
14
  export { default as Providers } from './src/contexts/Providers'
15
15
  export { default as PublicProviders } from './src/contexts/PublicProviders'
16
+ export { default as QueryClientProvider } from './src/contexts/QueryClientProvider'
16
17
 
17
18
  export * from './src/shared-state'
18
19
  export * from './src/assets/images'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@campxdev/shared",
3
- "version": "1.6.11",
3
+ "version": "1.7.0",
4
4
  "main": "./exports.ts",
5
5
  "scripts": {
6
6
  "start": "react-scripts start",
@@ -1,76 +1,56 @@
1
- import axios, { axiosErrorToast } from '../../config/axios'
2
1
  import { yupResolver } from '@hookform/resolvers/yup'
3
- import { Alert, Box, Button, ButtonProps, Typography } from '@mui/material'
4
- import RenderForm, { generateYupSchema, RenderFormProps } from './RenderForm'
2
+ import { Alert, Box, ButtonProps, Typography } from '@mui/material'
3
+ import { AxiosResponse } from 'axios'
5
4
  import { ReactNode, useState } from 'react'
6
5
  import { useForm } from 'react-hook-form'
7
6
  import { useMutation, useQueryClient } from 'react-query'
8
- import { toast } from 'react-toastify'
9
-
10
- const makePostRequest = async ({
11
- endpoint,
12
- postBody,
13
- }: {
14
- endpoint: string
15
- postBody: any
16
- }) => {
17
- const method = postBody?.id ? 'PUT' : 'POST'
18
- const url = postBody?.id ? `${endpoint}/${postBody?.id}` : endpoint
19
- return await axios({
20
- url,
21
- method,
22
- data: postBody,
23
- })
24
- }
7
+ import { axiosErrorToast } from '../../config/axios'
8
+ import ActionButton from '../ActionButton'
9
+ import RenderForm, { generateYupSchema, RenderFormProps } from './RenderForm'
25
10
 
26
11
  interface FormProps extends Omit<RenderFormProps, 'control'> {
27
12
  buttonProps?: ButtonProps
28
13
  submitBtn?: { label?: ReactNode }
29
- cancelBtn?: { label?: ReactNode }
30
- onSubmit?: (formData) => any
31
- onCancel?: () => void
32
- endPoint?: string
14
+ cancelBtn?: { label?: ReactNode; hide: boolean }
33
15
  defaultValues?: any
34
16
  refetchKey?: string
35
- onSuccess?: () => void
36
17
  previousData?: any
18
+ onCancel?: () => void
19
+ postFunction: (body: any) => Promise<AxiosResponse>
20
+ onPostSuccess: (response: any) => void
21
+ onPostError: (error: any) => void
37
22
  }
38
23
 
39
24
  export default function Form({
40
25
  submitBtn = { label: 'Submit' },
41
- cancelBtn = { label: 'Cancel' },
26
+ cancelBtn = { label: 'Cancel', hide: false },
42
27
  buttonProps,
43
- onSubmit = () => {},
44
- endPoint,
45
28
  defaultValues,
46
29
  cols,
47
30
  fields,
48
31
  dropdowns,
49
32
  refetchKey,
50
- onSuccess = () => {},
51
33
  onCancel,
52
34
  previousData,
35
+ postFunction,
36
+ onPostSuccess,
37
+ onPostError,
38
+ gap,
53
39
  }: FormProps) {
54
40
  const queryClient = useQueryClient()
55
41
  const [error, setError] = useState<any>(null)
56
42
  const { control, watch, handleSubmit } = useForm({
57
- defaultValues,
43
+ defaultValues: previousData ? previousData : null,
58
44
  resolver: yupResolver(generateYupSchema([fields])),
59
45
  })
60
46
 
61
- const {
62
- data,
63
- mutate,
64
- isLoading: posting,
65
- } = useMutation(makePostRequest, {
47
+ const { mutate, isLoading: posting } = useMutation(postFunction, {
66
48
  onSuccess(data) {
67
49
  queryClient.invalidateQueries(refetchKey)
68
- toast.success('Created/Updated Successfully')
69
- onSuccess && onSuccess()
50
+ onPostSuccess && onPostSuccess(data)
70
51
  },
71
52
  onError(error: any) {
72
- setError(error?.response?.data?.message)
73
- axiosErrorToast(error)
53
+ onPostError ? onPostError(error) : axiosErrorToast(error)
74
54
  },
75
55
  })
76
56
 
@@ -82,12 +62,7 @@ export default function Form({
82
62
  return (
83
63
  <form
84
64
  onSubmit={handleSubmit((originalFormData) => {
85
- const modifiedFormData = onSubmit(originalFormData) ?? originalFormData
86
-
87
- mutate({
88
- endpoint: endPoint,
89
- postBody: modifiedFormData,
90
- })
65
+ mutate(originalFormData)
91
66
  }, onError)}
92
67
  >
93
68
  <RenderForm
@@ -102,22 +77,30 @@ export default function Form({
102
77
  sx={{
103
78
  display: 'flex',
104
79
  gap: '20px',
105
- marginTop: '1rem',
80
+ marginTop: gap,
81
+ justifyContent: 'center',
106
82
  }}
107
83
  >
108
- <Button fullWidth type="submit" {...buttonProps}>
109
- {submitBtn?.label}
110
- </Button>
111
- <Button
112
- fullWidth
113
- variant="outlined"
114
- onClick={() => {
115
- onCancel ? onCancel() : null
116
- }}
84
+ <ActionButton
85
+ loading={posting}
86
+ disabled={posting}
87
+ type="submit"
117
88
  {...buttonProps}
118
89
  >
119
- {cancelBtn?.label}
120
- </Button>
90
+ {submitBtn?.label}
91
+ </ActionButton>
92
+ {!cancelBtn?.hide && (
93
+ <ActionButton
94
+ fullWidth
95
+ variant="outlined"
96
+ onClick={() => {
97
+ onCancel ? onCancel() : null
98
+ }}
99
+ {...buttonProps}
100
+ >
101
+ {cancelBtn?.label}
102
+ </ActionButton>
103
+ )}
121
104
  </Box>
122
105
  </form>
123
106
  )
@@ -28,6 +28,7 @@ import AsyncSearchSelect from './Input/AsyncSearchSelect'
28
28
  import FilterButton from './FilterComponents/FilterButton'
29
29
  import Helmet from './Layout/Helmet'
30
30
  import NavigationTabs from './Tabs/NavigationTabs'
31
+ import GlobalNetworkLoadingIndicator from './ErrorBoundary/GlobalNetworkLoadingIndicator'
31
32
  import { CustomDialog } from './ModalButtons/DialogButton'
32
33
  import { CustomDrawer } from './ModalButtons/DrawerButton'
33
34
  export { default as Image } from './Image'
@@ -88,6 +89,7 @@ export {
88
89
  NavigationTabs,
89
90
  CustomDialog,
90
91
  CustomDrawer,
92
+ GlobalNetworkLoadingIndicator,
91
93
  }
92
94
 
93
95
  export * from './UploadButton/types'