File size: 2,806 Bytes
11452ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Component, Inject, OnInit } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { Schedule, Task } from 'api-types';
import { PostsService } from 'app/posts.services';

@Component({
  selector: 'app-update-task-schedule-dialog',
  templateUrl: './update-task-schedule-dialog.component.html',
  styleUrls: ['./update-task-schedule-dialog.component.scss']
})
export class UpdateTaskScheduleDialogComponent implements OnInit {

  enabled = true;
  recurring = false;
  days_of_week = [];
  interval = 'daily';
  time = null;
  date: Date = null;
  today = new Date();
  Intl = Intl;

  constructor(@Inject(MAT_DIALOG_DATA) public data: {task: Task}, private dialogRef: MatDialogRef<UpdateTaskScheduleDialogComponent>, private postsService: PostsService) {
    this.processTask(this.data.task);
    this.postsService.getTask(this.data.task.key).subscribe(res => {
      this.processTask(res['task']);
    });
  }

  ngOnInit(): void {
  }

  processTask(task: Task): void {
    if (!task['schedule']) {
      this.enabled = false;
      return;
    }

    const schedule: Schedule = task['schedule'];

    this.recurring = schedule['type'] === Schedule.type.RECURRING;

    if (this.recurring) {
      const hour = schedule['data']['hour'];
      const minute = schedule['data']['minute'];

      // add padding 0s if necessary to hours and minutes
      this.time = (hour < 10 ? '0' : '') + hour + ':' + (minute < 10 ? '0' : '') + minute;

      if (schedule['data']['dayOfWeek']) {
        this.days_of_week = schedule['data']['dayOfWeek'];
        this.interval = 'weekly';
      } else {
        this.interval = 'daily';
      }
    } else {
      const schedule_date = new Date(schedule['data']['timestamp']);
      this.time = `${schedule_date.getHours()}:${schedule_date.getMinutes()}`
      this.date = schedule_date;
    }
  }

  updateTaskSchedule(): void {
    if (!this.enabled) {
      this.dialogRef.close(null);
      return;
    }

    if (!this.time) {
      // needs time!
      this.postsService.openSnackBar($localize`You must input a time!`);
      return;
    }

    const hours = parseInt(this.time.split(':')[0]);
    const minutes = parseInt(this.time.split(':')[1]);

    const schedule: Schedule = {type: this.recurring ? Schedule.type.RECURRING : Schedule.type.TIMESTAMP, data: null};
    if (this.recurring) {
      schedule['data'] = {hour: hours, minute: minutes};
      if (this.interval === 'weekly') {
        schedule['data']['dayOfWeek'] = this.days_of_week;
      }
    } else {
      this.date.setHours(hours, minutes);
      schedule['data'] = {timestamp: this.date.getTime()};
    }
    schedule['data']['tz'] = this.Intl?.DateTimeFormat().resolvedOptions().timeZone;
    this.dialogRef.close(schedule);
  }
}