From 716ce0f77934f71c2104cf24d3cd5c17d6298cac Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 5 Feb 2020 14:24:35 -0500 Subject: [PATCH] Adding if found to catch errors. --- ui/src/components/community.tsx | 30 +++++++++++------------ ui/src/components/main.tsx | 31 ++++++++++++------------ ui/src/components/post.tsx | 42 +++++++++++++++++++-------------- 3 files changed, 55 insertions(+), 48 deletions(-) diff --git a/ui/src/components/community.tsx b/ui/src/components/community.tsx index 0fa8f7b53..069f9158d 100644 --- a/ui/src/components/community.tsx +++ b/ui/src/components/community.tsx @@ -283,12 +283,12 @@ export class Community extends Component { } else if (res.op == UserOperation.EditPost) { let data = res.data as PostResponse; let found = this.state.posts.find(c => c.id == data.post.id); - - found.url = data.post.url; - found.name = data.post.name; - found.nsfw = data.post.nsfw; - - this.setState(this.state); + if (found) { + found.url = data.post.url; + found.name = data.post.name; + found.nsfw = data.post.nsfw; + this.setState(this.state); + } } else if (res.op == UserOperation.CreatePost) { let data = res.data as PostResponse; this.state.posts.unshift(data.post); @@ -296,16 +296,16 @@ export class Community extends Component { } else if (res.op == UserOperation.CreatePostLike) { let data = res.data as PostResponse; let found = this.state.posts.find(c => c.id == data.post.id); - - found.score = data.post.score; - found.upvotes = data.post.upvotes; - found.downvotes = data.post.downvotes; - if (data.post.my_vote !== null) { - found.my_vote = data.post.my_vote; - found.upvoteLoading = false; - found.downvoteLoading = false; + if (found) { + found.score = data.post.score; + found.upvotes = data.post.upvotes; + found.downvotes = data.post.downvotes; + if (data.post.my_vote !== null) { + found.my_vote = data.post.my_vote; + found.upvoteLoading = false; + found.downvoteLoading = false; + } } - this.setState(this.state); } else if (res.op == UserOperation.AddModToCommunity) { let data = res.data as AddModToCommunityResponse; diff --git a/ui/src/components/main.tsx b/ui/src/components/main.tsx index 0970381b5..161f5df45 100644 --- a/ui/src/components/main.tsx +++ b/ui/src/components/main.tsx @@ -590,26 +590,27 @@ export class Main extends Component { } else if (res.op == UserOperation.EditPost) { let data = res.data as PostResponse; let found = this.state.posts.find(c => c.id == data.post.id); + if (found) { + found.url = data.post.url; + found.name = data.post.name; + found.nsfw = data.post.nsfw; - found.url = data.post.url; - found.name = data.post.name; - found.nsfw = data.post.nsfw; - - this.setState(this.state); + this.setState(this.state); + } } else if (res.op == UserOperation.CreatePostLike) { let data = res.data as PostResponse; let found = this.state.posts.find(c => c.id == data.post.id); - - found.score = data.post.score; - found.upvotes = data.post.upvotes; - found.downvotes = data.post.downvotes; - if (data.post.my_vote !== null) { - found.my_vote = data.post.my_vote; - found.upvoteLoading = false; - found.downvoteLoading = false; + if (found) { + found.score = data.post.score; + found.upvotes = data.post.upvotes; + found.downvotes = data.post.downvotes; + if (data.post.my_vote !== null) { + found.my_vote = data.post.my_vote; + found.upvoteLoading = false; + found.downvoteLoading = false; + } + this.setState(this.state); } - - this.setState(this.state); } else if (res.op == UserOperation.AddAdmin) { let data = res.data as AddAdminResponse; this.state.siteRes.admins = data.admins; diff --git a/ui/src/components/post.tsx b/ui/src/components/post.tsx index b399d3b28..922fc01ea 100644 --- a/ui/src/components/post.tsx +++ b/ui/src/components/post.tsx @@ -409,33 +409,39 @@ export class Post extends Component { } else if (res.op == UserOperation.EditComment) { let data = res.data as CommentResponse; let found = this.state.comments.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; - found.read = data.comment.read; + if (found) { + 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; + found.read = data.comment.read; - this.setState(this.state); + this.setState(this.state); + } } else if (res.op == UserOperation.SaveComment) { let data = res.data as CommentResponse; let found = this.state.comments.find(c => c.id == data.comment.id); - found.saved = data.comment.saved; - this.setState(this.state); + if (found) { + found.saved = data.comment.saved; + this.setState(this.state); + } } else if (res.op == UserOperation.CreateCommentLike) { let data = res.data as CommentResponse; let found: Comment = this.state.comments.find( c => c.id === data.comment.id ); - 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; - found.upvoteLoading = false; - found.downvoteLoading = false; + if (found) { + 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; + found.upvoteLoading = false; + found.downvoteLoading = false; + } } this.setState(this.state); } else if (res.op == UserOperation.CreatePostLike) {