This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Upload.tsx
161 lines (147 loc) · 4.26 KB
/
Upload.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import * as React from "react";
import AWS, { Credentials } from "aws-sdk";
import { getCurrentUser } from "../../utils";
import { FileUploadArea } from "../elements/FileUploadArea";
import { FileInfoTable } from "../elements/FileInfoTable";
import { RouteComponentProps } from "react-router";
import { Mutation, MutationFn } from "react-apollo";
import gql from "graphql-tag";
import { CognitoUser } from "amazon-cognito-identity-js";
import { Page } from "./Page";
import { Button } from "../elements/Button";
import { getCognitoLoginId, getIdentityPoolId } from "../../utils/user";
interface IUploadProps {
id?: string;
}
interface IUpdateFileInfo {
id: string;
checksum: string;
size: string;
path: string;
lastModifiedDate: string;
fileName: string;
}
export interface IUpdateFile extends IUpdateFileInfo {
file: File;
}
interface IUpdateFileInput {
collectionId: string;
files: IUpdateFileInfo[];
}
const Upload: React.FunctionComponent<
RouteComponentProps<IUploadProps>
> = props => {
const [isLoading, setIsLoading]: [
boolean,
React.Dispatch<boolean>
] = React.useState<boolean>(false);
const [fileUpdate, setFileUpdate]: [
IUpdateFile[],
React.Dispatch<IUpdateFile[]>
] = React.useState<IUpdateFile[]>([]);
const onFilesProcessed: (
updateFiles: IUpdateFile[]
) => void = updateFiles => {
if (JSON.stringify(updateFiles) !== JSON.stringify(fileUpdate)) {
setFileUpdate(updateFiles);
setIsLoading(false);
}
};
const getCredentials: () => Promise<void> = async () => {
return new Promise<void>((resolve, reject) => {
const cognitoUser: CognitoUser | null = getCurrentUser();
if (cognitoUser !== null) {
cognitoUser.getSession(function(err: any, result: any) {
if (result) {
const IdentityPoolId = getIdentityPoolId();
const cognitoLoginId = getCognitoLoginId();
AWS.config.region = "eu-west-2";
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: IdentityPoolId,
Logins: {
[cognitoLoginId]: result.getIdToken().getJwtToken()
}
});
AWS.config.getCredentials(function(err: any) {
if (err === undefined || err === null) {
resolve();
} else {
reject(err.code);
}
});
}
});
}
});
return new Promise<void>((resolve, reject) => {});
};
const upload: () => void = async () => {
var Bucket = "tdr-files";
await getCredentials();
var s3: AWS.S3 = new AWS.S3({
params: {
Bucket
}
});
fileUpdate.forEach(update => {
s3.upload(
{
Key: `${props.match.params.id}/${update.id}`,
Body: update.file,
Bucket
},
{},
function(err: any) {
console.log(err);
}
);
});
};
const UPDATE_FILES_ON_COLLECTION = gql`
mutation UpdateFilesOnCollection(
$collectionId: ID
$files: [TdrCollectionFilesInput]
) {
updateFilesOnCollection(collectionId: $collectionId, files: $files) {
files {
id
}
}
}
`;
const onUpload: (
updateFunction: MutationFn<{}, IUpdateFileInput>
) => void = updateFunction => {
updateFunction({
variables: {
collectionId: props.match.params.id ? props.match.params.id : "",
files: fileUpdate.map(({ file, ...rest }) => rest)
}
});
upload();
};
return (
<Page title="Upload file" path={props.location.pathname}>
<Mutation<{}, IUpdateFileInput> mutation={UPDATE_FILES_ON_COLLECTION}>
{updateFilesOnCollection => (
<>
<div className="govuk-grid-row">
<FileInfoTable fileUpdate={fileUpdate} isLoading={isLoading} />
<FileUploadArea
onFilesProcessed={onFilesProcessed}
setIsLoading={setIsLoading}
/>
</div>
<div className="govuk-grid-row">
<Button
text="Upload"
onClick={() => onUpload(updateFilesOnCollection)}
/>
</div>
</>
)}
</Mutation>
</Page>
);
};
export { Upload };