forked from learningequality/kolibri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManageSyncSchedule.vue
355 lines (335 loc) · 9.83 KB
/
ManageSyncSchedule.vue
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<template>
<ImmersivePage
:appBarTitle="$tr('syncSchedules')"
:route="goBackRoute"
>
<KPageContainer>
<KGrid gutter="48">
<KGridItem>
<h1>{{ $tr('syncSchedules') }}</h1>
</KGridItem>
<KGridItem>
<p>{{ $tr('introduction') }}</p>
</KGridItem>
<KGridItem
:layout8="{ span: 4 }"
:layout12="{ span: 6 }"
class="separate"
>
<b v-if="facility">{{ facility.name }}</b>
<KCircularLoader v-else />
</KGridItem>
<KGridItem
:layout="{ alignment: 'right' }"
:layout8="{ span: 4 }"
:layout12="{ span: 6 }"
class="separate"
>
<KButton @click="deviceModal = true">
{{ $tr('addDevice') }}
</KButton>
</KGridItem>
</KGrid>
<!-- creating the table-->
<CoreTable>
<template #tbody>
<tbody v-if="scheduledTasks.length > 0">
<tr>
<th>{{ coreString('deviceNameLabel') }}</th>
<th>{{ $tr('Schedule') }}</th>
<th>{{ coreString('statusLabel') }}</th>
<th></th>
</tr>
<tr
v-for="task in scheduledTasks"
:key="task.id"
>
<td>
<span>{{ task.deviceName }}<br >
{{ task.extra_metadata.baseurl }}
</span>
</td>
<td>
<div>
{{ scheduleTime(task.repeat_interval, task.scheduled_datetime) }}
</div>
</td>
<td>
<span v-if="task.deviceAvailable">
<KIcon icon="onDevice" />
<span>{{ $tr('connected') }}</span>
</span>
<span v-else-if="!task.isKDP">
<KIcon icon="disconnected" />
<span>{{ $tr('disconnected') }}</span>
</span>
<KEmptyPlaceholder v-else />
</td>
<td>
<KButton
class="right"
@click="editButton(task)"
>
{{ coreString('editAction') }}
</KButton>
</td>
</tr>
</tbody>
<tbody v-else>
<tr>
<th>{{ coreString('deviceNameLabel') }}</th>
<th>{{ $tr('Schedule') }}</th>
<th>{{ coreString('statusLabel') }}</th>
<th></th>
</tr>
<tr>
<td
colspan="3"
style="text-align: center"
>
<b>{{ $tr('NoSync') }}</b>
</td>
</tr>
</tbody>
</template>
</CoreTable>
<SyncFacilityModalGroup
v-if="deviceModal"
:facilityForSync="facility"
@close="closeModal"
@syncKDP="handleKDPSync"
@syncPeer="handlePeerSync"
/>
</KPageContainer>
</ImmersivePage>
</template>
<script>
import { computed } from 'vue';
import ImmersivePage from 'kolibri/components/pages/ImmersivePage';
import CoreTable from 'kolibri/components/CoreTable';
import FacilityResource from 'kolibri-common/apiResources/FacilityResource';
import commonCoreStrings from 'kolibri/uiText/commonCoreStrings';
import commonSyncElements from 'kolibri-common/mixins/commonSyncElements';
import SyncFacilityModalGroup from 'kolibri-common/components/syncComponentSet/SyncFacilityModalGroup';
import {
useDeviceFacilityFilter,
useDevicesWithFilter,
} from 'kolibri-common/components/syncComponentSet/SelectDeviceModalGroup/useDevices';
import { TaskTypes } from 'kolibri-common/utils/syncTaskUtils';
import useTaskPolling from '../../composables/useTaskPolling';
import { KDP_ID, oneHour, oneDay, oneWeek, twoWeeks, oneMonth } from './constants';
import { kdpNameTranslator } from './i18n';
export default {
name: 'ManageSyncSchedule',
components: {
ImmersivePage,
CoreTable,
SyncFacilityModalGroup,
},
extends: ImmersivePage,
mixins: [commonCoreStrings, commonSyncElements],
setup(props) {
const deviceFilter = useDeviceFacilityFilter({ id: props.facilityId });
const { tasks } = useTaskPolling('facility_task');
const { devices } = useDevicesWithFilter(
{
subset_of_users_device: false,
},
deviceFilter,
);
const devicesById = computed(() => {
return devices.value.reduce(
(acc, device) => {
acc[device.id] = device;
return acc;
},
{
[KDP_ID]: {
device_id: KDP_ID,
// eslint-disable-next-line kolibri/vue-no-undefined-string-uses
device_name: kdpNameTranslator.$tr('syncToKDP'),
base_url: '',
},
},
);
});
return {
devicesById,
tasks,
};
},
props: {
facilityId: {
type: String,
required: true,
},
goBackRoute: {
type: Object,
required: true,
},
editSyncRoute: {
type: Function,
required: true,
},
},
data() {
return {
deviceModal: false,
facility: null,
};
},
computed: {
facilitySyncTasks() {
return this.tasks.filter(
t =>
t.facility_id === this.facilityId &&
t.repeat === null &&
(t.type === TaskTypes.SYNCDATAPORTAL || t.type === TaskTypes.SYNCPEERFULL),
);
},
scheduledTasks() {
return this.facilitySyncTasks.map(task => {
const deviceName = this.devicesById[this.getDeviceId(task)]
? this.devicesById[this.getDeviceId(task)].device_name
: task.extra_metadata.device_name;
const deviceAvailable =
this.devicesById[task.extra_metadata.device_id] &&
this.devicesById[task.extra_metadata.device_id].available;
const isKDP = task.type === TaskTypes.SYNCDATAPORTAL;
return {
...task,
deviceName,
deviceAvailable,
isKDP,
};
});
},
},
beforeMount() {
this.fetchFacility();
},
methods: {
fetchFacility() {
FacilityResource.fetchModel({ id: this.facilityId, force: true }).then(facility => {
this.facility = { ...facility };
});
},
closeModal() {
this.deviceModal = false;
},
handlePeerSync(device) {
this.deviceModal = false;
if (device.id) {
this.$router.push(this.editSyncRoute(device.id));
}
},
handleKDPSync() {
this.deviceModal = false;
this.$router.push(this.editSyncRoute(KDP_ID));
},
editButton(task) {
this.$router.push(this.editSyncRoute(this.getDeviceId(task)));
},
getDeviceId(task) {
if (task.type === TaskTypes.SYNCPEERFULL) {
return task.extra_metadata.device_id;
} else if (task.type === TaskTypes.SYNCDATAPORTAL) {
return KDP_ID;
}
},
scheduleTime(time, timestamp) {
timestamp = new Date(Date.parse(timestamp));
if (time === oneHour) {
return this.$tr('everyHour');
}
const options = {
weekday: 'long',
hour: 'numeric',
minute: 'numeric',
};
let frequencyString;
if (time === oneDay) {
frequencyString = this.$tr('everyDay');
delete options.weekday;
}
if (time === oneWeek) {
frequencyString = this.$tr('everyWeek');
}
if (time === twoWeeks) {
frequencyString = this.$tr('everyTwoWeeks');
}
if (time === oneMonth) {
frequencyString = this.$tr('everyMonth');
}
return `${frequencyString}, ${this.$formatTime(timestamp, options)}`;
},
},
$trs: {
syncSchedules: {
message: 'Sync schedules',
context: "Heading or title for 'manage sync schedule' page.",
},
introduction: {
message:
'Set a schedule for Kolibri to automatically sync with other Kolibri devices sharing this facility. Devices with the same sync schedule will be synced one at a time.',
context: 'Introduction on the manage sync schedule',
},
addDevice: {
message: 'Add device',
context: 'Add device button',
},
Schedule: {
message: 'Schedule',
context: 'Schedule label',
},
connected: {
message: 'Connected',
context: 'Connected device',
},
disconnected: {
message: 'Not connected',
context: 'Disconnected device',
},
NoSync: {
message: 'There are no syncs scheduled',
context: 'Text to display when there is no schedule sync to be managed.',
},
everyHour: {
message: 'Every hour',
context: 'Period for scheduling the sync between devices every hour',
},
everyDay: {
message: 'Every day',
context: 'Period for scheduling the sync between devices every day',
},
everyWeek: {
message: 'Every week',
context: 'Period for scheduling the sync between devices every week',
},
everyMonth: {
message: 'Every month',
context: 'Period for scheduling the sync between devices every month',
},
everyTwoWeeks: {
message: 'Every two weeks',
context: 'Period for scheduling the sync between devices every two weeks',
},
},
};
</script>
<style scoped>
.separate {
margin-top: 35px;
margin-bottom: 35px;
}
.add-space {
margin: 4px;
}
.right {
position: absolute;
right: 50px;
}
.loader-size {
margin-top: 10px;
}
</style>