From 2efab6764076426615727936aaa8ca2fbd927b65 Mon Sep 17 00:00:00 2001 From: Pavel Mineev Date: Sat, 4 Jan 2020 01:30:15 +0300 Subject: [PATCH] fix warnings --- .../app/components/comment/comment.test.tsx | 58 ++++++++++++++++++- frontend/app/components/comment/comment.tsx | 33 +++++------ 2 files changed, 72 insertions(+), 19 deletions(-) diff --git a/frontend/app/components/comment/comment.test.tsx b/frontend/app/components/comment/comment.test.tsx index d6ae81dd..99c3f6cf 100644 --- a/frontend/app/components/comment/comment.test.tsx +++ b/frontend/app/components/comment/comment.test.tsx @@ -1,9 +1,10 @@ /** @jsx createElement */ import { createElement } from 'preact'; -import { mount } from 'enzyme'; +import { mount, shallow, HTMLAttributes } from 'enzyme'; import { Props, Comment } from './comment'; import { User, Comment as CommentType, PostInfo } from '@app/common/types'; import { sleep } from '@app/utils/sleep'; +import { StaticStore } from '@app/common/static_store'; const DefaultProps: Partial = { post_info: { @@ -25,6 +26,7 @@ const DefaultProps: Partial = { user: { admin: false, id: 'testuser', + picture: 'somepicture-url', } as User, }; @@ -221,5 +223,59 @@ describe('', () => { const controls = element.find('.comment__verification').first(); expect(controls.hasClass('comment__verification_clickable')).toEqual(false); }); + + it('should be editable', () => { + const initTime = new Date().toString(); + const changedTime = new Date(Date.now() + 10 * 1000).toString(); + const props: Partial = { + ...DefaultProps, + user: DefaultProps.user as User, + data: { + ...DefaultProps.data, + id: '100', + user: DefaultProps.user as User, + vote: 1, + time: initTime, + delete: false, + orig: 'test', + } as CommentType, + repliesCount: 0, + }; + StaticStore.config.edit_duration = 300; + + const component = shallow(); + + expect((component.state('editDeadline') as Date).getTime()).toBe( + new Date(new Date(initTime).getTime() + 300 * 1000).getTime() + ); + + component.setProps({ + data: { ...props.data, time: changedTime }, + }); + + expect((component.state('editDeadline') as Date).getTime()).toBe( + new Date(new Date(changedTime).getTime() + 300 * 1000).getTime() + ); + }); + + it('shoud not be editable', () => { + const props: Partial = { + ...DefaultProps, + user: DefaultProps.user as User, + data: { + ...DefaultProps.data, + id: '100', + user: DefaultProps.user as User, + vote: 1, + time: new Date(new Date().getDate() - 300).toString(), + orig: 'test', + } as CommentType, + }; + StaticStore.config.edit_duration = 300; + + const component = shallow(); + + expect(component.state('editDeadline')).toBe(null); + }); }); }); diff --git a/frontend/app/components/comment/comment.tsx b/frontend/app/components/comment/comment.tsx index bbd49330..06131a9b 100644 --- a/frontend/app/components/comment/comment.tsx +++ b/frontend/app/components/comment/comment.tsx @@ -84,12 +84,11 @@ export class Comment extends Component { scoreDelta: 0, cachedScore: props.data.score, initial: true, + ...this.updateState(props), }; this.votingPromise = Promise.resolve(); - this.updateState(props); - this.toggleEditing = this.toggleEditing.bind(this); this.toggleReplying = this.toggleReplying.bind(this); this.blockUser = debounce(this.blockUser, 100).bind(this); @@ -102,7 +101,7 @@ export class Comment extends Component { // }; componentWillReceiveProps(nextProps: Props) { - this.updateState(nextProps); + this.setState(this.updateState(nextProps)); } componentDidMount() { @@ -110,25 +109,25 @@ export class Comment extends Component { } updateState = (props: Props) => { - this.setState({ + const newState: Partial = { scoreDelta: props.data.vote, cachedScore: props.data.score, - }); + }; - if (props.user) { - const userId = props.user!.id; + // set comment edit timer + if (props.user && props.user.id === props.data.user.id) { + const editDuration = StaticStore.config.edit_duration; + const timeDiff = StaticStore.serverClientTimeDiff || 0; + const editDeadline = new Date(new Date(props.data.time).getTime() + timeDiff + editDuration * 1000); - // set comment edit timer - if (userId === props.data.user.id) { - const editDuration = StaticStore.config.edit_duration; - const timeDiff = StaticStore.serverClientTimeDiff || 0; - let editDeadline: Date | null = new Date(new Date(props.data.time).getTime() + timeDiff + editDuration * 1000); - if (editDeadline < new Date()) editDeadline = null; - this.setState({ - editDeadline, - }); + if (editDeadline < new Date()) { + newState.editDeadline = null; + } else { + newState.editDeadline = editDeadline; } } + + return newState; }; toggleReplying = () => { @@ -454,7 +453,6 @@ export class Comment extends Component { const isReplying = props.editMode === CommentMode.Reply; const isEditing = props.editMode === CommentMode.Edit; - const lowCommentScore = StaticStore.config.low_score; const downvotingDisabledReason = this.getDownvoteDisabledReason(); const isDownvotingDisabled = downvotingDisabledReason !== null; @@ -684,7 +682,6 @@ export class Comment extends Component { {isReplying ? 'Cancel' : 'Reply'} )} - {!props.data.delete && !props.disabled && !!o.orig &&