mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
fixed uploads
This commit is contained in:
parent
cad4e683bc
commit
32b9a0dbbb
9 changed files with 52 additions and 37 deletions
|
@ -6,6 +6,7 @@ import * as styles from './styles.scss';
|
||||||
import { ImageUpload } from '~/components/upload/ImageUpload';
|
import { ImageUpload } from '~/components/upload/ImageUpload';
|
||||||
import { IFile } from '~/redux/types';
|
import { IFile } from '~/redux/types';
|
||||||
import { IUploadStatus } from '~/redux/uploads/reducer';
|
import { IUploadStatus } from '~/redux/uploads/reducer';
|
||||||
|
import { getURL } from '~/utils/dom';
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
items: IFile[];
|
items: IFile[];
|
||||||
|
@ -30,9 +31,10 @@ const SortableList = SortableContainer(
|
||||||
<div className={styles.grid}>
|
<div className={styles.grid}>
|
||||||
{items.map((file, index) => (
|
{items.map((file, index) => (
|
||||||
<SortableItem key={file.id} index={index} collection={0}>
|
<SortableItem key={file.id} index={index} collection={0}>
|
||||||
<ImageUpload id={file.id} thumb={file.url} />
|
<ImageUpload id={file.id} thumb={getURL(file.url)} />
|
||||||
</SortableItem>
|
</SortableItem>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
{locked.map((item, index) => (
|
{locked.map((item, index) => (
|
||||||
<SortableItem key={item.temp_id} index={index} collection={1} disabled>
|
<SortableItem key={item.temp_id} index={index} collection={1} disabled>
|
||||||
<ImageUpload thumb={item.preview} progress={item.progress} is_uploading />
|
<ImageUpload thumb={item.preview} progress={item.progress} is_uploading />
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
import React, { FC } from 'react';
|
import React, { FC } from 'react';
|
||||||
import * as styles from './styles.scss';
|
|
||||||
import range from 'ramda/es/range';
|
import range from 'ramda/es/range';
|
||||||
import classNames = require("classnames");
|
import * as styles from './styles.scss';
|
||||||
|
|
||||||
|
import classNames = require('classnames');
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
total: number;
|
total: number;
|
||||||
|
@ -11,11 +12,9 @@ interface IProps {
|
||||||
const ImageSwitcher: FC<IProps> = ({ total, current }) => (
|
const ImageSwitcher: FC<IProps> = ({ total, current }) => (
|
||||||
<div className={styles.wrap}>
|
<div className={styles.wrap}>
|
||||||
<div className={styles.switcher}>
|
<div className={styles.switcher}>
|
||||||
{
|
{range(0, total).map(item => (
|
||||||
range(0, total).map((item) => (
|
<div className={classNames({ is_active: item === current })} key={item} />
|
||||||
<div className={classNames({ is_active: item === current })} />
|
))}
|
||||||
))
|
|
||||||
}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
@ -16,12 +16,7 @@ const ImageUpload: FC<IProps> = ({
|
||||||
}) => (
|
}) => (
|
||||||
<div className={styles.wrap}>
|
<div className={styles.wrap}>
|
||||||
<div className={classNames(styles.thumb_wrap, { is_uploading })}>
|
<div className={classNames(styles.thumb_wrap, { is_uploading })}>
|
||||||
{thumb && (
|
{thumb && <div className={styles.thumb} style={{ backgroundImage: `url("${thumb}")` }} />}
|
||||||
<div
|
|
||||||
className={styles.thumb}
|
|
||||||
style={{ backgroundImage: `url("${process.env.API_HOST}${thumb}")` }}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{is_uploading && (
|
{is_uploading && (
|
||||||
<div className={styles.progress}>
|
<div className={styles.progress}>
|
||||||
<ArcProgress size={72} progress={progress} />
|
<ArcProgress size={72} progress={progress} />
|
||||||
|
|
|
@ -17,12 +17,8 @@ import { IUser } from './types';
|
||||||
export function* reqWrapper(requestAction, props = {}): ReturnType<typeof requestAction> {
|
export function* reqWrapper(requestAction, props = {}): ReturnType<typeof requestAction> {
|
||||||
const access = yield select(selectToken);
|
const access = yield select(selectToken);
|
||||||
|
|
||||||
console.log('firing reqWrapper');
|
|
||||||
|
|
||||||
const result = yield call(requestAction, { access, ...props });
|
const result = yield call(requestAction, { access, ...props });
|
||||||
|
|
||||||
console.log('at reqWrapper', { result });
|
|
||||||
|
|
||||||
if (result && result.status === 401) {
|
if (result && result.status === 401) {
|
||||||
yield put(push(URLS.BASE));
|
yield put(push(URLS.BASE));
|
||||||
yield put(modalShowDialog(DIALOGS.LOGIN));
|
yield put(modalShowDialog(DIALOGS.LOGIN));
|
||||||
|
@ -30,8 +26,6 @@ export function* reqWrapper(requestAction, props = {}): ReturnType<typeof reques
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('reqWrapper will return');
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -112,4 +112,4 @@ export interface INode {
|
||||||
updatedAt?: string;
|
updatedAt?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type IUploadProgressHandler = (current: number, total: number) => void;
|
export type IUploadProgressHandler = (progressEvent: any) => void;
|
||||||
|
|
|
@ -9,6 +9,7 @@ export const postUploadFile = ({
|
||||||
file,
|
file,
|
||||||
target = 'others',
|
target = 'others',
|
||||||
type = 'image',
|
type = 'image',
|
||||||
|
onProgress,
|
||||||
}: IFileWithUUID & {
|
}: IFileWithUUID & {
|
||||||
access: string;
|
access: string;
|
||||||
onProgress: IUploadProgressHandler;
|
onProgress: IUploadProgressHandler;
|
||||||
|
@ -16,5 +17,9 @@ export const postUploadFile = ({
|
||||||
const data = new FormData();
|
const data = new FormData();
|
||||||
data.append('file', file);
|
data.append('file', file);
|
||||||
|
|
||||||
return api.post(API.USER.UPLOAD(target, type), data, configWithToken(access));
|
return api.post(
|
||||||
|
API.USER.UPLOAD(target, type),
|
||||||
|
data,
|
||||||
|
configWithToken(access, { onUploadProgress: onProgress })
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -57,18 +57,20 @@ function* uploadWorker({
|
||||||
{ temp_id, target, type }
|
{ temp_id, target, type }
|
||||||
);
|
);
|
||||||
|
|
||||||
fork(onUploadProgress, chan);
|
yield fork(onUploadProgress, chan);
|
||||||
|
|
||||||
return yield call(promise, {
|
const result = yield call(promise, {
|
||||||
temp_id,
|
temp_id,
|
||||||
file,
|
file,
|
||||||
target,
|
target,
|
||||||
type,
|
type,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function* uploadFile({
|
function* uploadFile({
|
||||||
file, temp_id, type, target, subject,
|
file, temp_id, type, target,
|
||||||
}: IFileWithUUID) {
|
}: IFileWithUUID) {
|
||||||
if (!file.type || !VALIDATORS.IS_IMAGE_MIME(file.type)) {
|
if (!file.type || !VALIDATORS.IS_IMAGE_MIME(file.type)) {
|
||||||
return { error: 'File_Not_Image', status: HTTP_RESPONSES.BAD_REQUEST, data: {} };
|
return { error: 'File_Not_Image', status: HTTP_RESPONSES.BAD_REQUEST, data: {} };
|
||||||
|
|
|
@ -11,11 +11,11 @@ export const getStyle = (oElm: any, strCssRule: string) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
|
function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
|
||||||
const angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;
|
const angleInRadians = ((angleInDegrees - 90) * Math.PI) / 180.0;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
x: centerX + (radius * Math.cos(angleInRadians)),
|
x: centerX + radius * Math.cos(angleInRadians),
|
||||||
y: centerY + (radius * Math.sin(angleInRadians)),
|
y: centerY + radius * Math.sin(angleInRadians),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ export const describeArc = (
|
||||||
y: number,
|
y: number,
|
||||||
radius: number,
|
radius: number,
|
||||||
startAngle: number = 0,
|
startAngle: number = 0,
|
||||||
endAngle: number = 360,
|
endAngle: number = 360
|
||||||
): string => {
|
): string => {
|
||||||
const start = polarToCartesian(x, y, radius, endAngle);
|
const start = polarToCartesian(x, y, radius, endAngle);
|
||||||
const end = polarToCartesian(x, y, radius, startAngle);
|
const end = polarToCartesian(x, y, radius, startAngle);
|
||||||
|
@ -32,9 +32,24 @@ export const describeArc = (
|
||||||
const largeArcFlag = endAngle - startAngle <= 180 ? 0 : 1;
|
const largeArcFlag = endAngle - startAngle <= 180 ? 0 : 1;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'M', start.x, start.y,
|
'M',
|
||||||
'A', radius, radius, 0, largeArcFlag, 0, end.x, end.y,
|
start.x,
|
||||||
'L', x, y,
|
start.y,
|
||||||
'L', start.x, start.y,
|
'A',
|
||||||
|
radius,
|
||||||
|
radius,
|
||||||
|
0,
|
||||||
|
largeArcFlag,
|
||||||
|
0,
|
||||||
|
end.x,
|
||||||
|
end.y,
|
||||||
|
'L',
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
'L',
|
||||||
|
start.x,
|
||||||
|
start.y,
|
||||||
].join(' ');
|
].join(' ');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getURL = url => `${process.env.API_HOST}${url}`;
|
||||||
|
|
|
@ -10,7 +10,10 @@ export const IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/
|
||||||
export function createUploader<T extends {}, R extends {}>(
|
export function createUploader<T extends {}, R extends {}>(
|
||||||
callback: (args: any) => any,
|
callback: (args: any) => any,
|
||||||
payload: R
|
payload: R
|
||||||
): [(args: T) => (args: T & { onProgress: (current: number, total: number) => void }) => any, EventChannel<any>] {
|
): [
|
||||||
|
(args: T) => (args: T & { onProgress: (current: number, total: number) => void }) => any,
|
||||||
|
EventChannel<any>
|
||||||
|
] {
|
||||||
let emit;
|
let emit;
|
||||||
|
|
||||||
const chan = eventChannel((emitter) => {
|
const chan = eventChannel((emitter) => {
|
||||||
|
@ -18,8 +21,8 @@ export function createUploader<T extends {}, R extends {}>(
|
||||||
return () => null;
|
return () => null;
|
||||||
});
|
});
|
||||||
|
|
||||||
const onProgress = (current: number, total: number): void => {
|
const onProgress = ({ loaded, total }: { loaded: number; total: number }): void => {
|
||||||
emit(current >= total ? END : { ...payload, progress: parseFloat((current / total).toFixed(1)) });
|
emit(loaded >= total ? END : { ...payload, progress: parseFloat((loaded / total).toFixed(1)) });
|
||||||
};
|
};
|
||||||
|
|
||||||
const wrappedCallback = args => callback({ ...args, onProgress });
|
const wrappedCallback = args => callback({ ...args, onProgress });
|
||||||
|
@ -40,7 +43,7 @@ export const uploadGetThumb = async (file) => {
|
||||||
export const fakeUploader = ({
|
export const fakeUploader = ({
|
||||||
file,
|
file,
|
||||||
onProgress,
|
onProgress,
|
||||||
mustSucceed
|
mustSucceed,
|
||||||
}: {
|
}: {
|
||||||
file: { url?: string; error?: string };
|
file: { url?: string; error?: string };
|
||||||
onProgress: (current: number, total: number) => void;
|
onProgress: (current: number, total: number) => void;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue