@campxdev/shared 1.6.12 → 1.7.1
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 +41 -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,16 +62,12 @@ 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
|
|
94
69
|
cols={cols}
|
|
70
|
+
gap={gap}
|
|
95
71
|
fields={fields}
|
|
96
72
|
control={control}
|
|
97
73
|
dropdowns={dropdowns}
|
|
@@ -102,22 +78,30 @@ export default function Form({
|
|
|
102
78
|
sx={{
|
|
103
79
|
display: 'flex',
|
|
104
80
|
gap: '20px',
|
|
105
|
-
marginTop:
|
|
81
|
+
marginTop: `20px`,
|
|
82
|
+
justifyContent: 'center',
|
|
106
83
|
}}
|
|
107
84
|
>
|
|
108
|
-
<
|
|
109
|
-
{
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
fullWidth
|
|
113
|
-
variant="outlined"
|
|
114
|
-
onClick={() => {
|
|
115
|
-
onCancel ? onCancel() : null
|
|
116
|
-
}}
|
|
85
|
+
<ActionButton
|
|
86
|
+
loading={posting}
|
|
87
|
+
disabled={posting}
|
|
88
|
+
type="submit"
|
|
117
89
|
{...buttonProps}
|
|
118
90
|
>
|
|
119
|
-
{
|
|
120
|
-
</
|
|
91
|
+
{submitBtn?.label}
|
|
92
|
+
</ActionButton>
|
|
93
|
+
{!cancelBtn?.hide && (
|
|
94
|
+
<ActionButton
|
|
95
|
+
fullWidth
|
|
96
|
+
variant="outlined"
|
|
97
|
+
onClick={() => {
|
|
98
|
+
onCancel ? onCancel() : null
|
|
99
|
+
}}
|
|
100
|
+
{...buttonProps}
|
|
101
|
+
>
|
|
102
|
+
{cancelBtn?.label}
|
|
103
|
+
</ActionButton>
|
|
104
|
+
)}
|
|
121
105
|
</Box>
|
|
122
106
|
</form>
|
|
123
107
|
)
|