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

added saga to change tags

This commit is contained in:
Fedor Katurov 2019-10-09 16:12:27 +07:00
parent 033b5ca45d
commit 9df5e022dd
7 changed files with 44 additions and 11 deletions

View file

@ -9,14 +9,10 @@ import React, {
useRef, useRef,
} from 'react'; } from 'react';
import { TagField } from '~/components/containers/TagField'; import { TagField } from '~/components/containers/TagField';
import { ITag } from '~/redux/types'; import { ITag, INode } from '~/redux/types';
import { Tag } from '~/components/node/Tag'; import { Tag } from '~/components/node/Tag';
import uniq from 'ramda/es/uniq'; import uniq from 'ramda/es/uniq';
import equals from 'ramda/es/equals';
import { setTimeout } from 'timers'; import { setTimeout } from 'timers';
import identity from 'ramda/es/identity';
import countBy from 'ramda/es/countBy';
import eqBy from 'ramda/es/eqBy';
import length from 'ramda/es/length'; import length from 'ramda/es/length';
import isEmpty from 'ramda/es/isEmpty'; import isEmpty from 'ramda/es/isEmpty';
import symmetricDifference from 'ramda/es/symmetricDifference'; import symmetricDifference from 'ramda/es/symmetricDifference';
@ -34,10 +30,8 @@ export const Tags: FC<IProps> = ({ tags, is_editable, onTagsChange, ...props })
const onInput = useCallback( const onInput = useCallback(
({ target: { value } }: ChangeEvent<HTMLInputElement>) => { ({ target: { value } }: ChangeEvent<HTMLInputElement>) => {
clearTimeout(timer.current);
setInput(value); setInput(value);
if (timer) {
clearTimeout(timer.current);
}
}, },
[setInput, timer] [setInput, timer]
); );

View file

@ -13,5 +13,6 @@ export const API = {
GET_NODE: (id: number | string) => `/node/${id}`, GET_NODE: (id: number | string) => `/node/${id}`,
COMMENT: (id: INode['id']) => `/node/${id}/comment`, COMMENT: (id: INode['id']) => `/node/${id}/comment`,
UPDATE_TAGS: (id: INode['id']) => `/node/${id}/tags`,
}, },
}; };

View file

