-
Notifications
You must be signed in to change notification settings - Fork 263
/
TimelineScreen.jsx
240 lines (208 loc) · 6.68 KB
/
TimelineScreen.jsx
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import React, { Component } from 'react';
import { compose } from 'redux';
import { connect } from 'react-redux';
import withRouter from 'app/withRouter';
import {
fetchEntitySet,
queryEntities,
entitySetAddEntity,
updateEntitySet,
updateEntitySetItemMutate,
} from 'actions';
import { selectModel, selectEntitySet, selectEntitiesResult } from 'selectors';
import { entitySetEntitiesQuery, entitySuggestQuery } from 'queries';
import Screen from 'components/Screen/Screen';
import EntitySetManageMenu from 'components/EntitySet/EntitySetManageMenu';
import CollectionWrapper from 'components/Collection/CollectionWrapper';
import LoadingScreen from 'components/Screen/LoadingScreen';
import ErrorScreen from 'components/Screen/ErrorScreen';
import collectionViewIds from 'components/Collection/collectionViewIds';
import CollectionView from 'components/Collection/CollectionView';
import { Breadcrumbs, UpdateStatus } from 'components/common';
import { Timeline } from 'components/Timeline';
export class TimelineScreen extends Component {
constructor(props) {
super(props);
this.state = {
updateStatus: null,
};
this.fetchEntitySuggestions = this.fetchEntitySuggestions.bind(this);
this.onEntityCreateOrUpdate = this.onEntityCreateOrUpdate.bind(this);
this.onEntityRemove = this.onEntityRemove.bind(this);
this.onLayoutUpdate = this.onLayoutUpdate.bind(this);
}
componentDidMount() {
this.fetchIfNeeded();
}
componentDidUpdate() {
this.fetchIfNeeded();
}
fetchIfNeeded() {
const {
entitySetId,
timeline,
entities,
fetchEntitySet,
queryEntities,
entitiesQuery,
} = this.props;
if (timeline.shouldLoad) {
fetchEntitySet({ id: entitySetId });
}
if (entities.shouldLoad) {
queryEntities({ query: entitiesQuery });
}
}
async fetchEntitySuggestions(schema, queryText) {
const { queryEntities, location, timeline, model } = this.props;
const query = entitySuggestQuery(
location,
timeline?.collection,
schema.name,
{ prefix: queryText }
);
const response = await queryEntities({ query });
return (response.result?.results || []).map((result) =>
model.getEntity(result)
);
}
onSearch(queryText) {
const { query } = this.props;
const newQuery = query.set('q', queryText);
this.updateQuery(newQuery);
}
updateQuery(newQuery) {
const { navigate, location } = this.props;
navigate({
pathname: location.pathname,
search: newQuery.toLocation(),
hash: location.hash,
});
}
async saveChanges(callback) {
try {
this.setState({ updateStatus: UpdateStatus.IN_PROGRESS });
const result = await callback();
this.setState({ updateStatus: UpdateStatus.SUCCESS });
return result;
} catch (error) {
this.setState({ updateStatus: UpdateStatus.ERROR });
throw error;
}
}
async onEntityCreateOrUpdate(entity) {
const { model, entitySetId, entitySetAddEntity } = this.props;
const result = await this.saveChanges(() =>
entitySetAddEntity({ entitySetId, entity, sync: true })
);
return model.getEntity({
id: result.data.id,
schema: result.data.schema,
properties: result.data.properties,
});
}
async onEntityRemove(entity) {
const { entitySetId, updateEntitySetItemMutate } = this.props;
this.saveChanges(() =>
updateEntitySetItemMutate({
entitySetId,
entityId: entity.id,
// The API to remove an entity from an entity set (without deleting
// the entity itself) is weird because entity sets are (or have been)
// also used to model entity profiles. Setting `no_judgement` will de
// facto remove the entity from the entity set.
judgement: 'no_judgement',
})
);
}
async onLayoutUpdate(layout) {
const { entitySetId, updateEntitySet } = this.props;
const entitySet = {
layout: {
edges: [],
...layout,
},
};
this.saveChanges(() => updateEntitySet(entitySetId, entitySet));
}
render() {
const { model, timeline, entities } = this.props;
const { updateStatus } = this.state;
if (timeline.isError || entities.isError) {
return <ErrorScreen error={timeline.error || entities.error} />;
}
// `isPending` will be true every time the query is loaded, including when
// it's refreshed due to mutations. We only want to show a loading indicator
// on first load, so we check if data has been loaded previously.
// TODO: It may be agood idea to add this to our data loading abstraction
// (for example as `isRefreshing`) rather than implementing this here.
if (
(timeline.isPending && !timeline.id) ||
(entities.isPending && !entities.page)
) {
return <LoadingScreen />;
}
const operation = <EntitySetManageMenu entitySet={timeline} />;
const status = <UpdateStatus status={updateStatus} />;
const breadcrumbs = (
<Breadcrumbs operation={operation} status={status}>
<Breadcrumbs.Text>
<CollectionView.Link
id={collectionViewIds.TIMELINES}
collection={timeline.collection}
icon
/>
</Breadcrumbs.Text>
<Breadcrumbs.EntitySet
key="timeline"
entitySet={timeline}
icon={false}
/>
</Breadcrumbs>
);
return (
<Screen title={timeline.label} description={timeline.summary || ''}>
<CollectionWrapper collection={timeline.collection}>
{breadcrumbs}
<Timeline
model={model}
entities={entities.results}
layout={timeline.layout}
writeable={timeline.writeable}
fetchEntitySuggestions={this.fetchEntitySuggestions}
onEntityCreateOrUpdate={this.onEntityCreateOrUpdate}
onEntityRemove={this.onEntityRemove}
onLayoutUpdate={this.onLayoutUpdate}
/>
</CollectionWrapper>
</Screen>
);
}
}
const mapStateToProps = (state, ownProps) => {
const { location, params } = ownProps;
const { entitySetId } = params;
const model = selectModel(state);
const timeline = selectEntitySet(state, entitySetId);
const entitiesQuery = entitySetEntitiesQuery(location, entitySetId, null)
.defaultSortBy('properties.date', 'asc')
.limit(1000);
const entities = selectEntitiesResult(state, entitiesQuery);
return {
model,
entitySetId,
entitiesQuery,
timeline,
entities,
};
};
export default compose(
withRouter,
connect(mapStateToProps, {
fetchEntitySet,
queryEntities,
entitySetAddEntity,
updateEntitySet,
updateEntitySetItemMutate,
})
)(TimelineScreen);