Migrate authorize{ actions, actionType, reducer} to TS & make factory service for interfaces #15

This commit is contained in:
Qolzam
2017-10-08 17:30:03 +07:00
parent f7c1a1ac00
commit 3b3899e7af
26 changed files with 652 additions and 218 deletions

View File

@@ -1,139 +0,0 @@
// - Import react components
import {firebaseRef, firebaseAuth} from 'app/firebase/'
import moment from 'moment'
import {push} from 'react-router-redux'
// - Import action types
import * as types from 'actionTypes'
// - Import actions
import * as globalActions from 'globalActions'
/* _____________ CRUD DB _____________ */
/**
* Log in user in server
* @param {string} email
* @param {string} password
*/
export var dbLogin = (email, password) => {
return (dispatch, getState) => {
dispatch(globalActions.showNotificationRequest())
return firebaseAuth().signInWithEmailAndPassword(email, password).then((result) => {
dispatch(globalActions.showNotificationSuccess())
dispatch(login(result.uid))
dispatch(push('/'))
}, (error) => dispatch(globalActions.showErrorMessage(error.code)))
}
}
/**
* Log out user in server
*/
export var dbLogout = () => {
return (dispatch, getState) => {
return firebaseAuth().signOut().then((result) => {
dispatch(logout())
dispatch(push('/login'))
}, (error) => dispatch(globalActions.showErrorMessage(error.code)))
}
}
/**
* Register user in database
* @param {object} user
*/
export var dbSignup = (user) => {
return (dispatch, getState) => {
dispatch(globalActions.showNotificationRequest())
return firebaseAuth().createUserWithEmailAndPassword(user.email, user.password).then((signupResult) => {
firebaseRef.child(`users/${signupResult.uid}/info`).set({
...user,
avatar:'noImage'
}).then((result) => {
dispatch(globalActions.showNotificationSuccess())
}, (error) => dispatch(globalActions.showErrorMessage(error.code)))
dispatch(signup({
uid: signupResult.uid,
...user
}))
dispatch(push('/'))
}, (error) => dispatch(globalActions.showErrorMessage(error.code)))
}
}
/**
* Change user's password
* @param {string} newPassword
*/
export const dbUpdatePassword = (newPassword) => {
return (dispatch, getState) => {
dispatch(globalActions.showNotificationRequest())
firebaseAuth().onAuthStateChanged((user) => {
if (user) {
user.updatePassword(newPassword).then(() => {
// Update successful.
dispatch(globalActions.showNotificationSuccess())
dispatch(updatePassword())
dispatch(push('/'))
}, (error) => {
// An error happened.
switch (error.code) {
case 'auth/requires-recent-login':
dispatch(globalActions.showErrorMessage(error.code))
dispatch(dbLogout())
break;
default:
}
})
}
})
}
}
/* _____________ CRUD State _____________ */
/**
* Loing user
* @param {string} uid
*/
export var login = (uid) => {
return {type: types.LOGIN, authed: true, uid}
}
/**
* Logout user
*/
export var logout = () => {
return {type: types.LOGOUT}
}
/**
* Register user
* @param {object} user
*/
export var signup = (user) => {
return {
type: types.SIGNUP,
...user
}
}
/**
* Update user's password
*/
export const updatePassword = () => {
return {type: types.UPDATE_PASSWORD}
}

View File

