@@ -3,63 +3,111 @@ import {
33 getSolidDataset ,
44 getTerm ,
55 getTermAll ,
6+ getThing ,
67 getThingAll ,
8+ saveSolidDatasetAt ,
9+ setStringWithLocale ,
10+ setThing ,
11+ setUrl ,
12+ Thing ,
13+ ThingPersisted ,
714 UrlString ,
815} from '@inrupt/solid-client'
916import { fetch } from '@inrupt/solid-client-authn-browser'
1017import { rdf , rdfs } from 'rdf-namespaces'
11- import { Definition , Statement } from './types'
18+ import { Definition , PartialNode , Statement } from './types'
1219
1320interface Graph {
1421 nodes : ( Definition | Statement ) [ ]
1522 links : [ string , string ] [ ]
1623}
1724
18- export const fetchGraph = async ( uri : UrlString ) : Promise < Graph > => {
19- const dataset = await getSolidDataset ( uri , { fetch } )
25+ const term = {
26+ definition : 'https://terms.math.livegraph.org#Definition' ,
27+ statement : 'https://terms.math.livegraph.org#Statement' ,
28+ dependsOn : 'https://terms.math.livegraph.org#dependsOn' ,
29+ }
30+
31+ export const fetchGraph = async ( documentUri : UrlString ) : Promise < Graph > => {
32+ const dataset = await getSolidDataset ( documentUri , { fetch } )
2033 const things = getThingAll ( dataset )
2134 const graph : Graph = { nodes : [ ] , links : [ ] }
2235 things . forEach ( thing => {
23- const uri = asUrl ( thing )
24- const type = getTerm ( thing , rdf . type ) ?. value ?? ''
25- const description = getTerm ( thing , rdf . value ) ?. value ?? ''
26- const label = getTerm ( thing , rdfs . label ) ?. value ?? ''
27- switch ( type ) {
28- case 'https://terms.math.livegraph.org#Definition' :
29- graph . nodes . push ( {
30- id : uri ,
31- type : 'definition' ,
32- label : { en : label } ,
33- description : { en : description } ,
34- dependencies : [ ] ,
35- dependents : [ ] ,
36- examples : [ ] ,
37- created : 0 ,
38- updated : 0 ,
39- } )
40- break
41- case 'https://terms.math.livegraph.org#Statement' :
42- graph . nodes . push ( {
43- id : uri ,
44- type : 'statement' ,
45- label : { en : label } ,
46- description : { en : description } ,
47- dependencies : [ ] ,
48- dependents : [ ] ,
49- examples : [ ] ,
50- proofs : [ ] ,
51- created : 0 ,
52- updated : 0 ,
53- } )
54- break
55- default :
56- break
57- }
58- getTermAll ( thing , 'https://terms.math.livegraph.org#dependsOn' ) . forEach (
59- dependency => {
60- graph . links . push ( [ uri , dependency . value ] )
61- } ,
62- )
36+ const node = thingToNode ( thing , documentUri )
37+ graph . nodes . push ( node )
38+ getTermAll ( thing , term . dependsOn ) . forEach ( dependency => {
39+ graph . links . push ( [ node . id , dependency . value ] )
40+ } )
6341 } )
6442 return graph
6543}
44+
45+ export const updateNode = async (
46+ node : PartialNode ,
47+ ) : Promise < Definition | Statement > => {
48+ // we want to save any partial data that are provided
49+ // so how do we do it?
50+
51+ // save a label
52+
53+ const dataset = await getSolidDataset ( node . document , { fetch } )
54+ const thing = getThing ( dataset , node . id ) as ThingPersisted
55+ if ( thing ) {
56+ let newThing = thing
57+ if ( node . label ) {
58+ newThing = setStringWithLocale ( newThing , rdfs . label , node . label . en , 'en' )
59+ }
60+
61+ if ( node . type ) {
62+ newThing = setUrl ( newThing , rdf . type , term [ node . type ] )
63+ }
64+
65+ if ( newThing !== thing ) {
66+ const newDataset = setThing ( dataset , newThing )
67+ await saveSolidDatasetAt ( node . document , newDataset , { fetch } )
68+ }
69+ return thingToNode ( newThing , node . document )
70+ }
71+ throw new Error ( 'node to update not found' )
72+ }
73+
74+ const thingToNode = (
75+ thing : Thing ,
76+ document : string ,
77+ ) : Definition | Statement => {
78+ const uri = asUrl ( thing )
79+ const type = getTerm ( thing , rdf . type ) ?. value ?? ''
80+ const description = getTerm ( thing , rdf . value ) ?. value ?? ''
81+ const label = getTerm ( thing , rdfs . label ) ?. value ?? ''
82+ switch ( type ) {
83+ case term . definition :
84+ return {
85+ id : uri ,
86+ type : 'definition' ,
87+ label : { en : label } ,
88+ description : { en : description } ,
89+ dependencies : [ ] ,
90+ dependents : [ ] ,
91+ examples : [ ] ,
92+ created : 0 ,
93+ updated : 0 ,
94+ document,
95+ }
96+ case term . statement :
97+ return {
98+ id : uri ,
99+ type : 'statement' ,
100+ label : { en : label } ,
101+ description : { en : description } ,
102+ dependencies : [ ] ,
103+ dependents : [ ] ,
104+ examples : [ ] ,
105+ proofs : [ ] ,
106+ created : 0 ,
107+ updated : 0 ,
108+ document,
109+ }
110+ default :
111+ throw new Error ( 'thing is not a Definition or Statement' )
112+ }
113+ }
0 commit comments