admin.ts 3.82 KB
Newer Older
1 2 3 4 5
import * as _ from 'lodash';

// constants
import {ADMIN_APPEND_MATERIAL_REQUESTS, ADMIN_RECEIVE_MATERIAL_REQUEST, ADMIN_RECEIVE_MATERIAL_REQUESTS} from 'requests/constants';

6 7 8 9 10 11
// actions
import navigateTo from 'actions/navigation';

// services
import RequestService from 'service/genesys/RequestService';

12
// models
13
import MaterialRequest from 'model/request/MaterialRequest';
14
import FilteredPage, { IPageRequest } from 'model/FilteredPage';
15
import Page from 'model/Page';
16
import MaterialRequestFilter from 'model/request/MaterialRequestFilter';
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47


const receiveRequests = (paged: FilteredPage<MaterialRequest>, error = null) => ({
  type: ADMIN_RECEIVE_MATERIAL_REQUESTS,
  payload: { paged, error },
});

const appendRequests = (paged: FilteredPage<MaterialRequest>, error = null) => ({
  type: ADMIN_APPEND_MATERIAL_REQUESTS,
  payload: { paged, error },
});

const receiveRequest = (request: MaterialRequest, error = null) => ({
  type: ADMIN_RECEIVE_MATERIAL_REQUEST,
  payload: { request, error },
});


const refreshRequestPID = (request: MaterialRequest) => (dispatch, getState) => {
  const page = getState().requests.admin.paged;
  if (page) {
    const toUpdateIndex = _.findIndex(page.content, (contentItem) => contentItem.uuid === request.uuid);

    if (toUpdateIndex !== -1) {
      page.content[toUpdateIndex].body.pid = request.body.pid;
    }

    return dispatch(receiveRequests(page));
  }
};

48 49
export const loadMoreRequests = (paged?: FilteredPage<MaterialRequest>) => (dispatch) => {
  return RequestService.list(paged && paged.filterCode, Page.nextPage(paged))
50 51 52 53 54 55
    .then((page) => {
      if (page.number === 0) {
        dispatch(receiveRequests(page));
      } else {
        dispatch(appendRequests(page));
      }
Oleksii Savran's avatar
Oleksii Savran committed
56
      dispatch(updateRoute(paged));
57 58 59 60 61 62 63
    })
    .catch((error) => {
      dispatch(receiveRequests(null, error));
    });
};

export const loadMaterialRequest = (uuid: string) => (dispatch) => {
64
  return RequestService.getRequest(uuid)
65 66 67 68 69 70 71 72 73
    .then((request) => {
      dispatch(receiveRequest(request));
    })
    .catch((error) => {
      dispatch(receiveRequest(null, error));
    });
};

export const recheckPidAction = (uuid: string) => (dispatch) => {
74
  return RequestService.recheckPid(uuid)
75 76 77 78 79 80 81 82 83 84 85
    .then((request) => {
      dispatch(receiveRequest(request));
      dispatch(refreshRequestPID(request));
    })
    .catch((error) => {
      dispatch(receiveRequest(null, error));
    });
};


export const validateRequestAction = (uuid: string) => (dispatch) => {
86
  return RequestService.validateRequest(uuid)
87 88 89 90 91 92 93 94 95
    .then((request) => {
      dispatch(receiveRequest(request));
    })
    .catch((error) => {
      dispatch(receiveRequest(null, error));
    });
};

export const sendValidationEmailAction = (uuid: string) => (dispatch) => {
96
  return RequestService.sendValidationEmail(uuid)
97 98 99 100 101 102 103
    .then((request) => {
      dispatch(receiveRequest(request));
    })
    .catch((error) => {
      dispatch(receiveRequest(null, error));
    });
};
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

export const applyFilters = (filters: string | MaterialRequestFilter, page: IPageRequest = { page: 0 }) => (dispatch) => {
  return RequestService.list(filters, page)
    .then((paged) => {
      if (paged.number === 0) {
        dispatch(receiveRequests(paged));
      } else {
        dispatch(appendRequests(paged));
      }
      dispatch(updateRoute(paged));
    }).catch((error) => {
      console.log(`API error`, error);
      dispatch(receiveRequests(null, error));
    });
};

export const updateRoute = (paged: FilteredPage<MaterialRequest>) => (dispatch) => {
Oleksii Savran's avatar
Oleksii Savran committed
121 122 123 124 125
  const qs = {
    s: paged.sort[0].property === MaterialRequest.DEFAULT_SORT.property ? undefined : paged.sort[0].property,
    d: paged.sort[0].direction === MaterialRequest.DEFAULT_SORT.direction ? undefined : paged.sort[0].direction,
  };
  dispatch(navigateTo(paged.filterCode ? `/admin/requests/${paged.filterCode}` : '/admin/requests', qs));
126 127
};