Skip to content

Commit

Permalink
feat: creating and updating resources in a component (#67)
Browse files Browse the repository at this point in the history
* chore: updated handlersjs version and tsconfig

* feat: made changes to NodeHttpServer handler structure

* feat: created input component with fetch put request
  • Loading branch information
Arne Vandoorslaer authored May 3, 2021
1 parent 487e4e2 commit b8f5415
Show file tree
Hide file tree
Showing 9 changed files with 7,761 additions and 7,636 deletions.
121 changes: 121 additions & 0 deletions packages/semcom-components/components/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* eslint-disable no-console -- is a web component */
import * as N3 from 'n3';
import { LitElement, css, html, property } from 'lit-element';
import type { Component } from '@digita-ai/semcom-core';

export default class InputComponent extends LitElement implements Component {

data (
entry: string,
customFetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>,
): Promise<void> {

const myFetch = customFetch ? customFetch : fetch;
const parser = new N3.Parser();
const store = new N3.Store();

this.localFetch = myFetch;
this.localEntry = entry;

return myFetch(entry)
.then((response) => response.text())
.then((text) => {
store.addQuads(parser.parse(text));
});

}

@property() localFetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
@property() localEntry?: string;

static get styles() {
return [
css`
:host {
font-family: 'Roboto', sans-serif;
font-weight: 300;
}
.container {
width: 100%;
text-align: center;
padding: 40px 0;
}
input[type=text], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
button[type=submit]:hover {
background-color: #45a049;
}
`,
];
}

fetcher = async () => {
if(this.localFetch && this.localEntry) {
const inputField = this.shadowRoot?.getElementById('inputField') as HTMLInputElement;
const uri = this.localEntry.split('/profile')[0] + '/inputTest';
await this.localFetch(uri, { method: 'PUT', body: inputField.value, headers: {'content-type': 'text/plain'} });
inputField.value = '';
}
};

render() {
return html`
<div class="container">
<label for="name">Your Input:</label>
<br>
<input type="text" id="inputField"/>
<br>
<button @click="${this.fetcher}" type="submit">Submit</button>
</div>
`;
}

/*
* W3C Custom Element Specification (from MDN)
*/

// Invoked each time the element is appended into a DOM (i.e. when node is added or moved).
connectedCallback() {
super.connectedCallback();
console.info('[DGT-InputComponent] Element connected');
}

// Invoked each time the element is disconnected from a DOM.
disconnectedCallback() {
super.disconnectedCallback();
console.info('[DGT-InputComponent] Element disconnected');
}
// Invoked each time the custom element is moved to a new DOM.
adoptedCallback() {
// super.adoptedCallback();
console.info('[DGT-InputComponent] Element moved to other DOM');
}

// Invoked each time one of the element's attributes specified in observedAttributes is changed.
attributeChangedCallback(name: string, oldValue: string, newValue: string) {
super.attributeChangedCallback(name, oldValue, newValue);
console.info(`[DGT-InputComponent] Changed ${name} attribute from "${oldValue}" to "${newValue}"`);
}

}
2 changes: 1 addition & 1 deletion packages/semcom-demo-app/src/app/home/home.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class HomeEffects {

queryMetadataFromShapes$ = createEffect(() => this.actions$.pipe(
ofType(shapesDetected),
mergeMap(({shapeIds}) => forkJoin(shapeIds.map((shapeId) => this.semComService.queryComponents(shapeId)))),
mergeMap(({shapeIds}) => forkJoin(shapeIds.concat([ 'http://digita.ai/voc/input#input' ]).map((shapeId) => this.semComService.queryComponents(shapeId)))),
map((resultsPerShape) => resultsPerShape.filter((results) => results.length > 0)),
map((resultsPerShape) => resultsPerShape.map((results) => results[0])),
map((selection) => componentsSelected({ components: selection })),
Expand Down
56 changes: 34 additions & 22 deletions packages/semcom-node/config/presets/server.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,48 @@
],
"@graph": [
{
"@id": "urn:handlersjs-http:default:NodeHttpServer",
"@id": "urn:semcom-node:default:NodeHttpServer",
"@type": "NodeHttpServer",
"NodeHttpServer:_port": "3000",
"NodeHttpServer:_host": "localhost",
"NodeHttpServer:_nodeHttpStreamsHandler": {
"@id": "urn:handlersjs-http:default:NodeHttpRequestResponseHandler",
"@id": "urn:semcom-node:default:NodeHttpRequestResponseHandler",
"@type": "NodeHttpRequestResponseHandler",
"NodeHttpRequestResponseHandler:_httpHandler": {
"@type": "SequenceHandler",
"SequenceHandler:_handlers": [
"@type": "HttpCorsRequestHandler",
"HttpCorsRequestHandler:_handler": {
"@id": "urn:semcom-node:default:PipeThroughHandler"
}
}
}
},
{
"@id": "urn:semcom-node:default:PipeThroughHandler",
"@type": "SequenceHandler",
"SequenceHandler:_handlers": [
{
"@id": "urn:semcom-node:default:RoutedHttpRequestHandler",
"@type": "RoutedHttpRequestHandler",
"RoutedHttpRequestHandler:_handlerControllerList": [
{
"@id": "urn:handlersjs-http:default:RoutedHttpRequestHandler",
"@type": "RoutedHttpRequestHandler",
"RoutedHttpRequestHandler:_handlerControllerList": [
"@id": "urn:semcom-node:default:HttpHandlerController",
"@type": "HttpHandlerController",
"HttpHandlerController:_label": "ControllerList",
"HttpHandlerController:_routes": [
{
"@id": "urn:semcom-node:default:ComponentsRoute"
},
{
"@id": "urn:handlersjs-http:default:HttpHandlerController",
"@type": "HttpHandlerController",
"HttpHandlerController:_label": "ControllerList",
"HttpHandlerController:_routes": [
{
"@id": "urn:semcom-node:default:ComponentsRoute"
},
{
"@id": "urn:semcom-node:default:QueryComponentsRoute"
}
]
"@id": "urn:semcom-node:default:QueryComponentsRoute"
}
]
},
{
"@id": "urn:semcom-node:default:ContentNegotiationHttpHandler"
}
]
},
{
"@id": "urn:semcom-node:default:ContentNegotiationHttpHandler"
}
}
]
},
{
"@id": "urn:semcom-node:default:ComponentsRoute",
Expand Down Expand Up @@ -69,6 +76,11 @@
"@type": "HttpHandlerOperation",
"HttpHandlerOperation:_method": "POST",
"HttpHandlerOperation:_publish": true
},
{
"@type": "HttpHandlerOperation",
"HttpHandlerOperation:_method": "OPTIONS",
"HttpHandlerOperation:_publish": true
}
],
"HttpHandlerRoute:_handler": {
Expand Down
11 changes: 11 additions & 0 deletions packages/semcom-node/config/presets/store.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
"ComponentMetadata:_version": "0.1.0",
"ComponentMetadata:_latest": true
},
{
"@type": "ComponentMetadata",
"ComponentMetadata:_description": "Digita SemCom component with an input field.",
"ComponentMetadata:_label": "SemCom Input Component",
"ComponentMetadata:_uri": "https://components.semcom.digita.ai/components/input.js",
"ComponentMetadata:_shapes": "http://digita.ai/voc/input#input",
"ComponentMetadata:_author": "https://digita.ai",
"ComponentMetadata:_tag": "input",
"ComponentMetadata:_version": "0.1.0",
"ComponentMetadata:_latest": true
},
{
"@type": "ComponentMetadata",
"ComponentMetadata:_description": "Digita SemCom component for payslip information.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('ServerHandlerContentNegotiationService', () => {
headers: {
accept: '*/*',
},
path: '/path',
url: new URL('http://example.com'),
method: 'GET',
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/semcom-node/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const launch = async () => {
await manager.configRegistry.register(configPath);

const server: NodeHttpServer = await manager.instantiate(
'urn:handlersjs-http:default:NodeHttpServer',
'urn:semcom-node:default:NodeHttpServer',
);

server.start();
Expand Down
Loading

0 comments on commit b8f5415

Please sign in to comment.