Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite CommentForm w/o project deps, like goalId and trpc #1469

Merged
merged 8 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const nextConfig = {
fs: false,
};

if (dev && !isServer) {
if (process.env.NEXT_PUBLIC_WDYR === '1' && dev && !isServer) {
const originalEntry = config.entry;
config.entry = async () => {
const wdrPath = path.resolve(__dirname, './src/utils/wdyr.ts');
Expand Down
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
Loading