@@ -0,0 +1,148 @@
// - Import react components
import { firebaseRef, firebaseAuth } from 'app/firebase/';
import moment from 'moment';
import { push } from 'react-router-redux';
// -Import domain
import { User } from 'domain/users';
import { SocialError } from "domain/common";
// - Import action types
import { AuthorizeActionType } from 'constants/authorizeActionType';
// - Import services
import { IAuthorizeService } from "services/authorize";
import { IServiceProvider } from "factories";
import { ServiceProvide } from "factories";
const serviceProvider: IServiceProvider = new ServiceProvide();
const authorizeService: IAuthorizeService = serviceProvider.createAuthorizeService();
// - Import actions
import * as globalActions from 'actions/globalActions';
/* _____________ CRUD DB _____________ */
/**
* Log in user in server
* @param {string} email
* @param {string} password
*/
export const dbLogin = (email: string, password: string) => {
return (dispatch: any, getState: any) => {
dispatch(globalActions.showNotificationRequest())
return authorizeService.login(email, password).then((result) => {
dispatch(globalActions.showNotificationSuccess())
dispatch(login(result.uid))
dispatch(push('/'))
}, (error: SocialError) => dispatch(globalActions.showErrorMessage(error.code)))
}
}
/**
* Log out user in server
*/
export const dbLogout = () => {
return (dispatch: any, getState: any) => {
return authorizeService.logout().then((result) => {
dispatch(logout());
dispatch(push('/login'));
}, (error: SocialError) => dispatch(globalActions.showErrorMessage(error.code)))
}
}
/**
* Register user in database
* @param {object} user
*/
export const dbSignup = (user: User) => {
return (dispatch: any, getState: any) => {
dispatch(globalActions.showNotificationRequest())
return authorizeService.registerUser(user).then((result) => {
dispatch(signup({
userId: result.uid,
...user
}))
dispatch(push('/'))
})
.catch((error: SocialError) => dispatch(globalActions.showErrorMessage(error.code)));
}
}
/**
* Change user's password
* @param {string} newPassword
*/
export const dbUpdatePassword = (newPassword: string) => {
return (dispatch: any, getState: any) => {
dispatch(globalActions.showNotificationRequest())
return authorizeService.updatePassword(newPassword).then(() => {
// Update successful.
dispatch(globalActions.showNotificationSuccess())
dispatch(updatePassword())
dispatch(push('/'))
})
.catch((error: SocialError) => {
// An error happened.
switch (error.code) {
case 'auth/requires-recent-login':
dispatch(globalActions.showErrorMessage(error.code))
dispatch(dbLogout())
break;
default:
}
})
}
}
/* _____________ CRUD State _____________ */
/**
* Loing user
* @param {string} uid
*/
export const login = (uid: string) => {
return {
type: AuthorizeActionType.LOGIN,
payload: { authed: true, uid }
}
}
/**
* Logout user
*/
export const logout = () => {
return { type: AuthorizeActionType.LOGOUT }
}
/**
* Register user
* @param {object} user
*/
export const signup = (user: User) => {
return {
type: AuthorizeActionType.SIGNUP,
payload: { ...user }
}
}
/**
* Update user's password
*/
export const updatePassword = () => {
return { type: AuthorizeActionType.UPDATE_PASSWORD }
}

View File

@@ -8,7 +8,7 @@ import firebase, { firebaseRef } from '../firebase';
import moment from 'moment';
// - Import action types
import { postActionType } from 'constants/postActionType';
import { PostActionType } from 'constants/postActionType';
// - Import actions
import * as globalActions from 'actions/globalActions';
@@ -278,7 +278,7 @@ export const dbGetPostsByUserId = (uid: string) => {
*/
export const addPost = (uid: string, post: any) => {
return {
type: postActionType.ADD_POST,
type: PostActionType.ADD_POST,
payload: { uid, post }
}
}
@@ -290,7 +290,7 @@ export const addPost = (uid: string, post: any) => {
*/
export const updatePost = (uid: string, post: any) => {
return {
type: postActionType.UPDATE_POST,
type: PostActionType.UPDATE_POST,
payload: { uid, post }
}
}
@@ -302,7 +302,7 @@ export const updatePost = (uid: string, post: any) => {
*/
export const deletePost = (uid: string, id: string) => {
return {
type: postActionType.DELETE_POST,
type: PostActionType.DELETE_POST,
payload: { uid, id }
}
}
@@ -315,7 +315,7 @@ export const deletePost = (uid: string, id: string) => {
*/
export const addPosts = (uid: string, posts: any) => {
return {
type: postActionType.ADD_LIST_POST,
type: PostActionType.ADD_LIST_POST,
payload: { uid, posts }
}
}
@@ -325,7 +325,7 @@ export const addPosts = (uid: string, posts: any) => {
*/
export const clearAllData = () => {
return {
type: postActionType.CLEAR_ALL_DATA_POST
type: PostActionType.CLEAR_ALL_DATA_POST
}
}
@@ -335,7 +335,7 @@ export const clearAllData = () => {
*/
export const addImagePost = (uid: string, post: any) => {
return {
type: postActionType.ADD_IMAGE_POST,
type: PostActionType.ADD_IMAGE_POST,
payload: { uid, post }
}