summaryrefslogtreecommitdiffstats
path: root/ui/src/utils.ts
diff options
context:
space:
mode:
authorDessalines <tyhou13@gmx.com>2020-02-08 23:20:11 -0500
committerDessalines <tyhou13@gmx.com>2020-02-08 23:20:11 -0500
commit56cd103209605471b27aa5a854cc3b051f2a65f5 (patch)
tree704e96abe61a6481fe9243b4b4d33271f785e960 /ui/src/utils.ts
parent8baa483c8907945921d962be9b34cad824c2e294 (diff)
Fixing some technical debt. Fixes #524
Diffstat (limited to 'ui/src/utils.ts')
-rw-r--r--ui/src/utils.ts88
1 files changed, 88 insertions, 0 deletions
diff --git a/ui/src/utils.ts b/ui/src/utils.ts
index 51836c6f..8cdc02f0 100644
--- a/ui/src/utils.ts
+++ b/ui/src/utils.ts
@@ -15,6 +15,8 @@ import 'moment/locale/pt-br';
import {
UserOperation,
Comment,
+ CommentNode,
+ Post,
PrivateMessage,
User,
SortType,
@@ -25,6 +27,8 @@ import {
WebSocketJsonResponse,
SearchForm,
SearchResponse,
+ CommentResponse,
+ PostResponse,
} from './interfaces';
import { UserService, WebSocketService } from './services';
@@ -551,3 +555,87 @@ export function getSortTypeFromProps(props: any): SortType {
export function getPageFromProps(props: any): number {
return props.match.params.page ? Number(props.match.params.page) : 1;
}
+
+export function editCommentRes(
+ data: CommentResponse,
+ comments: Array<Comment>
+) {
+ let found = comments.find(c => c.id == data.comment.id);
+ 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;
+ }
+}
+
+export function saveCommentRes(
+ data: CommentResponse,
+ comments: Array<Comment>
+) {
+ let found = comments.find(c => c.id == data.comment.id);
+ if (found) {
+ found.saved = data.comment.saved;
+ }
+}
+
+export function createCommentLikeRes(
+ data: CommentResponse,
+ comments: Array<Comment>
+) {
+ let found: Comment = comments.find(c => c.id === data.comment.id);
+ 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;
+ }
+ }
+}
+
+export function createPostLikeFindRes(data: PostResponse, posts: Array<Post>) {
+ let found = posts.find(c => c.id == data.post.id);
+ if (found) {
+ createPostLikeRes(data, found);
+ }
+}
+
+export function createPostLikeRes(data: PostResponse, post: Post) {
+ post.score = data.post.score;
+ post.upvotes = data.post.upvotes;
+ post.downvotes = data.post.downvotes;
+ if (data.post.my_vote !== null) {
+ post.my_vote = data.post.my_vote;
+ post.upvoteLoading = false;
+ post.downvoteLoading = false;
+ }
+}
+
+export function editPostFindRes(data: PostResponse, posts: Array<Post>) {
+ let found = posts.find(c => c.id == data.post.id);
+ if (found) {
+ editPostRes(data, found);
+ }
+}
+
+export function editPostRes(data: PostResponse, post: Post) {
+ post.url = data.post.url;
+ post.name = data.post.name;
+ post.nsfw = data.post.nsfw;
+}
+
+export function commentsToFlatNodes(
+ comments: Array<Comment>
+): Array<CommentNode> {
+ let nodes: Array<CommentNode> = [];
+ for (let comment of comments) {
+ nodes.push({ comment: comment });
+ }
+ return nodes;
+}