mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
removed upload redux store
This commit is contained in:
parent
140e36b6b7
commit
95b92b643f
38 changed files with 398 additions and 691 deletions
28
src/utils/context/UploaderContextProvider.tsx
Normal file
28
src/utils/context/UploaderContextProvider.tsx
Normal file
|
@ -0,0 +1,28 @@
|
|||
import React, { createContext, FC, useContext } from 'react';
|
||||
import { useUploader } from '~/hooks/data/useUploader';
|
||||
import { IFile } from '~/redux/types';
|
||||
import { EMPTY_FILE } from '~/constants/uploads';
|
||||
|
||||
export type Uploader = ReturnType<typeof useUploader>;
|
||||
|
||||
const UploaderContext = createContext<Uploader>({
|
||||
files: [],
|
||||
filesAudios: [],
|
||||
filesImages: [],
|
||||
uploadFile: async () => EMPTY_FILE,
|
||||
uploadFiles: async () => {},
|
||||
pending: {},
|
||||
pendingAudios: [],
|
||||
pendingImages: [],
|
||||
isUploading: false,
|
||||
setFiles: (files: IFile[]) => files,
|
||||
});
|
||||
|
||||
export const UploaderContextProvider: FC<{
|
||||
value: Uploader;
|
||||
children;
|
||||
}> = ({ value, children }) => (
|
||||
<UploaderContext.Provider value={value}>{children}</UploaderContext.Provider>
|
||||
);
|
||||
|
||||
export const useUploaderContext = () => useContext(UploaderContext);
|
|
@ -1,81 +1,24 @@
|
|||
import uuid from 'uuid4';
|
||||
import { END, eventChannel, EventChannel } from 'redux-saga';
|
||||
import { VALIDATORS } from '~/utils/validators';
|
||||
import { IFile, IResultWithStatus } from '~/redux/types';
|
||||
import { HTTP_RESPONSES } from './api';
|
||||
import { EMPTY_FILE, FILE_MIMES, UPLOAD_TYPES } from '~/redux/uploads/constants';
|
||||
|
||||
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>
|
||||
] {
|
||||
let emit;
|
||||
|
||||
const chan = eventChannel(emitter => {
|
||||
emit = emitter;
|
||||
return () => null;
|
||||
});
|
||||
|
||||
const onProgress = ({ loaded, total }: { loaded: number; total: number }): void => {
|
||||
emit(loaded >= total ? END : { ...payload, progress: parseFloat((loaded / total).toFixed(1)) });
|
||||
};
|
||||
|
||||
const wrappedCallback = args => callback({ ...args, onProgress });
|
||||
|
||||
return [wrappedCallback, chan];
|
||||
}
|
||||
import { FILE_MIMES, UploadType } from '~/constants/uploads';
|
||||
|
||||
/** if file is image, returns data-uri of thumbnail */
|
||||
export const uploadGetThumb = async file => {
|
||||
if (!file.type || !VALIDATORS.IS_IMAGE_MIME(file.type)) return '';
|
||||
|
||||
return new Promise<string | ArrayBuffer>(resolve => {
|
||||
return new Promise<string>(resolve => {
|
||||
const reader = new FileReader();
|
||||
reader.onloadend = () => resolve(reader.result || '');
|
||||
reader.onloadend = () => resolve(reader.result?.toString() || '');
|
||||
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 { error } = file;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
onProgress(1, 3);
|
||||
}, 1000);
|
||||
|
||||
setTimeout(() => {
|
||||
onProgress(2, 3);
|
||||
}, 2000);
|
||||
|
||||
setTimeout(() => {
|
||||
onProgress(3, 3);
|
||||
if (mustSucceed) {
|
||||
resolve({ status: HTTP_RESPONSES.CREATED, data: { ...EMPTY_FILE, id: uuid() } });
|
||||
} else {
|
||||
reject({ response: { statusText: error } });
|
||||
}
|
||||
}, 3000);
|
||||
});
|
||||
};
|
||||
|
||||
export const getFileType = (file: File): keyof typeof UPLOAD_TYPES | undefined =>
|
||||
(file.type && Object.keys(FILE_MIMES).find(mime => FILE_MIMES[mime].includes(file.type))) ||
|
||||
/** returns UploadType by file */
|
||||
export const getFileType = (file: File): UploadType | undefined =>
|
||||
((file.type &&
|
||||
Object.keys(FILE_MIMES).find(mime => FILE_MIMES[mime].includes(file.type))) as UploadType) ||
|
||||
undefined;
|
||||
|
||||
// getImageFromPaste returns any images from paste event
|
||||
/** getImageFromPaste returns any images from paste event */
|
||||
export const getImageFromPaste = (event: ClipboardEvent): Promise<File | undefined> => {
|
||||
const items = event.clipboardData?.items;
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import isValid from 'date-fns/isValid';
|
||||
import { IMAGE_MIME_TYPES } from '~/utils/uploader';
|
||||
import { IMAGE_MIME_TYPES } from '~/constants/uploads';
|
||||
|
||||
const isValidEmail = (email: string): boolean =>
|
||||
!!email &&
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue