From c5eecd055e79510421aebe26e5d4f173a5916a3e Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sun, 19 Jan 2020 00:38:45 -0500 Subject: [PATCH] Strongly typing WebsocketJsonResponse. Forgot comment-form.tsx --- ui/src/components/comment-form.tsx | 18 +++++++++--------- ui/src/components/communities.tsx | 3 ++- ui/src/components/community-form.tsx | 2 +- ui/src/components/community.tsx | 3 ++- ui/src/components/inbox.tsx | 3 ++- ui/src/components/login.tsx | 3 ++- ui/src/components/main.tsx | 3 ++- ui/src/components/modlog.tsx | 2 +- ui/src/components/moment-time.tsx | 2 +- ui/src/components/navbar.tsx | 3 ++- ui/src/components/password_change.tsx | 3 ++- ui/src/components/post-form.tsx | 3 ++- ui/src/components/post.tsx | 3 ++- ui/src/components/search.tsx | 3 ++- ui/src/components/setup.tsx | 9 +++++++-- ui/src/components/user.tsx | 3 ++- ui/src/interfaces.ts | 6 ++++++ ui/src/utils.ts | 3 ++- 18 files changed, 49 insertions(+), 26 deletions(-) diff --git a/ui/src/components/comment-form.tsx b/ui/src/components/comment-form.tsx index f581689..dddcbe7 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, @@ -311,10 +311,10 @@ export class CommentForm extends Component { 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); @@ -343,10 +343,10 @@ export class CommentForm extends Component { 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 683d495..ebcbc34 100644 --- a/ui/src/components/communities.tsx +++ b/ui/src/components/communities.tsx @@ -10,6 +10,7 @@ import { FollowCommunityForm, ListCommunitiesForm, SortType, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService } from '../services'; import { wsJsonToRes } from '../utils'; @@ -227,7 +228,7 @@ export class Communities extends Component { WebSocketService.Instance.listCommunities(listCommunitiesForm); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); let res = wsJsonToRes(msg); if (res.error) { diff --git a/ui/src/components/community-form.tsx b/ui/src/components/community-form.tsx index 93f4d64..14cd8e4 100644 --- a/ui/src/components/community-form.tsx +++ b/ui/src/components/community-form.tsx @@ -239,7 +239,7 @@ export class CommunityForm extends Component< i.props.onCancel(); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { let res = wsJsonToRes(msg); console.log(msg); if (res.error) { diff --git a/ui/src/components/community.tsx b/ui/src/components/community.tsx index a48f468..357fe26 100644 --- a/ui/src/components/community.tsx +++ b/ui/src/components/community.tsx @@ -14,6 +14,7 @@ import { ListingType, GetPostsResponse, CreatePostLikeResponse, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { PostListings } from './post-listings'; @@ -251,7 +252,7 @@ export class Community extends Component { WebSocketService.Instance.getPosts(getPostsForm); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); let res = wsJsonToRes(msg); if (res.error) { diff --git a/ui/src/components/inbox.tsx b/ui/src/components/inbox.tsx index a46017e..4aa9ceb 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) { diff --git a/ui/src/components/login.tsx b/ui/src/components/login.tsx index 42d5e05..29482f4 100644 --- a/ui/src/components/login.tsx +++ b/ui/src/components/login.tsx @@ -8,6 +8,7 @@ import { UserOperation, PasswordResetForm, GetSiteResponse, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { wsJsonToRes, validEmail } from '../utils'; @@ -292,7 +293,7 @@ export class Login extends Component { WebSocketService.Instance.passwordReset(resetForm); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { let res = wsJsonToRes(msg); if (res.error) { alert(i18n.t(res.error)); diff --git a/ui/src/components/main.tsx b/ui/src/components/main.tsx index 6351905..1ccebc8 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'; @@ -561,7 +562,7 @@ export class Main extends Component { WebSocketService.Instance.getPosts(getPostsForm); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); let res = wsJsonToRes(msg); if (res.error) { diff --git a/ui/src/components/modlog.tsx b/ui/src/components/modlog.tsx index 5a66744..b2011af 100644 --- a/ui/src/components/modlog.tsx +++ b/ui/src/components/modlog.tsx @@ -422,7 +422,7 @@ export class Modlog extends Component { WebSocketService.Instance.getModlog(modlogForm); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); let res = wsJsonToRes(msg); if (res.error) { diff --git a/ui/src/components/moment-time.tsx b/ui/src/components/moment-time.tsx index 6bb4d99..fd2a7ef 100644 --- a/ui/src/components/moment-time.tsx +++ b/ui/src/components/moment-time.tsx @@ -1,5 +1,5 @@ import { Component } from 'inferno'; -import * as moment from 'moment'; +import moment from 'moment'; import { getMomentLanguage } from '../utils'; import { i18n } from '../i18next'; diff --git a/ui/src/components/navbar.tsx b/ui/src/components/navbar.tsx index 50bf19c..fac5421 100644 --- a/ui/src/components/navbar.tsx +++ b/ui/src/components/navbar.tsx @@ -12,6 +12,7 @@ import { SortType, GetSiteResponse, Comment, + WebSocketJsonResponse, } from '../interfaces'; import { wsJsonToRes, @@ -181,7 +182,7 @@ export class Navbar extends Component { i.setState(i.state); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { let res = wsJsonToRes(msg); if (res.error) { if (res.error == 'not_logged_in') { diff --git a/ui/src/components/password_change.tsx b/ui/src/components/password_change.tsx index d25c0da..97f1088 100644 --- a/ui/src/components/password_change.tsx +++ b/ui/src/components/password_change.tsx @@ -5,6 +5,7 @@ import { UserOperation, LoginResponse, PasswordChangeForm, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { wsJsonToRes, capitalizeFirstLetter } from '../utils'; @@ -133,7 +134,7 @@ export class PasswordChange extends Component { WebSocketService.Instance.passwordChange(i.state.passwordChangeForm); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { let res = wsJsonToRes(msg); if (msg.error) { alert(i18n.t(msg.error)); diff --git a/ui/src/components/post-form.tsx b/ui/src/components/post-form.tsx index a254150..454a569 100644 --- a/ui/src/components/post-form.tsx +++ b/ui/src/components/post-form.tsx @@ -16,6 +16,7 @@ import { SearchType, SearchResponse, GetSiteResponse, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { @@ -457,7 +458,7 @@ export class PostForm extends Component { }); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { let res = wsJsonToRes(msg); if (res.error) { alert(i18n.t(res.error)); diff --git a/ui/src/components/post.tsx b/ui/src/components/post.tsx index 866894a..1e334b1 100644 --- a/ui/src/components/post.tsx +++ b/ui/src/components/post.tsx @@ -26,6 +26,7 @@ import { SearchResponse, GetSiteResponse, GetCommunityResponse, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { wsJsonToRes, hotRank } from '../utils'; @@ -341,7 +342,7 @@ export class Post extends Component { ); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); let res = wsJsonToRes(msg); if (res.error) { diff --git a/ui/src/components/search.tsx b/ui/src/components/search.tsx index f310b80..ae0f8db 100644 --- a/ui/src/components/search.tsx +++ b/ui/src/components/search.tsx @@ -12,6 +12,7 @@ import { SearchForm, SearchResponse, SearchType, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService } from '../services'; import { @@ -460,7 +461,7 @@ export class Search extends Component { ); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); let res = wsJsonToRes(msg); if (res.error) { diff --git a/ui/src/components/setup.tsx b/ui/src/components/setup.tsx index 5b08111..c4c9dc6 100644 --- a/ui/src/components/setup.tsx +++ b/ui/src/components/setup.tsx @@ -1,7 +1,12 @@ import { Component, linkEvent } from 'inferno'; import { Subscription } from 'rxjs'; import { retryWhen, delay, take } from 'rxjs/operators'; -import { RegisterForm, LoginResponse, UserOperation } from '../interfaces'; +import { + RegisterForm, + LoginResponse, + UserOperation, + WebSocketJsonResponse, +} from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { wsJsonToRes } from '../utils'; import { SiteForm } from './site-form'; @@ -181,7 +186,7 @@ export class Setup extends Component { i.setState(i.state); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { let res = wsJsonToRes(msg); if (res.error) { alert(i18n.t(res.error)); diff --git a/ui/src/components/user.tsx b/ui/src/components/user.tsx index 8987fdf..606d85a 100644 --- a/ui/src/components/user.tsx +++ b/ui/src/components/user.tsx @@ -18,6 +18,7 @@ import { BanUserResponse, AddAdminResponse, DeleteAccountForm, + WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { @@ -968,7 +969,7 @@ export class User extends Component { WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm); } - parseMessage(msg: any) { + parseMessage(msg: WebSocketJsonResponse) { console.log(msg); let res = wsJsonToRes(msg); if (res.error) { diff --git a/ui/src/interfaces.ts b/ui/src/interfaces.ts index 3af5be0..63c6085 100644 --- a/ui/src/interfaces.ts +++ b/ui/src/interfaces.ts @@ -726,3 +726,9 @@ export interface WebSocketResponse { data: ResponseType; error?: string; } + +export interface WebSocketJsonResponse { + op: string; + data: ResponseType; + error?: string; +} diff --git a/ui/src/utils.ts b/ui/src/utils.ts index 363de66..90ef689 100644 --- a/ui/src/utils.ts +++ b/ui/src/utils.ts @@ -16,6 +16,7 @@ import { ListingType, SearchType, WebSocketResponse, + WebSocketJsonResponse, } from './interfaces'; import { UserService } from './services/UserService'; import markdown_it from 'markdown-it'; @@ -39,7 +40,7 @@ export function randomStr() { .substr(2, 10); } -export function wsJsonToRes(msg: any): WebSocketResponse { +export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse { let opStr: string = msg.op; return { op: UserOperation[opStr],