From 9c4584d76c1d2e900a7668b4d0ec7cf5c91dca2f Mon Sep 17 00:00:00 2001 From: Joep Meindertsma Date: Sun, 5 Feb 2023 11:44:54 +0100 Subject: [PATCH] `store.createSubject` allows creating nested paths --- CHANGELOG.md | 3 ++- .../components/NewInstanceButton/useCreateAndNavigate.ts | 2 +- .../NewInstanceButton/useDefaultNewInstanceHandler.tsx | 3 ++- data-browser/src/views/ChatRoomPage.tsx | 2 +- data-browser/src/views/DocumentPage.tsx | 5 ++++- lib/src/store.ts | 6 +++++- 6 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73c2ac5ca..333e45b01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,10 @@ This changelog covers all three packages, as they are (for now) updated as a who ## UNRELEASED +- Let users register using e-mail address, improve sign-up UX. - Add `Store.parseMetaTags` to load JSON-AD objects stored in the DOM. Speeds up initial page load by allowing server to set JSON-AD objects in the initial HTML response. - Move static assets around, align build with server and fix PWA #292 -- Let users register using e-mail address, improve sign-up UX. +- `store.createSubject` allows creating nested paths ## v0.35.0 diff --git a/data-browser/src/components/NewInstanceButton/useCreateAndNavigate.ts b/data-browser/src/components/NewInstanceButton/useCreateAndNavigate.ts index 402ba779e..9ce168b52 100644 --- a/data-browser/src/components/NewInstanceButton/useCreateAndNavigate.ts +++ b/data-browser/src/components/NewInstanceButton/useCreateAndNavigate.ts @@ -34,7 +34,7 @@ export function useCreateAndNavigate(klass: string, parent?: string) { /** Do not set a parent for the new resource. Useful for top-level resources */ noParent?: boolean, ): Promise => { - const subject = store.createSubject(className); + const subject = store.createSubject(className, parent); const resource = new Resource(subject, true); await Promise.all([ diff --git a/data-browser/src/components/NewInstanceButton/useDefaultNewInstanceHandler.tsx b/data-browser/src/components/NewInstanceButton/useDefaultNewInstanceHandler.tsx index dbdc02315..1c46cc5b1 100644 --- a/data-browser/src/components/NewInstanceButton/useDefaultNewInstanceHandler.tsx +++ b/data-browser/src/components/NewInstanceButton/useDefaultNewInstanceHandler.tsx @@ -8,6 +8,7 @@ import { import { useCallback } from 'react'; import { useNavigate } from 'react-router-dom'; import { useSettings } from '../../helpers/AppSettings'; +import { handleError } from '../../helpers/loggingHandlers'; import { newURL } from '../../helpers/navigation'; import { useCreateAndNavigate } from './useCreateAndNavigate'; @@ -99,7 +100,7 @@ export function useDefaultNewInstanceHandler(klass: string, parent?: string) { } } } catch (e) { - store.handleError(e); + store.notifyError(e); } }, [klass, store, parent, createResourceAndNavigate]); diff --git a/data-browser/src/views/ChatRoomPage.tsx b/data-browser/src/views/ChatRoomPage.tsx index a45ec716b..4e6717c80 100644 --- a/data-browser/src/views/ChatRoomPage.tsx +++ b/data-browser/src/views/ChatRoomPage.tsx @@ -75,7 +75,7 @@ export function ChatRoomPage({ resource }: ResourcePageProps) { e && e.preventDefault(); if (!disableSend) { - const subject = store.createSubject('messages'); + const subject = store.createSubject('messages', resource.getSubject()); const msgResource = new Resource(subject, true); await msgResource.set( properties.parent, diff --git a/data-browser/src/views/DocumentPage.tsx b/data-browser/src/views/DocumentPage.tsx index 02fd581a7..aaf7f8a3c 100644 --- a/data-browser/src/views/DocumentPage.tsx +++ b/data-browser/src/views/DocumentPage.tsx @@ -187,7 +187,10 @@ function DocumentPageEdit({ async function addElement(position: number) { // When an element is created, it should be a Resource that has this document as its parent. // or maybe a nested resource? - const elementSubject = store.createSubject('element'); + const elementSubject = store.createSubject( + 'element', + resource.getSubject(), + ); elements.splice(position, 0, elementSubject); try { diff --git a/lib/src/store.ts b/lib/src/store.ts index 14410ec02..9dd393e41 100644 --- a/lib/src/store.ts +++ b/lib/src/store.ts @@ -166,10 +166,14 @@ export class Store { } /** Creates a random URL. Add a classnme (e.g. 'persons') to make a nicer name */ - public createSubject(className?: string): string { + public createSubject(className?: string, parentSubject?: string): string { const random = this.randomPart(); className = className ? className : 'things'; + if (parentSubject) { + return `${parentSubject}/${className}/${random}`; + } + return `${this.getServerUrl()}/${className}/${random}`; }