You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All you need to do is import and call the function `uploadFileToS3` with your file and it will chain the three requests that the frontend needs to make to upload a file from the browser to S3:
25
-
1. request upload from server
26
-
2. upload file to AWS S3
27
-
3. mark upload as complete on server
24
+
This library abstracts away the process of chaining the three requests that the frontend needs to make to upload a file from the browser to S3:
25
+
1. Request upload from server
26
+
2. Upload file to AWS S3
27
+
3. Mark upload as complete on server
28
+
29
+
The implementation is specific to the endpoints setup in this repo [github.com/dabapps/django-s3-file-upload-server](https://github.com/dabapps/django-s3-file-upload-server) so be sure to have the backend configured accordingly.
30
+
31
+
### Redux integration
32
+
33
+
This library provides 3 functions that can be combined to handle uploading multiple files, and storing the results, errors, and loading states in redux.
28
34
29
-
The function `uploadFileToS3` returns a `Promise` of type `Promise<UploadData>` with:
35
+
#### createActionSet
36
+
37
+
This function simply creates an object with some keys that will be used internally to allow the action and reducer to communicate.
This creates a [redux-thunk](https://github.com/reduxjs/redux-thunk) action that handles dispatching requests, and actions that track the loading states of the files, as well as the responses / errors.
`createFileUploadAction` takes an action set, and an optional options object, and returns a `Promise<UploadData>` (see [Types](#types)).
52
+
53
+
Actions created with this function will not throw errors by default. This means that calling `.catch` on the returned promise will not be effective unless you utilize the following option...
54
+
55
+
Currently the options object only exists to allow you to provide a `shouldRethrow` function, which is called with any errors, and will cause the action to rethrow any errors if `true` is returned from `shouldRethrow`.
56
+
57
+
Once you've created your action you can then dispatch this from your store, or connected component e.g.
The implementation is specific to the endpoints setup in this repo [github.com/dabapps/django-s3-file-upload-server](https://github.com/dabapps/django-s3-file-upload-server) so be sure to have the backend configured accordingly.
71
+
This takes an array of files. If you only have a single file to upload just provide an array containing that file.
46
72
47
-
## Examples
73
+
You should prevent the user from attempting to upload the same set of, or additional files while the requests are in progress (as this will cause issues with the loading states). You can check the current loading state from the [reducer](#createFileUploadReducer), and disable your submit button.
74
+
75
+
#### createFileUploadReducer
76
+
77
+
This function is used to create a reducer for a specific set of file uploads.
78
+
79
+
DO NOT use the same reducer for uploading multiple sets of files unless you really know what you are doing.
This can then be added to your store, and connected to React components to provide you with the `UploadState` (see [Types](#types)).
86
+
87
+
### Basic usage
88
+
89
+
The function `uploadFileToS3` is used internally by the other functions that integrate with redux. If you're not using redux, you can use this to upload individual files, and manually store the loading state, responses, and errors.
90
+
91
+
`uploadFileToS3` returns a `Promise` of type `Promise<UploadData>` (see [Types](#types)).
48
92
49
93
Let's say we have a form which contains a `file`, which we want to upload to S3 on submit, we can do the following:
fileCount:number; // Total number of files to be uploaded
159
+
inFlightCount:number; // Number of files being uploaded (but have not finished)
160
+
completeCount:number;
161
+
successCount:number;
162
+
failureCount:number;
163
+
data:undefined|ReadonlyArray<UploadData>;
164
+
error:undefined|ReadonlyArray<unknown>;
165
+
}
166
+
```
167
+
91
168
## Code of conduct
92
169
93
170
For guidelines regarding the code of conduct when contributing to this repository please review [https://www.dabapps.com/open-source/code-of-conduct/](https://www.dabapps.com/open-source/code-of-conduct/)
0 commit comments