From a044718066623f19d51196d444bd199c1231cebe Mon Sep 17 00:00:00 2001
From: Dessalines <tyhou13@gmx.com>
Date: Sat, 18 Jan 2020 23:54:10 -0500
Subject: First pass at fixing UI to work with new websocketresponses.

---
 ui/src/components/login.tsx | 37 +++++++++++++++----------------------
 1 file changed, 15 insertions(+), 22 deletions(-)

(limited to 'ui/src/components/login.tsx')

diff --git a/ui/src/components/login.tsx b/ui/src/components/login.tsx
index 53b7a22f..42d5e05f 100644
--- a/ui/src/components/login.tsx
+++ b/ui/src/components/login.tsx
@@ -10,7 +10,7 @@ import {
   GetSiteResponse,
 } from '../interfaces';
 import { WebSocketService, UserService } from '../services';
-import { msgOp, validEmail } from '../utils';
+import { wsJsonToRes, validEmail } from '../utils';
 import { i18n } from '../i18next';
 import { T } from 'inferno-i18next';
 
@@ -48,14 +48,7 @@ export class Login extends Component<any, State> {
     this.state = this.emptyState;
 
     this.subscription = WebSocketService.Instance.subject
-      .pipe(
-        retryWhen(errors =>
-          errors.pipe(
-            delay(3000),
-            take(10)
-          )
-        )
-      )
+      .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
       .subscribe(
         msg => this.parseMessage(msg),
         err => console.error(err),
@@ -300,30 +293,30 @@ export class Login extends Component<any, State> {
   }
 
   parseMessage(msg: any) {
-    let op: UserOperation = msgOp(msg);
-    if (msg.error) {
-      alert(i18n.t(msg.error));
+    let res = wsJsonToRes(msg);
+    if (res.error) {
+      alert(i18n.t(res.error));
       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);
         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) {
         alert(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
-- 
cgit v1.2.3

