-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for setting preferred language
- Loading branch information
1 parent
a8943f7
commit 526b677
Showing
16 changed files
with
389 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "@ldkit/example-dbpedia-actors", | ||
"version": "0.1.5", | ||
"author": "Karel Klima", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "parcel src/index.html", | ||
"build": "parcel build src/index.html --no-scope-hoist" | ||
}, | ||
"dependencies": { | ||
"@emotion/react": "^11.4.1", | ||
"@emotion/styled": "^11.3.0", | ||
"@ldkit/core": "*", | ||
"@ldkit/namespaces": "*", | ||
"@ldkit/rdf": "*", | ||
"@ldkit/react": "*", | ||
"@ldkit/sparql": "*", | ||
"@mui/material": "^5.0.4", | ||
"rxjs": "^7.4.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { firstValueFrom } from "rxjs"; | ||
|
||
import { createLocalizedActorResource, ActorInterface } from "../store"; | ||
import { Grid, Typography } from "@mui/material"; | ||
|
||
type ActorProps = { | ||
actorIri: string; | ||
language: string; | ||
}; | ||
|
||
export const Actor: React.FC<ActorProps> = ({ actorIri, language }) => { | ||
const [actor, setActor] = useState<ActorInterface | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
setActor(null); | ||
const result = await firstValueFrom( | ||
createLocalizedActorResource(language).findByIris([actorIri]) | ||
); | ||
setActor(result[0]); | ||
}; | ||
fetchData(); | ||
}, [setActor, actorIri, language]); | ||
|
||
if (!actor) { | ||
return <>Loading...</>; | ||
} | ||
|
||
return ( | ||
<Grid container spacing={2}> | ||
<Grid item xs={12}> | ||
<Typography variant="h2">{actor.name}</Typography> | ||
<Typography> | ||
Born {actor.birthDate.toLocaleDateString()} as {actor.birthName} | ||
</Typography> | ||
</Grid> | ||
<Grid item xs={8}> | ||
<Typography>{actor.abstract}</Typography> | ||
</Grid> | ||
<Grid item xs={4}> | ||
<img src={actor.thumbnail} /> | ||
</Grid> | ||
</Grid> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { Container, Grid, TextField, MenuItem } from "@mui/material"; | ||
|
||
import { favouriteActors, languages } from "../store"; | ||
import { Actor } from "./Actor"; | ||
|
||
export const Actors: React.FC = () => { | ||
const [initialized, setInitialized] = useState(false); | ||
const [actorIri, setActorIri] = useState(""); | ||
const [language, setLanguage] = useState(""); | ||
|
||
useEffect(() => { | ||
setActorIri(favouriteActors[0]), setLanguage(languages[0]); | ||
setInitialized(true); | ||
}, [setActorIri, setLanguage, setInitialized]); | ||
|
||
if (!initialized) { | ||
return null; | ||
} | ||
|
||
const handleActorChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setActorIri(event.target.value); | ||
}; | ||
|
||
const handleLanguageChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setLanguage(event.target.value); | ||
}; | ||
|
||
return ( | ||
<Container maxWidth="md" sx={{ paddingTop: 4 }}> | ||
<Grid container spacing={2}> | ||
<Grid item xs={8}> | ||
<TextField | ||
select | ||
fullWidth | ||
label="Select actor resource" | ||
value={actorIri} | ||
onChange={handleActorChange} | ||
> | ||
{favouriteActors.map((option) => ( | ||
<MenuItem key={option} value={option}> | ||
{option} | ||
</MenuItem> | ||
))} | ||
</TextField> | ||
</Grid> | ||
<Grid item xs={4}> | ||
<TextField | ||
select | ||
fullWidth | ||
label="Select preferred language" | ||
value={language} | ||
onChange={handleLanguageChange} | ||
> | ||
{languages.map((option) => ( | ||
<MenuItem key={option} value={option}> | ||
{option} | ||
</MenuItem> | ||
))} | ||
</TextField> | ||
</Grid> | ||
<Grid item xs={12}> | ||
<Actor actorIri={actorIri} language={language} /> | ||
</Grid> | ||
</Grid> | ||
</Container> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from "react"; | ||
import { Actors } from "./Actors"; | ||
|
||
import { | ||
CssBaseline, | ||
AppBar, | ||
Toolbar, | ||
Typography, | ||
Box, | ||
Button, | ||
} from "@mui/material"; | ||
|
||
export const App = () => ( | ||
<> | ||
<CssBaseline /> | ||
<AppBar position="static"> | ||
<Toolbar> | ||
<Typography variant="h6" noWrap> | ||
DBpedia Actors example app | ||
</Typography> | ||
|
||
<Box sx={{ flexGrow: 1 }} /> | ||
<Button color="inherit" href="https://github.com/karelklima/ldkit"> | ||
LDKit | ||
</Button> | ||
</Toolbar> | ||
</AppBar> | ||
<Actors /> | ||
</> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<title>LDKit DBpedia Actors example</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="index.tsx"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from "react"; | ||
import { render } from "react-dom"; | ||
|
||
import { App } from "./components/App"; | ||
|
||
render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode>, | ||
document.getElementById("app") | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
createResource, | ||
createNamespace, | ||
createDefaultContext, | ||
createContext, | ||
} from "@ldkit/core"; | ||
import type { SchemaInterface } from "@ldkit/core"; | ||
import { xsd } from "@ldkit/namespaces"; | ||
|
||
export const dbo = createNamespace({ | ||
iri: "http://dbpedia.org/ontology/", | ||
prefix: "dbo:", | ||
terms: ["Person", "abstract", "thumbnail", "birthDate", "birthName"], | ||
} as const); | ||
|
||
export const dbp = createNamespace({ | ||
iri: "http://dbpedia.org/property/", | ||
prefix: "dbp:", | ||
terms: ["name"], | ||
} as const); | ||
|
||
export const rdfs = createNamespace({ | ||
iri: "http://www.w3.org/2000/01/rdf-schema#", | ||
prefix: "rdfs:", | ||
terms: ["label"], | ||
} as const); | ||
|
||
const ActorSchema = { | ||
"@type": dbo.Person, | ||
name: rdfs.label, | ||
abstract: dbo.abstract, | ||
thumbnail: dbo.thumbnail, | ||
birthDate: { | ||
"@id": dbo.birthDate, | ||
"@type": xsd.date, | ||
}, | ||
birthName: dbo.birthName, | ||
} as const; | ||
|
||
export const createLanguageContext = (language: string) => | ||
createContext({ | ||
sources: ["https://dbpedia.org/sparql"], | ||
language, | ||
}); | ||
|
||
export type ActorInterface = SchemaInterface<typeof ActorSchema>; | ||
|
||
export const createLocalizedActorResource = (language: string) => | ||
createResource(ActorSchema, createLanguageContext(language)); | ||
|
||
export const favouriteActors = [ | ||
"http://dbpedia.org/resource/Brad_Pitt", | ||
"http://dbpedia.org/resource/Karl_Urban", | ||
"http://dbpedia.org/resource/Margot_Robbie", | ||
"http://dbpedia.org/resource/Gal_Gadot", | ||
]; | ||
|
||
export const languages = ["en", "cs", "ru", "de"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"noEmit": true | ||
}, | ||
"include": ["src"], | ||
"references": [ | ||
{ | ||
"path": "../../library/context" | ||
}, | ||
{ | ||
"path": "../../library/core" | ||
}, | ||
{ | ||
"path": "../../library/namespaces" | ||
}, | ||
{ | ||
"path": "../../library/rdf" | ||
}, | ||
{ | ||
"path": "../../library/react" | ||
}, | ||
{ | ||
"path": "../../library/sparql" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import { useMemo } from "react"; | ||
import type { Observable } from "rxjs"; | ||
import { useObservable, useObservableState } from "observable-hooks"; | ||
|
||
export const useResource = <T extends any>( | ||
observableInit: () => Observable<T>, | ||
defaultState: T | ||
defaultState: T, | ||
deps: React.DependencyList = [] | ||
) => { | ||
const o$ = useObservable(observableInit); | ||
const o$ = useMemo(() => { | ||
console.log("REMEMOIZING"); | ||
return observableInit(); | ||
}, deps); | ||
// const o$ = useObservable(memoizedObservableInit); | ||
return useObservableState(o$, defaultState); | ||
}; |
Oops, something went wrong.