-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogics.ts
82 lines (74 loc) · 2.99 KB
/
logics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { compact, find } from "lodash";
import { createLogic } from "redux-logic";
import { interaction, ReduxLogicDeps, selection } from "..";
import {
receiveAnnotations,
receiveCollections,
REQUEST_ANNOTATIONS,
REQUEST_COLLECTIONS,
} from "./actions";
import { AnnotationName, TOP_LEVEL_FILE_ANNOTATIONS } from "../../constants";
import AnnotationService from "../../services/AnnotationService";
import Annotation from "../../entity/Annotation";
/**
* Interceptor responsible for turning REQUEST_ANNOTATIONS action into a network call for available annotations. Outputs
* RECEIVE_ANNOTATIONS actions.
*/
const requestAnnotations = createLogic({
async process(deps: ReduxLogicDeps, dispatch, done) {
const { getState, httpClient } = deps;
const applicationVersion = interaction.selectors.getApplicationVersion(getState());
const baseUrl = interaction.selectors.getFileExplorerServiceBaseUrl(getState());
const displayAnnotations = selection.selectors.getAnnotationsToDisplay(getState());
const annotationService = new AnnotationService({
applicationVersion,
baseUrl,
httpClient,
});
let annotations: Annotation[] = [];
try {
annotations = await annotationService.fetchAnnotations();
dispatch(receiveAnnotations(annotations));
} catch (err) {
console.error("Failed to fetch annotations", err);
done();
return;
}
if (!displayAnnotations.length) {
const defaultDisplayAnnotations = compact([
find(
TOP_LEVEL_FILE_ANNOTATIONS,
(annotation) => annotation.name === AnnotationName.FILE_NAME
),
find(annotations, (annotation) => annotation.name === AnnotationName.KIND),
find(annotations, (annotation) => annotation.name === AnnotationName.TYPE),
find(
TOP_LEVEL_FILE_ANNOTATIONS,
(annotation) => annotation.name === AnnotationName.FILE_SIZE
),
]);
dispatch(selection.actions.selectDisplayAnnotation(defaultDisplayAnnotations, true));
}
done();
},
type: REQUEST_ANNOTATIONS,
});
/**
* Interceptor responsible for turning REQUEST_COLLECTIONS action into a network call for collections. Outputs
* RECEIVE_COLLECTIONS action.
*/
const requestCollections = createLogic({
async process(deps: ReduxLogicDeps, dispatch, done) {
const datasetService = interaction.selectors.getDatasetService(deps.getState());
try {
const datasets = await datasetService.getDatasets();
dispatch(receiveCollections(datasets));
} catch (err) {
console.error("Failed to fetch datasets", err);
} finally {
done();
}
},
type: [REQUEST_COLLECTIONS, interaction.actions.REFRESH],
});
export default [requestAnnotations, requestCollections];