Created
December 26, 2017 16:08
-
-
Save gogumai/8ab4c99cd27feba8d2f40e410c913c63 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import ApiClient from '../api'; | |
| import { loginOptions, landing } from './navigationRedux'; | |
| import { persistance } from '../helpers'; | |
| // ACTIONS | |
| export const CREATE_USER_PENDING = 'Kaigi/Users/CREATE_USER_PENDING'; | |
| export const CREATE_USER_SUCCESS = 'Kaigi/Users/CREATE_USER_SUCCESS'; | |
| export const CREATE_USER_FAILURE = 'Kaigi/Users/CREATE_USER_FAILURE'; | |
| export const USER_LOGOUT = 'Kaigi/Users/USER_LOGOUT'; | |
| export const UPDATE_USER_ORGS_PENDING = 'Kaigi/Users/UPDATE_USER_ORGS_PENDING'; | |
| export const UPDATE_USER_ORGS_SUCCESS = 'Kaigi/Users/UPDATE_USER_ORGS_SUCCESS'; | |
| export const UPDATE_USER_ORGS_FAILURE = 'Kaigi/Users/UPDATE_USER_ORGS_FAILURE'; | |
| export const GUEST_SAVE_TOKEN = 'Kaigi/Users/GUEST_SAVE_TOKEN'; | |
| /** | |
| * NOTICE: Organizations data is included in the store but logic is not implemented | |
| * yet. Leaving it here for a future implementation. | |
| */ | |
| // REDUCER | |
| const initialState = { isLogged: false, organizations: [], userData: {}, isFetching: false }; | |
| export default function reducer(state = initialState, action) { | |
| switch (action.type) { | |
| case CREATE_USER_PENDING: | |
| return { ...state, isFetching: true }; | |
| case CREATE_USER_SUCCESS: | |
| return { ...state, | |
| isFetching: false, | |
| isLogged: true, | |
| userData: { ...state.userData, ...action.user }, | |
| }; | |
| case CREATE_USER_FAILURE: | |
| return { ...state, isFetching: false, error: action.error }; | |
| case USER_LOGOUT: | |
| return { ...state, userData: {}, isLogged: false }; | |
| case UPDATE_USER_ORGS_PENDING: | |
| return { ...state, isFetching: true }; | |
| case UPDATE_USER_ORGS_SUCCESS: | |
| return { ...state, organizations: action.organizations }; | |
| case UPDATE_USER_ORGS_FAILURE: | |
| return { ...state, error: action.error }; | |
| case GUEST_SAVE_TOKEN: | |
| return { | |
| ...state, | |
| userData: { | |
| token: action.token, | |
| name: action.username, | |
| login: action.username, | |
| }, | |
| }; | |
| default: | |
| return state; | |
| } | |
| } | |
| // ACTION CREATORS | |
| function createUserPending() { | |
| return { type: CREATE_USER_PENDING }; | |
| } | |
| export function createUserSuccess(user) { | |
| return { type: CREATE_USER_SUCCESS, user }; | |
| } | |
| function createUserFailure({ error }) { | |
| return { type: CREATE_USER_FAILURE, error }; | |
| } | |
| export function createUserFromGithub(user) { | |
| return { type: CREATE_USER_SUCCESS, user }; | |
| } | |
| function userLogout() { | |
| return { type: USER_LOGOUT }; | |
| } | |
| function updateUserOrgsPending() { | |
| return { type: UPDATE_USER_ORGS_PENDING }; | |
| } | |
| function updateUserOrgsSuccess(organizations) { | |
| return { type: UPDATE_USER_ORGS_SUCCESS, organizations }; | |
| } | |
| function updateUserOrgsFailure(error) { | |
| return { type: UPDATE_USER_ORGS_FAILURE, error }; | |
| } | |
| function addGuestToken(token, username) { | |
| return { type: GUEST_SAVE_TOKEN, token, username }; | |
| } | |
| export function saveGuestToken(token, username) { | |
| return async (dispatch) => { | |
| dispatch(addGuestToken(token, username)); | |
| }; | |
| } | |
| // THUNK ACTIONS | |
| export function createNewUser({ id, username, avatar }) { | |
| return (dispatch) => { | |
| dispatch(createUserPending()); | |
| return ApiClient.createUser(id, username, avatar) | |
| .then((response) => { | |
| dispatch(createUserSuccess(response.data.user)); | |
| }) | |
| .catch(error => dispatch(createUserFailure(error))); | |
| }; | |
| } | |
| export function logout() { | |
| return async (dispatch) => { | |
| await persistance.removeLoggedUser(); | |
| await persistance.removeLoggedUser('@guest'); | |
| dispatch(userLogout()); | |
| dispatch(loginOptions()); | |
| }; | |
| } | |
| export function fetchUserOrgs({ userId }) { | |
| return (dispatch) => { | |
| dispatch(updateUserOrgsPending()); | |
| return ApiClient.getUserOrgs(userId) | |
| .then((response) => { | |
| dispatch(updateUserOrgsSuccess(response.data)); | |
| dispatch(landing()); | |
| }) | |
| .catch(error => dispatch(updateUserOrgsFailure(error))); | |
| }; | |
| } | |
| export function githubLoginSuccess(user) { | |
| return async (dispatch, getState) => { | |
| dispatch(createUserFromGithub(user)); | |
| await persistance.saveLoggedUser(user); | |
| const userInfo = await ApiClient.getUserInformation(); | |
| dispatch(createUserFromGithub(userInfo.data.user)); | |
| const { id } = user; | |
| dispatch(fetchUserOrgs({ userId: id })); | |
| return getState().user.userData; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment