public.ts 3.21 KB
Newer Older
1 2 3
import update from 'immutability-helper';
import * as _ from 'lodash';

4 5 6 7 8 9
import {
  APPEND_DATASET_PAGE,
  RECEIVE_DATASET,
  APPEND_ACCESSIONS_PAGE,
  RECEIVE_DATASET_OVERVIEW,
} from 'datasets/constants';
10

11 12 13
import { LOGIN_APP, LOGIN_USER, LOGOUT } from 'constants/login';

import Dataset from 'model/catalog/Dataset';
14
import { AccessionRef } from 'model/accession/AccessionRef';
15
import Page from 'model/Page';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
16 17
import FilteredPage from 'model/FilteredPage';
import ApiCall from 'model/ApiCall';
18
import DatasetOverview from 'model/catalog/DatasetOverview';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
19
import {dereferenceReferences} from 'utilities';
20 21

const INITIAL_STATE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
22 23
  dataset: ApiCall<Dataset>,
  paged: ApiCall<FilteredPage<Dataset>>,
24
  overview: ApiCall<DatasetOverview>,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
25
  accessionRefs: ApiCall<Page<AccessionRef>>,
26 27 28
} = {
  dataset: null,
  paged: null,
29
  overview: null,
30
  accessionRefs: null,
31 32 33 34 35 36 37 38 39 40 41 42
};

function datasetsPublic(state = INITIAL_STATE, action: { type?: string, payload?: any } = { type: '', payload: {} }) {

  switch (action.type) {
    case LOGIN_USER:
    case LOGIN_APP:
    case LOGOUT: {
      return update(state, { $set: INITIAL_STATE });
    }

    case RECEIVE_DATASET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
43 44 45
      const {apiCall} = action.payload;

      const receivedIndex = state.paged && state.paged.data ? _.findIndex(state.paged.data.content, (item) => item.uuid === apiCall.uuid) : -1;
46
      const mustRemoveAccessions = state.dataset && state.dataset.data && apiCall.data && (apiCall.uuid !== state.dataset.data.uuid);
47 48 49

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

67
    case APPEND_ACCESSIONS_PAGE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
68
      const {apiCall: {loading, error, timestamp, data}} = action.payload;
69

70
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
71 72 73 74 75
        accessionRefs: {
          $set: {
            loading,
            error,
            timestamp,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
76
            data: Page.merge(state.accessionRefs && state.accessionRefs.data, data),
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
77 78
          },
        },
79 80 81 82
      });
    }

    case APPEND_DATASET_PAGE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
83 84 85 86 87 88 89 90 91 92 93
      const {apiCall: {loading, error, timestamp, data}} = action.payload;
      if (data) {
        dereferenceReferences(data.content, 'owner');
      }
      return update(state, {
        paged: {
          $set: {
            loading,
            error,
            timestamp,
            data: FilteredPage.merge(state.paged && state.paged.data, data),
94
          },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
95 96
        },
      });
97 98
    }

99 100 101 102 103 104 105 106 107 108 109 110 111 112
    case RECEIVE_DATASET_OVERVIEW: {
      const { apiCall: { loading, error, timestamp, data } } = action.payload;
      return update(state, {
        overview: {
          $set: {
            loading,
            error,
            timestamp,
            data: data !== undefined ? data : state.overview && state.overview.data,
          },
        },
      });
    }

113 114 115 116 117 118
    default:
      return state;
  }
}

export default datasetsPublic;