@@ -9,22 +9,21 @@ import {
99 clearLoginCredentials ,
1010} from '../../lib/login'
1111
12- import {
13- setTokenValid ,
14- clearTokenValid ,
15- setAnalyticsOptOut ,
16- getAnalyticsOptOut ,
17- } from '../../lib/storage'
12+ import { setAnalyticsOptOut , getAnalyticsOptOut } from '../../lib/storage'
1813
19- import { updateBalances , updateCourses } from './sis'
14+ import { updateBalances } from './sis'
2015
2116export const SET_LOGIN_CREDENTIALS = 'settings/SET_LOGIN_CREDENTIALS'
22- export const CREDENTIALS_LOGIN = 'settings/CREDENTIALS_LOGIN'
17+ export const CREDENTIALS_LOGIN_START = 'settings/CREDENTIALS_LOGIN_START'
18+ export const CREDENTIALS_LOGIN_SUCCESS = 'settings/CREDENTIALS_LOGIN_SUCCESS'
19+ export const CREDENTIALS_LOGIN_FAILURE = 'settings/CREDENTIALS_LOGIN_FAILURE'
2320export const CREDENTIALS_LOGOUT = 'settings/CREDENTIALS_LOGOUT'
24- export const CREDENTIALS_VALIDATE = 'settings/CREDENTIALS_VALIDATE'
21+ export const CREDENTIALS_VALIDATE_START = 'settings/CREDENTIALS_VALIDATE_START'
22+ export const CREDENTIALS_VALIDATE_SUCCESS =
23+ 'settings/CREDENTIALS_VALIDATE_SUCCESS'
24+ export const CREDENTIALS_VALIDATE_FAILURE =
25+ 'settings/CREDENTIALS_VALIDATE_FAILURE'
2526export const SET_FEEDBACK = 'settings/SET_FEEDBACK'
26- export const TOKEN_LOGIN = 'settings/TOKEN_LOGIN'
27- export const TOKEN_LOGOUT = 'settings/TOKEN_LOGOUT'
2827export const CHANGE_THEME = 'settings/CHANGE_THEME'
2928
3029export async function setFeedbackStatus ( feedbackEnabled : boolean ) {
@@ -43,24 +42,15 @@ export async function setLoginCredentials(username: string, password: string) {
4342
4443export function logInViaCredentials ( username : string , password : string ) {
4544 return async ( dispatch : any => any ) => {
45+ dispatch ( { type : CREDENTIALS_LOGIN_START } )
4646 const result = await performLogin ( username , password )
47- dispatch ( { type : CREDENTIALS_LOGIN , payload : { username, password, result} } )
4847
49- // if we logged in successfully, go ahead and fetch the meals remaining number
5048 if ( result ) {
49+ dispatch ( { type : CREDENTIALS_LOGIN_SUCCESS , payload : { username, password} } )
50+ // since we logged in successfully, go ahead and fetch the meals info
5151 dispatch ( updateBalances ( ) )
52- }
53- }
54- }
55-
56- export function logInViaToken ( tokenStatus : boolean ) {
57- return async ( dispatch : any => any ) => {
58- await setTokenValid ( tokenStatus )
59- dispatch ( { type : TOKEN_LOGIN , payload : tokenStatus } )
60-
61- // if we logged in successfully, go ahead and fetch the data that requires a valid token
62- if ( tokenStatus ) {
63- dispatch ( updateCourses ( ) )
52+ } else {
53+ dispatch ( { type : CREDENTIALS_LOGIN_FAILURE } )
6454 }
6555 }
6656}
@@ -69,118 +59,81 @@ export function logOutViaCredentials() {
6959 return { type : CREDENTIALS_LOGOUT , payload : clearLoginCredentials ( ) }
7060}
7161
72- export async function validateLoginCredentials (
73- username ?: string ,
74- password ?: string ,
75- ) {
76- const result = await performLogin ( username , password )
77- return { type : CREDENTIALS_VALIDATE , payload : { result} }
62+ export function validateLoginCredentials ( username ? : string , password ? : string ) {
63+ return async ( dispatch : any => any ) = > {
64+ if ( ! username || ! password ) {
65+ return
66+ }
67+
68+ dispatch ( { type : CREDENTIALS_VALIDATE_START } )
69+
70+ const result = await performLogin ( username , password )
71+ if ( result ) {
72+ dispatch ( { type : CREDENTIALS_VALIDATE_SUCCESS } )
73+ } else {
74+ dispatch ( { type : CREDENTIALS_VALIDATE_FAILURE } )
75+ }
76+ }
7877}
7978
80- export async function logOutViaToken ( ) {
81- await clearTokenValid ( )
82- return { type : TOKEN_LOGOUT }
79+ export type LoginStateType = 'logged-out' | 'logged-in' | 'checking' | 'invalid'
80+ export type CredentialsState = {
81+ username : string ,
82+ password : string ,
83+ state : LoginStateType ,
8384}
8485
85- const initialCredentialsState = {
86+ const initialCredentialsState : CredentialsState = {
8687 username : '' ,
8788 password : '' ,
88- error : null ,
89- valid : false ,
89+ state : 'logged-out' ,
9090}
91- function credentialsReducer ( state = initialCredentialsState , action ) {
92- const { type, payload, error} = action
91+
92+ function credentialsReducer (
93+ state : CredentialsState = initialCredentialsState ,
94+ action ,
95+ ) {
96+ const { type, payload} = action
9397
9498 switch ( type ) {
95- case CREDENTIALS_VALIDATE : {
96- if ( error === true || payload . result === false ) {
97- return {
98- ...state ,
99- valid : false ,
100- error : payload . message ,
101- }
102- }
99+ case CREDENTIALS_VALIDATE_START :
100+ return { ...state , state : 'checking' }
103101
104- return {
105- ...state ,
106- valid : true ,
107- error : null ,
108- }
109- }
102+ case CREDENTIALS_VALIDATE_SUCCESS :
103+ return { ...state , state : 'logged-in' }
110104
111- case SET_LOGIN_CREDENTIALS : {
112- return {
113- ...state ,
114- username : payload . username ,
115- password : payload . password ,
116- }
117- }
105+ case CREDENTIALS_VALIDATE_FAILURE :
106+ return { ...state , state : 'invalid' }
118107
119- case CREDENTIALS_LOGIN : {
120- if ( error === true || payload . result === false ) {
121- return {
122- ...state ,
123- valid : false ,
124- error : payload . message ,
125- }
126- }
108+ case CREDENTIALS_LOGIN_START :
109+ return { ...state , state : 'checking' }
127110
111+ case CREDENTIALS_LOGIN_SUCCESS : {
128112 return {
129113 ...state ,
130- valid : true ,
131- error : null ,
114+ state : 'logged-in' ,
132115 username : payload . username ,
133116 password : payload . password ,
134117 }
135118 }
136119
120+ case CREDENTIALS_LOGIN_FAILURE :
121+ return { ...state , state : 'invalid' }
122+
137123 case CREDENTIALS_LOGOUT : {
138124 return {
139125 ...state ,
126+ state : 'logged-out' ,
140127 username : '' ,
141128 password : '' ,
142- valid : false ,
143- error : null ,
144129 }
145130 }
146131
147- default :
148- return state
149- }
150- }
151-
152- const initialTokenState = {
153- status : false ,
154- error : null ,
155- valid : false ,
156- }
157- function tokenReducer ( state = initialTokenState , action ) {
158- const { type, payload, error} = action
159- switch ( type ) {
160- case TOKEN_LOGIN : {
161- if ( error === true ) {
162- return {
163- ...state ,
164- valid : false ,
165- error : payload ,
166- status : false ,
167- }
168- }
169-
170- return {
171- ...state ,
172- valid : payload === true ,
173- error : null ,
174- status : payload ,
175- }
176- }
177-
178- case TOKEN_LOGOUT : {
132+ case SET_LOGIN_CREDENTIALS : {
179133 return {
180134 ...state ,
181- valid : false ,
182- error : null ,
183- status : false ,
135+ username : payload . username ,
136+ password : payload . password ,
184137 }
185138 }
186139
@@ -189,20 +142,29 @@ function tokenReducer(state = initialTokenState, action) {
189142 }
190143}
191144
192- const initialSettingsState = {
145+ export type SettingsState = {
146+ theme : string ,
147+ dietaryPreferences : [ ] ,
148+ credentials : CredentialsState ,
149+ feedbackDisabled : boolean ,
150+ }
151+
152+ const initialSettingsState : SettingsState = {
193153 theme : 'All About Olaf' ,
194154 dietaryPreferences : [ ] ,
195155
196- credentials : undefined ,
197- token : undefined ,
156+ credentials : initialCredentialsState ,
198157 feedbackDisabled : false ,
199158}
200- export function settings ( state : Object = initialSettingsState , action : Object ) {
159+
160+ export function settings (
161+ state : SettingsState = initialSettingsState ,
162+ action : Object ,
163+ ) {
201164 // start out by running the reducers for the complex chunks of the state
202165 state = {
203166 ...state ,
204167 credentials : credentialsReducer ( state . credentials , action ) ,
205- token : tokenReducer ( state . token , action ) ,
206168 }
207169
208170 const { type , payload } = action
0 commit comments