aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-main/date/date-toggle.component.ts
blob: bedf0ba4e5193bba56a873784b1b45c0e211a726 (plain) (blame)
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
import { DatePipe } from '@angular/common'
import { Component, Input, OnChanges, OnInit } from '@angular/core'
import { FromNowPipe } from '../angular/from-now.pipe'

@Component({
  selector: 'my-date-toggle',
  templateUrl: './date-toggle.component.html',
  styleUrls: [ './date-toggle.component.scss' ]
})
export class DateToggleComponent implements OnInit, OnChanges {
  @Input() date: Date
  @Input() toggled = false

  dateRelative: string
  dateAbsolute: string

  constructor (
    private datePipe: DatePipe,
    private fromNowPipe: FromNowPipe
  ) { }

  ngOnInit () {
    this.updateDates()
  }

  ngOnChanges () {
    this.updateDates()
  }

  toggle () {
    this.toggled = !this.toggled
  }

  getTitle () {
    return this.toggled ? this.dateRelative : this.dateAbsolute
  }

  getContent () {
    return this.toggled ? this.dateAbsolute : this.dateRelative
  }

  private updateDates () {
    this.dateRelative = this.fromNowPipe.transform(this.date)
    this.dateAbsolute = this.datePipe.transform(this.date, 'long')
  }
}