From e42ba7bd3f873ffdcf7877366b5d5d99c5bea59b Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Mon, 2 Dec 2024 15:52:22 +0100 Subject: [PATCH] [Enhancement #565] Do not reset vocabulary in state when just reloading one. When files in a vocabulary are changed, the vocabulary needs to be reloaded. Ensure vocabulary reload does not cause VocabularyMetadata unmount, which caused switch of vocabulary tab to the default one - glossary. --- src/action/AsyncActions.ts | 3 +- .../vocabulary/VocabularySummary.tsx | 9 ++++-- src/reducer/TermItReducers.ts | 7 +---- src/reducer/__tests__/TermItReducers.test.ts | 30 ------------------- 4 files changed, 9 insertions(+), 40 deletions(-) diff --git a/src/action/AsyncActions.ts b/src/action/AsyncActions.ts index c3e5f581..03592ff0 100644 --- a/src/action/AsyncActions.ts +++ b/src/action/AsyncActions.ts @@ -28,7 +28,7 @@ import * as jsonld from "jsonld"; import Message from "../model/Message"; import MessageType from "../model/MessageType"; import Term, { CONTEXT as TERM_CONTEXT, TermData } from "../model/Term"; -import VocabularyUtils, { IRI } from "../util/VocabularyUtils"; +import VocabularyUtils, { IRI, IRIImpl } from "../util/VocabularyUtils"; import ActionType, { PendingAsyncAction } from "./ActionType"; import Resource, { ResourceData } from "../model/Resource"; import RdfsResource, { @@ -150,6 +150,7 @@ export function createVocabulary(vocabulary: Vocabulary) { export function loadVocabulary(iri: IRI, timestamp?: string) { const action = { type: ActionType.LOAD_VOCABULARY, + iri: IRIImpl.toString(iri), }; return (dispatch: ThunkDispatch, getState: () => TermItState) => { if (isActionRequestPending(getState(), action)) { diff --git a/src/component/vocabulary/VocabularySummary.tsx b/src/component/vocabulary/VocabularySummary.tsx index 7abeec36..92fa2c16 100644 --- a/src/component/vocabulary/VocabularySummary.tsx +++ b/src/component/vocabulary/VocabularySummary.tsx @@ -82,7 +82,6 @@ interface VocabularySummaryProps } export interface VocabularySummaryState extends EditableComponentState { - selectDocumentDialogOpen: boolean; showExportDialog: boolean; showSnapshotDialog: boolean; language: string; @@ -109,7 +108,6 @@ export class VocabularySummary extends EditableComponent< showRemoveDialog: false, showExportDialog: false, showSnapshotDialog: false, - selectDocumentDialogOpen: false, language: resolveInitialLanguage( props.vocabulary, props.locale, @@ -171,6 +169,11 @@ export class VocabularySummary extends EditableComponent< this.props.requestVocabularyValidation(iriFromUrl, this.props.stompClient); }; + public reloadVocabulary = () => { + const iri = VocabularyUtils.create(this.props.vocabulary.iri); + this.props.loadVocabulary(iri); + }; + public setLanguage = (language: string) => { this.setState({ language }); }; @@ -325,7 +328,7 @@ export class VocabularySummary extends EditableComponent< match={this.props.match} language={this.state.language} selectLanguage={this.setLanguage} - onChange={this.loadVocabulary} + onChange={this.reloadVocabulary} /> )} diff --git a/src/reducer/TermItReducers.ts b/src/reducer/TermItReducers.ts index baebede5..fe3abb6f 100644 --- a/src/reducer/TermItReducers.ts +++ b/src/reducer/TermItReducers.ts @@ -126,7 +126,7 @@ function vocabulary( switch (action.type) { case ActionType.LOAD_VOCABULARY: if (action.status === AsyncActionStatus.REQUEST) { - return EMPTY_VOCABULARY; + return (action as any).iri === state.iri ? state : EMPTY_VOCABULARY; } else if (isAsyncSuccess(action)) { return action.payload as Vocabulary; } else { @@ -144,11 +144,6 @@ function vocabulary( return onTermCountLoaded(state, action); case ActionType.LOGOUT: return EMPTY_VOCABULARY; - case ActionType.REMOVE_RESOURCE: - case ActionType.UPDATE_RESOURCE: - case ActionType.CREATE_RESOURCE: // intentional fall-through - // the resource might have been/be related to the vocabulary - return isAsyncSuccess(action) ? EMPTY_VOCABULARY : state; default: return state; } diff --git a/src/reducer/__tests__/TermItReducers.test.ts b/src/reducer/__tests__/TermItReducers.test.ts index 76949d80..13be8dae 100644 --- a/src/reducer/__tests__/TermItReducers.test.ts +++ b/src/reducer/__tests__/TermItReducers.test.ts @@ -350,36 +350,6 @@ describe("Reducers", () => { expect(vocabulary.allImportedVocabularies).toEqual(imports); }); - it("resets vocabulary to empty when resource is removed", () => { - // The removed resource could have been a file from a document related to that vocabulary, in which case - // the vocabulary needs to be reloaded - const action = { type: ActionType.REMOVE_RESOURCE }; - initialState.vocabulary = new Vocabulary({ - label: langString("Test vocabulary"), - iri: Generator.generateUri(), - types: [VocabularyUtils.VOCABULARY], - }); - expect( - reducers(stateToPlainObject(initialState), asyncActionSuccess(action)) - .vocabulary - ).toEqual(EMPTY_VOCABULARY); - }); - - it("resets vocabulary to empty when resource is created", () => { - // The created resource could be a file added to a document related to that vocabulary, in which case - // the vocabulary needs to be reloaded - const action = { type: ActionType.CREATE_RESOURCE }; - initialState.vocabulary = new Vocabulary({ - label: langString("Test vocabulary"), - iri: Generator.generateUri(), - types: [VocabularyUtils.VOCABULARY], - }); - expect( - reducers(stateToPlainObject(initialState), asyncActionSuccess(action)) - .vocabulary - ).toEqual(EMPTY_VOCABULARY); - }); - it("sets term count on vocabulary when it is loaded", () => { initialState.vocabulary = new Vocabulary({ label: langString("Test vocabulary"),