mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
file uploading template
This commit is contained in:
parent
cfe4731ded
commit
8dc43c77b7
7 changed files with 57 additions and 10 deletions
|
@ -5,11 +5,12 @@ import { INode } from '~/redux/types';
|
||||||
interface IProps {
|
interface IProps {
|
||||||
data: INode;
|
data: INode;
|
||||||
setData: (val: INode) => void;
|
setData: (val: INode) => void;
|
||||||
|
onUpload: (val: File[]) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
const EditorPanel: FC<IProps> = ({
|
const EditorPanel: FC<IProps> = ({
|
||||||
}) => (
|
}) => (
|
||||||
<div className={styles.panel} />
|
<div className={styles.panel} />
|
||||||
)
|
);
|
||||||
|
|
||||||
export { EditorPanel };
|
export { EditorPanel };
|
|
@ -5,14 +5,32 @@ import * as styles from './styles.scss';
|
||||||
interface IProps {
|
interface IProps {
|
||||||
data: INode,
|
data: INode,
|
||||||
setData: (val: INode) => void;
|
setData: (val: INode) => void;
|
||||||
};
|
onUpload: (val: File[]) => void;
|
||||||
|
}
|
||||||
|
|
||||||
const ImageEditor: FC<IProps> = ({
|
const ImageEditor: FC<IProps> = ({
|
||||||
data,
|
data,
|
||||||
setData,
|
setData,
|
||||||
|
onUpload,
|
||||||
}) => {
|
}) => {
|
||||||
const eventPreventer = useCallback(event => event.preventDefault(), []);
|
const eventPreventer = useCallback(event => event.preventDefault(), []);
|
||||||
|
|
||||||
|
const onDrop = useCallback(event => {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
if (!event.dataTransfer || !event.dataTransfer.files || !event.dataTransfer.files.length) return;
|
||||||
|
|
||||||
|
onUpload(event.dataTransfer.files);
|
||||||
|
}, [onUpload]);
|
||||||
|
|
||||||
|
const onInputChange = useCallback(event => {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
if (!event.target.files || !event.target.files.length) return;
|
||||||
|
|
||||||
|
onUpload(event.target.files);
|
||||||
|
}, [onUpload]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
window.addEventListener("dragover", eventPreventer,false);
|
window.addEventListener("dragover", eventPreventer,false);
|
||||||
window.addEventListener("drop",eventPreventer,false);
|
window.addEventListener("drop",eventPreventer,false);
|
||||||
|
@ -26,9 +44,10 @@ const ImageEditor: FC<IProps> = ({
|
||||||
return (
|
return (
|
||||||
<form
|
<form
|
||||||
className={styles.uploads}
|
className={styles.uploads}
|
||||||
onDrop={e => { console.log(e.dataTransfer.files);}}
|
onDrop={onDrop}
|
||||||
>
|
>
|
||||||
<div>{data.type}</div>
|
<div>{data.type}</div>
|
||||||
|
<input type="file" onChange={onInputChange} />
|
||||||
</form>
|
</form>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -11,23 +11,33 @@ import { connect } from 'react-redux';
|
||||||
import { selectNode } from '~/redux/node/selectors';
|
import { selectNode } from '~/redux/node/selectors';
|
||||||
import { ImageEditor } from '~/components/editors/ImageEditor';
|
import { ImageEditor } from '~/components/editors/ImageEditor';
|
||||||
import { EditorPanel } from '~/components/editors/EditorPanel';
|
import { EditorPanel } from '~/components/editors/EditorPanel';
|
||||||
|
import * as UPLOAD_ACTIONS from '~/redux/uploads/actions';
|
||||||
|
import {uploadUploadFiles} from "~/redux/uploads/actions";
|
||||||
|
|
||||||
const mapStateToProps = selectNode;
|
const mapStateToProps = selectNode;
|
||||||
const mapDispatchToProps = {};
|
const mapDispatchToProps = {
|
||||||
|
uploadUploadFiles: UPLOAD_ACTIONS.uploadUploadFiles,
|
||||||
|
};
|
||||||
|
|
||||||
type IProps = IDialogProps & ReturnType<typeof mapStateToProps> & typeof mapDispatchToProps & {};
|
type IProps = IDialogProps & ReturnType<typeof mapStateToProps> & typeof mapDispatchToProps & {};
|
||||||
|
|
||||||
const EditorDialogUnconnected: FC<IProps> = ({ onRequestClose, editor }) => {
|
const EditorDialogUnconnected: FC<IProps> = ({ onRequestClose, editor, uploadUploadFiles }) => {
|
||||||
const [data, setData] = useState(editor);
|
const [data, setData] = useState(editor);
|
||||||
|
|
||||||
const setTitle = useCallback(title => {
|
const setTitle = useCallback(title => {
|
||||||
setData({ ...data, title });
|
setData({ ...data, title });
|
||||||
}, [setData, data]);
|
}, [setData, data]);
|
||||||
|
|
||||||
|
const onUpload = useCallback((files: File[]) => {
|
||||||
|
uploadUploadFiles(files, 'editor');
|
||||||
|
}, [uploadUploadFiles]);
|
||||||
|
|
||||||
const buttons = (
|
const buttons = (
|
||||||
<Padder style={{ position: 'relative' }}>
|
<Padder style={{ position: 'relative' }}>
|
||||||
<EditorPanel
|
<EditorPanel
|
||||||
data={data}
|
data={data}
|
||||||
setData={setData}
|
setData={setData}
|
||||||
|
onUpload={onUpload}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Group horizontal>
|
<Group horizontal>
|
||||||
|
@ -46,6 +56,7 @@ const EditorDialogUnconnected: FC<IProps> = ({ onRequestClose, editor }) => {
|
||||||
<ImageEditor
|
<ImageEditor
|
||||||
data={data}
|
data={data}
|
||||||
setData={setData}
|
setData={setData}
|
||||||
|
onUpload={onUpload}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</ScrollDialog>
|
</ScrollDialog>
|
||||||
|
|
|
@ -15,7 +15,7 @@ import nodeReducer, { INodeState } from "~/redux/node/reducer";
|
||||||
import nodeSaga from "~/redux/node/sagas";
|
import nodeSaga from "~/redux/node/sagas";
|
||||||
|
|
||||||
import uploadReducer, { IUploadState } from "~/redux/uploads/reducer";
|
import uploadReducer, { IUploadState } from "~/redux/uploads/reducer";
|
||||||
import uploadSaga from "~/redux/node/sagas";
|
import uploadSaga from "~/redux/uploads/sagas";
|
||||||
|
|
||||||
import { IAuthState } from "~/redux/auth/types";
|
import { IAuthState } from "~/redux/auth/types";
|
||||||
|
|
||||||
|
|
7
src/redux/uploads/actions.ts
Normal file
7
src/redux/uploads/actions.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import {UPLOAD_ACTIONS} from "~/redux/uploads/constants";
|
||||||
|
|
||||||
|
export const uploadUploadFiles = (files: File[], subject: string) => ({
|
||||||
|
files,
|
||||||
|
subject,
|
||||||
|
type: UPLOAD_ACTIONS.UPLOAD_FILES,
|
||||||
|
});
|
|
@ -1,5 +1,11 @@
|
||||||
import { IFile } from "~/redux/types";
|
import { IFile } from "~/redux/types";
|
||||||
|
|
||||||
|
const prefix = 'UPLOAD.';
|
||||||
|
|
||||||
|
export const UPLOAD_ACTIONS = {
|
||||||
|
UPLOAD_FILES: `${prefix}UPLOAD_FILES`,
|
||||||
|
};
|
||||||
|
|
||||||
export const EMPTY_FILE: IFile = {
|
export const EMPTY_FILE: IFile = {
|
||||||
id: null,
|
id: null,
|
||||||
user_id: null,
|
user_id: null,
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
export default function* () {
|
import {takeEvery} from "redux-saga/effects";
|
||||||
|
import {UPLOAD_ACTIONS} from "~/redux/uploads/constants";
|
||||||
|
|
||||||
|
export default function* () {
|
||||||
|
yield takeEvery(UPLOAD_ACTIONS.UPLOAD_FILES, console.log);
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue