-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
408 additions
and
137 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// @flow | ||
|
||
import { combineReducers } from 'redux'; | ||
import { success } from './notifications'; | ||
import { CALL_API } from '../middleware/api'; | ||
import { HTTP_METHODS } from '../constants/api'; | ||
import type { ActionType, UsersType, UserType } from '../types'; | ||
|
||
const OPEN_USER_MODAL = 'our-wedding-heroes/users/OPEN_USER_MODAL'; | ||
const CLOSE_USER_MODAL = 'our-wedding-heroes/users/CLOSE_USER_MODAL'; | ||
const LOAD_USERS_REQUEST = 'our-wedding-heroes/users/LOAD_USERS_REQUEST'; | ||
const LOAD_USERS_SUCCESS = 'our-wedding-heroes/users/LOAD_USERS_SUCCESS'; | ||
const LOAD_USERS_ERROR = 'our-wedding-heroes/users/LOAD_USERS_ERROR'; | ||
const CREATE_USER_REQUEST = 'our-wedding-heroes/users/CREATE_USER_REQUEST'; | ||
const CREATE_USER_SUCCESS = 'our-wedding-heroes/users/CREATE_USER_SUCCESS'; | ||
const CREATE_USER_ERROR = 'our-wedding-heroes/users/CREATE_USER_ERROR'; | ||
const DELETE_USER_REQUEST = 'our-wedding-heroes/users/DELETE_USER_REQUEST'; | ||
const DELETE_USER_SUCCESS = 'our-wedding-heroes/users/DELETE_USER_SUCCESS'; | ||
const DELETE_USER_ERROR = 'our-wedding-heroes/users/DELETE_USER_ERROR'; | ||
const CHANGE_PASSWORD_REQUEST = 'our-wedding-heroes/users/CHANGE_PASSWORD_REQUEST'; | ||
const CHANGE_PASSWORD_SUCCESS = 'our-wedding-heroes/users/CHANGE_PASSWORD_SUCCESS'; | ||
const CHANGE_PASSWORD_ERROR = 'our-wedding-heroes/users/CHANGE_PASSWORD_ERROR'; | ||
|
||
const userModalOpen = (state: boolean = false, action: ActionType) => { | ||
switch (action.type) { | ||
case OPEN_USER_MODAL: | ||
return true; | ||
case CLOSE_USER_MODAL: | ||
return false; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const loading = (state: boolean = false, action: ActionType) => { | ||
switch (action.type) { | ||
case LOAD_USERS_REQUEST: | ||
return true; | ||
case LOAD_USERS_SUCCESS: | ||
case LOAD_USERS_ERROR: | ||
return false; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const saving = (state: boolean = false, action: ActionType) => { | ||
switch (action.type) { | ||
case CREATE_USER_REQUEST: | ||
return true; | ||
case CREATE_USER_SUCCESS: | ||
case CREATE_USER_ERROR: | ||
return false; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const deleting = (state: boolean = false, action: ActionType) => { | ||
switch (action.type) { | ||
case DELETE_USER_REQUEST: | ||
return true; | ||
case DELETE_USER_SUCCESS: | ||
case DELETE_USER_ERROR: | ||
return false; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const users = (state: UsersType = [], action: ActionType) => { | ||
switch (action.type) { | ||
case LOAD_USERS_SUCCESS: | ||
return action.payload; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default combineReducers({ userModalOpen, loading, saving, deleting, users }); | ||
|
||
export const openUserModal = (): ActionType => ({ type: OPEN_USER_MODAL }); | ||
export const closeUserModal = (): ActionType => ({ type: CLOSE_USER_MODAL }); | ||
|
||
export const loadUsers = () => ({ | ||
[CALL_API]: { | ||
endpoint: 'user', | ||
method: HTTP_METHODS.GET, | ||
authenticated: true, | ||
types: [LOAD_USERS_REQUEST, LOAD_USERS_SUCCESS, LOAD_USERS_ERROR], | ||
}, | ||
}); | ||
|
||
export const createUser = (user: UserType) => ({ | ||
[CALL_API]: { | ||
data: user, | ||
endpoint: 'user', | ||
method: HTTP_METHODS.POST, | ||
authenticated: true, | ||
onSuccess: dispatch => { | ||
dispatch(success({ message: 'User created successfully' })); | ||
dispatch(closeUserModal()); | ||
}, | ||
types: [CREATE_USER_REQUEST, CREATE_USER_SUCCESS, CREATE_USER_ERROR], | ||
}, | ||
}); | ||
|
||
export const deleteUser = (user: UserType) => ({ | ||
[CALL_API]: { | ||
data: user, | ||
endpoint: `user/${user.id}`, | ||
method: HTTP_METHODS.DELETE, | ||
authenticated: true, | ||
onSuccess: dispatch => { | ||
dispatch(success({ message: 'User deleted successfully' })); | ||
}, | ||
types: [DELETE_USER_REQUEST, DELETE_USER_SUCCESS, DELETE_USER_ERROR], | ||
}, | ||
}); | ||
|
||
// TODO: extract to profile | ||
export const changePassword = (user: UserType) => ({ | ||
[CALL_API]: { | ||
data: user, | ||
endpoint: 'user/password', | ||
method: HTTP_METHODS.PUT, | ||
authenticated: true, | ||
onSuccess: dispatch => { | ||
dispatch(success({ message: 'Password Changed' })); | ||
}, | ||
types: [CHANGE_PASSWORD_REQUEST, CHANGE_PASSWORD_SUCCESS, CHANGE_PASSWORD_ERROR], | ||
}, | ||
}); |
Oops, something went wrong.