all repos — kōi @ 62018c919e2d50c3848c408156f446ef40292efd

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  startTime: getDatetime(taskThing, SCHEMA_INRUPT.startTime),
35  endTime: getDatetime(taskThing, SCHEMA_INRUPT.endTime),
36});
37
38export const taskToThing = (task: Task) => {
39  const taskThing = buildThing(createThing({ url: task.id }))
40    .addStringNoLocale(SCHEMA_INRUPT.name, task.name)
41    .addUrl(RDF.type, SCHEMA_CUSTOM.ScheduleAction)
42    .addUrl(
43      SCHEMA_CUSTOM.actionStatus,
44      task.done
45        ? SCHEMA_CUSTOM.ActionStatusType.done
46        : SCHEMA_CUSTOM.ActionStatusType.active
47    );
48  if (task.dueDate)
49    taskThing.addDatetime(SCHEMA_CUSTOM.scheduledTime, task.dueDate);
50  if (task.startTime)
51    taskThing.addDatetime(SCHEMA_INRUPT.startTime, task.startTime);
52  if (task.endTime) taskThing.addDatetime(SCHEMA_INRUPT.endTime, task.endTime);
53  return taskThing.build();
54};