Strongly typing WebsocketJsonResponse. Forgot comment-form.tsx

This commit is contained in:
Dessalines 2020-01-19 00:38:45 -05:00
parent 0c5eb47135
commit c5eecd055e
18 changed files with 49 additions and 26 deletions

View file

@ -10,9 +10,9 @@ import {
} from '../interfaces'; } from '../interfaces';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { import {
wsJsonToRes,
capitalizeFirstLetter, capitalizeFirstLetter,
mentionDropdownFetchLimit, mentionDropdownFetchLimit,
msgOp,
mdToHtml, mdToHtml,
randomStr, randomStr,
markdownHelpUrl, markdownHelpUrl,
@ -311,10 +311,10 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
this.userSub = WebSocketService.Instance.subject.subscribe( this.userSub = WebSocketService.Instance.subject.subscribe(
msg => { msg => {
let op: UserOperation = msgOp(msg); let res = wsJsonToRes(msg);
if (op == UserOperation.Search) { if (res.op == UserOperation.Search) {
let res: SearchResponse = msg; let data = res.data as SearchResponse;
let users = res.users.map(u => { let users = data.users.map(u => {
return { key: u.name }; return { key: u.name };
}); });
cb(users); cb(users);
@ -343,10 +343,10 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
this.communitySub = WebSocketService.Instance.subject.subscribe( this.communitySub = WebSocketService.Instance.subject.subscribe(
msg => { msg => {
let op: UserOperation = msgOp(msg); let res = wsJsonToRes(msg);
if (op == UserOperation.Search) { if (res.op == UserOperation.Search) {
let res: SearchResponse = msg; let data = res.data as SearchResponse;
let communities = res.communities.map(u => { let communities = data.communities.map(u => {
return { key: u.name }; return { key: u.name };
}); });
cb(communities); cb(communities);

View file

@ -10,6 +10,7 @@ import {
FollowCommunityForm, FollowCommunityForm,
ListCommunitiesForm, ListCommunitiesForm,
SortType, SortType,
WebSocketJsonResponse,
} from '../interfaces'; } from '../interfaces';
import { WebSocketService } from '../services'; import { WebSocketService } from '../services';
import { wsJsonToRes } from '../utils'; import { wsJsonToRes } from '../utils';
@ -227,7 +228,7 @@ export class Communities extends Component<any, CommunitiesState> {
WebSocketService.Instance.listCommunities(listCommunitiesForm); WebSocketService.Instance.listCommunities(listCommunitiesForm);
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
console.log(msg); console.log(msg);
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (res.error) { if (res.error) {

View file

@ -239,7 +239,7 @@ export class CommunityForm extends Component<
i.props.onCancel(); i.props.onCancel();
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
console.log(msg); console.log(msg);
if (res.error) { if (res.error) {

View file

@ -14,6 +14,7 @@ import {
ListingType, ListingType,
GetPostsResponse, GetPostsResponse,
CreatePostLikeResponse, CreatePostLikeResponse,
WebSocketJsonResponse,
} from '../interfaces'; } from '../interfaces';
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { PostListings } from './post-listings'; import { PostListings } from './post-listings';
@ -251,7 +252,7 @@ export class Community extends Component<any, State> {
WebSocketService.Instance.getPosts(getPostsForm); WebSocketService.Instance.getPosts(getPostsForm);
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
console.log(msg); console.log(msg);
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (res.error) { if (res.error) {

View file

@ -12,6 +12,7 @@ import {
GetUserMentionsResponse, GetUserMentionsResponse,
UserMentionResponse, UserMentionResponse,
CommentResponse, CommentResponse,
WebSocketJsonResponse,
} from '../interfaces'; } from '../interfaces';
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { wsJsonToRes, fetchLimit } from '../utils'; import { wsJsonToRes, fetchLimit } from '../utils';
@ -296,7 +297,7 @@ export class Inbox extends Component<any, InboxState> {
WebSocketService.Instance.markAllAsRead(); WebSocketService.Instance.markAllAsRead();
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
console.log(msg); console.log(msg);
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (res.error) { if (res.error) {

View file

@ -8,6 +8,7 @@ import {
UserOperation, UserOperation,
PasswordResetForm, PasswordResetForm,
GetSiteResponse, GetSiteResponse,
WebSocketJsonResponse,
} from '../interfaces'; } from '../interfaces';
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { wsJsonToRes, validEmail } from '../utils'; import { wsJsonToRes, validEmail } from '../utils';
@ -292,7 +293,7 @@ export class Login extends Component<any, State> {
WebSocketService.Instance.passwordReset(resetForm); WebSocketService.Instance.passwordReset(resetForm);
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (res.error) { if (res.error) {
alert(i18n.t(res.error)); alert(i18n.t(res.error));

View file

@ -17,6 +17,7 @@ import {
CreatePostLikeResponse, CreatePostLikeResponse,
Post, Post,
GetPostsForm, GetPostsForm,
WebSocketJsonResponse,
} from '../interfaces'; } from '../interfaces';
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { PostListings } from './post-listings'; import { PostListings } from './post-listings';
@ -561,7 +562,7 @@ export class Main extends Component<any, MainState> {
WebSocketService.Instance.getPosts(getPostsForm); WebSocketService.Instance.getPosts(getPostsForm);
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
console.log(msg); console.log(msg);
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (res.error) { if (res.error) {

View file

@ -422,7 +422,7 @@ export class Modlog extends Component<any, ModlogState> {
WebSocketService.Instance.getModlog(modlogForm); WebSocketService.Instance.getModlog(modlogForm);
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
console.log(msg); console.log(msg);
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (res.error) { if (res.error) {

View file

@ -1,5 +1,5 @@
import { Component } from 'inferno'; import { Component } from 'inferno';
import * as moment from 'moment'; import moment from 'moment';
import { getMomentLanguage } from '../utils'; import { getMomentLanguage } from '../utils';
import { i18n } from '../i18next'; import { i18n } from '../i18next';

View file

@ -12,6 +12,7 @@ import {
SortType, SortType,
GetSiteResponse, GetSiteResponse,
Comment, Comment,
WebSocketJsonResponse,
} from '../interfaces'; } from '../interfaces';
import { import {
wsJsonToRes, wsJsonToRes,
@ -181,7 +182,7 @@ export class Navbar extends Component<any, NavbarState> {
i.setState(i.state); i.setState(i.state);
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (res.error) { if (res.error) {
if (res.error == 'not_logged_in') { if (res.error == 'not_logged_in') {

View file

@ -5,6 +5,7 @@ import {
UserOperation, UserOperation,
LoginResponse, LoginResponse,
PasswordChangeForm, PasswordChangeForm,
WebSocketJsonResponse,
} from '../interfaces'; } from '../interfaces';
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { wsJsonToRes, capitalizeFirstLetter } from '../utils'; import { wsJsonToRes, capitalizeFirstLetter } from '../utils';
@ -133,7 +134,7 @@ export class PasswordChange extends Component<any, State> {
WebSocketService.Instance.passwordChange(i.state.passwordChangeForm); WebSocketService.Instance.passwordChange(i.state.passwordChangeForm);
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (msg.error) { if (msg.error) {
alert(i18n.t(msg.error)); alert(i18n.t(msg.error));

View file

@ -16,6 +16,7 @@ import {
SearchType, SearchType,
SearchResponse, SearchResponse,
GetSiteResponse, GetSiteResponse,
WebSocketJsonResponse,
} from '../interfaces'; } from '../interfaces';
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { import {
@ -457,7 +458,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
}); });
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (res.error) { if (res.error) {
alert(i18n.t(res.error)); alert(i18n.t(res.error));

View file

@ -26,6 +26,7 @@ import {
SearchResponse, SearchResponse,
GetSiteResponse, GetSiteResponse,
GetCommunityResponse, GetCommunityResponse,
WebSocketJsonResponse,
} from '../interfaces'; } from '../interfaces';
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { wsJsonToRes, hotRank } from '../utils'; import { wsJsonToRes, hotRank } from '../utils';
@ -341,7 +342,7 @@ export class Post extends Component<any, PostState> {
); );
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
console.log(msg); console.log(msg);
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (res.error) { if (res.error) {

View file

@ -12,6 +12,7 @@ import {
SearchForm, SearchForm,
SearchResponse, SearchResponse,
SearchType, SearchType,
WebSocketJsonResponse,
} from '../interfaces'; } from '../interfaces';
import { WebSocketService } from '../services'; import { WebSocketService } from '../services';
import { import {
@ -460,7 +461,7 @@ export class Search extends Component<any, SearchState> {
); );
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
console.log(msg); console.log(msg);
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (res.error) { if (res.error) {

View file

@ -1,7 +1,12 @@
import { Component, linkEvent } from 'inferno'; import { Component, linkEvent } from 'inferno';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { retryWhen, delay, take } from 'rxjs/operators'; 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 { WebSocketService, UserService } from '../services';
import { wsJsonToRes } from '../utils'; import { wsJsonToRes } from '../utils';
import { SiteForm } from './site-form'; import { SiteForm } from './site-form';
@ -181,7 +186,7 @@ export class Setup extends Component<any, State> {
i.setState(i.state); i.setState(i.state);
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (res.error) { if (res.error) {
alert(i18n.t(res.error)); alert(i18n.t(res.error));

View file

@ -18,6 +18,7 @@ import {
BanUserResponse, BanUserResponse,
AddAdminResponse, AddAdminResponse,
DeleteAccountForm, DeleteAccountForm,
WebSocketJsonResponse,
} from '../interfaces'; } from '../interfaces';
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { import {
@ -968,7 +969,7 @@ export class User extends Component<any, UserState> {
WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm); WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
} }
parseMessage(msg: any) { parseMessage(msg: WebSocketJsonResponse) {
console.log(msg); console.log(msg);
let res = wsJsonToRes(msg); let res = wsJsonToRes(msg);
if (res.error) { if (res.error) {

View file

@ -726,3 +726,9 @@ export interface WebSocketResponse {
data: ResponseType; data: ResponseType;
error?: string; error?: string;
} }
export interface WebSocketJsonResponse {
op: string;
data: ResponseType;
error?: string;
}

3
ui/src/utils.ts vendored
View file

@ -16,6 +16,7 @@ import {
ListingType, ListingType,
SearchType, SearchType,
WebSocketResponse, WebSocketResponse,
WebSocketJsonResponse,
} from './interfaces'; } from './interfaces';
import { UserService } from './services/UserService'; import { UserService } from './services/UserService';
import markdown_it from 'markdown-it'; import markdown_it from 'markdown-it';
@ -39,7 +40,7 @@ export function randomStr() {
.substr(2, 10); .substr(2, 10);
} }
export function wsJsonToRes(msg: any): WebSocketResponse { export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
let opStr: string = msg.op; let opStr: string = msg.op;
return { return {
op: UserOperation[opStr], op: UserOperation[opStr],