view src/UpcomingEvents.ts @ 15:20d1fa4250c0

refactor
author drewp@bigasterisk.com
date Thu, 06 Jun 2024 17:52:28 -0700
parents
children 472003015880
line wrap: on
line source

import { LitElement, css, html } from "lit";
import { customElement, property } from "lit/decorators.js";
import { sortBy } 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`
      ol {
        list-style-type: circle;
        font-size: 16px;
        background: #cd66bb2e;
        padding: 9px;
        width: fit-content;
        position: relative;
        top: -21px;
        border-radius: 14px;
      }
      span.d {
        opacity: 0.5;
      }
    `,
  ];
  @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 = sortBy(this.evs, "start");
    });
  }

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