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

comment rendering

This commit is contained in:
muerwre 2019-08-27 13:37:53 +07:00
parent 398f44e232
commit 9531edcd19
10 changed files with 83 additions and 37 deletions

View file

@ -1,18 +1,20 @@
import React, { FC, useCallback, useState } from 'react'; import React, { FC, useCallback, useEffect } from 'react';
import { Textarea } from '~/components/input/Textarea'; import { Textarea } from '~/components/input/Textarea';
import { CommentWrapper } from '~/components/containers/CommentWrapper'; import { CommentWrapper } from '~/components/containers/CommentWrapper';
import * as styles from './styles.scss'; import * as styles from './styles.scss';
import { Filler } from '~/components/containers/Filler'; import { Filler } from '~/components/containers/Filler';
import { Button } from '~/components/input/Button'; import { Button } from '~/components/input/Button';
import assocPath from 'ramda/es/assocPath'; import assocPath from 'ramda/es/assocPath';
import { InputHandler, INode, IComment } from '~/redux/types'; import { InputHandler, INode } from '~/redux/types';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import * as NODE_ACTIONS from '~/redux/node/actions'; import * as NODE_ACTIONS from '~/redux/node/actions';
import { EMPTY_COMMENT } from '~/redux/node/constants'; import { store } from '~/redux/store';
import { selectNode } from '~/redux/node/selectors';
const mapStateToProps = () => ({}); const mapStateToProps = selectNode;
const mapDispatchToProps = { const mapDispatchToProps = {
nodePostComment: NODE_ACTIONS.nodePostComment, nodePostComment: NODE_ACTIONS.nodePostComment,
nodeSetCommentData: NODE_ACTIONS.nodeSetCommentData,
}; };
type IProps = ReturnType<typeof mapStateToProps> & type IProps = ReturnType<typeof mapStateToProps> &
@ -20,29 +22,38 @@ type IProps = ReturnType<typeof mapStateToProps> &
id: INode['id']; id: INode['id'];
}; };
const CommentFormUnconnected: FC<IProps> = ({ nodePostComment, id }) => { const CommentFormUnconnected: FC<IProps> = ({
const [data, setData] = useState<IComment>({ ...EMPTY_COMMENT }); nodePostComment,
nodeSetCommentData,
comment_data,
id,
}) => {
// const [data, setData] = useState<IComment>({ ...EMPTY_COMMENT });
const onInput = useCallback<InputHandler>( const onInput = useCallback<InputHandler>(
text => { text => {
setData(assocPath(['text'], text, data)); nodeSetCommentData(assocPath(['text'], text, comment_data));
}, },
[setData, data] [nodeSetCommentData, comment_data]
); );
const onSubmit = useCallback( const onSubmit = useCallback(
event => { event => {
event.preventDefault(); event.preventDefault();
nodePostComment(data, id); nodePostComment();
}, },
[data, nodePostComment, id] [nodePostComment]
); );
useEffect(() => {
store.subscribe(console.log);
}, []);
return ( return (
<CommentWrapper> <CommentWrapper>
<form onSubmit={onSubmit}> <form onSubmit={onSubmit}>
<div className={styles.input}> <div className={styles.input}>
<Textarea value={data.text} handler={onInput} /> <Textarea value={comment_data.text} handler={onInput} />
</div> </div>
<div className={styles.buttons}> <div className={styles.buttons}>
<Filler /> <Filler />

View file

@ -12,13 +12,13 @@ interface IProps {
} }
const NodeComments: FC<IProps> = ({ comments }) => ( const NodeComments: FC<IProps> = ({ comments }) => (
<div className={styles.wrap}> <Group className={styles.wrap}>
<Filler />
{comments.map(comment => ( {comments.map(comment => (
<Comment key={comment.id} comment={comment} /> <Comment key={comment.id} comment={comment} />
))} ))}
</div>
<Filler />
</Group>
); );
export { NodeComments }; export { NodeComments };

View file

@ -1,16 +1,16 @@
.wrap { .wrap {
display: flex; // display: flex;
flex-direction: column-reverse !important; // flex-direction: column !important;
& > div { // & > div {
margin: ($gap / 2) 0; // margin: ($gap / 2) 0;
&:last-child { // &:last-child {
margin-top: 0; // margin-top: 0;
} // }
&:first-child { // &:first-child {
margin-bottom: 0; // margin-bottom: 0;
} // }
} // }
} }

View file

@ -21,6 +21,7 @@ interface IProps {
} }
const NodeImageBlock: FC<IProps> = ({ node, is_loading }) => { const NodeImageBlock: FC<IProps> = ({ node, is_loading }) => {
const [is_animated, setIsAnimated] = useState(false);
const [current, setCurrent] = useState(0); const [current, setCurrent] = useState(0);
const [height, setHeight] = useState(320); const [height, setHeight] = useState(320);
const [loaded, setLoaded] = useState<Record<number, boolean>>({}); const [loaded, setLoaded] = useState<Record<number, boolean>>({});
@ -48,8 +49,14 @@ const NodeImageBlock: FC<IProps> = ({ node, is_loading }) => {
setHeight(element_height); setHeight(element_height);
}, [refs, current, loaded]); }, [refs, current, loaded]);
useEffect(() => {
const timer = setTimeout(() => setIsAnimated(true), 250);
return () => clearTimeout(timer);
}, []);
return ( return (
<div className={classNames(styles.wrap, { is_loading })}> <div className={classNames(styles.wrap, { is_loading, is_animated })}>
<div> <div>
<ImageSwitcher <ImageSwitcher
total={images.length} total={images.length}

View file

@ -1,3 +1,15 @@
.wrap {
&:global(.is_animated) {
.image_container {
transition: height 0.5s;
}
.image_wrap {
transition: opacity 0.5s;
}
}
}
.image_container { .image_container {
width: 100%; width: 100%;
background: $node_image_bg; background: $node_image_bg;
@ -7,7 +19,6 @@
justify-content: center; justify-content: center;
position: relative; position: relative;
overflow: hidden; overflow: hidden;
transition: height 0.5s;
user-select: none; user-select: none;
.image { .image {
@ -27,7 +38,6 @@
pointer-events: none; pointer-events: none;
touch-action: none; touch-action: none;
z-index: 1; z-index: 1;
transition: opacity 0.5s;
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;

View file

@ -33,9 +33,7 @@ export const nodeSetCurrent = (current: INodeState['current']) => ({
type: NODE_ACTIONS.SET_CURRENT, type: NODE_ACTIONS.SET_CURRENT,
}); });
export const nodePostComment = (data: IComment, id: INode['id']) => ({ export const nodePostComment = () => ({
data,
id,
type: NODE_ACTIONS.POST_COMMENT, type: NODE_ACTIONS.POST_COMMENT,
}); });
@ -48,3 +46,8 @@ export const nodeSetComments = (comments: IComment[]) => ({
comments, comments,
type: NODE_ACTIONS.SET_COMMENTS, type: NODE_ACTIONS.SET_COMMENTS,
}); });
export const nodeSetCommentData = (comment_data: IComment) => ({
comment_data,
type: NODE_ACTIONS.SET_COMMENT_DATA,
});

View file

@ -12,6 +12,8 @@ export const NODE_ACTIONS = {
SET_LOADING_COMMENTS: `${prefix}SET_LOADING_COMMENTS`, SET_LOADING_COMMENTS: `${prefix}SET_LOADING_COMMENTS`,
SET_SENDING_COMMENT: `${prefix}SET_SENDING_COMMENT`, SET_SENDING_COMMENT: `${prefix}SET_SENDING_COMMENT`,
SET_CURRENT: `${prefix}SET_CURRENT`, SET_CURRENT: `${prefix}SET_CURRENT`,
SET_COMMENT_DATA: `${prefix}SET_COMMENT_DATA`,
POST_COMMENT: `${prefix}POST_COMMENT`, POST_COMMENT: `${prefix}POST_COMMENT`,
SET_COMMENTS: `${prefix}SET_COMMENTS`, SET_COMMENTS: `${prefix}SET_COMMENTS`,
}; };

View file

@ -7,6 +7,7 @@ import {
nodeSetLoadingComments, nodeSetLoadingComments,
nodeSetSendingComment, nodeSetSendingComment,
nodeSetComments, nodeSetComments,
nodeSetCommentData,
} from './actions'; } from './actions';
import { INodeState } from './reducer'; import { INodeState } from './reducer';
@ -32,6 +33,11 @@ const setSendingComment = (
const setComments = (state: INodeState, { comments }: ReturnType<typeof nodeSetComments>) => const setComments = (state: INodeState, { comments }: ReturnType<typeof nodeSetComments>) =>
assocPath(['comments'], comments, state); assocPath(['comments'], comments, state);
const setCommentData = (
state: INodeState,
{ comment_data }: ReturnType<typeof nodeSetCommentData>
) => assocPath(['comment_data'], comment_data, state);
export const NODE_HANDLERS = { export const NODE_HANDLERS = {
[NODE_ACTIONS.SAVE]: setSaveErrors, [NODE_ACTIONS.SAVE]: setSaveErrors,
[NODE_ACTIONS.SET_LOADING]: setLoading, [NODE_ACTIONS.SET_LOADING]: setLoading,
@ -39,4 +45,5 @@ export const NODE_HANDLERS = {
[NODE_ACTIONS.SET_CURRENT]: setCurrent, [NODE_ACTIONS.SET_CURRENT]: setCurrent,
[NODE_ACTIONS.SET_SENDING_COMMENT]: setSendingComment, [NODE_ACTIONS.SET_SENDING_COMMENT]: setSendingComment,
[NODE_ACTIONS.SET_COMMENTS]: setComments, [NODE_ACTIONS.SET_COMMENTS]: setComments,
[NODE_ACTIONS.SET_COMMENT_DATA]: setCommentData,
}; };

View file

@ -1,12 +1,13 @@
import { createReducer } from '~/utils/reducer'; import { createReducer } from '~/utils/reducer';
import { INode, IComment } from '../types'; import { INode, IComment } from '../types';
import { EMPTY_NODE } from './constants'; import { EMPTY_NODE, EMPTY_COMMENT } from './constants';
import { NODE_HANDLERS } from './handlers'; import { NODE_HANDLERS } from './handlers';
export type INodeState = Readonly<{ export type INodeState = Readonly<{
editor: INode; editor: INode;
current: INode; current: INode;
comments: IComment[]; comments: IComment[];
comment_data: IComment;
error: string; error: string;
errors: Record<string, string>; errors: Record<string, string>;
@ -24,6 +25,7 @@ const INITIAL_STATE: INodeState = {
files: [], files: [],
}, },
current: { ...EMPTY_NODE }, current: { ...EMPTY_NODE },
comment_data: { ...EMPTY_COMMENT },
comments: [], comments: [],
is_loading: false, is_loading: false,

View file

@ -1,7 +1,7 @@
import { takeLatest, call, put, select, delay } from 'redux-saga/effects'; import { takeLatest, call, put, select, delay } from 'redux-saga/effects';
import { push } from 'connected-react-router'; import { push } from 'connected-react-router';
import { NODE_ACTIONS, EMPTY_NODE } from './constants'; import { NODE_ACTIONS, EMPTY_NODE, EMPTY_COMMENT } from './constants';
import { import {
nodeSave, nodeSave,
nodeSetSaveErrors, nodeSetSaveErrors,
@ -12,6 +12,7 @@ import {
nodePostComment, nodePostComment,
nodeSetSendingComment, nodeSetSendingComment,
nodeSetComments, nodeSetComments,
nodeSetCommentData,
} from './actions'; } from './actions';
import { postNode, getNode, postNodeComment, getNodeComments } from './api'; import { postNode, getNode, postNodeComment, getNodeComments } from './api';
import { reqWrapper } from '../auth/sagas'; import { reqWrapper } from '../auth/sagas';
@ -76,12 +77,14 @@ function* onNodeLoad({ id, node_type }: ReturnType<typeof nodeLoadNode>) {
return; return;
} }
function* onPostComment({ data, id }: ReturnType<typeof nodePostComment>) { function* onPostComment() {
const { current, comment_data } = yield select(selectNode);
yield put(nodeSetSendingComment(true)); yield put(nodeSetSendingComment(true));
const { const {
data: { comment }, data: { comment },
error, error,
} = yield call(reqWrapper, postNodeComment, { data, id }); } = yield call(reqWrapper, postNodeComment, { data: comment_data, id: current.id });
yield put(nodeSetSendingComment(false)); yield put(nodeSetSendingComment(false));
if (error || !comment) { if (error || !comment) {
@ -92,7 +95,8 @@ function* onPostComment({ data, id }: ReturnType<typeof nodePostComment>) {
const { comments } = yield select(selectNode); const { comments } = yield select(selectNode);
yield put(nodeSetComments([...comments, comment])); yield put(nodeSetComments([comment, ...comments]));
yield put(nodeSetCommentData({ ...EMPTY_COMMENT }));
} }
export default function* nodeSaga() { export default function* nodeSaga() {