admin.ts 2.71 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
1
2
3
import update from 'immutability-helper';

import { IReducerAction } from 'model/common.model';
4
import MaterialRequest from 'model/request/MaterialRequest';
Matija Obreza's avatar
Matija Obreza committed
5
import FilteredPage from 'model/FilteredPage';
6
import Accession from 'model/accession/Accession';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
7

8
9
10
11
12
13
import {
  ADMIN_APPEND_MATERIAL_REQUESTS,
  ADMIN_RECEIVE_MATERIAL_REQUEST,
  ADMIN_RECEIVE_MATERIAL_REQUEST_ACCESSIONS,
  ADMIN_RECEIVE_MATERIAL_REQUESTS,
} from 'requests/constants';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
14
15
16

const INITIAL_STATE: {
    request: Request;
17
    accessions: Accession[];
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
18
    requestError: any;
Matija Obreza's avatar
Matija Obreza committed
19
    paged: FilteredPage<MaterialRequest>;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
20
21
22
    pagedError: any;
} = {
    request: null,
23
    accessions: null,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
24
25
26
27
28
    requestError: null,
    paged: null,
    pagedError: null,
};

29
function adminRequests(state = INITIAL_STATE, action: IReducerAction) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
30
31
32

    switch (action.type) {

33
        case ADMIN_RECEIVE_MATERIAL_REQUEST: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
34
            const { request, error } = action.payload;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
35
36
37
38
39
            const receivedIndex = state.paged ? state.paged.content.findIndex((item) => item.uuid === request.uuid) : -1;

            if (receivedIndex !== -1) {
                return update(state, {
                    request: { $set: request },
40
                    accessions: {$set: null},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
41
42
43
44
45
46
47
48
49
50
                    paged: {
                        content: {
                          [receivedIndex]: {$set: request},
                        },
                    },
                    requestError: {$set: error},
                });
            } else {
                return update(state, {
                    request: { $set: request},
51
                    accessions: {$set: null},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
52
53
54
55
                    paged: {$set: null},
                    requestError: {$set: error},
                });
            }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
56
57
        }

58
59
60
61
62
63
        case ADMIN_RECEIVE_MATERIAL_REQUEST_ACCESSIONS: {
            const {accessions} = action.payload;
            return update(state, {
              accessions: {$set: accessions},
            });
        }
64
        case ADMIN_RECEIVE_MATERIAL_REQUESTS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
65
66
67
68
69
70
71
            const { paged, error } = action.payload;
            return update(state, {
                paged: { $set: paged },
                pagedError: { $set: error },
            });
        }

72
      case ADMIN_APPEND_MATERIAL_REQUESTS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
        const {paged, error} = action.payload;

        return !state.paged ? update(state, {
            paged: {$set: paged},
            pagedError: {$set: error},
          }) :
          update(state, {
            paged: {
              content: {$push: paged.content},
              number: {$set: paged.number},
              last: {$set: paged.last},
            },
            pagedError: {$set: error},
          });
      }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
88
89
90
91
92
        default:
            return state;
    }
}

93
export default adminRequests;