-
Notifications
You must be signed in to change notification settings - Fork 5
/
UploadForm.tsx
138 lines (125 loc) · 3.58 KB
/
UploadForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { Button } from 'react-bootstrap'
import { useForm } from 'react-hook-form'
import type {
CreateUploadMutation,
CreateUploadMutationVariables,
EditUploadById,
} from 'types/graphql'
import {
Form,
FileField,
FormError,
FieldError,
Submit,
} from '@redwoodjs/forms'
import type { RWGqlError } from '@redwoodjs/forms'
import { navigate, routes } from '@redwoodjs/router'
import { useMutation } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'
import OrganizationPickListsCell from 'src/components/Organization/OrganizationPickListsCell'
const FILE_SIZE_THRESHOLD = 5 * 1048576 // 5 MB in bytes
const CREATE_UPLOAD = gql`
mutation CreateUploadMutation($input: CreateUploadInput!) {
createUpload(input: $input) {
id
signedUrl
validations {
id
passed
initiatedById
results
}
}
}
`
// type FormUpload = NonNullable<EditUploadById['upload']>
interface UploadFormProps {
upload?: EditUploadById['upload']
error: RWGqlError
loading: boolean
}
const UploadForm = ({ error, loading }: UploadFormProps) => {
const formMethods = useForm()
const [create] = useMutation<
CreateUploadMutation,
CreateUploadMutationVariables
>(CREATE_UPLOAD, {
onCompleted: ({ createUpload }) => {
toast.success('Upload created')
navigate(routes.upload({ id: createUpload?.id }))
},
onError: (error) => {
toast.error(error.message)
},
})
const onSubmit = async (data) => {
if (data.file[0].size > FILE_SIZE_THRESHOLD) {
toast.error('Upload should not be larger than 5MB')
return
}
data.filename = data.file[0].name
data.agencyId = parseInt(data.agencyId)
data.reportingPeriodId = parseInt(data.reportingPeriodId)
const uploadInput = {
agencyId: data.agencyId,
filename: data.file[0].name,
reportingPeriodId: data.reportingPeriodId,
}
const res = await create({ variables: { input: uploadInput } })
const formData = new FormData()
formData.append('file', data.file[0])
res.data?.createUpload?.signedUrl &&
fetch(res.data?.createUpload?.signedUrl, {
method: 'PUT',
headers: {
'Content-Type': data.file[0].type,
'x-amz-server-side-encryption': 'AES256',
},
body: data.file[0],
})
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`)
}
})
.then((responseData) => {
console.log('File upload successful. Response:', responseData)
})
.catch((error) => {
console.error('Error uploading file:', error)
})
}
const onReset = () => {
console.log('resetting form...')
formMethods.reset()
}
return (
<Form onSubmit={onSubmit}>
<FormError
error={error}
wrapperClassName="rw-form-error-wrapper"
titleClassName="rw-form-error-title"
listClassName="rw-form-error-list"
/>
<div className="col-md-8">
<OrganizationPickListsCell />
<FileField
name="file"
validation={{ required: true }}
accept=".xlsm"
className="form-control mt-3"
/>
<FieldError name="file" className="rw-field-error" />
</div>
<div className="rw-button-group pt-2">
<Submit disabled={loading} className="btn btn-primary me-2">
Submit
</Submit>
<Button className="btn btn-danger me-2" onClick={onReset}>
Reset
</Button>
</div>
</Form>
)
}
export default UploadForm