public.ts 9.04 KB
Newer Older
1
2
3
/**
 * This is a top-level group for actions in /accessions/* routes.
 */
4
import * as _ from 'lodash';
5
import navigateTo from 'actions/navigation';
6
import {filterCodeToUrl} from 'actions/filterCode';
7
import FilteredPage, { IPageRequest } from 'model/FilteredPage';
8
9
10
import Accession from 'model/accession/Accession';
import AccessionFilter from 'model/accession/AccessionFilter';
import AccessionMapInfo from 'model/accession/AccessionMapInfo';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
11
import MapLayer from 'model/genesys/MapTileLayer';
12

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
13
14
15
16
17
18
19
import {
  RECEIVE_ACCESSION,
  RECEIVE_ACCESSION_OVERVIEW,
  RECEIVE_ACCESSION_MAPINFO,
  RECEIVE_ACCESSION_AUDIT_LOG,
  RECEIVE_TILE_LAYER,
  APPEND_ACCESSIONS,
20
  APPEND_ACCESSIONS_WITH_SUGGESTIONS,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
21
} from 'accessions/constants';
22
import AccessionService from 'service/genesys/AccessionService';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
23
import ClimateService from 'service/genesys/ClimateService';
Matija Obreza's avatar
Matija Obreza committed
24
import { showSnackbar } from 'actions/snackbar';
25
import Page from 'model/Page';
26
import TileClimate from 'model/genesys/TileClimate';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
27
28
import {createApiCaller, createPureApiCaller} from 'actions/ApiCall';
import ApiCall from 'model/ApiCall';
29

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
30
31
32
const receiveAccessionMapInfo = (apiCall: ApiCall<any>) => ({
  type: RECEIVE_ACCESSION_MAPINFO,
  payload: { apiCall },
33
34
});

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
35
36
// Wrapped API calls
const apiListAccessions = createApiCaller(AccessionService.list, APPEND_ACCESSIONS);
37
const apiListAccessionSugestions = createApiCaller(AccessionService.listSuggestions, APPEND_ACCESSIONS_WITH_SUGGESTIONS);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
38
39
const apiAccessionsOverview = createApiCaller(AccessionService.listOverview, RECEIVE_ACCESSION_OVERVIEW);
const apiAccessionsMapInfo = createApiCaller(AccessionService.mapInfo, RECEIVE_ACCESSION_MAPINFO);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
40

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
41
42
const apiGeoJson = createPureApiCaller(AccessionService.geoJson);
const apiClimateInfo = createPureApiCaller(ClimateService.getCurrentClimate);
Maxym Borodenko's avatar
Maxym Borodenko committed
43
const apiAutocomplete = createPureApiCaller(AccessionService.autocomplete);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
44

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
45
46
47
const apiListAccessionByUuid = createPureApiCaller(AccessionService.listAllByUuid);
const apiAccessionByUuid = createApiCaller(AccessionService.getDetailsByUuid, RECEIVE_ACCESSION);
const apiAccessionByDoi = createApiCaller(AccessionService.getDetailsByDoi, RECEIVE_ACCESSION);
48

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
49
50
const apiAccessionAuditByUuid = createApiCaller(AccessionService.getAccessionAuditLogByUUID, RECEIVE_ACCESSION_AUDIT_LOG);
const apiAccessionAuditByDoi = createApiCaller(AccessionService.getAccessionAuditLogByDoi, RECEIVE_ACCESSION_AUDIT_LOG);
51
52


Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
53
export const updateRoute = (paged: FilteredPage<Accession>, path: string = '/a') => (dispatch) => {
54
55
56
57
  const qs = {
    s: paged.sort[0].property === Accession.DEFAULT_SORT.property ? undefined : paged.sort[0].property,
    d: paged.sort[0].direction === Accession.DEFAULT_SORT.direction ? undefined : paged.sort[0].direction,
  };
58
  dispatch(updateRouteWithFilterCode(paged.filterCode, path, qs));
59
60
};

