|
31 | 31 | :style="selectorStyle"
|
32 | 32 | :options="selectArray"
|
33 | 33 | :label="$tr('frequency')"
|
| 34 | + @select="handleUserInput" |
34 | 35 | />
|
35 | 36 | </KGridItem>
|
36 | 37 | </KGrid>
|
|
43 | 44 | :style="selectorStyle"
|
44 | 45 | :options="getDays"
|
45 | 46 | :label="$tr('day')"
|
| 47 | + @select="handleUserInput" |
46 | 48 | />
|
47 | 49 | </KGridItem>
|
48 | 50 | </KGrid>
|
|
55 | 57 | :style="selectorStyle"
|
56 | 58 | :options="SyncTime"
|
57 | 59 | :label="$tr('time')"
|
| 60 | + @select="handleUserInput" |
58 | 61 | />
|
59 | 62 | </KGridItem>
|
60 | 63 | </KGrid>
|
|
78 | 81 | <KCheckbox
|
79 | 82 | :checked="retryFlag"
|
80 | 83 | :disabled="currentTaskRunning"
|
81 |
| - @change="retryFlag = !retryFlag" |
| 84 | + @change="handleRetryCheckboxChange" |
82 | 85 | >
|
83 | 86 | {{ $tr('checkboxLabel') }}
|
84 | 87 | </KCheckbox>
|
|
156 | 159 | import { now } from 'kolibri/utils/serverClock';
|
157 | 160 | import commonCoreStrings from 'kolibri/uiText/commonCoreStrings';
|
158 | 161 | import { TaskStatuses, TaskTypes } from 'kolibri-common/utils/syncTaskUtils';
|
| 162 | + import useTaskPolling from '../../composables/useTaskPolling'; |
159 | 163 | import { KDP_ID, oneHour, oneDay, oneWeek, twoWeeks, oneMonth } from './constants';
|
160 | 164 | import { kdpNameTranslator } from './i18n';
|
161 | 165 |
|
|
192 | 196 | BottomAppBar,
|
193 | 197 | },
|
194 | 198 | mixins: [commonCoreStrings],
|
| 199 | + setup() { |
| 200 | + const { tasks } = useTaskPolling('facility_task'); |
| 201 | + return { tasks }; |
| 202 | + }, |
195 | 203 | props: {
|
196 | 204 | icon: {
|
197 | 205 | type: String,
|
|
217 | 225 | device: null,
|
218 | 226 | now: null,
|
219 | 227 | selectedItem: {},
|
220 |
| - tasks: [], |
221 | 228 | selectedDay: {},
|
222 | 229 | selectedTime: {},
|
| 230 | + userHasEdited: false, |
223 | 231 | };
|
224 | 232 | },
|
225 | 233 | computed: {
|
|
266 | 274 | };
|
267 | 275 | });
|
268 | 276 | },
|
| 277 | + filteredTasks() { |
| 278 | + return this.tasks.filter( |
| 279 | + task => |
| 280 | + (this.isKdp || task.extra_metadata.device_id === this.device?.id) && |
| 281 | + task.facility_id === this.facilityId && |
| 282 | + task.type === this.taskType && |
| 283 | + // Only show tasks that are repeating indefinitely |
| 284 | + task.repeat === null, |
| 285 | + ); |
| 286 | + }, |
269 | 287 | deviceName() {
|
270 | 288 | return this.device && this.device.nickname && this.device.nickname.length
|
271 | 289 | ? this.device.nickname
|
272 | 290 | : this.device.device_name;
|
273 | 291 | },
|
274 | 292 | currentTask() {
|
275 |
| - return this.tasks && this.tasks.length ? this.tasks[0] : null; |
| 293 | + return this.filteredTasks.length ? this.filteredTasks[0] : null; |
276 | 294 | },
|
277 | 295 | currentTaskRunning() {
|
278 |
| - return this.currentTask && this.currentTask.status === TaskStatuses.RUNNING; |
| 296 | + return this.currentTask?.status === TaskStatuses.RUNNING; |
279 | 297 | },
|
280 | 298 | timeRequired() {
|
281 | 299 | return this.selectedItem.value > oneHour;
|
|
304 | 322 | );
|
305 | 323 | },
|
306 | 324 | },
|
| 325 | + watch: { |
| 326 | + currentTask() { |
| 327 | + if (this.currentTask && !this.userHasEdited) { |
| 328 | + const enqueueAt = new Date(Date.parse(this.currentTask.scheduled_datetime)); |
| 329 | + const day = enqueueAt.getDay(); |
| 330 | + const hours = enqueueAt.getHours(); |
| 331 | + const minutes = enqueueAt.getMinutes(); |
| 332 | + this.selectedItem = |
| 333 | + this.selectArray.find(item => item.value === this.currentTask.repeat_interval) || {}; |
| 334 | + this.selectedDay = this.getDays.find(item => item.value === day) || {}; |
| 335 | + for (const time of this.SyncTime) { |
| 336 | + // Because there can be some drift in the task scheduling process, |
| 337 | + // we round the 'scheduled' time to the nearest 30 minutes |
| 338 | + if ( |
| 339 | + time.minutes === 0 && |
| 340 | + ((time.hours === hours && minutes < 15) || |
| 341 | + (time.hours === hours + 1 && minutes >= 45)) |
| 342 | + ) { |
| 343 | + this.selectedTime = time; |
| 344 | + break; |
| 345 | + } |
| 346 | + if (time.minutes === 30 && time.hours === hours && minutes >= 15 && minutes < 45) { |
| 347 | + this.selectedTime = time; |
| 348 | + break; |
| 349 | + } |
| 350 | + } |
| 351 | + this.retryFlag = Boolean(this.currentTask.retry_interval); |
| 352 | + } |
| 353 | + }, |
| 354 | + }, |
307 | 355 | created() {
|
308 | 356 | this.fetchDevice();
|
309 | 357 | this.now = now();
|
|
393 | 441 | })
|
394 | 442 | .catch(() => {
|
395 | 443 | this.createTaskFailedSnackbar();
|
396 |
| - if (this.currentTask) { |
397 |
| - this.fetchSyncTasks(); |
398 |
| - } |
399 | 444 | });
|
400 | 445 | },
|
401 |
| -
|
402 | 446 | goBack() {
|
403 | 447 | this.$router.push(this.goBackRoute);
|
404 | 448 | },
|
405 |
| - pollFetchSyncTasks() { |
406 |
| - this.pollInterval = setInterval(() => { |
407 |
| - this.fetchSyncTasks(); |
408 |
| - }, 10000); |
409 |
| - }, |
410 |
| - fetchSyncTasks() { |
411 |
| - TaskResource.list({ queue: 'facility_task' }).then(tasks => { |
412 |
| - this.tasks = tasks.filter( |
413 |
| - task => |
414 |
| - (this.isKdp || task.extra_metadata.device_id === this.device.id) && |
415 |
| - task.facility_id === this.facilityId && |
416 |
| - task.type === this.taskType && |
417 |
| - // Only show tasks that are repeating indefinitely |
418 |
| - task.repeat === null, |
419 |
| - ); |
420 |
| - this.$nextTick(() => { |
421 |
| - if (this.currentTask) { |
422 |
| - const enqueueAt = new Date(Date.parse(this.currentTask.scheduled_datetime)); |
423 |
| - const day = enqueueAt.getDay(); |
424 |
| - const hours = enqueueAt.getHours(); |
425 |
| - const minutes = enqueueAt.getMinutes(); |
426 |
| - this.selectedItem = |
427 |
| - this.selectArray.find(item => item.value === this.currentTask.repeat_interval) || |
428 |
| - {}; |
429 |
| - this.selectedDay = this.getDays.find(item => item.value === day) || {}; |
430 |
| - for (const time of this.SyncTime) { |
431 |
| - // Because there can be some drift in the task scheduling process, |
432 |
| - // we round the 'scheduled' time to the nearest 30 minutes |
433 |
| - if ( |
434 |
| - time.minutes === 0 && |
435 |
| - ((time.hours === hours && minutes < 15) || |
436 |
| - (time.hours === hours + 1 && minutes >= 45)) |
437 |
| - ) { |
438 |
| - this.selectedTime = time; |
439 |
| - break; |
440 |
| - } |
441 |
| - if (time.minutes === 30 && time.hours === hours && minutes >= 15 && minutes < 45) { |
442 |
| - this.selectedTime = time; |
443 |
| - break; |
444 |
| - } |
445 |
| - } |
446 |
| - this.retryFlag = Boolean(this.currentTask.retry_interval); |
447 |
| - if (this.currentTaskRunning) { |
448 |
| - this.pollFetchSyncTasks(); |
449 |
| - } else { |
450 |
| - clearInterval(this.pollInterval); |
451 |
| - this.pollInterval = null; |
452 |
| - } |
453 |
| - } |
454 |
| - }); |
455 |
| - }); |
456 |
| - }, |
457 | 449 | fetchDevice() {
|
458 | 450 | if (this.isKdp) {
|
459 | 451 | this.device = {
|
|
462 | 454 | device_name: kdpNameTranslator.$tr('syncToKDP'),
|
463 | 455 | base_url: '',
|
464 | 456 | };
|
465 |
| - this.fetchSyncTasks(); |
466 | 457 | return;
|
467 | 458 | }
|
468 | 459 | NetworkLocationResource.fetchModel({ id: this.deviceId }).then(device => {
|
469 | 460 | this.device = device;
|
470 |
| - this.fetchSyncTasks(); |
471 | 461 | });
|
472 | 462 | },
|
| 463 | + handleUserInput() { |
| 464 | + this.userHasEdited = true; |
| 465 | + }, |
| 466 | + handleRetryCheckboxChange() { |
| 467 | + this.retryFlag = !this.retryFlag; |
| 468 | + this.handleUserInput(); |
| 469 | + }, |
473 | 470 | },
|
474 | 471 | $trs: {
|
475 | 472 | editSyncScheduleTitle: {
|
|
0 commit comments