Skip to content

Commit

Permalink
[Enhancement #565] Do not reset vocabulary in state when just reloadi…
Browse files Browse the repository at this point in the history
…ng 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.
  • Loading branch information
ledsoft committed Dec 2, 2024
1 parent d9f208c commit bba7c1f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 40 deletions.
3 changes: 2 additions & 1 deletion src/action/AsyncActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -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)) {
Expand Down
9 changes: 6 additions & 3 deletions src/component/vocabulary/VocabularySummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ interface VocabularySummaryProps
}

export interface VocabularySummaryState extends EditableComponentState {
selectDocumentDialogOpen: boolean;
showExportDialog: boolean;
showSnapshotDialog: boolean;
language: string;
Expand All @@ -109,7 +108,6 @@ export class VocabularySummary extends EditableComponent<
showRemoveDialog: false,
showExportDialog: false,
showSnapshotDialog: false,
selectDocumentDialogOpen: false,
language: resolveInitialLanguage(
props.vocabulary,
props.locale,
Expand Down Expand Up @@ -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 });
};
Expand Down Expand Up @@ -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}
/>
)}
</div>
Expand Down
7 changes: 1 addition & 6 deletions src/reducer/TermItReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down
30 changes: 0 additions & 30 deletions src/reducer/__tests__/TermItReducers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit bba7c1f

Please sign in to comment.