From a044718066623f19d51196d444bd199c1231cebe Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sat, 18 Jan 2020 23:54:10 -0500 Subject: First pass at fixing UI to work with new websocketresponses. --- ui/src/components/inbox.tsx | 106 ++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 53 deletions(-) (limited to 'ui/src/components/inbox.tsx') diff --git a/ui/src/components/inbox.tsx b/ui/src/components/inbox.tsx index a302b834..a46017e2 100644 --- a/ui/src/components/inbox.tsx +++ b/ui/src/components/inbox.tsx @@ -14,7 +14,7 @@ import { CommentResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; -import { msgOp, fetchLimit } from '../utils'; +import { wsJsonToRes, fetchLimit } from '../utils'; import { CommentNodes } from './comment-nodes'; import { SortSelect } from './sort-select'; import { i18n } from '../i18next'; @@ -298,92 +298,92 @@ export class Inbox extends Component { parseMessage(msg: any) { console.log(msg); - let op: UserOperation = msgOp(msg); - if (msg.error) { - alert(i18n.t(msg.error)); + let res = wsJsonToRes(msg); + if (res.error) { + alert(i18n.t(res.error)); return; - } else if (op == UserOperation.GetReplies) { - let res: GetRepliesResponse = msg; - this.state.replies = res.replies; + } else if (res.op == UserOperation.GetReplies) { + let data = res.data as GetRepliesResponse; + this.state.replies = data.replies; this.sendUnreadCount(); window.scrollTo(0, 0); this.setState(this.state); - } else if (op == UserOperation.GetUserMentions) { - let res: GetUserMentionsResponse = msg; - this.state.mentions = res.mentions; + } else if (res.op == UserOperation.GetUserMentions) { + let data = res.data as GetUserMentionsResponse; + this.state.mentions = data.mentions; this.sendUnreadCount(); window.scrollTo(0, 0); this.setState(this.state); - } else if (op == UserOperation.MarkAllAsRead) { + } else if (res.op == UserOperation.MarkAllAsRead) { this.state.replies = []; this.state.mentions = []; window.scrollTo(0, 0); this.setState(this.state); - } else if (op == UserOperation.EditComment) { - let res: CommentResponse = msg; - - let found = this.state.replies.find(c => c.id == res.comment.id); - found.content = res.comment.content; - found.updated = res.comment.updated; - found.removed = res.comment.removed; - found.deleted = res.comment.deleted; - found.upvotes = res.comment.upvotes; - found.downvotes = res.comment.downvotes; - found.score = res.comment.score; + } else if (res.op == UserOperation.EditComment) { + let data = res.data as CommentResponse; + + let found = this.state.replies.find(c => c.id == data.comment.id); + found.content = data.comment.content; + found.updated = data.comment.updated; + found.removed = data.comment.removed; + found.deleted = data.comment.deleted; + found.upvotes = data.comment.upvotes; + found.downvotes = data.comment.downvotes; + found.score = data.comment.score; // If youre in the unread view, just remove it from the list - if (this.state.unreadOrAll == UnreadOrAll.Unread && res.comment.read) { + if (this.state.unreadOrAll == UnreadOrAll.Unread && data.comment.read) { this.state.replies = this.state.replies.filter( - r => r.id !== res.comment.id + r => r.id !== data.comment.id ); } else { - let found = this.state.replies.find(c => c.id == res.comment.id); - found.read = res.comment.read; + let found = this.state.replies.find(c => c.id == data.comment.id); + found.read = data.comment.read; } this.sendUnreadCount(); this.setState(this.state); - } else if (op == UserOperation.EditUserMention) { - let res: UserMentionResponse = msg; - - let found = this.state.mentions.find(c => c.id == res.mention.id); - found.content = res.mention.content; - found.updated = res.mention.updated; - found.removed = res.mention.removed; - found.deleted = res.mention.deleted; - found.upvotes = res.mention.upvotes; - found.downvotes = res.mention.downvotes; - found.score = res.mention.score; + } else if (res.op == UserOperation.EditUserMention) { + let data = res.data as UserMentionResponse; + + let found = this.state.mentions.find(c => c.id == data.mention.id); + found.content = data.mention.content; + found.updated = data.mention.updated; + found.removed = data.mention.removed; + found.deleted = data.mention.deleted; + found.upvotes = data.mention.upvotes; + found.downvotes = data.mention.downvotes; + found.score = data.mention.score; // If youre in the unread view, just remove it from the list - if (this.state.unreadOrAll == UnreadOrAll.Unread && res.mention.read) { + if (this.state.unreadOrAll == UnreadOrAll.Unread && data.mention.read) { this.state.mentions = this.state.mentions.filter( - r => r.id !== res.mention.id + r => r.id !== data.mention.id ); } else { - let found = this.state.mentions.find(c => c.id == res.mention.id); - found.read = res.mention.read; + let found = this.state.mentions.find(c => c.id == data.mention.id); + found.read = data.mention.read; } this.sendUnreadCount(); this.setState(this.state); - } else if (op == UserOperation.CreateComment) { + } else if (res.op == UserOperation.CreateComment) { // let res: CommentResponse = msg; alert(i18n.t('reply_sent')); // this.state.replies.unshift(res.comment); // TODO do this right // this.setState(this.state); - } else if (op == UserOperation.SaveComment) { - let res: CommentResponse = msg; - let found = this.state.replies.find(c => c.id == res.comment.id); - found.saved = res.comment.saved; + } else if (res.op == UserOperation.SaveComment) { + let data = res.data as CommentResponse; + let found = this.state.replies.find(c => c.id == data.comment.id); + found.saved = data.comment.saved; this.setState(this.state); - } else if (op == UserOperation.CreateCommentLike) { - let res: CommentResponse = msg; + } else if (res.op == UserOperation.CreateCommentLike) { + let data = res.data as CommentResponse; let found: Comment = this.state.replies.find( - c => c.id === res.comment.id + c => c.id === data.comment.id ); - found.score = res.comment.score; - found.upvotes = res.comment.upvotes; - found.downvotes = res.comment.downvotes; - if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote; + found.score = data.comment.score; + found.upvotes = data.comment.upvotes; + found.downvotes = data.comment.downvotes; + if (data.comment.my_vote !== null) found.my_vote = data.comment.my_vote; this.setState(this.state); } } -- cgit v1.2.3 From dcd60228ed753a146bcb56bb5bf51166835acfb7 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sun, 19 Jan 2020 00:38:45 -0500 Subject: Strongly typing WebsocketJsonResponse. Forgot comment-form.tsx --- ui/src/components/inbox.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'ui/src/components/inbox.tsx') diff --git a/ui/src/components/inbox.tsx b/ui/src/components/inbox.tsx index a46017e2..4aa9cebe 100644 --- a/ui/src/components/inbox.tsx +++ b/ui/src/components/inbox.tsx @@ -12,6 +12,7 @@ import { GetUserMentionsResponse, UserMentionResponse, CommentResponse, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { wsJsonToRes, fetchLimit } from '../utils'; @@ -296,7 +297,7 @@ export class Inbox extends Component { WebSocketService.Instance.markAllAsRead(); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); let res = wsJsonToRes(msg); if (res.error) { -- cgit v1.2.3