public.ts 4.04 KB
Newer Older
1 2 3
import update from 'immutability-helper';

import { IReducerAction } from 'model/common.model';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
4 5 6 7 8 9
import {
  RECEIVE_ACCESSION,
  RECEIVE_ACCESSION_OVERVIEW,
  APPEND_ACCESSIONS,
  RECEIVE_ACCESSION_MAPINFO,
  RECEIVE_ACCESSION_AUDIT_LOG,
10
  RECEIVE_TILE_LAYER, APPEND_ACCESSIONS_WITH_SUGGESTIONS,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
11
} from 'accessions/constants';
12 13

import FilteredPage from 'model/FilteredPage';
14 15 16
import Accession from 'model/accession/Accession';
import AccessionMapInfo from 'model/accession/AccessionMapInfo';
import AccessionOverview from 'model/accession/AccessionOverview';
17
import AccessionAuditLog from 'model/accession/AccessionAuditLog';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
18
import MapLayer, { AVAILABLE_LAYERS } from 'model/genesys/MapTileLayer';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
19
import ApiCall from 'model/ApiCall';
20 21

const INITIAL_STATE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
22 23 24
  accession: ApiCall<Accession>;
  auditLog: ApiCall<AccessionAuditLog>,
  paged: ApiCall<FilteredPage<Accession>>;
25
  suggestions: any;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
26 27
  overview: ApiCall<AccessionOverview>;
  mapInfo: ApiCall<AccessionMapInfo>;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
28
  mapLayers: MapLayer[]
29 30
} = {
  accession: null,
31
  auditLog: null,
32
  paged: null,
33
  suggestions: null,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
34
  overview: null,
35
  mapInfo: null,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
36
  mapLayers: AVAILABLE_LAYERS,
37 38
};

39
function publicAccessions(state = INITIAL_STATE, action: IReducerAction) {
40 41 42 43

  switch (action.type) {

    case RECEIVE_ACCESSION: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
44 45
      const {apiCall} = action.payload;
      const receivedIndex = state.paged && apiCall.data ? state.paged.data.content.findIndex((item) => item.uuid === apiCall.data.uuid) : -1;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
46 47 48

      if (receivedIndex !== -1) {
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
49
          accession: { $set: apiCall },
50
          auditLog: {$set: null},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
51
          paged: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
52 53 54 55
            data: {
              content: {
                [receivedIndex]: {$set: apiCall.data},
              },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
56 57 58 59 60
            },
          },
        });
      } else {
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
61
          accession: { $set: apiCall},
62
          auditLog: {$set: null},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
63 64
        });
      }
65 66
    }

67
    case RECEIVE_ACCESSION_AUDIT_LOG: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
68
      const {apiCall} = action.payload;
69
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
70
        auditLog: { $set: apiCall },
71 72 73
      });
    }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
74
    case RECEIVE_ACCESSION_OVERVIEW: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
75
      const { apiCall: { loading, error, timestamp, data } } = action.payload;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
76
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
77 78 79 80 81 82 83 84
        overview: { $set: {
            loading,
            error,
            timestamp,
            data: data !== undefined ? data : state.overview && state.overview.data,
          } },
        // paged: { $set: null },
        // mapInfo: { $set: null },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
85 86
      });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
87 88

    case APPEND_ACCESSIONS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
89 90 91 92 93 94 95 96 97 98
      const { apiCall: { loading, error, timestamp, data } } = action.payload;
      return update(state, {
          paged: { $set: {
            loading,
            error,
            timestamp,
            data: FilteredPage.merge(state.paged && state.paged.data, data),
          } },
          // mapInfo: { $set: null },
          // overview: { $set: null },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
99 100
        });
    }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    case APPEND_ACCESSIONS_WITH_SUGGESTIONS: {
      const {apiCall: {loading, error, timestamp, data}} = action.payload;
      return update(state, {
        paged: {
          $set: {
            loading,
            error,
            timestamp,
            data: FilteredPage.merge(state.paged && state.paged.data, data),
          },
        },
        suggestions: {$set: data ?  data.suggestions : state.suggestions},
        // mapInfo: { $set: null },
        // overview: { $set: null },
      });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
117

118
    case RECEIVE_ACCESSION_MAPINFO: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
119 120 121
      const { apiCall } = action.payload;
      apiCall.data = apiCall.loading && state.mapInfo ? state.mapInfo.data : apiCall.data;

122
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
123 124 125
        mapInfo: { $set: apiCall },
        // paged: {$set: null},
        // overview: {$set: null},
126 127
      });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
128 129 130 131 132 133 134 135 136 137
    case RECEIVE_TILE_LAYER: {
      const {tileLayer} = action.payload;
      const indexToUpdate = state.mapLayers.findIndex((layerItem) => layerItem.name === tileLayer.name);

      return update(state, {
        mapLayers: {
          [indexToUpdate]: {$set: tileLayer},
        },
      });
    }
138

139 140 141 142 143
    default:
      return state;
  }
}

144
export default publicAccessions;