mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
working upload saga?
This commit is contained in:
parent
3872ff5903
commit
d73437d8c6
8 changed files with 153 additions and 42 deletions
|
@ -1,11 +1,18 @@
|
|||
import { eventChannel, END } from 'redux-saga';
|
||||
import uuid from 'uuid4';
|
||||
import { eventChannel, END, EventChannel } from 'redux-saga';
|
||||
import { VALIDATORS } from '~/utils/validators';
|
||||
import { IResultWithStatus, IFile } from '~/redux/types';
|
||||
import { HTTP_RESPONSES } from './api';
|
||||
|
||||
export const IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg'];
|
||||
|
||||
export function createUploader<T extends {}, R extends {}>
|
||||
(callback: (args: any) => any, payload: R):
|
||||
[(args: T) => (args: T & { onProgress: (current: number, total: number) => void }) => any, EventChannel<any>] {
|
||||
export function createUploader<T extends {}, R extends {}>(
|
||||
callback: (args: any) => any,
|
||||
payload: R
|
||||
): [
|
||||
(args: T) => (args: T & { onProgress: (current: number, total: number) => void }) => any,
|
||||
EventChannel<any>
|
||||
] {
|
||||
let emit;
|
||||
|
||||
const chan = eventChannel(emitter => {
|
||||
|
@ -14,7 +21,9 @@ export function createUploader<T extends {}, R extends {}>
|
|||
});
|
||||
|
||||
const onProgress = (current: number, total: number): void => {
|
||||
emit(current >= total ? END : { ...payload, progress: parseFloat((current / total).toFixed(1)) });
|
||||
emit(
|
||||
current >= total ? END : { ...payload, progress: parseFloat((current / total).toFixed(1)) }
|
||||
);
|
||||
};
|
||||
|
||||
const wrappedCallback = args => callback({ ...args, onProgress });
|
||||
|
@ -31,3 +40,30 @@ export const uploadGetThumb = async file => {
|
|||
reader.readAsDataURL(file);
|
||||
});
|
||||
};
|
||||
|
||||
export const fakeUploader = ({
|
||||
file,
|
||||
onProgress,
|
||||
mustSucceed,
|
||||
}: {
|
||||
file: { url?: string; error?: string };
|
||||
onProgress: (current: number, total: number) => void;
|
||||
mustSucceed: boolean;
|
||||
}): Promise<IResultWithStatus<IFile>> => {
|
||||
const { url, error } = file;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
onProgress(1, 2);
|
||||
}, 100);
|
||||
|
||||
setTimeout(() => {
|
||||
onProgress(2, 2);
|
||||
if (mustSucceed) {
|
||||
resolve({ status: HTTP_RESPONSES.CREATED, data: { id: uuid() } });
|
||||
} else {
|
||||
reject({ response: { statusText: error } });
|
||||
}
|
||||
}, 500);
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue