1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00

refactored code

This commit is contained in:
muerwre 2019-08-28 20:03:18 +07:00
parent 59993f5beb
commit eda46bdb9d
4 changed files with 75 additions and 48 deletions

View file

@ -5,17 +5,21 @@ import * as styles from './styles.scss';
import { Filler } from '~/components/containers/Filler';
import { Button } from '~/components/input/Button';
import assocPath from 'ramda/es/assocPath';
import { InputHandler, INode } from '~/redux/types';
import { InputHandler, IFileWithUUID } from '~/redux/types';
import { connect } from 'react-redux';
import * as NODE_ACTIONS from '~/redux/node/actions';
import { selectNode } from '~/redux/node/selectors';
import { LoaderCircle } from '~/components/input/LoaderCircle';
import { Group } from '~/components/containers/Group';
import { UPLOAD_SUBJECTS, UPLOAD_TARGETS, UPLOAD_TYPES } from '~/redux/uploads/constants';
import uuid from 'uuid4';
import * as UPLOAD_ACTIONS from '~/redux/uploads/actions';
const mapStateToProps = selectNode;
const mapDispatchToProps = {
nodePostComment: NODE_ACTIONS.nodePostComment,
nodeSetCommentData: NODE_ACTIONS.nodeSetCommentData,
uploadUploadFiles: UPLOAD_ACTIONS.uploadUploadFiles,
};
type IProps = ReturnType<typeof mapStateToProps> &
@ -24,13 +28,40 @@ type IProps = ReturnType<typeof mapStateToProps> &
};
const CommentFormUnconnected: FC<IProps> = ({
nodePostComment,
nodeSetCommentData,
comment_data,
is_sending_comment,
id,
nodePostComment,
nodeSetCommentData,
uploadUploadFiles,
}) => {
// const [data, setData] = useState<IComment>({ ...EMPTY_COMMENT });
const onInputChange = useCallback(
event => {
event.preventDefault();
if (!event.target.files || !event.target.files.length) return;
const items: IFileWithUUID[] = Array.from(event.target.files).map(
(file: File): IFileWithUUID => ({
file,
temp_id: uuid(),
subject: UPLOAD_SUBJECTS.COMMENT,
target: UPLOAD_TARGETS.COMMENTS,
type: UPLOAD_TYPES.IMAGE,
})
);
const temps = items.map(file => file.temp_id);
nodeSetCommentData(
id,
assocPath(['temp_ids'], [...comment_data[id].temp_ids, ...temps], comment_data[id])
);
uploadUploadFiles(items);
},
[uploadUploadFiles, comment_data, id, nodeSetCommentData]
);
const onInput = useCallback<InputHandler>(
text => {
@ -69,6 +100,8 @@ const CommentFormUnconnected: FC<IProps> = ({
</div>
<Group horizontal className={styles.buttons}>
<input type="file" onInput={onInputChange} />
<Filler />
{is_sending_comment && <LoaderCircle size={20} />}