admin.ts 3.54 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
56
57
58
59
60
61
62
    .then((page) => {
      if (page.number === 0) {
        dispatch(receiveRequests(page));
      } else {
        dispatch(appendRequests(page));
      }
    })
    .catch((error) => {
      dispatch(receiveRequests(null, error));
    });
};

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

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


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

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

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) => {
  dispatch(navigateTo(paged.filterCode ? `/admin/requests/${paged.filterCode}` : '/admin/requests'));
};