From 3b3899e7afe30c148c0cb13b69f7bf508a24b9e5 Mon Sep 17 00:00:00 2001 From: Qolzam Date: Sun, 8 Oct 2017 17:30:03 +0700 Subject: [PATCH] Migrate authorize{ actions, actionType, reducer} to TS & make factory service for interfaces #15 --- app/actions/authorizeActions.jsx | 139 ---------------- app/actions/authorizeActions.ts | 148 ++++++++++++++++++ app/actions/postActions.ts | 14 +- app/constants/authorizeActionType.ts | 7 + app/constants/postActionType.ts | 4 +- app/domain/authorize/index.ts | 7 + app/domain/authorize/loginResult.ts | 25 +++ app/domain/authorize/registerUserResult.ts | 23 +++ app/domain/common/baseDomain.ts | 4 + app/domain/common/index.ts | 7 + app/domain/common/socialError.ts | 45 ++++++ app/domain/users/index.ts | 5 + app/domain/users/user.ts | 30 ++++ app/factories/IServiceProvider.ts | 10 ++ app/factories/index.ts | 7 + app/factories/serviceProvide.ts | 26 +++ .../authorize/AuthorizeService.ts | 112 +++++++++++++ app/firebaseServices/authorize/index.ts | 5 + app/reducers/authorizeReducer.jsx | 53 ------- app/reducers/authorizeReducer.ts | 96 ++++++++++++ app/reducers/postReducer.ts | 33 ++-- app/services/authorize/IAuthorizeService.ts | 41 +++++ app/services/authorize/index.ts | 5 + package.json | 2 + tsconfig.json | 13 +- webpack.config.js | 9 +- 26 files changed, 652 insertions(+), 218 deletions(-) delete mode 100644 app/actions/authorizeActions.jsx create mode 100644 app/actions/authorizeActions.ts create mode 100644 app/constants/authorizeActionType.ts create mode 100644 app/domain/authorize/index.ts create mode 100644 app/domain/authorize/loginResult.ts create mode 100644 app/domain/authorize/registerUserResult.ts create mode 100644 app/domain/common/baseDomain.ts create mode 100644 app/domain/common/index.ts create mode 100644 app/domain/common/socialError.ts create mode 100644 app/domain/users/index.ts create mode 100644 app/domain/users/user.ts create mode 100644 app/factories/IServiceProvider.ts create mode 100644 app/factories/index.ts create mode 100644 app/factories/serviceProvide.ts create mode 100644 app/firebaseServices/authorize/AuthorizeService.ts create mode 100644 app/firebaseServices/authorize/index.ts delete mode 100644 app/reducers/authorizeReducer.jsx create mode 100644 app/reducers/authorizeReducer.ts create mode 100644 app/services/authorize/IAuthorizeService.ts create mode 100644 app/services/authorize/index.ts diff --git a/app/actions/authorizeActions.jsx b/app/actions/authorizeActions.jsx deleted file mode 100644 index d1ae54d..0000000 --- a/app/actions/authorizeActions.jsx +++ /dev/null @@ -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} -} - diff --git a/app/actions/authorizeActions.ts b/app/actions/authorizeActions.ts new file mode 100644 index 0000000..c22d95a --- /dev/null +++ b/app/actions/authorizeActions.ts @@ -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 } + } + diff --git a/app/actions/postActions.ts b/app/actions/postActions.ts index 819d41f..e24226f 100644 --- a/app/actions/postActions.ts +++ b/app/actions/postActions.ts @@ -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 } } diff --git a/app/constants/authorizeActionType.ts b/app/constants/authorizeActionType.ts new file mode 100644 index 0000000..e50eef6 --- /dev/null +++ b/app/constants/authorizeActionType.ts @@ -0,0 +1,7 @@ +export enum AuthorizeActionType +{ + LOGIN = 'LOGIN', + LOGOUT = 'LOGOUT', + SIGNUP = 'SIGNUP', + UPDATE_PASSWORD = 'UPDATE_PASSWORD' +} diff --git a/app/constants/postActionType.ts b/app/constants/postActionType.ts index 1a0159e..832da30 100644 --- a/app/constants/postActionType.ts +++ b/app/constants/postActionType.ts @@ -1,5 +1,5 @@ - export enum postActionType { + export enum PostActionType { ADD_IMAGE_POST = "ADD_IMAGE_POST", ADD_VIDEO_POST = "ADD_VIDEO_POST", ADD_POST = "ADD_POST", @@ -7,5 +7,5 @@ DELETE_POST = "DELETE_POST", ADD_LIST_POST = "ADD_LIST_POST", CLEAR_ALL_DATA_POST = "CLEAR_ALL_DATA_POST" -}; +} diff --git a/app/domain/authorize/index.ts b/app/domain/authorize/index.ts new file mode 100644 index 0000000..374b4f8 --- /dev/null +++ b/app/domain/authorize/index.ts @@ -0,0 +1,7 @@ +import { LoginUser } from "./loginResult"; +import { RegisterUserResult } from "./registerUserResult"; + +export { + LoginUser, + RegisterUserResult +} \ No newline at end of file diff --git a/app/domain/authorize/loginResult.ts b/app/domain/authorize/loginResult.ts new file mode 100644 index 0000000..ec81669 --- /dev/null +++ b/app/domain/authorize/loginResult.ts @@ -0,0 +1,25 @@ +import { BaseDomain } from "domain/common"; + +export class LoginUser extends BaseDomain{ + + constructor(uid: string){ + super(); + + this._uid = uid; + } + + /** + * User identifier + * + * @type {string} + * @memberof LoginUser + */ + + private _uid : string; + public get uid() : string { + return this._uid; + } + + + +} \ No newline at end of file diff --git a/app/domain/authorize/registerUserResult.ts b/app/domain/authorize/registerUserResult.ts new file mode 100644 index 0000000..72856ee --- /dev/null +++ b/app/domain/authorize/registerUserResult.ts @@ -0,0 +1,23 @@ +import { BaseDomain } from "domain/common"; + +export class RegisterUserResult extends BaseDomain{ + + constructor(uid: string){ + super(); + + this._uid = uid; + } + /** + * User identifier + * + * @type {string} + * @memberof LoginUser + */ + + private _uid : string; + public get uid() : string { + return this._uid; + } +} + + diff --git a/app/domain/common/baseDomain.ts b/app/domain/common/baseDomain.ts new file mode 100644 index 0000000..7c10679 --- /dev/null +++ b/app/domain/common/baseDomain.ts @@ -0,0 +1,4 @@ + +export class BaseDomain{ + +} \ No newline at end of file diff --git a/app/domain/common/index.ts b/app/domain/common/index.ts new file mode 100644 index 0000000..91d22e0 --- /dev/null +++ b/app/domain/common/index.ts @@ -0,0 +1,7 @@ +import { SocialError } from "./socialError"; +import { BaseDomain } from "./baseDomain"; + +export { + SocialError, + BaseDomain +} \ No newline at end of file diff --git a/app/domain/common/socialError.ts b/app/domain/common/socialError.ts new file mode 100644 index 0000000..e0219ce --- /dev/null +++ b/app/domain/common/socialError.ts @@ -0,0 +1,45 @@ +export class SocialError{ + + constructor(code: string, description: string){ + this._code = code; + this._description = description; + this._isError = true; + } + + /** + * Error code + * + * @type {string} + * @memberof SocialError + */ + private _code : string; + public get code() : string { + return this._code; + } + + /** + * Error description + * + * @type {string} + * @memberof SocialError + */ + + private _description : string; + public get description() : string { + return this._description; + } + + + /** + * If is error {true} if not {false} + * + * @type {Boolean} + * @memberof SocialError + */ + + private _isError : Boolean; + public get isError() : Boolean { + return this._isError; + } + +} \ No newline at end of file diff --git a/app/domain/users/index.ts b/app/domain/users/index.ts new file mode 100644 index 0000000..79167ad --- /dev/null +++ b/app/domain/users/index.ts @@ -0,0 +1,5 @@ +import User from './user'; + +export { + User +} \ No newline at end of file diff --git a/app/domain/users/user.ts b/app/domain/users/user.ts new file mode 100644 index 0000000..235e0d9 --- /dev/null +++ b/app/domain/users/user.ts @@ -0,0 +1,30 @@ +import { BaseDomain } from "domain/common"; + +class User extends BaseDomain { + /** + * Email of the user + * + * @type {string} + * @memberof User + */ + public email: string; + + /** + * Password of the user + * + * @type {string} + * @memberof User + */ + public password: string; + + /** + * User identifier + * + * @type {string} + * @memberof User + */ + public userId: string; + +} + +export default User; \ No newline at end of file diff --git a/app/factories/IServiceProvider.ts b/app/factories/IServiceProvider.ts new file mode 100644 index 0000000..f88dc51 --- /dev/null +++ b/app/factories/IServiceProvider.ts @@ -0,0 +1,10 @@ +import {IAuthorizeService} from 'services/authorize/IAuthorizeService' +export interface IServiceProvider{ + /** + * Create authorize service + * + * @memberof IServiceProvider + */ + createAuthorizeService : () => IAuthorizeService; + +} \ No newline at end of file diff --git a/app/factories/index.ts b/app/factories/index.ts new file mode 100644 index 0000000..8deabc1 --- /dev/null +++ b/app/factories/index.ts @@ -0,0 +1,7 @@ +import { IServiceProvider } from "./IServiceProvider"; +import { ServiceProvide } from "./serviceProvide"; + +export { + IServiceProvider, + ServiceProvide +} \ No newline at end of file diff --git a/app/factories/serviceProvide.ts b/app/factories/serviceProvide.ts new file mode 100644 index 0000000..eb8e592 --- /dev/null +++ b/app/factories/serviceProvide.ts @@ -0,0 +1,26 @@ +//#region Interfaces + +import { IServiceProvider } from "factories"; +import { IAuthorizeService } from "services/authorize"; + +//#endregion + +//#region Service implemented classes + + // - Firebase services + import { AuthorizeService } from "firebaseServices/authorize"; + +//#endregion + +export class ServiceProvide implements IServiceProvider { + + /** + * Create instant for AuthorizeService + * + * @memberof ServiceProvide + */ + createAuthorizeService: () => IAuthorizeService = () => { + return new AuthorizeService(); + } + +} \ No newline at end of file diff --git a/app/firebaseServices/authorize/AuthorizeService.ts b/app/firebaseServices/authorize/AuthorizeService.ts new file mode 100644 index 0000000..3629571 --- /dev/null +++ b/app/firebaseServices/authorize/AuthorizeService.ts @@ -0,0 +1,112 @@ +// - Import react components +import { firebaseRef, firebaseAuth } from 'app/firebase/'; + +import { IAuthorizeService } from "services/authorize"; +import { User } from "Domain/users"; +import { LoginUser, RegisterUserResult } from "domain/authorize"; +import { SocialError } from "domain/common"; + +/** + * Firbase authorize service + * + * @export + * @class AuthorizeService + * @implements {IAuthorizeService} + */ +export class AuthorizeService implements IAuthorizeService { + + /** + * Login the user + * + * @returns {Promise} + * @memberof IAuthorizeService + */ + public login: (email: string, password: string) => Promise = (email, password) => { + + return new Promise((resolve, reject) => { + firebaseAuth() + .signInWithEmailAndPassword(email, password) + .then((result) => { + resolve(new LoginUser(result.uid)); + }) + .catch((error: any) => { + reject(new SocialError(error.code, error.message)); + }) + }); + }; + + /** + * Logs out the user + * + * @returns {Promise} + * @memberof IAuthorizeService + */ + public logout: () => Promise = () => { + return new Promise((resolve, reject) => { + firebaseAuth() + .signOut() + .then((result) => { + resolve(); + }) + .catch((error: any) => { + + reject(new SocialError(error.code, error.message)); + }) + }); + } + + /** + * Register a user + * + * @returns {Promise} + * @memberof IAuthorizeService + */ + public registerUser: (user: User) => Promise = (user) => { + return new Promise((resolve, reject) => { + firebaseAuth() + .createUserWithEmailAndPassword(user.email, user.password) + .then((signupResult) => { + firebaseRef.child(`users/${signupResult.uid}/info`) + .set({ + ...user, + avatar: 'noImage' + }) + .then((result) => { + resolve(new RegisterUserResult(signupResult.uid)); + }) + .catch((error: any) => reject(new SocialError(error.name, error.message))); + }) + .catch((error: any) => reject(new SocialError(error.code, error.message))); + }); + } + + /** + * Update user password + * + * @returns {Promise} + * @memberof IAuthorizeService + */ + public updatePassword: (newPassword: string) => Promise = (newPassword) => { + console.log('===================================='); + console.log("update password"); + console.log('===================================='); + return new Promise((resolve, reject) => { + var user = firebaseAuth().currentUser; + console.log('===================================='); + console.log(user); + console.log('===================================='); + if (user) { + user.updatePassword(newPassword).then(() => { + // Update successful. + resolve(); + }).catch((error: any) => { + // An error happened. + reject(new SocialError(error.code, error.message)); + }); + } + + }); + } + + +} \ No newline at end of file diff --git a/app/firebaseServices/authorize/index.ts b/app/firebaseServices/authorize/index.ts new file mode 100644 index 0000000..7e35377 --- /dev/null +++ b/app/firebaseServices/authorize/index.ts @@ -0,0 +1,5 @@ +import { AuthorizeService } from "./AuthorizeService"; + +export { + AuthorizeService +} \ No newline at end of file diff --git a/app/reducers/authorizeReducer.jsx b/app/reducers/authorizeReducer.jsx deleted file mode 100644 index 8024d9f..0000000 --- a/app/reducers/authorizeReducer.jsx +++ /dev/null @@ -1,53 +0,0 @@ -// - Import action types -import * as types from 'actionTypes' - - -/** - * Default state - */ -var defaultState = { - uid: 0, - authed: false, - updatePassword: false, - guest:false -} - - -/** - * Authorize reducer - * @param {object} state - * @param {object} action - */ -export var authorizeReducer = (state = defaultState, action) =>{ - switch (action.type) { - case types.LOGIN: - return{ - ...state, - uid: action.uid, - authed: true, - guest:false - } - case types.LOGOUT: - return{ - ...state, - uid: 0, - authed: false, - guest:true - } - - case types.SIGNUP: - return{ - ...state, - uid: action.uid - } - case types.UPDATE_PASSWORD: - return{ - ...state, - updatePassword: action.updatePassword - } - default: - return state - - } - -} diff --git a/app/reducers/authorizeReducer.ts b/app/reducers/authorizeReducer.ts new file mode 100644 index 0000000..eaf8eab --- /dev/null +++ b/app/reducers/authorizeReducer.ts @@ -0,0 +1,96 @@ +// - Import react components +import {Reducer, Action} from "redux"; + +// - Import action types +import {AuthorizeActionType} from 'constants/authorizeActionType' + + +/** + * Default state + */ +var defaultState = { + uid: 0, + authed: false, + updatePassword: false, + guest:false +} + +/** + * Default post state interface + * + * @export + * @interface IAuthorizeState + */ +export interface IAuthorizeState { + uid: number, + authed: Boolean, + updatePassword: Boolean, + guest:Boolean +} + +/** + * Default authorize action interface + * + * @export + * @interface IAuthorizeAction + */ +export interface IAuthorizeAction { + payload: any, + type: AuthorizeActionType + +} + +/** + * Default authorize reducer state + * + * @export + * @class DefaultAuthorizeState + * @implements {IAuthorizeState} + */ +export class DefaultAuthorizeState implements IAuthorizeState{ + uid: number = 0; + authed: Boolean = false; + updatePassword: Boolean = false; + guest: Boolean = false; + +} + +/** + * Authorize reducer + * @param {object} state + * @param {object} action + */ +export var authorizeReducer = (state : IAuthorizeState = new DefaultAuthorizeState(), action: IAuthorizeAction) =>{ + const { payload } = action; + switch (action.type) { + case AuthorizeActionType.LOGIN: + return{ + ...state, + uid: payload.uid, + authed: true, + guest:false + } + case AuthorizeActionType.LOGOUT: + return{ + ...state, + uid: 0, + authed: false, + guest:true + } + + case AuthorizeActionType.SIGNUP: + return{ + ...state, + uid: payload.userId + } + case AuthorizeActionType.UPDATE_PASSWORD: + return{ + ...state, + updatePassword: payload.updatePassword + } + default: + return state + + } + +} diff --git a/app/reducers/postReducer.ts b/app/reducers/postReducer.ts index 088d103..8a85e43 100644 --- a/app/reducers/postReducer.ts +++ b/app/reducers/postReducer.ts @@ -6,7 +6,7 @@ import {Reducer, Action} from "redux"; // - Import action types -import { postActionType } from "constants/postActionType"; +import { PostActionType } from "constants/postActionType"; /* ---------------- */ @@ -28,15 +28,22 @@ export interface IPostState { * * * @export - * @interface postState + * @interface IPostAction */ export interface IPostAction { payload: any, - type: postActionType + type: PostActionType } -export class defaultPostState implements IPostState{ +/** + * Default post reducer state + * + * @export + * @class DefaultPostState + * @implements {IPostState} + */ +export class DefaultPostState implements IPostState{ userPosts: any = {}; loaded: boolean = false; @@ -47,13 +54,13 @@ export class defaultPostState implements IPostState{ * @param {object} state * @param {object} action */ -export var postReducer = (state : IPostState = new defaultPostState(), action : IPostAction) => { - const { payload } = action +export var postReducer = (state : IPostState = new DefaultPostState(), action : IPostAction) => { + const { payload } = action; switch (action.type) { - case postActionType.CLEAR_ALL_DATA_POST: - return new defaultPostState() + case PostActionType.CLEAR_ALL_DATA_POST: + return new DefaultPostState() - case postActionType.ADD_IMAGE_POST: + case PostActionType.ADD_IMAGE_POST: return { ...state, userPosts: { @@ -65,7 +72,7 @@ export var postReducer = (state : IPostState = new defaultPostState(), action : } } - case postActionType.ADD_POST: + case PostActionType.ADD_POST: return { ...state, userPosts: { @@ -77,7 +84,7 @@ export var postReducer = (state : IPostState = new defaultPostState(), action : } } - case postActionType.UPDATE_POST: + case PostActionType.UPDATE_POST: return { ...state, userPosts: { @@ -92,7 +99,7 @@ export var postReducer = (state : IPostState = new defaultPostState(), action : } } - case postActionType.DELETE_POST: + case PostActionType.DELETE_POST: let filteredPosts = {} Object.keys(state.userPosts[payload.uid]).map((key) => { if (key !== payload.id) { @@ -108,7 +115,7 @@ export var postReducer = (state : IPostState = new defaultPostState(), action : } } } - case postActionType.ADD_LIST_POST: + case PostActionType.ADD_LIST_POST: return { ...state, userPosts: { diff --git a/app/services/authorize/IAuthorizeService.ts b/app/services/authorize/IAuthorizeService.ts new file mode 100644 index 0000000..d32c74e --- /dev/null +++ b/app/services/authorize/IAuthorizeService.ts @@ -0,0 +1,41 @@ +import { User } from "domain/users"; +import { LoginUser, RegisterUserResult } from "domain/authorize"; + + + + +/** + * Authentication service interface + * + * @export + * @interface IAuthorizeService + */ +export interface IAuthorizeService { + + /** + * Login the user + * + * @returns {Promise} + * @memberof IAuthorizeService + */ + login: (email: string, password: string) => Promise; + + /** + * Logs out the user + * + * @returns {Promise} + * @memberof IAuthorizeService + */ + logout: () => Promise; + + /** + * @returns {Promise} + */ + updatePassword: (newPassword: string) => Promise; + + /** + * @returns {Promise} + */ + registerUser: (user: User) => Promise; + +} \ No newline at end of file diff --git a/app/services/authorize/index.ts b/app/services/authorize/index.ts new file mode 100644 index 0000000..3b65030 --- /dev/null +++ b/app/services/authorize/index.ts @@ -0,0 +1,5 @@ +import { IAuthorizeService } from "./IAuthorizeService"; + +export { + IAuthorizeService +} \ No newline at end of file diff --git a/package.json b/package.json index 3b34ce8..0442709 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "faker": "^4.1.0", "file-loader": "^0.11.1", "firebase": "^3.9.0", + "inversify": "^4.3.0", "keycode": "^2.1.9", "lodash": "^4.17.4", "material-ui": "^0.19.3", @@ -44,6 +45,7 @@ "redux": "^3.7.2", "redux-actions": "^2.0.3", "redux-thunk": "^2.2.0", + "reflect-metadata": "^0.1.10", "sass-loader": "^6.0.3", "save": "^2.3.0", "script-loader": "^0.7.0", diff --git a/tsconfig.json b/tsconfig.json index 7628335..5ad7cef 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,18 +1,25 @@ { "compilerOptions": { - "module": "es6", // use ES2015 modules - "target": "es6", // compile to ES2015 (Babel will do the rest) + "module": "es6", + "target": "es6", + "types": ["reflect-metadata"], "allowSyntheticDefaultImports": true, // see below "baseUrl": "./app/", // enables you to import relative to this folder "paths": { - "app/*" : ["*"] + "app/*" : ["*"], + "domain/*" : ["domain/*"], + "factories/*" : ["factories/*"], + "services/*" : ["services/*"], + "firebaseServices/*" : ["firebaseServices/*"] }, "sourceMap": true, // make TypeScript generate sourcemaps "outDir": "public", // output directory to build to (irrelevant because we use Webpack most of the time) "jsx": "preserve", // enable JSX mode, but "preserve" tells TypeScript to not transform it (we'll use Babel) "strict": true, "moduleResolution": "node", + "experimentalDecorators": true, + "emitDecoratorMetadata": true, "allowJs": true }, diff --git a/webpack.config.js b/webpack.config.js index a4cff87..b28f277 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -72,7 +72,10 @@ module.exports = { path.resolve(__dirname, './app/api'), path.resolve(__dirname, './app/constants'), path.resolve(__dirname, './app/actions'), - path.resolve(__dirname, './app/reducers') + path.resolve(__dirname, './app/reducers'), + path.resolve(__dirname, './app/services'), + path.resolve(__dirname, './app/factories'), + path.resolve(__dirname, './app/domain') @@ -83,6 +86,10 @@ module.exports = { components: 'app/components', reducers: 'app/reducers', constants: 'app/constants', + services: 'app/services', + factories: 'app/factories', + firebaseServices: 'app/firebaseServices', + domain: 'app/domain', api: 'app/api', db: 'app/db', store: 'app/store',