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: getUrl(taskThing, SCHEMA_CUSTOM.actionStatus) === SCHEMA_CUSTOM.ActionStatusType.done,
31 dueDate: getDatetime(taskThing, SCHEMA_CUSTOM.scheduledTime)
32});
33
34export const taskToThing = (task: Task) => {
35 const taskThing = buildThing(createThing({ url: task.id }))
36 .addStringNoLocale(SCHEMA_INRUPT.name, task.name)
37 .addUrl(RDF.type, SCHEMA_CUSTOM.ScheduleAction)
38 .addUrl(
39 SCHEMA_CUSTOM.actionStatus,
40 task.done ? SCHEMA_CUSTOM.ActionStatusType.done : SCHEMA_CUSTOM.ActionStatusType.active
41 );
42 if (task.dueDate) taskThing.addDatetime(SCHEMA_CUSTOM.scheduledTime, task.dueDate);
43
44 return taskThing.build();
45};