all repos — kōi @ main

Minimalist task manager

src/lib/models/task.ts (view raw)

 1import {
 2  getStringNoLocale,
 3  getUrl,
 4  getDatetime,
 5  type Thing,
 6  createThing,
 7} from "@inrupt/solid-client";
 8import { RDF, SCHEMA_INRUPT } from "@inrupt/vocab-common-rdf";
 9import { buildThing } from "@inrupt/solid-client";
10
11const SCHEMA_CUSTOM = {
12  // Types
13  ScheduleAction: "http://schema.org/ScheduleAction",
14
15  // Properties
16  scheduledTime: "http://schema.org/scheduledTime",
17  actionStatus: "http://schema.org/actionStatus",
18
19  // Enumeration types
20  ActionStatusType: {
21    active: "http://schema.org/ActiveActionStatus",
22    done: "http://schema.org/CompletedActionStatus",
23  },
24};
25
26export const thingToTask = (taskThing: Thing) => ({
27  type: SCHEMA_CUSTOM.ScheduleAction,
28  id: taskThing.url,
29  name: getStringNoLocale(taskThing, SCHEMA_INRUPT.name) || "",
30  done:
31    getUrl(taskThing, SCHEMA_CUSTOM.actionStatus) ===
32    SCHEMA_CUSTOM.ActionStatusType.done,
33  dueDate: getDatetime(taskThing, SCHEMA_CUSTOM.scheduledTime),
34});
35
36export const taskToThing = (task: Task) => {
37  const taskThing = buildThing(createThing({ url: task.id }))
38    .addStringNoLocale(SCHEMA_INRUPT.name, task.name)
39    .addUrl(RDF.type, SCHEMA_CUSTOM.ScheduleAction)
40    .addUrl(
41      SCHEMA_CUSTOM.actionStatus,
42      task.done
43        ? SCHEMA_CUSTOM.ActionStatusType.done
44        : SCHEMA_CUSTOM.ActionStatusType.active
45    );
46  if (task.dueDate)
47    taskThing.addDatetime(SCHEMA_CUSTOM.scheduledTime, task.dueDate);
48
49  return taskThing.build();
50};