Newer
Older
Website / src / app / home / home.component.ts
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { interval } from 'rxjs';
import { environment } from 'src/environments/environment';

class Time {
  seconds: number
  minutes: number
  hours: number

  constructor(time: number) {
    this.seconds = time % 60
    this.minutes = Math.floor(time / 60) % 60
    this.hours = Math.floor(time / 3600)
  }

  toString(short: boolean = false) {
    var result = ""
    if (this.hours > 0) {
      result += ` ${this.hours} hour`
      if (this.hours > 1) {
        result += 's'
      }
      if (short) {
        return result
      }
    }
    if (this.minutes > 0) {
      result += ` ${this.minutes} minute`
      if (this.minutes > 1) {
        result += 's'
      }
      if (short) {
        return result
      }
    }
    if (this.seconds > 0) {
      result += ` ${this.seconds} second`
      if (this.seconds > 1) {
        result += 's'
      }
      if (short) {
        return result
      }
    }
    return result
  }
}

class Status {
  user: string
  message: string
  postTime: Time
  lifetime: Time

  constructor(
    statusInterface: StatusInterface
  ) {
    this.user = statusInterface.user
    this.message = statusInterface.message
    var postedAgo = Math.floor(Date.now()/1000) - statusInterface.createdAt
    this.postTime = new Time(postedAgo)
    this.lifetime = new Time(statusInterface.lifetime)
  }
}
interface StatusInterface {
  user: string
  message: string
  createdAt: number
  lifetime: number
}
class StatusAnswer {
  constructor(public success: boolean = false, public userStatuses: StatusInterface[] = []) {}
}

class Quote {
  constructor(public user: string, public quote: string) {}
}
class QuotesAnswer {
  constructor(public success: boolean = false, public quotes: Quote[] = []) {}
}

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
  statuses: Status[] = []
  quotes: Quote[] = []
  loadingStatuses = true
  loadingQuotes = true

  constructor(private http: HttpClient) { }

  ngOnInit(): void {
    this.updateElements()
    interval(20000).subscribe((() => {
      this.updateElements()
    }))
  }

  updateElements() {
    this.http.get(`${environment.apiURL}/users/status`).subscribe((answer) => {
      var userStatusAnswer = Object.assign(new StatusAnswer(), answer)
      this.loadingStatuses &&= !userStatusAnswer.success
      if (userStatusAnswer.success) {
        this.statuses = []
        userStatusAnswer.userStatuses.forEach(status => {
          this.statuses.push(new Status(status))
        })
      }
    })
    this.http.get(`${environment.apiURL}/users/quotes`).subscribe((answer) => {
      var quotesAnswer = Object.assign(new QuotesAnswer(), answer)
      this.loadingQuotes &&= !quotesAnswer.success
      if (quotesAnswer.success) {
        this.quotes = quotesAnswer.quotes
      }
    })
  }
}