|
| 1 | +import { GET_ALL_MEMBERS, GET_ORG_OVERVIEW, GET_PERSONAL_OVERVIEW, SEARCH_MEMBER, BLOCK_USER, UNBLOCK_USER } from './types' |
| 2 | +import axios from 'axios' |
| 3 | +import { errorHandler } from '../utils/errorHandler' |
| 4 | +import { setRequestStatus } from '../utils/setRequestStatus' |
| 5 | + |
| 6 | +// GET ORGANIZATIONAL OVERVIEW |
| 7 | +export const getOrgOverview = () => async (dispatch) => { |
| 8 | + try { |
| 9 | + const res = await axios.get('/org/overview/all') |
| 10 | + dispatch(setRequestStatus(false)) |
| 11 | + if (res.status === 200) { |
| 12 | + dispatch(setRequestStatus(true)) |
| 13 | + console.log('getOrgOverview ', res.data) |
| 14 | + dispatch({ |
| 15 | + type: GET_ORG_OVERVIEW, |
| 16 | + payload: res.data.orgOverView |
| 17 | + }) |
| 18 | + } |
| 19 | + } catch (error) { |
| 20 | + dispatch(errorHandler(error)) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// GET PERSONAL OVERVIEW |
| 25 | +export const getPersonalOverview = () => async (dispatch) => { |
| 26 | + try { |
| 27 | + const res = await axios.get('/user/overview') |
| 28 | + dispatch(setRequestStatus(false)) |
| 29 | + if (res.status === 200) { |
| 30 | + dispatch(setRequestStatus(true)) |
| 31 | + console.log('getPersonalOverview ', res.data) |
| 32 | + dispatch({ |
| 33 | + type: GET_PERSONAL_OVERVIEW, |
| 34 | + payload: res.data.personalOverview |
| 35 | + }) |
| 36 | + } |
| 37 | + } catch (error) { |
| 38 | + dispatch(errorHandler(error)) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// GET ALL MEMBERS |
| 43 | +export const getMembers = () => async (dispatch) => { |
| 44 | + try { |
| 45 | + const res = await axios.get('/org/members/all') |
| 46 | + dispatch(setRequestStatus(false)) |
| 47 | + if (res.status === 200) { |
| 48 | + dispatch(setRequestStatus(true)) |
| 49 | + console.log('getMembers ', res.data) |
| 50 | + dispatch({ |
| 51 | + type: GET_ALL_MEMBERS, |
| 52 | + payload: res.data.members |
| 53 | + }) |
| 54 | + } |
| 55 | + } catch (error) { |
| 56 | + dispatch(errorHandler(error)) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// SEARCH MEMBER BY FIRST NAME, LAST NAME, FULL NAME |
| 61 | +export const getMember = (query) => async (dispatch) => { |
| 62 | + try { |
| 63 | + console.log('Looking for member ', query) |
| 64 | + const res = await axios.get(`/org/members/all?search=${query}`) |
| 65 | + dispatch(setRequestStatus(false)) |
| 66 | + if (res.status === 200) { |
| 67 | + dispatch(setRequestStatus(true)) |
| 68 | + console.log('getMember by name ', res.data) |
| 69 | + dispatch({ |
| 70 | + type: SEARCH_MEMBER, |
| 71 | + payload: res.data.member |
| 72 | + }) |
| 73 | + } |
| 74 | + } catch (error) { |
| 75 | + dispatch(errorHandler(error)) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// BLOCK A USER (BY ADMIN) |
| 80 | +export const blockUser = (userId) => async (dispatch) => { |
| 81 | + try { |
| 82 | + const res = await axios.patch(`/user/block/${userId}`) |
| 83 | + dispatch(setRequestStatus(false)) |
| 84 | + if (res.status === 200) { |
| 85 | + dispatch(setRequestStatus(true)) |
| 86 | + console.log('user blocked!', res.data) |
| 87 | + dispatch({ |
| 88 | + type: BLOCK_USER, |
| 89 | + payload: true |
| 90 | + }) |
| 91 | + } |
| 92 | + } catch (error) { |
| 93 | + dispatch(errorHandler(error)) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// UNBLOCK A USER (BY ADMIN) |
| 98 | +export const unBlockUser = (userId) => async (dispatch) => { |
| 99 | + try { |
| 100 | + const res = await axios.patch(`/user/unblock/${userId}`) |
| 101 | + dispatch(setRequestStatus(false)) |
| 102 | + if (res.status === 200) { |
| 103 | + dispatch(setRequestStatus(true)) |
| 104 | + console.log('user unblocked!', res.data) |
| 105 | + dispatch({ |
| 106 | + type: UNBLOCK_USER, |
| 107 | + payload: false // isBlocked = false |
| 108 | + }) |
| 109 | + } |
| 110 | + } catch (error) { |
| 111 | + dispatch(errorHandler(error)) |
| 112 | + } |
| 113 | +} |
0 commit comments