@ -1,4 +1,4 @@
import React, { FC, createElement, useEffect } from 'react'; import React, { FC, createElement, useEffect, useCallback } from 'react';
import { RouteComponentProps } from 'react-router'; import { RouteComponentProps } from 'react-router';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
@ -25,6 +25,7 @@ const mapStateToProps = state => ({
const mapDispatchToProps = { const mapDispatchToProps = {
nodeLoadNode: NODE_ACTIONS.nodeLoadNode, nodeLoadNode: NODE_ACTIONS.nodeLoadNode,
nodeUpdateTags: NODE_ACTIONS.nodeUpdateTags,
}; };
type IProps = ReturnType<typeof mapStateToProps> & type IProps = ReturnType<typeof mapStateToProps> &
@ -38,12 +39,19 @@ const NodeLayoutUnconnected: FC<IProps> = ({
node: { is_loading, is_loading_comments, comments = [], current: node }, node: { is_loading, is_loading_comments, comments = [], current: node },
user: { is_user }, user: { is_user },
nodeLoadNode, nodeLoadNode,
nodeUpdateTags,
}) => { }) => {
useEffect(() => { useEffect(() => {
if (is_loading) return; if (is_loading) return;
nodeLoadNode(parseInt(id, 10), null); nodeLoadNode(parseInt(id, 10), null);
}, [nodeLoadNode, id]); }, [nodeLoadNode, id]);
const onTagsChange = useCallback(
(tags: string[]) => {
nodeUpdateTags(node.id, tags);
},
[node, nodeUpdateTags]
);
const block = node && node.type && NODE_COMPONENTS[node.type] && NODE_COMPONENTS[node.type]; const block = node && node.type && NODE_COMPONENTS[node.type] && NODE_COMPONENTS[node.type];
return ( return (
@ -67,7 +75,7 @@ const NodeLayoutUnconnected: FC<IProps> = ({
<div className={styles.panel}> <div className={styles.panel}>
<Group style={{ flex: 1, minWidth: 0 }}> <Group style={{ flex: 1, minWidth: 0 }}>
<NodeTags is_editable={is_user} tags={node.tags} onChange={console.log} /> <NodeTags is_editable={is_user} tags={node.tags} onChange={onTagsChange} />
<NodeRelated title="First album" /> <NodeRelated title="First album" />

View file

@ -53,3 +53,9 @@ export const nodeSetCommentData = (id: number, comment: IComment) => ({
comment, comment,
type: NODE_ACTIONS.SET_COMMENT_DATA, type: NODE_ACTIONS.SET_COMMENT_DATA,
}); });
export const nodeUpdateTags = (id: INode['id'], tags: string[]) => ({
type: NODE_ACTIONS.UPDATE_TAGS,
id,
tags,
});

View file

@ -1,6 +1,7 @@
import { api, configWithToken, resultMiddleware, errorMiddleware } from '~/utils/api'; import { api, configWithToken, resultMiddleware, errorMiddleware } from '~/utils/api';
import { INode, IResultWithStatus, IComment } from '../types'; import { INode, IResultWithStatus, IComment } from '../types';
import { API } from '~/constants/api'; import { API } from '~/constants/api';
import { nodeUpdateTags } from './actions';
export const postNode = ({ export const postNode = ({
access, access,
@ -57,3 +58,15 @@ export const getNodeComments = ({
.get(API.NODE.COMMENT(id)) .get(API.NODE.COMMENT(id))
.then(resultMiddleware) .then(resultMiddleware)
.catch(errorMiddleware); .catch(errorMiddleware);
export const updateNodeTags = ({
id,
tags,
access,
}: ReturnType<typeof nodeUpdateTags> & { access: string }): Promise<
IResultWithStatus<{ node: INode }>
> =>
api
.post(API.NODE.UPDATE_TAGS(id), { tags }, configWithToken(access))
.then(resultMiddleware)
.catch(errorMiddleware);

View file

@ -16,6 +16,8 @@ export const NODE_ACTIONS = {
POST_COMMENT: `${prefix}POST_COMMENT`, POST_COMMENT: `${prefix}POST_COMMENT`,
SET_COMMENTS: `${prefix}SET_COMMENTS`, SET_COMMENTS: `${prefix}SET_COMMENTS`,
UPDATE_TAGS: `${prefix}UPDATE_TAGS`,
}; };
export const EMPTY_BLOCK: IBlock = { export const EMPTY_BLOCK: IBlock = {

View file

@ -13,8 +13,9 @@ import {
nodeSetSendingComment, nodeSetSendingComment,
nodeSetComments, nodeSetComments,
nodeSetCommentData, nodeSetCommentData,
nodeUpdateTags,
} from './actions'; } from './actions';
import { postNode, getNode, postNodeComment, getNodeComments } from './api'; import { postNode, getNode, postNodeComment, getNodeComments, updateNodeTags } from './api';
import { reqWrapper } from '../auth/sagas'; import { reqWrapper } from '../auth/sagas';
import { flowSetNodes } from '../flow/actions'; import { flowSetNodes } from '../flow/actions';
import { ERRORS } from '~/constants/errors'; import { ERRORS } from '~/constants/errors';
@ -102,8 +103,16 @@ function* onPostComment({ id }: ReturnType<typeof nodePostComment>) {
} }
} }
function* onUpdateTags({ id, tags }: ReturnType<typeof nodeUpdateTags>) {
yield delay(1000);
const result = yield call(reqWrapper, updateNodeTags, { id, tags });
console.log({ result });
}
export default function* nodeSaga() { export default function* nodeSaga() {
yield takeLatest(NODE_ACTIONS.SAVE, onNodeSave); yield takeLatest(NODE_ACTIONS.SAVE, onNodeSave);
yield takeLatest(NODE_ACTIONS.LOAD_NODE, onNodeLoad); yield takeLatest(NODE_ACTIONS.LOAD_NODE, onNodeLoad);
yield takeLatest(NODE_ACTIONS.POST_COMMENT, onPostComment); yield takeLatest(NODE_ACTIONS.POST_COMMENT, onPostComment);
yield takeLatest(NODE_ACTIONS.UPDATE_TAGS, onUpdateTags);
} }