61
export const updateRouteWithFilterCode = (filterCode: string, path: string = '/a', qs?: any, postfix: string = '') => (dispatch) => {
62
  console.log(`Update route2 ${filterCode} ${path}`, qs);
63
  dispatch(filterCodeToUrl(path, filterCode, qs, postfix));
64
65
};

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
66
export const applyFilters = (filters: string | AccessionFilter, page: IPageRequest = { page: 0 }) => (dispatch, getState) => {
67
  console.log('Applying new filter', filters);
Matija Obreza's avatar
Matija Obreza committed
68
  dispatch(showSnackbar('Applying filters...'));
69
  dispatch(navigateTo('/a'));
Matija Obreza's avatar
Matija Obreza committed
70

71
  return dispatch(apiListAccessionSugestions(filters, page))
72
73
    .then((paged) => {
      dispatch(updateRoute(paged));
Matija Obreza's avatar
Matija Obreza committed
74
      dispatch(showSnackbar(`Filters applied.`));
75
76
77
    });
};

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
78
79
80
81
export const listAccessionsByUuid = (uuidList: string[]) => (dispatch) => {
  return dispatch(apiListAccessionByUuid(uuidList));
};

82
83
export const loadMoreAccessions = (paged?: FilteredPage<Accession>) => (dispatch) => {
  console.log('Load more accessions', paged);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
84
85
  return dispatch(apiListAccessions(paged ? paged.filterCode : '', Page.nextPage(paged)))
    .then((paged) => dispatch(updateRoute(paged)));
86
87
};

88
export const applyOverviewFilters = (filters: string | AccessionFilter) => (dispatch) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
89
  console.log('Applying new filter', filters);
90
  dispatch(navigateTo('/a/overview'));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
91
  return dispatch(apiAccessionsOverview(filters))
92
    .then((overview) => {
93
      dispatch(updateRouteWithFilterCode(overview.filterCode, '/a/overview'));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
94
95
96
97
    });
};

export const loadAccessionsOverviewPage = (filterCode: string) => (dispatch) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
98
99
100
101
102
103
104
105
106
  return dispatch(apiAccessionsOverview(filterCode));
};

export const geoJsonRequest = (filter, limit) => (dispatch) => {
  return dispatch(apiGeoJson(filter, limit));
};

export const currentClimateRequest = (lat, lng) => (dispatch) => {
  return dispatch(apiClimateInfo(lat, lng));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
107
108
};

109
export const loadAccessionsMapInfo = (filters: string | AccessionFilter, viewPort?: {center: number[], zoom: number}) => (dispatch) => {
110
  dispatch(navigateTo('/a/map'));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
111
  return dispatch(apiAccessionsMapInfo(filters))
112
    .then((mapInfo: AccessionMapInfo) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
113
114
115
      if (!(mapInfo.bounds[0][0] && mapInfo.bounds[0][1] && mapInfo.bounds[1][0] && mapInfo.bounds[1][1])) {
        mapInfo.bounds = [[-170, 80], [170, -80]];
        console.log('One of map bounds is null, setting to default.');
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
116
        dispatch(receiveAccessionMapInfo(ApiCall.success(mapInfo)));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
117
      }
118
119
120
121
122
123
      if (viewPort && viewPort.zoom && viewPort.center && viewPort.center.length === 2) {
        console.log(`viewPort @..,z${viewPort.zoom}`, viewPort);
        dispatch(updateRouteWithFilterCode(mapInfo.filterCode, '/a/map', null, viewPort && viewPort.center && `/@${viewPort.center[0]},${viewPort.center[1]},${viewPort.zoom}z`));
      } else {
        dispatch(updateRouteWithFilterCode(mapInfo.filterCode, '/a/map'));
      }
124
125
126
    });
};

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
127
export const loadAccession = ({ uuid, doi }: { uuid?: string, doi?: string }) => (dispatch) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
128
  const loader = doi ? apiAccessionByDoi : apiAccessionByUuid;
129
130
  const lookup = doi ? doi : uuid;

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
131
  return dispatch(loader(lookup));
132
};
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
133

134
export const loadAccessionAuditLog = ({uuid, doi}: {uuid?: string, doi?: string}) => (dispatch) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
135
  const loader = doi ? apiAccessionAuditByDoi : apiAccessionAuditByUuid;
