{"size": 3751, "ext": "js", "max_stars_repo_path": "frontend/src/redux/reducers/userReducer.js", "max_stars_repo_name": "doramong0926/coinfactory_ico", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "frontend/src/redux/reducers/userReducer.js", "max_issues_repo_name": "doramong0926/coinfactory_ico", "max_issues_repo_issues_event_min_datetime": "2020-06-05T20:10:15.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T14:31:46.000Z", "max_forks_repo_path": "frontend/src/redux/reducers/userReducer.js", "max_forks_repo_name": "doramong0926/coinfactory_ico", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 9, "max_forks_count": null, "avg_line_length": 37.8888888889, "max_line_length": 136, "alphanum_fraction": 0.604638763}
import actionTypes from "../actionTypes";
import { 
    LOCAL_STORE_JWT, 
    LOCAL_STORE_USERNAME,
    LOCAL_STORE_EMAIL,
    LOCAL_STORE_REFERRAL_QUERY,
    LOCAL_STORE_REMEMBERME,
    LOCAL_STORE_TEMPKEY,
} from "../../config/constants"

const initialState = {
    isLoggedIn: (localStorage.getItem(LOCAL_STORE_JWT) ? true : false) || false,
    username: (localStorage.getItem(LOCAL_STORE_USERNAME) ? localStorage.getItem(LOCAL_STORE_USERNAME) : null) || null,
    email: (localStorage.getItem(LOCAL_STORE_EMAIL) ? localStorage.getItem(LOCAL_STORE_EMAIL) : null) || null,
    token: localStorage.getItem(LOCAL_STORE_JWT),    
    referralQuery: (localStorage.getItem(LOCAL_STORE_REFERRAL_QUERY) ? localStorage.getItem(LOCAL_STORE_REFERRAL_QUERY) : null) || null,
    profile: null,
    rememberme: (localStorage.getItem(LOCAL_STORE_REMEMBERME) ? localStorage.getItem(LOCAL_STORE_REMEMBERME) : null) || null,
    tempkey: (localStorage.getItem(LOCAL_STORE_TEMPKEY) ? localStorage.getItem(LOCAL_STORE_TEMPKEY) : null) || null,
}

export default (state = initialState, action) => {
    switch (action.type) {        
        case actionTypes.USER_SAVE_TEMP_KEY:
            localStorage.setItem(LOCAL_STORE_TEMPKEY, action.payload.tempkey);
            return Object.assign({}, state, {                
                tempkey: action.payload.tempkey,
            });

        case actionTypes.USER_SAVE_REMEMBER_ME:
            localStorage.setItem(LOCAL_STORE_REMEMBERME, action.payload.email);
            return Object.assign({}, state, {                
                referralQuery: action.payload.email,
            });

        case actionTypes.USER_SAVE_REFERRAL_QUERY:
            localStorage.setItem(LOCAL_STORE_REFERRAL_QUERY, action.payload.referralQuery);
            return Object.assign({}, state, {                
                referralQuery: action.payload.referralQuery,
            });

        case actionTypes.USER_SAVE_PROFILE:
            return Object.assign({}, state, {                
                profile: action.payload.profile,
            });

        case actionTypes.USER_LOG_IN:
            return Object.assign({}, state, {
                isLoggedIn: true,
            });

        case actionTypes.USER_LOG_OUT:
            return Object.assign({}, state, {
                isLoggedIn: false,
            });

        case actionTypes.USER_SAVE_JWT:
            localStorage.setItem(LOCAL_STORE_JWT, action.payload.token);
            return Object.assign({}, state, {
                // isLoggedIn: true,
                token: action.payload.token,
            });

        case actionTypes.USER_DELETE_JWT:
            localStorage.removeItem(LOCAL_STORE_JWT);
            return Object.assign({}, state, {
                // isLoggedIn: false,
                token: null,
            });

        case actionTypes.USER_SAVE_USERNAME:
            localStorage.setItem(LOCAL_STORE_USERNAME, action.payload.username);
            return Object.assign({}, state, {
                username: action.payload.username,
            });

        case actionTypes.USER_DELETE_USERNAME:
            localStorage.removeItem(LOCAL_STORE_USERNAME);
            return Object.assign({}, state, {
                username: null,
            });

        case actionTypes.USER_SAVE_EMAIL:
            localStorage.setItem(LOCAL_STORE_EMAIL, action.payload.email);
            return Object.assign({}, state, {
                email: action.payload.email,
            });

        case actionTypes.USER_DELETE_EMAIL:
            localStorage.removeItem(LOCAL_STORE_EMAIL);
            return Object.assign({}, state, {
                email: null,
            });

        default: 
            return state;
    }
}
