@campxdev/shared 1.6.12 → 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/package.json +1 -1
- package/src/components/Form/Form.tsx +40 -57
package/package.json
CHANGED
|
@@ -1,76 +1,56 @@
|
|
|
1
|
-
import axios, { axiosErrorToast } from '../../config/axios'
|
|
2
1
|
import { yupResolver } from '@hookform/resolvers/yup'
|
|
3
|
-
import { Alert, Box,
|
|
4
|
-
import
|
|
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 {
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
69
|
-
onSuccess && onSuccess()
|
|
50
|
+
onPostSuccess && onPostSuccess(data)
|
|
70
51
|
},
|
|
71
52
|
onError(error: any) {
|
|
72
|
-
|
|
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
|
-
|
|
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:
|
|
80
|
+
marginTop: gap,
|
|
81
|
+
justifyContent: 'center',
|
|
106
82
|
}}
|
|
107
83
|
>
|
|
108
|
-
<
|
|
109
|
-
{
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
{
|
|
120
|
-
</
|
|
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
|
)
|