requests.ts 3.41 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
1
import * as _ from 'lodash';
Matija Obreza's avatar
Matija Obreza committed
2
import {getRequest, listRequests, sendValidationEmail, recheckPid, validateRequest} from 'actions/genesys/requestService';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
3
4

// constants
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
5
import {APPEND_MATERIAL_REQUESTS, RECEIVE_MATERIAL_REQUEST, RECEIVE_MATERIAL_REQUESTS} from 'constants/requests';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
6
7
8

// models
import MaterialRequest from 'model/MaterialRequest';
Matija Obreza's avatar
Matija Obreza committed
9
import FilteredPage, {IPageRequest} from 'model/FilteredPage';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
10
11


Matija Obreza's avatar
Matija Obreza committed
12
const receiveRequests = (paged: FilteredPage<MaterialRequest>, error = null) => ({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
13
14
15
16
    type: RECEIVE_MATERIAL_REQUESTS,
    payload: { paged, error },
});

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
17
18
19
20
21
const appendRequests = (paged: FilteredPage<MaterialRequest>, error = null) => ({
    type: APPEND_MATERIAL_REQUESTS,
    payload: { paged, error },
});

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
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
48
49
50
51
52
53
const receiveRequest = (request: MaterialRequest, error = null) => ({
    type: RECEIVE_MATERIAL_REQUEST,
    payload: { request, error },
});


const refreshRequestPID = (request: MaterialRequest) => (dispatch, getState) => {
    const page = getState().requests.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));
    }
};

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

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

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

Matija Obreza's avatar
Matija Obreza committed
54
55
56
57
export { listRequests as listMaterialRequestsPromise };

export const listMaterialRequests = (page: IPageRequest) => (dispatch) => {
    return dispatch(listRequests(null, page))
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
58
        .then((page) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
59
60
61
62
63
            if (page.number === 0) {
                dispatch(receiveRequests(page));
            } else {
                dispatch(appendRequests(page));
            }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
64
65
        })
        .catch((error) => {
66
            dispatch(receiveRequests(null, error));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
67
68
69
70
71
72
73
74
75
        });
};

export const loadMaterialRequest = (uuid: string) => (dispatch) => {
    return dispatch(getRequest(uuid))
        .then((request) => {
            dispatch(receiveRequest(request));
        })
        .catch((error) => {
76
            dispatch(receiveRequest(null, error));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
77
78
79
        });
};

Matija Obreza's avatar
Matija Obreza committed
80
81
export const recheckPidAction = (uuid: string) => (dispatch) => {
    return dispatch(recheckPid(uuid))
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
82
83
84
85
86
        .then((request) => {
            dispatch(receiveRequest(request));
            dispatch(refreshRequestPID(request));
        })
        .catch((error) => {
87
            dispatch(receiveRequest(null, error));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
88
89
90
91
92
93
94
95
96
97
98
        });
};


export const validateRequestAction = (uuid: string) => (dispatch) => {
    return dispatch(validateRequest(uuid))
        .then((request) => {
            dispatch(receiveRequest(request));
            dispatch(refreshRequest(request));
        })
        .catch((error) => {
99
            dispatch(receiveRequest(null, error));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
100
101
102
        });
};

Matija Obreza's avatar
Matija Obreza committed
103
104
export const sendValidationEmailAction = (uuid: string) => (dispatch) => {
    return dispatch(sendValidationEmail(uuid))
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
105
106
107
108
109
        .then((request) => {
            dispatch(receiveRequest(request));
            dispatch(refreshRequest(request));
        })
        .catch((error) => {
110
            dispatch(receiveRequest(null, error));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
111
112
        });
};