1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-26 05:16:41 +07:00

added callbacks to upload

This commit is contained in:
Fedor Katurov 2019-11-19 11:46:17 +07:00
parent 59252232e0
commit b6756bb3e5
5 changed files with 95 additions and 58 deletions

View file

@ -1,37 +1,49 @@
import { takeEvery, all, spawn, call, put, take, fork, race } from 'redux-saga/effects';
import { postUploadFile } from './api';
import { UPLOAD_ACTIONS, FILE_MIMES } from '~/redux/uploads/constants';
import {
takeEvery,
all,
spawn,
call,
put,
take,
fork,
race
} from "redux-saga/effects";
import { postUploadFile } from "./api";
import { UPLOAD_ACTIONS, FILE_MIMES } from "~/redux/uploads/constants";
import {
uploadUploadFiles,
uploadSetStatus,
uploadAddStatus,
uploadDropStatus,
uploadAddFile,
} from './actions';
import { reqWrapper } from '../auth/sagas';
import { createUploader, uploadGetThumb } from '~/utils/uploader';
import { HTTP_RESPONSES } from '~/utils/api';
import { IFileWithUUID, IFile, IUploadProgressHandler } from '../types';
uploadAddFile
} from "./actions";
import { reqWrapper } from "../auth/sagas";
import { createUploader, uploadGetThumb } from "~/utils/uploader";
import { HTTP_RESPONSES } from "~/utils/api";
import { IFileWithUUID, IFile, IUploadProgressHandler } from "../types";
function* uploadCall({
file,
temp_id,
target,
type,
onProgress,
onProgress
}: IFileWithUUID & { onProgress: IUploadProgressHandler }) {
return yield call(reqWrapper, postUploadFile, {
file,
temp_id,
type,
target,
onProgress,
onProgress
});
}
function* onUploadProgress(chan) {
while (true) {
const { progress, temp_id }: { progress: number; temp_id: string } = yield take(chan);
const {
progress,
temp_id
}: { progress: number; temp_id: string } = yield take(chan);
yield put(uploadSetStatus(temp_id, { progress }));
}
@ -47,10 +59,10 @@ function* uploadCancelWorker(id) {
}
function* uploadWorker({ file, temp_id, target, type }: IFileWithUUID) {
const [promise, chan] = createUploader<Partial<IFileWithUUID>, Partial<IFileWithUUID>>(
uploadCall,
{ temp_id, target, type }
);
const [promise, chan] = createUploader<
Partial<IFileWithUUID>,
Partial<IFileWithUUID>
>(uploadCall, { temp_id, target, type });
yield fork(onUploadProgress, chan);
@ -58,15 +70,26 @@ function* uploadWorker({ file, temp_id, target, type }: IFileWithUUID) {
temp_id,
file,
target,
type,
type
});
return result;
}
function* uploadFile({ file, temp_id, type, target }: IFileWithUUID) {
function* uploadFile({
file,
temp_id,
type,
target,
onSuccess,
onFail
}: IFileWithUUID) {
if (!file.type || !file.type || !FILE_MIMES[type].includes(file.type)) {
return { error: 'File_Not_Image', status: HTTP_RESPONSES.BAD_REQUEST, data: {} };
return {
error: "File_Not_Image",
status: HTTP_RESPONSES.BAD_REQUEST,
data: {}
};
}
const preview = yield call(uploadGetThumb, file);
@ -81,7 +104,7 @@ function* uploadFile({ file, temp_id, type, target }: IFileWithUUID) {
// type: file.type,
temp_id,
type,
name: file.name,
name: file.name
}
)
);
@ -91,9 +114,9 @@ function* uploadFile({ file, temp_id, type, target }: IFileWithUUID) {
file,
temp_id,
target,
type,
type
}),
cancel: call(uploadCancelWorker, temp_id),
cancel: call(uploadCancelWorker, temp_id)
// subject_cancel: call(uploadSubjectCancelWorker, subject)
// add here CANCEL_UPLOADS worker, that will watch for subject
// cancel_editing: take(UPLOAD_ACTIONS.CANCEL_EDITING),
@ -101,14 +124,24 @@ function* uploadFile({ file, temp_id, type, target }: IFileWithUUID) {
});
if (cancel || cancel_editing) {
if (onFail) onFail();
return yield put(uploadDropStatus(temp_id));
}
const { data, error }: { data: IFile & { detail: string }; error: string } = result;
const {
data,
error
}: { data: IFile & { detail: string }; error: string } = result;
if (error) {
if (onFail) onFail();
return yield put(
uploadSetStatus(temp_id, { is_uploading: false, error: data.detail || error, type })
uploadSetStatus(temp_id, {
is_uploading: false,
error: data.detail || error,
type
})
);
}
@ -121,12 +154,14 @@ function* uploadFile({ file, temp_id, type, target }: IFileWithUUID) {
type,
thumbnail_url: data.full_path,
progress: 1,
name: file.name,
name: file.name
})
);
yield put(uploadAddFile(data));
if (onSuccess) onSuccess(data);
return { error: null, status: HTTP_RESPONSES.CREATED, data: {} }; // add file here as data
}