Strongly typing WebsocketJsonResponse. Forgot comment-form.tsx

pull/442/head
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';
import { Subscription } from 'rxjs';
import {
wsJsonToRes,
capitalizeFirstLetter,
mentionDropdownFetchLimit,
msgOp,
mdToHtml,
randomStr,
markdownHelpUrl,
@ -311,10 +311,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);
@ -343,10 +343,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);

View File

@ -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<any, CommunitiesState> {
WebSocketService.Instance.listCommunities(listCommunitiesForm);
}
parseMessage(msg: any) {
parseMessage(msg: WebSocketJsonResponse) {
console.log(msg);
let res = wsJsonToRes(msg);
if (res.error) {

View File

@ -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) {

View File

@ -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<any, State> {
WebSocketService.Instance.getPosts(getPostsForm);
}
parseMessage(msg: any) {
parseMessage(msg: WebSocketJsonResponse) {
console.log(msg);
let res = wsJsonToRes(msg);
if (res.error) {

View File

@ -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<any, InboxState> {
WebSocketService.Instance.markAllAsRead();
}
parseMessage(msg: any) {
parseMessage(msg: WebSocketJsonResponse) {
console.log(msg);
let res = wsJsonToRes(msg);
if (res.error) {

View File

@ -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<any, State> {
WebSocketService.Instance.passwordReset(resetForm);
}
parseMessage(msg: any) {
parseMessage(msg: WebSocketJsonResponse) {
let res = wsJsonToRes(msg);
if (res.error) {
alert(i18n.t(res.error));

View File

@ -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<any, MainState> {
WebSocketService.Instance.getPosts(getPostsForm);
}
parseMessage(msg: any) {
parseMessage(msg: WebSocketJsonResponse) {
console.log(msg);
let res = wsJsonToRes(msg);
if (res.error) {

View File

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

View File

@ -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';

View File

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

View File

@ -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<any, State> {
WebSocketService.Instance.passwordChange(i.state.passwordChangeForm);
}
parseMessage(msg: any) {
parseMessage(msg: WebSocketJsonResponse) {
let res = wsJsonToRes(msg);
if (msg.error) {
alert(i18n.t(msg.error));

View File

@ -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<PostFormProps, PostFormState> {
});
}
parseMessage(msg: any) {
parseMessage(msg: WebSocketJsonResponse) {
let res = wsJsonToRes(msg);
if (res.error) {
alert(i18n.t(res.error));

View File

@ -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<any, PostState> {
);
}
parseMessage(msg: any) {
parseMessage(msg: WebSocketJsonResponse) {
console.log(msg);
let res = wsJsonToRes(msg);
if (res.error) {

View File

@ -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<any, SearchState> {
);
}
parseMessage(msg: any) {
parseMessage(msg: WebSocketJsonResponse) {
console.log(msg);
let res = wsJsonToRes(msg);
if (res.error) {

View File

@ -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<any, State> {
i.setState(i.state);
}
parseMessage(msg: any) {
parseMessage(msg: WebSocketJsonResponse) {
let res = wsJsonToRes(msg);
if (res.error) {
alert(i18n.t(res.error));

View File

@ -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<any, UserState> {
WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
}
parseMessage(msg: any) {
parseMessage(msg: WebSocketJsonResponse) {
console.log(msg);
let res = wsJsonToRes(msg);
if (res.error) {

View File

@ -726,3 +726,9 @@ export interface WebSocketResponse {
data: ResponseType;
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,
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],