diff options
author | Dessalines <tyhou13@gmx.com> | 2020-01-23 19:17:42 -0500 |
---|---|---|
committer | Dessalines <tyhou13@gmx.com> | 2020-01-23 19:17:42 -0500 |
commit | ac1d5f2b86b0816cc9b5794dc6c38a38365ed839 (patch) | |
tree | bedaab04ca797113990e5702cad36f5e34e9e6e8 /ui/src | |
parent | 39d1fb4450cb418d9c11b31ea99484c2a177fab6 (diff) | |
parent | b45c83682c187737b092080e83ba741c39cfa695 (diff) |
Done merging http-api and private_message
Diffstat (limited to 'ui/src')
-rw-r--r-- | ui/src/components/comment-form.tsx | 18 | ||||
-rw-r--r-- | ui/src/components/communities.tsx | 25 | ||||
-rw-r--r-- | ui/src/components/community-form.tsx | 37 | ||||
-rw-r--r-- | ui/src/components/community.tsx | 53 | ||||
-rw-r--r-- | ui/src/components/inbox.tsx | 133 | ||||
-rw-r--r-- | ui/src/components/login.tsx | 29 | ||||
-rw-r--r-- | ui/src/components/main.tsx | 62 | ||||
-rw-r--r-- | ui/src/components/modlog.tsx | 14 | ||||
-rw-r--r-- | ui/src/components/navbar.tsx | 39 | ||||
-rw-r--r-- | ui/src/components/password_change.tsx | 13 | ||||
-rw-r--r-- | ui/src/components/post-form.tsx | 51 | ||||
-rw-r--r-- | ui/src/components/post.tsx | 179 | ||||
-rw-r--r-- | ui/src/components/private-message-form.tsx | 29 | ||||
-rw-r--r-- | ui/src/components/search.tsx | 47 | ||||
-rw-r--r-- | ui/src/components/setup.tsx | 24 | ||||
-rw-r--r-- | ui/src/components/user.tsx | 111 | ||||
-rw-r--r-- | ui/src/interfaces.ts | 64 | ||||
-rw-r--r-- | ui/src/utils.ts | 9 |
18 files changed, 482 insertions, 455 deletions
diff --git a/ui/src/components/comment-form.tsx b/ui/src/components/comment-form.tsx index 6fbdc5de..b8ea0a5a 100644 --- a/ui/src/components/comment-form.tsx +++ b/ui/src/components/comment-form.tsx @@ -10,9 +10,9 @@ import { } from '../interfaces'; import { Subscription } from 'rxjs'; import { + wsJsonToRes, capitalizeFirstLetter, mentionDropdownFetchLimit, - msgOp, mdToHtml, randomStr, markdownHelpUrl, @@ -312,10 +312,10 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> { this.userSub = WebSocketService.Instance.subject.subscribe( msg => { - let op: UserOperation = msgOp(msg); - if (op == UserOperation.Search) { - let res: SearchResponse = msg; - let users = res.users.map(u => { + let res = wsJsonToRes(msg); + if (res.op == UserOperation.Search) { + let data = res.data as SearchResponse; + let users = data.users.map(u => { return { key: u.name }; }); cb(users); @@ -344,10 +344,10 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> { this.communitySub = WebSocketService.Instance.subject.subscribe( msg => { - let op: UserOperation = msgOp(msg); - if (op == UserOperation.Search) { - let res: SearchResponse = msg; - let communities = res.communities.map(u => { + let res = wsJsonToRes(msg); + if (res.op == UserOperation.Search) { + let data = res.data as SearchResponse; + let communities = data.communities.map(u => { return { key: u.name }; }); cb(communities); diff --git a/ui/src/components/communities.tsx b/ui/src/components/communities.tsx index 129051fb..867cfd81 100644 --- a/ui/src/components/communities.tsx +++ b/ui/src/components/communities.tsx @@ -10,9 +10,10 @@ import { FollowCommunityForm, ListCommunitiesForm, SortType, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService } from '../services'; -import { msgOp, toast } from '../utils'; +import { wsJsonToRes, toast } from '../utils'; import { i18n } from '../i18next'; import { T } from 'inferno-i18next'; @@ -231,15 +232,15 @@ export class Communities extends Component<any, CommunitiesState> { WebSocketService.Instance.listCommunities(listCommunitiesForm); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); - let op: UserOperation = msgOp(msg); - if (msg.error) { + let res = wsJsonToRes(msg); + if (res.error) { toast(i18n.t(msg.error), 'danger'); return; - } else if (op == UserOperation.ListCommunities) { - let res: ListCommunitiesResponse = msg; - this.state.communities = res.communities; + } else if (res.op == UserOperation.ListCommunities) { + let data = res.data as ListCommunitiesResponse; + this.state.communities = data.communities; this.state.communities.sort( (a, b) => b.number_of_subscribers - a.number_of_subscribers ); @@ -248,11 +249,11 @@ export class Communities extends Component<any, CommunitiesState> { this.setState(this.state); let table = document.querySelector('#community_table'); Sortable.initTable(table); - } else if (op == UserOperation.FollowCommunity) { - let res: CommunityResponse = msg; - let found = this.state.communities.find(c => c.id == res.community.id); - found.subscribed = res.community.subscribed; - found.number_of_subscribers = res.community.number_of_subscribers; + } else if (res.op == UserOperation.FollowCommunity) { + let data = res.data as CommunityResponse; + let found = this.state.communities.find(c => c.id == data.community.id); + found.subscribed = data.community.subscribed; + found.number_of_subscribers = data.community.number_of_subscribers; this.setState(this.state); } } diff --git a/ui/src/components/community-form.tsx b/ui/src/components/community-form.tsx index ec58b010..4dc7bfcb 100644 --- a/ui/src/components/community-form.tsx +++ b/ui/src/components/community-form.tsx @@ -8,9 +8,10 @@ import { ListCategoriesResponse, CommunityResponse, GetSiteResponse, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService } from '../services'; -import { msgOp, capitalizeFirstLetter, toast } from '../utils'; +import { wsJsonToRes, capitalizeFirstLetter, toast } from '../utils'; import autosize from 'autosize'; import { i18n } from '../i18next'; import { T } from 'inferno-i18next'; @@ -239,34 +240,34 @@ export class CommunityForm extends Component< i.props.onCancel(); } - parseMessage(msg: any) { - let op: UserOperation = msgOp(msg); + parseMessage(msg: WebSocketJsonResponse) { + let res = wsJsonToRes(msg); console.log(msg); - if (msg.error) { + if (res.error) { toast(i18n.t(msg.error), 'danger'); this.state.loading = false; this.setState(this.state); return; - } else if (op == UserOperation.ListCategories) { - let res: ListCategoriesResponse = msg; - this.state.categories = res.categories; + } else if (res.op == UserOperation.ListCategories) { + let data = res.data as ListCategoriesResponse; + this.state.categories = data.categories; if (!this.props.community) { - this.state.communityForm.category_id = res.categories[0].id; + this.state.communityForm.category_id = data.categories[0].id; } this.setState(this.state); - } else if (op == UserOperation.CreateCommunity) { - let res: CommunityResponse = msg; + } else if (res.op == UserOperation.CreateCommunity) { + let data = res.data as CommunityResponse; this.state.loading = false; - this.props.onCreate(res.community); + this.props.onCreate(data.community); } - // TODO is ths necessary - else if (op == UserOperation.EditCommunity) { - let res: CommunityResponse = msg; + // TODO is this necessary + else if (res.op == UserOperation.EditCommunity) { + let data = res.data as CommunityResponse; this.state.loading = false; - this.props.onEdit(res.community); - } else if (op == UserOperation.GetSite) { - let res: GetSiteResponse = msg; - this.state.enable_nsfw = res.site.enable_nsfw; + this.props.onEdit(data.community); + } else if (res.op == UserOperation.GetSite) { + let data = res.data as GetSiteResponse; + this.state.enable_nsfw = data.site.enable_nsfw; this.setState(this.state); } } diff --git a/ui/src/components/community.tsx b/ui/src/components/community.tsx index dfd4d6b3..9d02dd86 100644 --- a/ui/src/components/community.tsx +++ b/ui/src/components/community.tsx @@ -14,13 +14,14 @@ import { ListingType, GetPostsResponse, CreatePostLikeResponse, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { PostListings } from './post-listings'; import { SortSelect } from './sort-select'; import { Sidebar } from './sidebar'; import { - msgOp, + wsJsonToRes, routeSortTypeToEnum, fetchLimit, postRefetchSeconds, @@ -254,43 +255,43 @@ export class Community extends Component<any, State> { WebSocketService.Instance.getPosts(getPostsForm); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); - let op: UserOperation = msgOp(msg); - if (msg.error) { + let res = wsJsonToRes(msg); + if (res.error) { toast(i18n.t(msg.error), 'danger'); this.context.router.history.push('/'); return; - } else if (op == UserOperation.GetCommunity) { - let res: GetCommunityResponse = msg; - this.state.community = res.community; - this.state.moderators = res.moderators; - this.state.admins = res.admins; + } else if (res.op == UserOperation.GetCommunity) { + let data = res.data as GetCommunityResponse; + this.state.community = data.community; + this.state.moderators = data.moderators; + this.state.admins = data.admins; document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`; this.setState(this.state); this.keepFetchingPosts(); - } else if (op == UserOperation.EditCommunity) { - let res: CommunityResponse = msg; - this.state.community = res.community; + } else if (res.op == UserOperation.EditCommunity) { + let data = res.data as CommunityResponse; + this.state.community = data.community; this.setState(this.state); - } else if (op == UserOperation.FollowCommunity) { - let res: CommunityResponse = msg; - this.state.community.subscribed = res.community.subscribed; + } else if (res.op == UserOperation.FollowCommunity) { + let data = res.data as CommunityResponse; + this.state.community.subscribed = data.community.subscribed; this.state.community.number_of_subscribers = - res.community.number_of_subscribers; + data.community.number_of_subscribers; this.setState(this.state); - } else if (op == UserOperation.GetPosts) { - let res: GetPostsResponse = msg; - this.state.posts = res.posts; + } else if (res.op == UserOperation.GetPosts) { + let data = res.data as GetPostsResponse; + this.state.posts = data.posts; this.state.loading = false; this.setState(this.state); - } else if (op == UserOperation.CreatePostLike) { - let res: CreatePostLikeResponse = msg; - let found = this.state.posts.find(c => c.id == res.post.id); - found.my_vote = res.post.my_vote; - found.score = res.post.score; - found.upvotes = res.post.upvotes; - found.downvotes = res.post.downvotes; + } else if (res.op == UserOperation.CreatePostLike) { + let data = res.data as CreatePostLikeResponse; + let found = this.state.posts.find(c => c.id == data.post.id); + found.my_vote = data.post.my_vote; + found.score = data.post.score; + found.upvotes = data.post.upvotes; + found.downvotes = data.post.downvotes; this.setState(this.state); } } diff --git a/ui/src/components/inbox.tsx b/ui/src/components/inbox.tsx index bf090179..5c3ff6d2 100644 --- a/ui/src/components/inbox.tsx +++ b/ui/src/components/inbox.tsx @@ -12,13 +12,14 @@ import { GetUserMentionsResponse, UserMentionResponse, CommentResponse, + WebSocketJsonResponse, PrivateMessage as PrivateMessageI, GetPrivateMessagesForm, PrivateMessagesResponse, PrivateMessageResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; -import { msgOp, fetchLimit, isCommentType, toast } from '../utils'; +import { wsJsonToRes, fetchLimit, isCommentType, toast } from '../utils'; import { CommentNodes } from './comment-nodes'; import { PrivateMessage } from './private-message'; import { SortSelect } from './sort-select'; @@ -320,122 +321,122 @@ export class Inbox extends Component<any, InboxState> { WebSocketService.Instance.markAllAsRead(); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); - let op: UserOperation = msgOp(msg); - if (msg.error) { + let res = wsJsonToRes(msg); + if (res.error) { toast(i18n.t(msg.error), 'danger'); 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.GetPrivateMessages) { - let res: PrivateMessagesResponse = msg; - this.state.messages = res.messages; + } else if (res.op == UserOperation.GetPrivateMessages) { + let data = res.data as PrivateMessagesResponse; + this.state.messages = data.messages; this.sendUnreadCount(); window.scrollTo(0, 0); this.setState(this.state); - } else if (op == UserOperation.EditPrivateMessage) { - let res: PrivateMessageResponse = msg; + } else if (res.op == UserOperation.EditPrivateMessage) { + let data = res.data as PrivateMessageResponse; let found: PrivateMessageI = this.state.messages.find( - m => m.id === res.message.id + m => m.id === data.message.id ); - found.content = res.message.content; - found.updated = res.message.updated; - found.deleted = res.message.deleted; + found.content = data.message.content; + found.updated = data.message.updated; + found.deleted = data.message.deleted; // If youre in the unread view, just remove it from the list - if (this.state.unreadOrAll == UnreadOrAll.Unread && res.message.read) { + if (this.state.unreadOrAll == UnreadOrAll.Unread && data.message.read) { this.state.messages = this.state.messages.filter( - r => r.id !== res.message.id + r => r.id !== data.message.id ); } else { - let found = this.state.messages.find(c => c.id == res.message.id); - found.read = res.message.read; + let found = this.state.messages.find(c => c.id == data.message.id); + found.read = data.message.read; } 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 = []; this.state.messages = []; this.sendUnreadCount(); 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; toast(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); } } diff --git a/ui/src/components/login.tsx b/ui/src/components/login.tsx index 0c8350aa..ac60ba74 100644 --- a/ui/src/components/login.tsx +++ b/ui/src/components/login.tsx @@ -8,9 +8,10 @@ import { UserOperation, PasswordResetForm, GetSiteResponse, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; -import { msgOp, validEmail, toast } from '../utils'; +import { wsJsonToRes, validEmail, toast } from '../utils'; import { i18n } from '../i18next'; import { T } from 'inferno-i18next'; @@ -292,32 +293,32 @@ export class Login extends Component<any, State> { WebSocketService.Instance.passwordReset(resetForm); } - parseMessage(msg: any) { - let op: UserOperation = msgOp(msg); - if (msg.error) { + parseMessage(msg: WebSocketJsonResponse) { + let res = wsJsonToRes(msg); + if (res.error) { toast(i18n.t(msg.error), 'danger'); this.state = this.emptyState; this.setState(this.state); return; } else { - if (op == UserOperation.Login) { + if (res.op == UserOperation.Login) { + let data = res.data as LoginResponse; this.state = this.emptyState; this.setState(this.state); - let res: LoginResponse = msg; - UserService.Instance.login(res); + UserService.Instance.login(data); toast(i18n.t('logged_in')); this.props.history.push('/'); - } else if (op == UserOperation.Register) { + } else if (res.op == UserOperation.Register) { + let data = res.data as LoginResponse; this.state = this.emptyState; this.setState(this.state); - let res: LoginResponse = msg; - UserService.Instance.login(res); + UserService.Instance.login(data); this.props.history.push('/communities'); - } else if (op == UserOperation.PasswordReset) { + } else if (res.op == UserOperation.PasswordReset) { toast(i18n.t('reset_password_mail_sent')); - } else if (op == UserOperation.GetSite) { - let res: GetSiteResponse = msg; - this.state.enable_nsfw = res.site.enable_nsfw; + } else if (res.op == UserOperation.GetSite) { + let data = res.data as GetSiteResponse; + this.state.enable_nsfw = data.site.enable_nsfw; this.setState(this.state); document.title = `${i18n.t('login')} - ${ WebSocketService.Instance.site.name diff --git a/ui/src/components/main.tsx b/ui/src/components/main.tsx index b244ce66..9f16edb5 100644 --- a/ui/src/components/main.tsx +++ b/ui/src/components/main.tsx @@ -17,6 +17,7 @@ import { CreatePostLikeResponse, Post, GetPostsForm, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { PostListings } from './post-listings'; @@ -24,7 +25,7 @@ import { SortSelect } from './sort-select'; import { ListingTypeSelect } from './listing-type-select'; import { SiteForm } from './site-form'; import { - msgOp, + wsJsonToRes, repoUrl, mdToHtml, fetchLimit, @@ -57,7 +58,6 @@ export class Main extends Component<any, MainState> { subscribedCommunities: [], trendingCommunities: [], site: { - op: null, site: { id: null, name: null, @@ -563,50 +563,50 @@ export class Main extends Component<any, MainState> { WebSocketService.Instance.getPosts(getPostsForm); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); - let op: UserOperation = msgOp(msg); - if (msg.error) { + let res = wsJsonToRes(msg); + if (res.error) { toast(i18n.t(msg.error), 'danger'); return; - } else if (op == UserOperation.GetFollowedCommunities) { - let res: GetFollowedCommunitiesResponse = msg; - this.state.subscribedCommunities = res.communities; + } else if (res.op == UserOperation.GetFollowedCommunities) { + let data = res.data as GetFollowedCommunitiesResponse; + this.state.subscribedCommunities = data.communities; this.setState(this.state); - } else if (op == UserOperation.ListCommunities) { - let res: ListCommunitiesResponse = msg; - this.state.trendingCommunities = res.communities; + } else if (res.op == UserOperation.ListCommunities) { + let data = res.data as ListCommunitiesResponse; + this.state.trendingCommunities = data.communities; this.setState(this.state); - } else if (op == UserOperation.GetSite) { - let res: GetSiteResponse = msg; + } else if (res.op == UserOperation.GetSite) { + let data = res.data as GetSiteResponse; // This means it hasn't been set up yet - if (!res.site) { + if (!data.site) { this.context.router.history.push('/setup'); } - this.state.site.admins = res.admins; - this.state.site.site = res.site; - this.state.site.banned = res.banned; - this.state.site.online = res.online; + this.state.site.admins = data.admins; + this.state.site.site = data.site; + this.state.site.banned = data.banned; + this.state.site.online = data.online; this.setState(this.state); document.title = `${WebSocketService.Instance.site.name}`; - } else if (op == UserOperation.EditSite) { - let res: SiteResponse = msg; - this.state.site.site = res.site; + } else if (res.op == UserOperation.EditSite) { + let data = res.data as SiteResponse; + this.state.site.site = data.site; this.state.showEditSite = false; this.setState(this.state); - } else if (op == UserOperation.GetPosts) { - let res: GetPostsResponse = msg; - this.state.posts = res.posts; + } else if (res.op == UserOperation.GetPosts) { + let data = res.data as GetPostsResponse; + this.state.posts = data.posts; this.state.loading = false; this.setState(this.state); - } else if (op == UserOperation.CreatePostLike) { - let res: CreatePostLikeResponse = msg; - let found = this.state.posts.find(c => c.id == res.post.id); - found.my_vote = res.post.my_vote; - found.score = res.post.score; - found.upvotes = res.post.upvotes; - found.downvotes = res.post.downvotes; + } else if (res.op == UserOperation.CreatePostLike) { + let data = res.data as CreatePostLikeResponse; + let found = this.state.posts.find(c => c.id == data.post.id); + found.my_vote = data.post.my_vote; + found.score = data.post.score; + found.upvotes = data.post.upvotes; + found.downvotes = data.post.downvotes; this.setState(this.state); } } diff --git a/ui/src/components/modlog.tsx b/ui/src/components/modlog.tsx index 6c35bce9..dd651092 100644 --- a/ui/src/components/modlog.tsx +++ b/ui/src/components/modlog.tsx @@ -17,7 +17,7 @@ import { ModAdd, } from '../interfaces'; import { WebSocketService } from '../services'; -import { msgOp, addTypeInfo, fetchLimit, toast } from '../utils'; +import { wsJsonToRes, addTypeInfo, fetchLimit, toast } from '../utils'; import { MomentTime } from './moment-time'; import moment from 'moment'; import { i18n } from '../i18next'; @@ -422,17 +422,17 @@ export class Modlog extends Component<any, ModlogState> { WebSocketService.Instance.getModlog(modlogForm); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); - let op: UserOperation = msgOp(msg); - if (msg.error) { + let res = wsJsonToRes(msg); + if (res.error) { toast(i18n.t(msg.error), 'danger'); return; - } else if (op == UserOperation.GetModlog) { - let res: GetModlogResponse = msg; + } else if (res.op == UserOperation.GetModlog) { + let data = res.data as GetModlogResponse; this.state.loading = false; window.scrollTo(0, 0); - this.setCombined(res); + this.setCombined(data); } } } diff --git a/ui/src/components/navbar.tsx b/ui/src/components/navbar.tsx index 81124f77..849822af 100644 --- a/ui/src/components/navbar.tsx +++ b/ui/src/components/navbar.tsx @@ -15,9 +15,10 @@ import { GetSiteResponse, Comment, PrivateMessage, + WebSocketJsonResponse, } from '../interfaces'; import { - msgOp, + wsJsonToRes, pictshareAvatarThumbnail, showAvatars, fetchLimit, @@ -199,17 +200,17 @@ export class Navbar extends Component<any, NavbarState> { i.setState(i.state); } - parseMessage(msg: any) { - let op: UserOperation = msgOp(msg); - if (msg.error) { - if (msg.error == 'not_logged_in') { + parseMessage(msg: WebSocketJsonResponse) { + let res = wsJsonToRes(msg); + if (res.error) { + if (res.error == 'not_logged_in') { UserService.Instance.logout(); location.reload(); } return; - } else if (op == UserOperation.GetReplies) { - let res: GetRepliesResponse = msg; - let unreadReplies = res.replies.filter(r => !r.read); + } else if (res.op == UserOperation.GetReplies) { + let data = res.data as GetRepliesResponse; + let unreadReplies = data.replies.filter(r => !r.read); if ( unreadReplies.length > 0 && this.state.fetchCount > 1 && @@ -221,9 +222,9 @@ export class Navbar extends Component<any, NavbarState> { this.state.replies = unreadReplies; this.setState(this.state); this.sendUnreadCount(); - } else if (op == UserOperation.GetUserMentions) { - let res: GetUserMentionsResponse = msg; - let unreadMentions = res.mentions.filter(r => !r.read); + } else if (res.op == UserOperation.GetUserMentions) { + let data = res.data as GetUserMentionsResponse; + let unreadMentions = data.mentions.filter(r => !r.read); if ( unreadMentions.length > 0 && this.state.fetchCount > 1 && @@ -235,9 +236,9 @@ export class Navbar extends Component<any, NavbarState> { this.state.mentions = unreadMentions; this.setState(this.state); this.sendUnreadCount(); - } else if (op == UserOperation.GetPrivateMessages) { - let res: PrivateMessagesResponse = msg; - let unreadMessages = res.messages.filter(r => !r.read); + } else if (res.op == UserOperation.GetPrivateMessages) { + let data = res.data as PrivateMessagesResponse; + let unreadMessages = data.messages.filter(r => !r.read); if ( unreadMessages.length > 0 && this.state.fetchCount > 1 && @@ -249,12 +250,12 @@ export class Navbar extends Component<any, NavbarState> { this.state.messages = unreadMessages; this.setState(this.state); this.sendUnreadCount(); - } else if (op == UserOperation.GetSite) { - let res: GetSiteResponse = msg; + } else if (res.op == UserOperation.GetSite) { + let data = res.data as GetSiteResponse; - if (res.site) { - this.state.siteName = res.site.name; - WebSocketService.Instance.site = res.site; + if (data.site) { + this.state.siteName = data.site.name; + WebSocketService.Instance.site = data.site; this.setState(this.state); } } diff --git a/ui/src/components/password_change.tsx b/ui/src/components/password_change.tsx index 76b4fb01..10b6867c 100644 --- a/ui/src/components/password_change.tsx +++ b/ui/src/components/password_change.tsx @@ -5,9 +5,10 @@ import { UserOperation, LoginResponse, PasswordChangeForm, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; -import { msgOp, capitalizeFirstLetter, toast } from '../utils'; +import { wsJsonToRes, capitalizeFirstLetter, toast } from '../utils'; import { i18n } from '../i18next'; import { T } from 'inferno-i18next'; @@ -133,19 +134,19 @@ export class PasswordChange extends Component<any, State> { WebSocketService.Instance.passwordChange(i.state.passwordChangeForm); } - parseMessage(msg: any) { - let op: UserOperation = msgOp(msg); + parseMessage(msg: WebSocketJsonResponse) { + let res = wsJsonToRes(msg); if (msg.error) { toast(i18n.t(msg.error), 'danger'); this.state.loading = false; this.setState(this.state); return; } else { - if (op == UserOperation.PasswordChange) { + if (res.op == UserOperation.PasswordChange) { + let data = res.data as LoginResponse; this.state = this.emptyState; this.setState(this.state); - let res: LoginResponse = msg; - UserService.Instance.login(res); + UserService.Instance.login(data); this.props.history.push('/'); } } diff --git a/ui/src/components/post-form.tsx b/ui/src/components/post-form.tsx index 97a44094..44061774 100644 --- a/ui/src/components/post-form.tsx +++ b/ui/src/components/post-form.tsx @@ -16,10 +16,11 @@ import { SearchType, |