import UserActionCreator from "../models/client/UserActionCreator" ; import { Dispatch , AnyAction as Action } from "redux" ; import fetch from "../shared/fetch" ; import { ACCESS_TOKEN_KEY , INVALID_TOKEN_ERROR } from "../shared/constants" ; import User from "../models/User" ; import actions from "./common" ; import Gender from "../models/Gender" ; import RedirectTask from "../models/client/RedirectTask" ; import { getToast as toast } from "../shared/toast" ; import { getStorage as localStorage } from "../shared/storage" ; import { DEFAULT_PREFERENCES } from "../shared/preferences" ; import Preferences from "../models/Preferences" ; import AuthenticationResponse from "../models/response/AuthenticationResponse.d" ; export const USER_REQUEST_START = "USER_REQUEST_START" ; export const CONSENT_REQUEST_FAILED = "CONSENT_REQUEST_FAILED" ; export const AUTHENTICATE_SUCCESS = "AUTHENTICATE_SUCCESS" ; export const CONSENT_REQUEST_SUCCESS = "CONSENT_REQUEST_SUCCESS" ; export const AUTHENTICATE_FAILED = "AUTHENTICATE_FAILED" ; export const LOGIN_SUCCESS = "LOGIN_SUCCESS" ; export const LOGIN_FAILED = "LOGIN_FAILED" ; export const UPDATE_PROFILE_SUCCESS = "UPDATE_PROFILE_SUCCESS" ; export const UPDATE_PROFILE_FAILED = "UPDATE_PROFILE_FAILED" ; export const UPDATE_PREFERENCES_SUCCESS = "UPDATE_PREFERENCES_SUCCESS" ; export const UPDATE_PREFERENCES_FAILED = "UPDATE_PREFERENCES_FAILED" ; export const SIGN_UP_SUCCESS = "SIGN_UP_SUCCESS" ; export const SIGN_UP_FAILED = "SIGN_UP_FAILED" ; export const LOGOUT = "LOGOUT" ; export const UPLOAD_AVATAR_START = "UPLOAD_AVATAR_START" ; export const UPLOAD_AVATAR_SUCCESS = "UPLOAD_AVATAR_SUCCESS" ; export const UPLOAD_AVATAR_FAILED = "UPLOAD_AVATAR_FAILED" ; export const RESET_UPLOADED_AVATAR = "RESET_UPLOADED_AVATAR" ; export const UPDATE_PASSWORD_START = "UPDATE_PASSWORD_START" ; export const UPDATE_PASSWORD_SUCCESS = "UPDATE_PASSWORD_SUCCESS" ; export const UPDATE_PASSWORD_FAILED = "UPDATE_PASSWORD_FAILED" ; export const SEND_OTP_START = "SEND_OTP_START" ; export const SEND_OTP_SUCCESS = "SEND_OTP_SUCCESS" ; export const SEND_OTP_COOL_DOWN = "SEND_OTP_COOL_DOWN" ; export const SEND_OTP_FAILED = "SEND_OTP_FAILED" ; const redirectToLogin = { redirected : false , to : "/login" } ; const userActionCreator = { allowConsent ( transactionId , OTP ? ) { return ( dispatch ) : void => { dispatch ( { type : USER_REQUEST_START } ) ; fetch ( "/oauth2/authorize/decision" , { transaction_id : transactionId , OTP : OTP } , "POST" ) . then ( ( json ) => { if ( json . user && json . accessToken ) { dispatch ( { type : CONSENT_REQUEST_SUCCESS , user : json . user , notifications : [ ] , others : json . others } ) ; return localStorage ( ) . setItem ( ACCESS_TOKEN_KEY , json . accessToken ) ; } else { return Promise . reject ( new Error ( "toast.user.general_error" ) ) ; } } ) . then ( ( ) => { toast ( ) . success ( "toast.user.sign_in_successfully" ) ; } ) . catch ( ( error ) => { dispatch ( actions . handleFetchError ( CONSENT_REQUEST_FAILED , error ) ) ; } ) ; } ; } , denyConsent ( ) { toast ( ) . error ( "toast.user.deny_consent" ) ; return { type : CONSENT_REQUEST_FAILED } ; } , authenticate ( ) { return ( dispatch ) : void => { localStorage ( ) . getItem ( ACCESS_TOKEN_KEY ) . then ( ( token ) => { if ( ! token ) { return Promise . reject ( new Error ( INVALID_TOKEN_ERROR ) ) ; } dispatch ( { type : USER_REQUEST_START } ) ; return fetch ( "/oauth2/profile" , undefined , "GET" , true ) ; } ) . then ( ( json ) => { if ( json . user ) { dispatch ( { type : AUTHENTICATE_SUCCESS , user : json . user , notifications : json . notifications , others : json . others } ) ; } else { return Promise . reject ( new Error ( INVALID_TOKEN_ERROR ) ) ; } } ) . catch ( ( error ) => { localStorage ( ) . setItem ( ACCESS_TOKEN_KEY , "" ) ; dispatch ( actions . handleFetchError ( AUTHENTICATE_FAILED , error ) ) ; } ) ; } ; } , login ( email , password ) { return ( dispatch ) : any => { dispatch ( { type : USER_REQUEST_START } ) ; return fetch ( "/oauth2/login" , { email : email , password : password } , "POST" ) . then ( ( json ) => { const response = json as AuthenticationResponse ; const task = json as RedirectTask ; if ( response . user && response . accessToken ) { dispatch ( { type : LOGIN_SUCCESS , user : response . user , notifications : response . notifications , others : response . others } ) ; return localStorage ( ) . setItem ( ACCESS_TOKEN_KEY , response . accessToken ) ; } else if ( ! task . redirected && task . to ) { dispatch ( { type : SIGN_UP_SUCCESS , redirectTask : task } ) ; } else { return Promise . reject ( new Error ( "toast.user.general_error" ) ) ; } } ) . then ( ( ) => { toast ( ) . success ( "toast.user.sign_in_successfully" ) ; } ) . catch ( ( error ) => { dispatch ( actions . handleFetchError ( LOGIN_FAILED , error ) ) ; } ) ; } ; } , logout ( ) { localStorage ( ) . setItem ( ACCESS_TOKEN_KEY , "" ) ; return { type : LOGOUT } ; } , updateProfile ( user ) { return ( dispatch ) : void => { localStorage ( ) . getItem ( ACCESS_TOKEN_KEY ) . then ( ( token ) => { if ( ! token ) { return Promise . reject ( new Error ( INVALID_TOKEN_ERROR ) ) ; } dispatch ( { type : USER_REQUEST_START } ) ; return fetch ( "/oauth2/profile" , user , "POST" , true ) ; } ) . then ( ( json ) => { if ( json ) { dispatch ( { type : UPDATE_PROFILE_SUCCESS , user : json } ) ; toast ( ) . success ( "toast.user.update_successfully" ) ; } else { return Promise . reject ( new Error ( "toast.user.update_failed" ) ) ; } } ) . catch ( ( error ) => { dispatch ( actions . handleFetchError ( UPDATE_PROFILE_FAILED , error ) ) ; } ) ; } ; } , updatePreferences ( id , preferences ) { return ( dispatch ) : void => { localStorage ( ) . getItem ( ACCESS_TOKEN_KEY ) . then ( ( token ) => { if ( ! token ) { return Promise . reject ( new Error ( INVALID_TOKEN_ERROR ) ) ; } dispatch ( { type : USER_REQUEST_START } ) ; return fetch ( "/oauth2/preferences" , { id , preferences } , "POST" , true ) ; } ) . then ( ( json ) => { if ( json ) { dispatch ( { type : UPDATE_PREFERENCES_SUCCESS , preferences : json } ) ; toast ( ) . success ( "toast.user.update_successfully" ) ; } else { return Promise . reject ( new Error ( "toast.user.update_failed" ) ) ; } } ) . catch ( ( error ) => { dispatch ( actions . handleFetchError ( UPDATE_PREFERENCES_FAILED , error ) ) ; } ) ; } ; } , uploadAvatar ( payload ) { return ( dispatch ) : void => { localStorage ( ) . getItem ( ACCESS_TOKEN_KEY ) . then ( ( token ) => { if ( ! token ) { return Promise . reject ( new Error ( INVALID_TOKEN_ERROR ) ) ; } dispatch ( { type : UPLOAD_AVATAR_START } ) ; return fetch ( "/api/avatar/create" , payload , "PUT" , true ) ; } ) . then ( ( json ) => { if ( json . url ) { dispatch ( { type : UPLOAD_AVATAR_SUCCESS , url : json . url } ) ; } else { return Promise . reject ( new Error ( "toast.user.upload_avatar_failed" ) ) ; } } , ( error ) => { dispatch ( actions . handleFetchError ( UPLOAD_AVATAR_FAILED , error ) ) ; } ) ; } ; } , resetAvatar ( ) { return { type : RESET_UPLOADED_AVATAR } ; } , signUp ( email , password , confirmPassword , name , gender , invitationCode ? ) { return ( dispatch ) : void => { dispatch ( { type : USER_REQUEST_START } ) ; const preferences = DEFAULT_PREFERENCES ; fetch ( "/oauth2/signup" , { email , password , confirmPassword , name , gender , preferences , invitationCode } , "POST" ) . then ( ( redirectTask ) => { dispatch ( { type : SIGN_UP_SUCCESS , redirectTask : redirectTask } ) ; toast ( ) . success ( "toast.user.sign_up_successfully" ) ; } ) . catch ( ( error ) => { dispatch ( actions . handleFetchError ( SIGN_UP_FAILED , error ) ) ; } ) ; } ; } , updatePassword ( oldPassword , password , confirmPassword ) { return ( dispatch ) : void => { dispatch ( { type : UPDATE_PASSWORD_START } ) ; fetch ( "/oauth2/password/update" , { oldPassword , password , confirmPassword } , "POST" , true ) . then ( ( json ) => { dispatch ( { type : UPDATE_PASSWORD_SUCCESS , redirectTask : redirectToLogin } ) ; toast ( ) . success ( "toast.user.update_successfully" ) ; } ) . catch ( ( error ) => { dispatch ( actions . handleFetchError ( UPDATE_PASSWORD_FAILED , error ) ) ; } ) ; } ; } , resetPassword ( email , OTP , password , confirmPassword ) { return ( dispatch ) : void => { dispatch ( { type : UPDATE_PASSWORD_START } ) ; fetch ( "/oauth2/password/reset" , { email , OTP , password , confirmPassword } , "POST" , false ) . then ( ( json ) => { dispatch ( { type : UPDATE_PASSWORD_SUCCESS , redirectTask : redirectToLogin } ) ; toast ( ) . success ( "toast.user.update_successfully" ) ; } ) . catch ( ( error ) => { dispatch ( actions . handleFetchError ( UPDATE_PASSWORD_FAILED , error ) ) ; } ) ; } ; } , sendOtp ( email ) { return ( dispatch ) : void => { dispatch ( { type : SEND_OTP_START } ) ; fetch ( "/oauth2/sendotp?email=" + email , undefined , "GET" ) . then ( ( json ) => { dispatch ( { type : SEND_OTP_SUCCESS } ) ; } ) . catch ( ( error ) => { dispatch ( actions . handleFetchError ( SEND_OTP_FAILED , error ) ) ; } ) ; const handle = setInterval ( ( ) => { dispatch ( { type : SEND_OTP_COOL_DOWN , handle : handle } ) ; } , 1000 ) ; } ; } } ; export default userActionCreator ;