-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNewOutputTemplate.tsx
138 lines (124 loc) · 3.66 KB
/
NewOutputTemplate.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 type {
CreateOutputTemplateMutation,
CreateOutputTemplateInput,
CreateOutputTemplateMutationVariables,
} from 'types/graphql'
import { navigate, routes } from '@redwoodjs/router'
import { useMutation } from '@redwoodjs/web'
import type { TypedDocumentNode } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'
import OutputTemplateForm from 'src/components/OutputTemplate/OutputTemplateForm'
const CREATE_OUTPUT_TEMPLATE_MUTATION: TypedDocumentNode<
CreateOutputTemplateMutation,
CreateOutputTemplateMutationVariables
> = gql`
mutation CreateOutputTemplateMutation($input: CreateOutputTemplateInput!) {
createOutputTemplate(input: $input) {
outputTemplate {
id
}
signedUrls {
CPF1A
CPF1B
CPF1C
CPFSubrecipient
}
}
}
`
type ExtraFileFields = {
fileCPF1A: File[]
fileCPF1B: File[]
fileCPF1C: File[]
fileCPFSubrecipient: File[]
}
type FileNames = {
filenames: {
CPF1A: string
CPF1B: string
CPF1C: string
CPFSubrecipient: string
}
}
const NewOutputTemplate = () => {
const [createOutputTemplate, { loading, error }] = useMutation(
CREATE_OUTPUT_TEMPLATE_MUTATION,
{
onCompleted: () => {
toast.success('OutputTemplate created')
navigate(routes.outputTemplates())
},
onError: (error) => {
toast.error(error.message)
},
}
)
const putObject = async (signedUrl: string, file: File) => {
fetch(signedUrl, {
method: 'PUT',
headers: {
'Content-Type': file.type,
'x-amz-server-side-encryption': 'AES256',
},
body: file,
})
.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 verifyFileNames = (
input: CreateOutputTemplateInput & ExtraFileFields & FileNames
) => {
const { filenames } = input
return (
filenames.CPF1A.includes('CPF1ABroadbandInfrastructureTemplate') &&
filenames.CPF1B.includes('CPF1BDigitalConnectivityTechTemplate') &&
filenames.CPF1C.includes('CPF1CMultiPurposeCommunityTemplate') &&
filenames.CPFSubrecipient.includes('CPFSubrecipientTemplate')
)
}
const onSave = async (
input: CreateOutputTemplateInput & ExtraFileFields & FileNames
) => {
if (!verifyFileNames(input)) {
toast.error(
'Invalid file names. Please ensure the files contain the correct template names.'
)
return
}
const fileCPF1A = input.fileCPF1A[0]
const fileCPF1B = input.fileCPF1B[0]
const fileCPF1C = input.fileCPF1C[0]
const fileCPFSubrecipient = input.fileCPFSubrecipient[0]
delete input.fileCPF1A
delete input.fileCPF1B
delete input.fileCPF1C
delete input.fileCPFSubrecipient
const response = await createOutputTemplate({ variables: { input } })
// Upload files to S3
const urls = response.data?.createOutputTemplate?.signedUrls
putObject(urls.CPF1A, fileCPF1A)
putObject(urls.CPF1B, fileCPF1B)
putObject(urls.CPF1C, fileCPF1C)
putObject(urls.CPFSubrecipient, fileCPFSubrecipient)
}
return (
<div className="rw-segment">
<header className="rw-segment-header">
<h2 className="rw-heading rw-heading-secondary">New OutputTemplate</h2>
</header>
<div className="rw-segment-main">
<OutputTemplateForm onSave={onSave} loading={loading} error={error} />
</div>
</div>
)
}
export default NewOutputTemplate