-
Notifications
You must be signed in to change notification settings - Fork 5
/
DownloadTreasuryFiles.tsx
95 lines (87 loc) · 2.48 KB
/
DownloadTreasuryFiles.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
import Button from 'react-bootstrap/Button'
import { useAuth } from 'web/src/auth'
import { useMutation } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'
const DOWNLOAD_TREASURY_FILE = gql`
mutation DownloadTreasuryFile($input: DownloadTreasuryFileInput!) {
downloadTreasuryFile(input: $input) {
fileLink
}
}
`
const UPLOAD_SUBRECIPIENTS = gql`
mutation UploadSubrecipients($input: UploadSubrecipientsInput!) {
uploadSubrecipients(input: $input) {
success
message
countSubrecipients
}
}
`
const DownloadTreasuryFiles = () => {
const { currentUser } = useAuth()
const [downloadTreasuryFile] = useMutation(DOWNLOAD_TREASURY_FILE, {
onCompleted: ({ downloadTreasuryFile }) => {
const { fileLink } = downloadTreasuryFile
window.open(fileLink, '_blank').focus()
},
onError: (error) => {
toast.error(error.message)
},
})
const onClick = (event) => {
downloadTreasuryFile({
variables: { input: { fileType: event.target.getAttribute('name') } },
})
}
const [uploadSubrecipients] = useMutation(UPLOAD_SUBRECIPIENTS, {
onCompleted: ({ uploadSubrecipients }) => {
const { success, message, countSubrecipients } = uploadSubrecipients
if (success) {
toast.success(
`${message} - ${countSubrecipients} subrecipients uploaded`
)
} else {
toast.error(message)
}
},
onError: (error) => {
toast.error(error.message)
},
})
const onSubrecipientClick = () => {
uploadSubrecipients({
variables: {
input: {
organizationId: currentUser.agency.organizationId,
},
},
})
}
return (
<div className="rw-segment">
<div className="rw-button-group">
<Button name="1A" onClick={onClick} className="rw-button">
Download Project 1A file
</Button>
<Button name="1B" onClick={onClick} className="rw-button">
Download Project 1B file
</Button>
<Button name="1C" onClick={onClick} className="rw-button">
Download Project 1C file
</Button>
<Button name="Subrecipient" onClick={onClick} className="rw-button">
Download Subrecipient file
</Button>
<Button
name="Subrecipient"
onClick={onSubrecipientClick}
className="rw-button"
>
Upload new Subrecipient file
</Button>
</div>
</div>
)
}
export default DownloadTreasuryFiles