-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Chamindu36/ts-dev-migration
[TS-Migration] Migrate user reducer code files and firebase related code to TypeScript
- Loading branch information
Showing
9 changed files
with
197 additions
and
95 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { createAction, withMatcher, Action, ActionWithPayload } from "../../utils/reducer/reducer.utils"; | ||
import { USER_ACTION_TYPES } from "./user.types"; | ||
import { UserData, AdditionalInfo } from "../../utils/firebase/firebase.utils"; | ||
|
||
// Degine action types | ||
export type CheckUserSession = Action<USER_ACTION_TYPES.CHECK_USER_SESSION>; | ||
export type SetCurrentUser = ActionWithPayload<USER_ACTION_TYPES.SET_CURRENT_USER, UserData>; | ||
export type GoogleSignInStart = Action<USER_ACTION_TYPES.GOOGLE_SIGN_IN_START>; | ||
export type EmailSignInStart = ActionWithPayload<USER_ACTION_TYPES.EMAIL_SIGN_IN_START, { email: string, password: string }>; | ||
export type SignInSuccess = ActionWithPayload<USER_ACTION_TYPES.SIGN_IN_SUCCESS, UserData>; | ||
export type SignInFailed = ActionWithPayload<USER_ACTION_TYPES.SIGN_IN_FAILED, Error>; | ||
export type SignUpStart = ActionWithPayload<USER_ACTION_TYPES.SIGN_UP_START, { email: string, password: string, displayName: string }>; | ||
export type SignUpSuccess = ActionWithPayload<USER_ACTION_TYPES.SIGN_UP_SUCCESS, { user: UserData, additionalDetails: AdditionalInfo }>; | ||
export type SignUpFailed = ActionWithPayload<USER_ACTION_TYPES.SIGN_UP_FAILED, Error>; | ||
export type SignOutStart = Action<USER_ACTION_TYPES.SIGN_OUT_START>; | ||
export type SignOutSuccess = Action<USER_ACTION_TYPES.SIGN_OUT_SUCCESS>; | ||
export type SignOutFailed = ActionWithPayload<USER_ACTION_TYPES.SIGN_OUT_FAILED, Error>; | ||
|
||
export const setCurrentUser = withMatcher((user: UserData): SetCurrentUser => | ||
createAction(USER_ACTION_TYPES.SET_CURRENT_USER, user)); | ||
|
||
export const checkUserSession = withMatcher((): CheckUserSession => | ||
createAction(USER_ACTION_TYPES.CHECK_USER_SESSION)); | ||
|
||
export const googleSignInStart = withMatcher((): GoogleSignInStart => | ||
createAction(USER_ACTION_TYPES.GOOGLE_SIGN_IN_START)); | ||
|
||
export const emailSignInStart = withMatcher((email:string, password:string): EmailSignInStart => | ||
createAction(USER_ACTION_TYPES.EMAIL_SIGN_IN_START, { email, password })); | ||
|
||
export const signInSuccess = withMatcher((user:UserData): SignInSuccess => | ||
createAction(USER_ACTION_TYPES.SIGN_IN_SUCCESS, user)); | ||
|
||
export const signInFailed = withMatcher((error: Error): SignInFailed => | ||
createAction(USER_ACTION_TYPES.SIGN_IN_FAILED, error)); | ||
|
||
export const signUpStart = withMatcher(( | ||
email:string, | ||
password:string, | ||
displayName:string): SignUpStart => | ||
createAction(USER_ACTION_TYPES.SIGN_UP_START,{ email, password, displayName })); | ||
|
||
export const signUpSuccess = withMatcher((user:UserData, additionalDetails: AdditionalInfo) => | ||
createAction(USER_ACTION_TYPES.SIGN_UP_SUCCESS, { user, additionalDetails })); | ||
|
||
export const signUpFailed = withMatcher((error: Error): SignUpFailed => | ||
createAction(USER_ACTION_TYPES.SIGN_UP_FAILED, error)); | ||
|
||
export const signOutStart = withMatcher((): SignOutStart => | ||
createAction(USER_ACTION_TYPES.SIGN_OUT_START)); | ||
|
||
export const signOutSuccess = withMatcher((): SignOutSuccess => | ||
createAction(USER_ACTION_TYPES.SIGN_OUT_SUCCESS)); | ||
|
||
export const signOutFailed = withMatcher((error: Error): SignOutFailed => | ||
createAction(USER_ACTION_TYPES.SIGN_OUT_FAILED, error)); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { AnyAction } from "redux"; | ||
|
||
import { | ||
signInFailed, | ||
signInSuccess, | ||
signUpFailed, | ||
signUpSuccess, | ||
signOutFailed, | ||
signOutSuccess, | ||
} from './user.action' | ||
|
||
import { UserData } from "../../utils/firebase/firebase.utils"; | ||
|
||
// Define user state object | ||
export type UserState = { | ||
readonly currentUser: UserData | null, | ||
readonly isLoading: boolean, | ||
readonly error: Error | null, | ||
}; | ||
|
||
// Setup initial state | ||
const INITIAL_STATE: UserState = { | ||
currentUser: null, | ||
isLoading: false, | ||
error: null, | ||
}; | ||
|
||
export const userReducer = (state = INITIAL_STATE, action: AnyAction) => { | ||
|
||
if (signInSuccess.match(action)) { | ||
return { | ||
...state, | ||
currentUser: action.payload, | ||
isLoading: false, | ||
error: null, | ||
}; | ||
}; | ||
|
||
if (signOutSuccess.match(action)) { | ||
return { | ||
...state, | ||
currentUser: null, | ||
isLoading: false, | ||
error: null, | ||
}; | ||
}; | ||
|
||
if (signUpSuccess.match(action)) { | ||
return { | ||
...state, | ||
currentUser: action.payload.user, | ||
isLoading: false, | ||
error: null, | ||
}; | ||
}; | ||
|
||
// All failed actions | ||
if (signInFailed.match(action) || signOutFailed.match(action) || signUpFailed.match(action)) { | ||
return { | ||
...state, | ||
error: action.payload, | ||
isLoading: false, | ||
}; | ||
}; | ||
|
||
return state; | ||
|
||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { createSelector } from 'reselect'; | ||
|
||
import { UserState } from './user.reducer'; | ||
|
||
export const selectUserReducer = (state): UserState => state.user; | ||
|
||
export const selectCurrentUser = createSelector( | ||
selectUserReducer, | ||
(user) => user.currentUser | ||
); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export enum USER_ACTION_TYPES { | ||
SET_CURRENT_USER = 'user/SET_CURRENT_USER', | ||
CHECK_USER_SESSION = 'user/CHECK_USER_SESSION', | ||
GOOGLE_SIGN_IN_START = 'user/GOOGLE_SIGN_IN_START', | ||
EMAIL_SIGN_IN_START = 'user/EMAIL_SIGN_IN_START', | ||
SIGN_IN_SUCCESS = 'user/SIGN_IN_SUCCESS', | ||
SIGN_IN_FAILED = 'user/SIGN_IN_FAILED', | ||
SIGN_UP_START = 'user/SIGN_UP_START', | ||
SIGN_UP_SUCCESS = 'user/SIGN_UP_SUCCESS', | ||
SIGN_UP_FAILED = 'user/SIGN_UP_FAILED', | ||
SIGN_OUT_START = 'user/SIGN_OUT_START', | ||
SIGN_OUT_SUCCESS = 'user/SIGN_OUT_SUCCESS', | ||
SIGN_OUT_FAILED = 'user/SIGN_OUT_FAILED', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters