forked from SolidOS/profile-pane
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b9cb83
commit 69b4410
Showing
8 changed files
with
187 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
lib | ||
node_modules | ||
node_modules | ||
.history | ||
.vscode |
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,95 @@ | ||
import { html, TemplateResult } from "lit-html"; | ||
import { styleMap } from "lit-html/directives/style-map"; | ||
import { DataBrowserContext } from "pane-registry"; | ||
import * as $rdf from "rdflib"; | ||
import * as UI from "solid-ui"; | ||
import { padding, textCenter } from "./baseStyles"; | ||
|
||
const styles = { | ||
button: styleMap({ ...textCenter(), ...padding() }), | ||
}; | ||
|
||
let buttonContainer = <HTMLDivElement>document.createElement("div"); | ||
const messageDiv = <HTMLDivElement>document.createElement("div"); | ||
messageDiv.setAttribute( | ||
"style", | ||
"margin: 0.1em; padding: 0.5em; border: 0.05em solid gray; background-color: #efe; color:black;" | ||
); | ||
|
||
export const addMeToYourFriendsHtml = ( | ||
subject: $rdf.NamedNode, | ||
context: DataBrowserContext | ||
): TemplateResult => { | ||
buttonContainer = context.dom.createElement("div"); | ||
buttonContainer.appendChild(createAddMeToYourFriendsButton(subject, context)); | ||
return html` <div style="${styles.button}">${buttonContainer}</div> `; | ||
}; | ||
|
||
const createAddMeToYourFriendsButton = ( | ||
subject: $rdf.NamedNode, | ||
context: DataBrowserContext | ||
): HTMLButtonElement => { | ||
const button = UI.widgets.button(context.dom, undefined, "Add me to your friends", undefined, { | ||
needsBorder: true, | ||
}); | ||
button.addEventListener( | ||
"click", | ||
async () => | ||
await saveNewFriend(subject, context) | ||
.then(() => reportToFrontend(true, context, "Friend was added!")) | ||
.catch((error) => reportToFrontend(false, context, error)), | ||
false | ||
); | ||
return button; | ||
}; | ||
|
||
async function saveNewFriend(subject: $rdf.NamedNode, context: DataBrowserContext): Promise<void> { | ||
const loggedInContext = await UI.authn.logInLoadProfile(context).catch(() => { | ||
//For error text consistency reasons, we need to chatch this here. | ||
//Now it shows the same message like in the error upon clicking the chat button. | ||
throw new Error("Current user not found! Not logged in?"); | ||
}); | ||
const me = loggedInContext.me; | ||
const profileDoc = me.doc(); | ||
const store = context.session.store; | ||
const updater = store.updater; | ||
const toBeInserted = [UI.rdf.st(me, UI.ns.foaf("knows"), subject, profileDoc)]; | ||
try { | ||
await updater.update([], toBeInserted); | ||
} catch (error) { | ||
let errorMessage = error; | ||
if (errorMessage.toString().includes("Unauthenticated")) | ||
errorMessage = "Current user not found! Not logged in?"; | ||
throw new Error(errorMessage); | ||
} | ||
} | ||
|
||
function reportToFrontend(possitive: boolean, context: DataBrowserContext, message: string) { | ||
clearMessage(); | ||
if (possitive) reportPositive(context, message); | ||
else complain(context, message); | ||
} | ||
|
||
function complain(context: DataBrowserContext, error: string) { | ||
buttonContainer.appendChild(UI.widgets.errorMessageBlock(context.dom, error)); | ||
} | ||
|
||
function reportPositive(context: DataBrowserContext, message: string) { | ||
messageDiv.innerHTML = message; | ||
buttonContainer.appendChild(messageDiv); | ||
} | ||
function clearMessage() { | ||
while (buttonContainer.childNodes.length > 1) { | ||
buttonContainer.removeChild(buttonContainer.lastChild); | ||
} | ||
} | ||
//Because the code has unhandled Promises we still want to signal the user a message. | ||
//Console will contain actual error. | ||
window.addEventListener("unhandledrejection", function () { | ||
clearMessage(); | ||
buttonContainer.appendChild( | ||
UI.widgets.errorMessageBlock(window.document, "An internal error occured!") | ||
); | ||
}); | ||
|
||
export { saveNewFriend, createAddMeToYourFriendsButton }; |
2 changes: 1 addition & 1 deletion
2
src/CVPresenter.spec.ts → src/integration-tests/CVPresenter.spec.ts
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,28 @@ | ||
import {DataBrowserContext, PaneRegistry} from "pane-registry"; | ||
import {sym} from "rdflib"; | ||
import {SolidLogic} from "solid-logic"; | ||
import {store} from "solid-ui" | ||
|
||
export const subject = sym("https://testingsolidos.solidcommunity.net/profile/card#me"); | ||
export const doc = subject.doc(); | ||
|
||
export const context = { | ||
dom: document, | ||
getOutliner: () => null, | ||
session: { | ||
paneRegistry: { | ||
byName: (name: string) => { | ||
return { | ||
render: () => { | ||
return document.createElement('div') | ||
.appendChild( | ||
document.createTextNode(`mock ${name} pane`) | ||
); | ||
} | ||
} | ||
} | ||
} as PaneRegistry, | ||
store, | ||
logic: {} as SolidLogic, | ||
}, | ||
} as unknown as DataBrowserContext; |
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,56 @@ | ||
import { context, doc, subject } from "./setup"; | ||
import pane from "../"; | ||
import { findByText, fireEvent } from "@testing-library/dom"; | ||
import { parse } from "rdflib"; | ||
import { ns } from "solid-ui"; | ||
|
||
describe("add-me-to-your-friends", () => { | ||
describe("saveNewFriend with NO logged in user", () => { | ||
let result; | ||
beforeAll(() => { | ||
result = pane.render(subject, context); | ||
}); | ||
|
||
it("renders the Add me to friends button", async () => { | ||
const button = await findByText(result, "ADD ME TO YOUR FRIENDS"); | ||
expect(button).not.toBeNull(); | ||
}); | ||
|
||
it("saveNewFriend with user NOT logged in", async () => { | ||
const button = await findByText(result, "ADD ME TO YOUR FRIENDS"); | ||
fireEvent.click(button); | ||
const errorMessage = await findByText(result, "Current user not found! Not logged in?"); | ||
expect(errorMessage).not.toBeNull(); | ||
expect(button).toThrowError; | ||
}); | ||
}); | ||
|
||
/* describe("saveNewFriend with user logged in", () => { | ||
let result; | ||
beforeEach(() => { | ||
context.session.store.removeDocument(doc); | ||
const turtle = ` | ||
@prefix : <#>. | ||
@prefix foaf: <http://xmlns.com/foaf/0.1/> . | ||
@prefix vcard: <http://www.w3.org/2006/vcard/ns#> . | ||
:me foaf:name "Testing Solidos"; | ||
. | ||
`; | ||
parse(turtle, context.session.store, doc.uri); | ||
result = pane.render(subject, context); | ||
}); | ||
it("renders the Add me to friends button", async () => { | ||
const button = await findByText(result, "ADD ME TO YOUR FRIENDS"); | ||
expect(button).not.toBeNull(); | ||
}); | ||
it("verify added triple", async () => { | ||
const button = await findByText(result, "ADD ME TO YOUR FRIENDS"); | ||
fireEvent.click(button); | ||
console.dir(context.session.store.statements) | ||
const triple = context.session.store.any(subject, ns.foaf("knows"), null, doc); | ||
expect(triple).not.toBeNull(); | ||
}); | ||
}); */ | ||
}); |
2 changes: 1 addition & 1 deletion
2
src/presenter.spec.ts → src/integration-tests/presenter.spec.ts
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