view src/UpcomingEvents.ts @ 21:a90cb6927c7d default tip

fix countdown queries. Display "now" instead of "In -0.4 hours"
author drewp@bigasterisk.com
date Sat, 07 Sep 2024 17:47:36 -0700
parents e8c90d893919
children
line wrap: on
line source

import { LitElement, css, html } from "lit";
import { customElement, property } from "lit/decorators.js";
import { sortBy, sortedUniqBy } from "lodash";
import { DataFactory, Quad_Subject, Store } from "n3";
import { shared } from "./shared";
import { EV, fetchGraph, parseGraph, RDF } from "./parseRdf";
import { DisplayEvent } from "./DisplayEvent";

const { namedNode } = DataFactory;

@customElement("fd-upcoming-events")
export class UpcomingEvents extends LitElement {
  static styles = [
    shared,
    css`
    `,
  ];
  @property() evs: DisplayEvent[];
  constructor() {
    super();
    this.evs = [];
    this.load();
    setInterval(this.load.bind(this), 5 * 60 * 1000);
  }

  async load() {
    const r = await fetchGraph("/gcalendarwatch/graph/calendar/upcoming");
    await parseGraph(r, (store: Store) => {
      const graph = namedNode(EV + "gcalendar");
      this.evs = [];
      store.getSubjects(namedNode(RDF + "type"), namedNode(EV + "Event"), graph).forEach((ev: Quad_Subject) => {
        const de = new DisplayEvent(store, graph, ev);
        if (de.show()) {
          this.evs = [...this.evs, de];
        }
      });
      this.evs = sortedUniqBy(sortBy(this.evs, "start"), (de) => de.start + de.title);
    });
  }

  render() {
    return html`
      <h1>Calendar</h1>
      <ol>
        ${this.evs.map((d) => html`<li><span class="date">${d.shortDate()}</span> ${d.title}</li>`)}
      </ol>
    `;
  }
}