all repos — kōi @ 4a48ccfcb2820e792122c1a2d62d209c30239e47

Minimalist task manager

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

 1import {
 2  getStringNoLocale,
 3  getUrl,
 4  getDatetime,
 5  type Thing,
 6  createThing,
 7  getInteger,
 8} from "@inrupt/solid-client";
 9import { RDF, SCHEMA_INRUPT } from "@inrupt/vocab-common-rdf";
10import { buildThing } from "@inrupt/solid-client";
11
12const SCHEMA_CUSTOM = {
13  // Types
14  ScheduleAction: "http://schema.org/ScheduleAction",
15  Schedule: "http://schema.org/Schedule",
16
17  // Properties
18  scheduledTime: "http://schema.org/scheduledTime",
19  actionStatus: "http://schema.org/actionStatus",
20  byDay: "http://schema.org/byDay",
21  byMonth: "http://schema.org/byMonth",
22  byMonthDay: "http://schema.org/byMonthDay",
23  byMonthWeek: "http://schema.org/byMonthWeek",
24
25  // Enumeration types
26  ActionStatusType: {
27    active: "http://schema.org/ActiveActionStatus",
28    done: "http://schema.org/CompletedActionStatus",
29  },
30};
31
32export const thingToTask = (taskThing: Thing) => ({
33  type: [SCHEMA_CUSTOM.ScheduleAction, SCHEMA_CUSTOM.Schedule],
34  id: taskThing.url,
35  name: getStringNoLocale(taskThing, SCHEMA_INRUPT.name) || "",
36  done:
37    getUrl(taskThing, SCHEMA_CUSTOM.actionStatus) ===
38    SCHEMA_CUSTOM.ActionStatusType.done,
39  dueDate: getDatetime(taskThing, SCHEMA_CUSTOM.scheduledTime),
40  startTime: getDatetime(taskThing, SCHEMA_INRUPT.startTime),
41  endTime: getDatetime(taskThing, SCHEMA_INRUPT.endTime),
42
43  // Periodicity
44  byDay: getStringNoLocale(taskThing, SCHEMA_CUSTOM.byDay),
45  byMonth: getInteger(taskThing, SCHEMA_CUSTOM.byDay),
46  byMonthDay: getInteger(taskThing, SCHEMA_CUSTOM.byDay),
47  byMonthWeek: getInteger(taskThing, SCHEMA_CUSTOM.byDay),
48});
49
50export const taskToThing = (task: Task) => {
51  const taskThing = buildThing(createThing({ url: task.id }))
52    .addStringNoLocale(SCHEMA_INRUPT.name, task.name)
53    .addUrl(RDF.type, SCHEMA_CUSTOM.ScheduleAction)
54    .addUrl(RDF.type, SCHEMA_CUSTOM.Schedule)
55    .addUrl(
56      SCHEMA_CUSTOM.actionStatus,
57      task.done
58        ? SCHEMA_CUSTOM.ActionStatusType.done
59        : SCHEMA_CUSTOM.ActionStatusType.active
60    );
61  if (task.dueDate)
62    taskThing.addDatetime(SCHEMA_CUSTOM.scheduledTime, task.dueDate);
63  if (task.startTime)
64    taskThing.addDatetime(SCHEMA_INRUPT.startTime, task.startTime);
65  if (task.endTime) taskThing.addDatetime(SCHEMA_INRUPT.endTime, task.endTime);
66  if (task.byDay) taskThing.addStringNoLocale(SCHEMA_CUSTOM.byDay, task.byDay);
67  if (task.byMonth) taskThing.addInteger(SCHEMA_CUSTOM.byMonth, task.byMonth);
68  if (task.byMonthDay)
69    taskThing.addInteger(SCHEMA_CUSTOM.byMonthDay, task.byMonthDay);
70  if (task.byMonthWeek)
71    taskThing.addInteger(SCHEMA_CUSTOM.byMonthWeek, task.byMonthWeek);
72  return taskThing.build();
73};