Skip to content

Commit

Permalink
refactor(CommentForm): remove external entity props
Browse files Browse the repository at this point in the history
  • Loading branch information
awinogradov committed Aug 7, 2023
1 parent 450108e commit 36032bc
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 469 deletions.
97 changes: 43 additions & 54 deletions src/components/CommentCreateForm/CommentCreateForm.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import React, { useState, useCallback, useEffect, useMemo } from 'react';
import React, { useState, useCallback, useMemo } from 'react';
import { ArrowDownSmallIcon, ArrowUpSmallIcon, Button, Dropdown, UserPic } from '@taskany/bricks';
import { State } from '@prisma/client';
import styled from 'styled-components';

import { usePageContext } from '../../hooks/usePageContext';
import { useCommentResource } from '../../hooks/useCommentResource';
import { GoalCommentSchema } from '../../schema/goal';
import { GoalCommentFormSchema } from '../../schema/goal';
import { CommentSchema } from '../../schema/comment';
import { CommentForm } from '../CommentForm/CommentForm';
import { ActivityFeedItem } from '../ActivityFeed';
import { ColorizedMenuItem } from '../ColorizedMenuItem';
import { StateDot } from '../StateDot';

import { tr } from './CommentCreateForm.i18n';

interface CommentCreateFormProps {
goalId: string;
interface CommentCreateFormProps extends Omit<React.ComponentProps<typeof CommentForm>, 'actionButton'> {
states?: State[];
description?: string;
stateId?: string;

onSubmit?: (id?: string) => void;
onFocus?: () => void;
onCancel?: () => void;
onChange?: (comment?: { description?: string; stateId?: string }) => void;
onSubmit: (comment: GoalCommentFormSchema) => void;
onChange?: (comment: GoalCommentFormSchema) => void;
}

const StyledStateUpdate = styled.div`
Expand All @@ -31,10 +27,9 @@ const StyledStateUpdate = styled.div`
`;

const CommentCreateForm: React.FC<CommentCreateFormProps> = ({
goalId,
states,
description: currentDescription,
stateId = '',
description: currentDescription = '',
stateId,
onSubmit,
onFocus,
onCancel,
Expand All @@ -43,60 +38,48 @@ const CommentCreateForm: React.FC<CommentCreateFormProps> = ({
const statesMap = useMemo(() => {
if (!states) return {};

return states.reduce((acc, cur) => {
return states.reduce<Record<string, State>>((acc, cur) => {
acc[cur.id] = cur;
return acc;
}, {} as Record<string, State>);
}, {});
}, [states]);

const { user, themeId } = usePageContext();
const { create } = useCommentResource();
const [pushState, setPushState] = useState<State | undefined>(statesMap[stateId]);
const [description, setDescription] = useState<string | undefined>(currentDescription);

const [pushState, setPushState] = useState(stateId ? statesMap[stateId] : undefined);
const [description, setDescription] = useState(currentDescription);
const [focused, setFocused] = useState(Boolean(currentDescription));
const [busy, setBusy] = useState(false);
const [currentGoal, setCurrentGoal] = useState(goalId);
const [prevGoal, setPrevGoal] = useState('');

useEffect(() => {
if (!description) {
onChange?.();
return;
}

onChange?.({ description, stateId: pushState?.id });
}, [pushState?.id, description, onChange]);

useEffect(() => {
if (goalId === prevGoal) return;

setCurrentGoal(goalId);
setPrevGoal(goalId);
setDescription(currentDescription);
setFocused(Boolean(currentDescription));
setPushState(statesMap[stateId]);
}, [currentDescription, goalId, prevGoal, stateId, statesMap]);

const onCommentFocus = useCallback(() => {
setFocused(true);
onFocus?.();
}, [onFocus]);

const createComment = useCallback(
async (form: GoalCommentSchema) => {
const onCommentChange = useCallback(
({ description }: { description: string }) => {
onChange?.({ description, stateId: pushState?.id });
},
[onChange, pushState?.id],
);

const onCommentSubmit = useCallback(
async (form: CommentSchema) => {
setBusy(true);
setFocused(false);

await create(({ id }) => {
onSubmit?.(id);
setDescription('');
setPushState(undefined);
})(form);
await onSubmit?.({
...form,
stateId: pushState?.id,
});

setDescription('');
setPushState(undefined);

setBusy(false);
setFocused(true);
},
[create, onSubmit],
[onSubmit, pushState?.id],
);

const onCancelCreate = useCallback(() => {
Expand All @@ -107,22 +90,28 @@ const CommentCreateForm: React.FC<CommentCreateFormProps> = ({
onCancel?.();
}, [onCancel]);

const onStateSelect = useCallback((state: State) => {
setPushState((prev) => (state.id === prev?.id ? undefined : state));
}, []);
const onStateSelect = useCallback(
(state: State) => {
setPushState((prev) => {
const newState = state.id === prev?.id ? undefined : state;
onChange?.({ description, stateId: newState?.id });

return newState;
});
},
[onChange, setPushState, description],
);

return (
<ActivityFeedItem>
<UserPic size={32} src={user?.image} email={user?.email} />

<CommentForm
goalId={currentGoal}
stateId={pushState?.id}
description={description}
focused={focused}
busy={busy}
onDescriptionChange={setDescription}
onSubmit={createComment}
onChange={onCommentChange}
onSubmit={onCommentSubmit}
onCancel={onCancelCreate}
onFocus={onCommentFocus}
actionButton={
Expand Down
3 changes: 0 additions & 3 deletions src/components/CommentEditForm/CommentEditForm.i18n/en.json

This file was deleted.

17 changes: 0 additions & 17 deletions src/components/CommentEditForm/CommentEditForm.i18n/index.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/components/CommentEditForm/CommentEditForm.i18n/ru.json

This file was deleted.

82 changes: 0 additions & 82 deletions src/components/CommentEditForm/CommentEditForm.tsx

This file was deleted.

Loading

0 comments on commit 36032bc

Please sign in to comment.