|
| 1 | +import {normalize} from 'normalizr' |
| 2 | + |
| 3 | +import {getGraphQLFetcher} from 'src/common/util' |
| 4 | +import types from './types' |
| 5 | +import schemas from './schemas' |
| 6 | +import queries from './queries' |
| 7 | + |
| 8 | +export function deactivateMember(memberId) { |
| 9 | + return { |
| 10 | + types: [ |
| 11 | + types.DEACTIVATE_MEMBER_REQUEST, |
| 12 | + types.DEACTIVATE_MEMBER_SUCCESS, |
| 13 | + types.DEACTIVATE_MEMBER_FAILURE, |
| 14 | + ], |
| 15 | + shouldCallAPI: () => true, |
| 16 | + callAPI: (dispatch, getState) => { |
| 17 | + const query = queries.deactivateMember(memberId) |
| 18 | + return getGraphQLFetcher(dispatch, getState().auth)(query) |
| 19 | + .then(graphQLResponse => graphQLResponse.data.deactivateMember) |
| 20 | + }, |
| 21 | + redirect: _redirectMember, |
| 22 | + payload: {}, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +export function findMembers() { |
| 27 | + return { |
| 28 | + types: [ |
| 29 | + types.FIND_MEMBERS_REQUEST, |
| 30 | + types.FIND_MEMBERS_SUCCESS, |
| 31 | + types.FIND_MEMBERS_FAILURE, |
| 32 | + ], |
| 33 | + shouldCallAPI: () => true, |
| 34 | + callAPI: (dispatch, getState) => { |
| 35 | + const query = queries.findMembers() |
| 36 | + return getGraphQLFetcher(dispatch, getState().auth)(query) |
| 37 | + .then(graphQLResponse => graphQLResponse.data.findMembers) |
| 38 | + .then(members => normalize(members, schemas.members)) |
| 39 | + }, |
| 40 | + payload: {}, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export function getMemberSummary(identifier) { |
| 45 | + return { |
| 46 | + types: [ |
| 47 | + types.GET_MEMBER_SUMMARY_REQUEST, |
| 48 | + types.GET_MEMBER_SUMMARY_SUCCESS, |
| 49 | + types.GET_MEMBER_SUMMARY_FAILURE, |
| 50 | + ], |
| 51 | + shouldCallAPI: () => true, |
| 52 | + callAPI: (dispatch, getState) => { |
| 53 | + const query = queries.getMemberSummary(identifier) |
| 54 | + return getGraphQLFetcher(dispatch, getState().auth)(query) |
| 55 | + .then(graphQLResponse => graphQLResponse.data.getMemberSummary) |
| 56 | + }, |
| 57 | + payload: {}, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export function reassignMembersToChapter(memberIds, chapterId) { |
| 62 | + return { |
| 63 | + types: [ |
| 64 | + types.REASSIGN_MEMBERS_TO_CHAPTER_REQUEST, |
| 65 | + types.REASSIGN_MEMBERS_TO_CHAPTER_SUCCESS, |
| 66 | + types.REASSIGN_MEMBERS_TO_CHAPTER_FAILURE, |
| 67 | + ], |
| 68 | + shouldCallAPI: () => true, |
| 69 | + callAPI: (dispatch, getState) => { |
| 70 | + const mutation = queries.reassignMembersToChapter(memberIds, chapterId) |
| 71 | + return getGraphQLFetcher(dispatch, getState().auth)(mutation) |
| 72 | + .then(graphQLResponse => graphQLResponse.data.reassignMembersToChapter) |
| 73 | + .then(members => normalize(members, schemas.members)) |
| 74 | + }, |
| 75 | + redirect: '/members', |
| 76 | + payload: {memberIds, chapterId}, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export function updateMember(values) { |
| 81 | + return { |
| 82 | + types: [ |
| 83 | + types.UPDATE_MEMBER_REQUEST, |
| 84 | + types.UPDATE_MEMBER_SUCCESS, |
| 85 | + types.UPDATE_MEMBER_FAILURE, |
| 86 | + ], |
| 87 | + shouldCallAPI: () => true, |
| 88 | + callAPI: (dispatch, getState) => { |
| 89 | + const mutation = queries.updateMember(values) |
| 90 | + return getGraphQLFetcher(dispatch, getState().auth)(mutation) |
| 91 | + .then(graphQLResponse => graphQLResponse.data.updateMember) |
| 92 | + }, |
| 93 | + redirect: _redirectMember, |
| 94 | + payload: {}, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +function _redirectMember(member) { |
| 99 | + return member && member.handle ? `/members/${member.handle}` : '/members' |
| 100 | +} |
0 commit comments