-
Notifications
You must be signed in to change notification settings - Fork 4
/
DiscoveryServices.tsx
146 lines (134 loc) · 4.94 KB
/
DiscoveryServices.tsx
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
import * as React from "react";
import {
GenericEditableConfigList,
EditableConfigListStateProps,
EditableConfigListDispatchProps,
EditableConfigListOwnProps,
} from "./EditableConfigList";
import { connect } from "react-redux";
import * as PropTypes from "prop-types";
import ActionCreator from "../actions";
import {
DiscoveryServicesData,
DiscoveryServiceData,
LibraryData,
LibraryRegistrationsData,
} from "../interfaces";
import ServiceWithRegistrationsEditForm from "./ServiceWithRegistrationsEditForm";
export interface DiscoveryServicesStateProps
extends EditableConfigListStateProps<DiscoveryServicesData> {
isFetchingLibraryRegistrations?: boolean;
}
export interface DiscoveryServicesDispatchProps
extends EditableConfigListDispatchProps<DiscoveryServicesData> {
registerLibrary: (data: FormData) => Promise<void>;
fetchLibraryRegistrations?: () => Promise<LibraryRegistrationsData>;
}
export interface DiscoveryServicesProps
extends DiscoveryServicesStateProps,
DiscoveryServicesDispatchProps,
EditableConfigListOwnProps {}
export class DiscoveryServiceEditForm extends ServiceWithRegistrationsEditForm<
DiscoveryServicesData
> {}
/** Right panel for discovery services on the system configuration page.
Shows a list of current discovery services and allows creating a new
service or editing or deleting an existing service. */
export class DiscoveryServices extends GenericEditableConfigList<
DiscoveryServicesData,
DiscoveryServiceData,
DiscoveryServicesProps
> {
EditForm = DiscoveryServiceEditForm;
listDataKey = "discovery_services";
itemTypeName = "discovery service";
urlBase = "/admin/web/config/discovery/";
identifierKey = "id";
labelKey = "name";
static childContextTypes: React.ValidationMap<any> = {
registerLibrary: PropTypes.func,
};
getChildContext() {
return {
registerLibrary: (library: LibraryData, registration_stage: string) => {
if (this.itemToEdit()) {
const data = new (window as any).FormData();
data.append("library_short_name", library.short_name);
data.append("registration_stage", registration_stage);
data.append("integration_id", this.itemToEdit().id);
this.props.registerLibrary(data).then(() => {
if (this.props.fetchLibraryRegistrations) {
this.props.fetchLibraryRegistrations();
}
});
}
},
};
}
UNSAFE_componentWillMount() {
super.UNSAFE_componentWillMount();
if (this.props.fetchLibraryRegistrations) {
this.props.fetchLibraryRegistrations();
}
}
}
function mapStateToProps(state, ownProps) {
const data = Object.assign(
{},
(state.editor.discoveryServices && state.editor.discoveryServices.data) ||
{}
);
if (state.editor.libraries && state.editor.libraries.data) {
data.allLibraries = state.editor.libraries.data.libraries;
}
if (
state.editor.discoveryServiceLibraryRegistrations &&
state.editor.discoveryServiceLibraryRegistrations.data
) {
data.libraryRegistrations =
state.editor.discoveryServiceLibraryRegistrations.data.library_registrations;
}
// fetchError = an error involving loading the list of discovery services; formError = an error upon
// submission of the create/edit form (including upon submitting a change to a library's registration).
return {
data: data,
responseBody:
state.editor.discoveryServices &&
state.editor.discoveryServices.successMessage,
fetchError: state.editor.discoveryServices.fetchError,
formError:
state.editor.discoveryServices.formError ||
(state.editor.registerLibraryWithDiscoveryService &&
state.editor.registerLibraryWithDiscoveryService.fetchError),
isFetching:
state.editor.discoveryServices.isFetching ||
state.editor.discoveryServices.isEditing ||
(state.editor.registerLibraryWithDiscoveryService &&
state.editor.registerLibraryWithDiscoveryService.isFetching),
isFetchingLibraryRegistrations:
state.editor.discoveryServiceLibraryRegistrations &&
state.editor.discoveryServiceLibraryRegistrations.isFetching,
};
}
function mapDispatchToProps(dispatch, ownProps) {
const actions = new ActionCreator(null, ownProps.csrfToken);
return {
fetchData: () => dispatch(actions.fetchDiscoveryServices()),
editItem: (data: FormData) => dispatch(actions.editDiscoveryService(data)),
deleteItem: (identifier: string | number) =>
dispatch(actions.deleteDiscoveryService(identifier)),
registerLibrary: (data: FormData) =>
dispatch(actions.registerLibraryWithDiscoveryService(data)),
fetchLibraryRegistrations: () =>
dispatch(actions.fetchDiscoveryServiceLibraryRegistrations()),
};
}
const ConnectedDiscoveryServices = connect<
DiscoveryServicesStateProps,
DiscoveryServicesDispatchProps,
EditableConfigListOwnProps
>(
mapStateToProps,
mapDispatchToProps
)(DiscoveryServices);
export default ConnectedDiscoveryServices;