From 124af1a2cd42003b9e92d0e018f4b6119f3eb5a7 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 18 May 2021 23:43:01 -0400 Subject: [PATCH] login works --- vocodes/src/App.tsx | 8 ++ vocodes/src/modes/login/LoginComponent.tsx | 154 +++++++++++++++++++++ vocodes/src/navigation/TopNav.tsx | 2 +- 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 vocodes/src/modes/login/LoginComponent.tsx diff --git a/vocodes/src/App.tsx b/vocodes/src/App.tsx index ad0a474863..40ca484f4e 100644 --- a/vocodes/src/App.tsx +++ b/vocodes/src/App.tsx @@ -21,6 +21,7 @@ import { VideoQueueStats } from './modes/video/VideoQueueStats'; import { SignupComponent } from './modes/signup/SignupComponent'; import { ApiConfig } from './api/ApiConfig'; import { SessionStateResponse } from './api/SessionState'; +import { LoginComponent } from './modes/login/LoginComponent'; interface Props { // Certan browsers (iPhone) have pitiful support for drawing APIs. Worse yet, @@ -423,6 +424,13 @@ class App extends React.Component { switchModeCallback={this.switchMode} /> break; + case Mode.LOGIN_MODE: + component = + break; } return (
diff --git a/vocodes/src/modes/login/LoginComponent.tsx b/vocodes/src/modes/login/LoginComponent.tsx new file mode 100644 index 0000000000..a0e278ebdd --- /dev/null +++ b/vocodes/src/modes/login/LoginComponent.tsx @@ -0,0 +1,154 @@ +import React from 'react'; +import { ApiConfig } from '../../api/ApiConfig'; +import { Mode } from '../../AppMode'; + +enum FieldTriState { + EMPTY_FALSE, + FALSE, + TRUE, +} + +interface Props { + loggedIn: boolean, + querySessionCallback : () => void, + switchModeCallback: (mode: Mode) => void, +} + +interface State { + usernameOrEmail: string, + usernameOrEmailValid: FieldTriState, + usernameOrEmailInvalidReason: string, + + password: string, + passwordValid: FieldTriState, + passwordInvalidReason: string, +} + +class LoginComponent extends React.Component { + + constructor(props: Props) { + super(props); + this.state = { + usernameOrEmail: "", + usernameOrEmailValid: FieldTriState.EMPTY_FALSE, + usernameOrEmailInvalidReason: "", + + password: "", + passwordValid: FieldTriState.EMPTY_FALSE, + passwordInvalidReason: "", + }; + } + + handleUsernameOrEmailChange = (ev: React.FormEvent) => { + ev.preventDefault(); + + const usernameOrEmail = (ev.target as HTMLInputElement).value; + + this.setState({ + usernameOrEmail: usernameOrEmail, + }) + + return false; + } + + handlePasswordChange = (ev: React.FormEvent) => { + ev.preventDefault(); + + const password = (ev.target as HTMLInputElement).value; + + this.setState({ + password: password, + }) + + return false; + } + + handleFormSubmit = (ev: React.FormEvent) : boolean => { + ev.preventDefault(); + + const api = new ApiConfig(); + const endpointUrl = api.login(); + + const request = { + username_or_email: this.state.usernameOrEmail, + password: this.state.password, + } + + fetch(endpointUrl, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify(request), + }) + .then(res => res.json()) + .then(res => { + console.log('login response', res) + if (res.success) { + console.log('querying new session'); + this.props.querySessionCallback(); + this.props.switchModeCallback(Mode.SPEAK_MODE); + return; + } + }) + .catch(e => { + //this.props.onSpeakErrorCallback(); + }); + + return false; + } + + public render() { + if (this.props.loggedIn) { + return
Invalid view for logged in users.
; + } + + return ( +
+

Sign Up

+ +
+
+ +
+ + + + + + + +
+ {/*

*/} +
+ +
+ +
+ + + + + + + +
+ {/*

*/} +
+ +
+ +
+
+ +
+
+
+
+ ) + } +} + +export { LoginComponent }; \ No newline at end of file diff --git a/vocodes/src/navigation/TopNav.tsx b/vocodes/src/navigation/TopNav.tsx index 0f3886e03b..00ce3b5448 100644 --- a/vocodes/src/navigation/TopNav.tsx +++ b/vocodes/src/navigation/TopNav.tsx @@ -64,7 +64,7 @@ function TopNav(props: Props) { props.switchModeCallback(Mode.COMMUNITY_TTS_MODE)}>Community TTS props.switchModeCallback(Mode.COMMUNITY_VIDEO_MODE)}>Community Video props.switchModeCallback(Mode.UPLOAD_VOICES_MODE)}>Upload Voices - props.switchModeCallback(Mode.UPLOAD_VIDEO_MODE)}>Upload Voices + props.switchModeCallback(Mode.UPLOAD_VIDEO_MODE)}>Upload Video props.switchModeCallback(Mode.MY_DATA_MODE)}>My Data

(Yes, this needs a redesign!)