-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConfigShows.vue
More file actions
337 lines (324 loc) · 10.1 KB
/
ConfigShows.vue
File metadata and controls
337 lines (324 loc) · 10.1 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<template>
<b-container class="mx-0" fluid>
<template v-if="loaded">
<b-row>
<b-col>
<b-table
id="shows-table"
:items="AVAILABLE_SHOWS"
:fields="showFields"
:per-page="rowsPerPage"
:current-page="currentPage"
>
<template #head(btn)="data">
<b-button v-b-modal.show-config variant="success"> Setup New Show </b-button>
</template>
<template #cell(btn)="data">
<b-button-group>
<b-button
variant="primary"
:disabled="
isSubmittingLoad || (CURRENT_SHOW != null && CURRENT_SHOW.id === data.item.id)
"
@click="loadShow(data.item)"
>
{{
(CURRENT_SHOW != null && CURRENT_SHOW.id !== data.item.id) ||
CURRENT_SHOW == null
? 'Load Show'
: 'Loaded'
}}
</b-button>
<b-button
variant="danger"
:disabled="CURRENT_SHOW != null && CURRENT_SHOW.id === data.item.id"
@click.stop.prevent="deleteShow(data.item)"
>
Delete
</b-button>
</b-button-group>
</template>
</b-table>
<b-pagination
v-show="AVAILABLE_SHOWS.length > rowsPerPage"
v-model="currentPage"
:total-rows="AVAILABLE_SHOWS.length"
:per-page="rowsPerPage"
aria-controls="shows-table"
class="justify-content-center"
/>
</b-col>
</b-row>
</template>
<b-row v-else>
<b-col class="text-center">
<b-spinner label="Loading shows..." variant="primary" />
</b-col>
</b-row>
<b-modal
id="show-config"
ref="modal"
title="Setup New Show"
:ok-disabled="isSubmittingShow"
@show="resetForm"
@hidden="resetForm"
@ok="onSubmit"
>
<div>
<b-form ref="form" @submit.stop.prevent="onSubmit">
<b-form-group id="name-input-group" label="Name" label-for="name-input">
<b-form-input
id="name-input"
v-model="$v.formState.name.$model"
name="name-input"
:state="validateState('name')"
aria-describedby="name-feedback"
/>
<b-form-invalid-feedback id="name-feedback">
This is a required field and must be less than 100 characters.
</b-form-invalid-feedback>
</b-form-group>
<b-form-group id="start-input-group" label="Start Date" label-for="start-input">
<b-form-input
id="start-input"
v-model="$v.formState.start.$model"
name="start-input"
type="date"
:state="validateState('start')"
aria-describedby="start-feedback"
/>
<b-form-invalid-feedback id="start-feedback">
This is a required field and must be before or the same as the end date.
</b-form-invalid-feedback>
</b-form-group>
<b-form-group id="end-input-group" label="End Date" label-for="end-input">
<b-form-input
id="end-input"
v-model="$v.formState.end.$model"
name="end-input"
type="date"
:state="validateState('end')"
aria-describedby="end-feedback"
/>
<b-form-invalid-feedback id="end-feedback">
This is a required field and must be after or the same as the start date.
</b-form-invalid-feedback>
</b-form-group>
</b-form>
</div>
<hr />
<div>
<b-button v-b-toggle.advanced-options-collapse>
<b-icon icon="gear-wide-connected" /> Advanced Options
</b-button>
<b-collapse id="advanced-options-collapse" style="margin-top: 0.5rem">
<b-card>
<b-form-group id="script-mode-group" label="Script Mode" label-for="script-mode-input">
<b-alert variant="secondary" show>
<p>Change the type of script for this show.</p>
</b-alert>
<b-form-select
id="script-mode-input"
v-model="$v.formState.script_mode.$model"
:options="SCRIPT_MODES"
:state="validateState('script_mode')"
/>
</b-form-group>
</b-card>
</b-collapse>
</div>
<template #modal-footer="{ ok, cancel }">
<b-button variant="secondary" @click.stop="cancel()"> Cancel </b-button>
<b-button variant="primary" :disabled="isSubmittingShow" @click.stop="saveAndLoad">
Save and Load
</b-button>
<b-button variant="primary" :disabled="isSubmittingShow" @click.stop="ok()">
Save
</b-button>
</template>
</b-modal>
</b-container>
</template>
<script>
import { required, maxLength } from 'vuelidate/lib/validators';
import { mapGetters, mapActions } from 'vuex';
import { makeURL } from '@/js/utils';
import log from 'loglevel';
export default {
name: 'ConfigShows',
data() {
return {
loaded: false,
showFields: [
{ key: 'id', label: 'ID' },
'name',
'start_date',
'end_date',
'created_at',
{ key: 'btn', label: '' },
],
rowsPerPage: 15,
currentPage: 1,
isSubmittingLoad: false,
isSubmittingShow: false,
formState: {
name: null,
start: null,
end: null,
script_mode: null,
},
};
},
validations: {
formState: {
name: {
required,
maxLength: maxLength(100),
},
start: {
required,
beforeEnd: (value, vm) =>
value == null && vm.end != null ? false : new Date(value) <= new Date(vm.end),
},
end: {
required,
afterStart: (value, vm) =>
value == null && vm.start != null ? false : new Date(value) >= new Date(vm.start),
},
script_mode: {
required,
},
},
},
computed: {
...mapGetters(['AVAILABLE_SHOWS', 'CURRENT_SHOW', 'SCRIPT_MODES']),
},
async mounted() {
await Promise.all([this.GET_AVAILABLE_SHOWS(), this.GET_SCRIPT_MODES()]);
this.loaded = true;
},
methods: {
async saveAndLoad(event) {
await this.saveShow(event, true);
},
async onSubmit(event) {
await this.saveShow(event, false);
},
async saveShow(event, load) {
this.$v.formState.$touch();
if (this.$v.formState.$anyError) {
event.preventDefault();
return;
}
if (this.isSubmittingShow) {
event.preventDefault();
return;
}
this.isSubmittingShow = true;
try {
const searchParams = new URLSearchParams({
load,
});
const response = await fetch(`${makeURL('/api/v1/show')}?${searchParams}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(this.formState),
});
if (response.ok) {
await this.GET_AVAILABLE_SHOWS();
this.$toast.success('Created new show!');
this.$bvModal.hide('show-config');
this.resetForm();
} else {
this.$toast.error('Unable to save show');
log.error('Unable to create new show');
event.preventDefault();
}
} catch (error) {
this.$toast.error('Unable to save show');
log.error('Error creating new show:', error);
event.preventDefault();
} finally {
this.isSubmittingShow = false;
}
},
async loadShow(show) {
if (this.isSubmittingLoad) {
return;
}
this.isSubmittingLoad = true;
try {
const response = await fetch(`${makeURL('/api/v1/settings')}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
current_show: show.id,
}),
});
if (response.ok) {
this.$toast.success('Loaded show!');
this.$bvModal.hide('show-load');
} else {
this.$toast.error('Unable to load show');
log.error('Unable to load show');
}
} catch (error) {
this.$toast.error('Unable to load show');
log.error('Error loading show:', error);
} finally {
this.isSubmittingLoad = false;
}
},
validateState(name) {
const { $dirty, $error } = this.$v.formState[name];
return $dirty ? !$error : null;
},
resetForm() {
this.formState = {
name: null,
start: null,
end: null,
script_mode: this.SCRIPT_MODES[0].value,
};
this.isSubmittingShow = false;
this.$nextTick(() => {
this.$v.$reset();
});
},
async deleteShow(item) {
if (this.CURRENT_SHOW != null && this.CURRENT_SHOW.id === item.id) {
this.$toast.error('Unable to delete currently loaded show');
return;
}
const msg = `Are you sure you want to delete ${item.name}? This will delete all data associated with this show and cannot be undone.`;
const action = await this.$bvModal.msgBoxConfirm(msg, {});
if (action !== true) {
return;
}
try {
const searchParams = new URLSearchParams({
id: item.id,
});
const response = await fetch(`${makeURL('/api/v1/show')}?${searchParams}`, {
method: 'DELETE',
});
if (response.ok) {
await this.GET_AVAILABLE_SHOWS();
this.$toast.success('Deleted show!');
} else {
this.$toast.error('Unable to delete show');
log.error('Unable to delete show');
}
} catch (error) {
this.$toast.error('Unable to delete show');
log.error('Error deleting show:', error);
}
},
...mapActions(['GET_AVAILABLE_SHOWS', 'GET_SCRIPT_MODES']),
},
};
</script>