136
137
  const lookup = doi ? doi : uuid;

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
138
  return dispatch(loader(lookup))
139
140
141
142
143
144
145
    .then((auditLog) => {
      if (_.isEmpty(auditLog.auditAccession) && _.isEmpty(auditLog.auditAccessionCollect) && _.isEmpty(auditLog.auditAccessionGeo) && _.isEmpty(auditLog.auditAccessionId)) {
        return dispatch(showSnackbar('common:label.noChanges'));
      }
    });
};

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
146
export const overviewAccessions = (filterCode: string) => (dispatch) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
147
  return dispatch(apiAccessionsOverview(filterCode));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
148
};
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
149
150
151
152
153
154
155

// TileLayers
const receiveTileLayerInfo = (tileLayer: MapLayer) => ({
  type: RECEIVE_TILE_LAYER,
  payload: {tileLayer},
});

156
export const updateTileLayerInfo = (tileLayer: MapLayer) => (dispatch) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
157
158
  return dispatch(receiveTileLayerInfo(tileLayer));
};
159
160
161
162
163
164
165
166
167


const makeRange = (variable: number, diff: number) => {
  return {
    ge: Math.round(variable - diff),
    le: Math.round(variable + diff),
  };
};

Maxym Borodenko's avatar
Maxym Borodenko committed
168
169
170
171
export const autocomplete = (field: string, term: string, filters: string | AccessionFilter) => (dispatch, getState) => {
  return dispatch(apiAutocomplete(field, term, filters));
};

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
export const applyClimateFilters = (climate: TileClimate, extraFilters?: any) => (dispatch) => {
  // BIO1 = Annual Mean Temperature !!
  // BIO2 = Mean Diurnal Range (Mean of monthly (max temp - min temp))
  // BIO3 = Isothermality (BIO2/BIO7) (* 100)
  // BIO4 = Temperature Seasonality (standard deviation *100)
  // BIO5 = Max Temperature of Warmest Month
  // BIO6 = Min Temperature of Coldest Month
  // BIO7 = Temperature Annual Range (BIO5-BIO6)
  // BIO8 = Mean Temperature of Wettest Quarter
  // BIO9 = Mean Temperature of Driest Quarter
  // BIO10 = Mean Temperature of Warmest Quarter
  // BIO11 = Mean Temperature of Coldest Quarter
  // BIO12 = Annual Precipitation
  // BIO13 = Precipitation of Wettest Month
  // BIO14 = Precipitation of Driest Month
  // BIO15 = Precipitation Seasonality (Coefficient of Variation)
  // BIO16 = Precipitation of Wettest Quarter
  // BIO17 = Precipitation of Driest Quarter
  // BIO18 = Precipitation of Warmest Quarter !!
  // BIO19 = Precipitation of Coldest Quarter !!
  const tempDiff = 2;

  const filter = {
    // taxa: {
    //   genus: [ accession.taxonomy.genus ],
    //   // species: [ accession.taxonomy.species ],
    // },
    geo: {
      climate: {
        // temperature
        bio1: makeRange(climate.bio1, tempDiff),
        // bio2: makeRange(climate.bio2, tempDiff),
        bio4: makeRange(climate.bio4, climate.bio4 * .50),

        // bio7: makeRange(climate.bio7, tempDiff),
        // bio8: makeRange(climate.bio8, tempDiff),
        // bio9: makeRange(climate.bio9, tempDiff),
        // bio10: makeRange(climate.bio10, tempDiff),
        // bio11: makeRange(climate.bio11, tempDiff),
        // precipitation
        bio12: makeRange(climate.bio12, climate.bio12 * .25),
        // bio13: makeRange(climate.bio13, precDif),
        // bio14: makeRange(climate.bio14, precDif),
        // bio15: makeRange(climate.bio15, climate.bio15 * .25),
        bio18: makeRange(climate.bio18, climate.bio18 * .25),
        bio19: makeRange(climate.bio19, climate.bio19 * .25),
      },
    },
    ...extraFilters,
  };
  dispatch(applyFilters(filter));
};

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
225