-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathInputsStore.ts
More file actions
186 lines (158 loc) · 5.21 KB
/
InputsStore.ts
File metadata and controls
186 lines (158 loc) · 5.21 KB
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
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import Reflux from 'reflux';
import * as URLUtils from 'util/URLUtils';
import fetch from 'logic/rest/FetchProvider';
import UserNotification from 'util/UserNotification';
import { singletonStore, singletonActions } from 'logic/singleton';
import { InputStaticFieldsStore } from 'stores/inputs/InputStaticFieldsStore';
import type { Input, ConfiguredInput } from 'components/messageloaders/Types';
type InputsActionsType = {
list: () => Promise<{ inputs: Array<Input>; total: number }>;
get: (id: string) => Promise<Input>;
getOptional: (id: string, showError: boolean) => Promise<Input>;
create: (input: ConfiguredInput) => Promise<{ id: string }>;
delete: (input: Input) => Promise<void>;
update: (id: string, input: ConfiguredInput) => Promise<void>;
};
type InputsStoreState = {
input?: Input | undefined;
inputs: Array<Input> | undefined;
};
export const InputsActions = singletonActions('core.Inputs', () =>
Reflux.createActions<InputsActionsType>({
list: { asyncResult: true },
get: { asyncResult: true },
getOptional: { asyncResult: true },
create: { asyncResult: true },
delete: { asyncResult: true },
update: { asyncResult: true },
}),
);
export const InputsStore = singletonStore('core.Inputs', () =>
Reflux.createStore<InputsStoreState>({
listenables: [InputsActions],
sourceUrl: '/system/inputs',
inputs: undefined,
input: undefined,
init() {
this.trigger(this._state());
this.listenTo(InputStaticFieldsStore, this.list);
},
getInitialState() {
return this._state();
},
_state() {
return { inputs: this.inputs, input: this.input };
},
list() {
const promise = fetch('GET', URLUtils.qualifyUrl(this.sourceUrl));
promise.then(
(response) => {
this.inputs = response.inputs;
this.trigger(this._state());
return this.inputs;
},
(error) => {
UserNotification.error(`Fetching Inputs failed with status: ${error}`, 'Could not retrieve Inputs');
},
);
InputsActions.list.promise(promise);
},
get(inputId) {
return this.getOptional(inputId, true);
},
getOptional(inputId, showError) {
const promise = fetch('GET', URLUtils.qualifyUrl(`${this.sourceUrl}/${inputId}`));
promise.then(
(response) => {
this.input = response;
this.trigger(this._state());
return this.input;
},
(error) => {
if (showError) {
UserNotification.error(
`Fetching input ${inputId} failed with status: ${error}`,
'Could not retrieve input',
);
} else {
this.trigger(this._state());
}
},
);
InputsActions.get.promise(promise);
},
create(input) {
const promise = fetch('POST', URLUtils.qualifyUrl(`${this.sourceUrl}?setup_wizard=true`), input);
promise.then(
() => {
UserNotification.success(`Input '${input.title}' launched successfully`);
InputsActions.list();
},
(error) => {
UserNotification.error(
`Launching input '${input.title}' failed with status: ${error}`,
'Could not launch input',
);
},
);
InputsActions.create.promise(promise);
},
delete(input) {
const inputId = input.id;
const inputTitle = input.title;
const promise = fetch('DELETE', URLUtils.qualifyUrl(`${this.sourceUrl}/${inputId}`));
promise.then(
() => {
UserNotification.success(`Input '${inputTitle}' deleted successfully`);
InputsActions.list();
},
(error) => {
UserNotification.error(
`Deleting input '${inputTitle}' failed with status: ${error}`,
'Could not delete input',
);
},
);
InputsActions.delete.promise(promise);
},
update(id, input) {
const promise = fetch('PUT', URLUtils.qualifyUrl(`${this.sourceUrl}/${id}`), input);
promise.then(
() => {
UserNotification.success(`Input '${input.title}' updated successfully`);
InputsActions.list();
},
(error) => {
UserNotification.error(
`Updating input '${input.title}' failed with status: ${error}`,
'Could not update input',
);
},
);
InputsActions.update.promise(promise);
},
}),
);
InputsStore.inputsAsMap = (inputsList) => {
const inputsMap = {};
inputsList.forEach((input) => {
inputsMap[input.id] = input;
});
return inputsMap;
};