From: Dessalines Date: Sun, 19 Jan 2020 05:38:45 +0000 (-0500) Subject: Strongly typing WebsocketJsonResponse. Forgot comment-form.tsx X-Git-Url: http://these/git/%7B%60https:/%7B%60css/themes/%22https:/nerdica.net/%7Bs.link%7D?a=commitdiff_plain;h=dcd60228ed753a146bcb56bb5bf51166835acfb7;p=lemmy.git Strongly typing WebsocketJsonResponse. Forgot comment-form.tsx --- diff --git a/ui/src/components/comment-form.tsx b/ui/src/components/comment-form.tsx index f5816899..dddcbe72 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 683d495e..ebcbc345 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 93f4d649..14cd8e4f 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 a48f468b..357fe260 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 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) { diff --git a/ui/src/components/login.tsx b/ui/src/components/login.tsx index 42d5e05f..29482f45 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 63519056..1ccebc80 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 5a66744b..b2011af5 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 6bb4d99c..fd2a7efa 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 50bf19c7..fac54212 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 d25c0daf..97f10888 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 a2541504..454a569f 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 866894a9..1e334b1d 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 f310b80c..ae0f8dbc 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 5b081111..c4c9dc63 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 8987fdfc..606d85ab 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 3af5be02..63c60856 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 363de663..90ef6899 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],