Skip to content

Commit

Permalink
[Fix] Force reload of vocabularies after import of vocabulary.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Mar 12, 2024
1 parent c59d76e commit 81b4178
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
68 changes: 31 additions & 37 deletions src/reducer/TermItReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ import VocabularyUtils, { IRIImpl } from "../util/VocabularyUtils";
import TermOccurrence from "../model/TermOccurrence";
import { Breadcrumb } from "../model/Breadcrumb";

function isAsyncSuccess(action: AsyncAction) {
return action.status === AsyncActionStatus.SUCCESS;
}

/**
* Handles changes to the currently logged in user.
*
Expand All @@ -50,9 +54,7 @@ function user(
): User {
switch (action.type) {
case ActionType.FETCH_USER:
return action.status === AsyncActionStatus.SUCCESS
? action.payload
: state;
return isAsyncSuccess(action) ? action.payload : state;
case ActionType.LOGOUT:
return EMPTY_USER;
default:
Expand Down Expand Up @@ -121,13 +123,13 @@ function vocabulary(
case ActionType.LOAD_VOCABULARY:
if (action.status === AsyncActionStatus.REQUEST) {
return EMPTY_VOCABULARY;
} else if (action.status === AsyncActionStatus.SUCCESS) {
} else if (isAsyncSuccess(action)) {
return action.payload as Vocabulary;
} else {
return state;
}
case ActionType.LOAD_VOCABULARY_IMPORTS:
return action.status === AsyncActionStatus.SUCCESS
return isAsyncSuccess(action)
? new Vocabulary(
Object.assign(state, {
allImportedVocabularies: action.payload as string[],
Expand All @@ -142,9 +144,7 @@ function vocabulary(
case ActionType.UPDATE_RESOURCE:
case ActionType.CREATE_RESOURCE: // intentional fall-through
// the resource might have been/be related to the vocabulary
return action.status === AsyncActionStatus.SUCCESS
? EMPTY_VOCABULARY
: state;
return isAsyncSuccess(action) ? EMPTY_VOCABULARY : state;
default:
return state;
}
Expand All @@ -171,7 +171,7 @@ function selectedFile(
): File {
switch (action.type) {
case ActionType.LOAD_RESOURCE:
return action.status === AsyncActionStatus.SUCCESS
return isAsyncSuccess(action)
? action.payload.owner
? new File(action.payload)
: action.payload
Expand All @@ -190,7 +190,7 @@ function vocabularies(
): { [key: string]: Vocabulary } {
switch (action.type) {
case ActionType.LOAD_VOCABULARIES:
if (action.status === AsyncActionStatus.SUCCESS) {
if (isAsyncSuccess(action)) {
const map = {};
action.payload.forEach((v) => (map[v.iri] = v));
return map;
Expand All @@ -199,6 +199,11 @@ function vocabularies(
}
case ActionType.LOGOUT:
return {};
case ActionType.IMPORT_SKOS:
if (isAsyncSuccess(action)) {
return {};
}
return state;
default:
return state;
}
Expand Down Expand Up @@ -233,7 +238,7 @@ function selectedTerm(
function createdTermsCounter(state: number = 0, action: AsyncAction) {
switch (action.type) {
case ActionType.CREATE_VOCABULARY_TERM:
return action.status === AsyncActionStatus.SUCCESS ? state + 1 : state;
return isAsyncSuccess(action) ? state + 1 : state;
case ActionType.LOGOUT:
return 0;
default:
Expand All @@ -247,7 +252,7 @@ function queryResults(
) {
switch (action.type) {
case ActionType.EXECUTE_QUERY:
if (action.status === AsyncActionStatus.SUCCESS) {
if (isAsyncSuccess(action)) {
return {
...state,
[action.queryString]: new QueryResult(
Expand All @@ -271,7 +276,7 @@ function fileContent(
): string | null {
switch (action.type) {
case ActionType.LOAD_FILE_CONTENT:
if (action.status === AsyncActionStatus.SUCCESS) {
if (isAsyncSuccess(action)) {
return action.payload;
} else if (action.status === AsyncActionStatus.REQUEST) {
return null;
Expand Down Expand Up @@ -350,7 +355,7 @@ function types(
): { [key: string]: Term } {
switch (action.type) {
case ActionType.LOAD_TYPES:
if (action.status === AsyncActionStatus.SUCCESS) {
if (isAsyncSuccess(action)) {
const map = {};
action.payload.forEach((v) => (map[v.iri] = v));
return map;
Expand All @@ -368,7 +373,7 @@ function states(
) {
switch (action.type) {
case ActionType.LOAD_STATES:
if (action.status === AsyncActionStatus.SUCCESS) {
if (isAsyncSuccess(action)) {
return Utils.mapArray(action.payload);
} else {
return state;
Expand All @@ -384,7 +389,7 @@ function terminalStates(
) {
switch (action.type) {
case ActionType.LOAD_STATES:
if (action.status === AsyncActionStatus.SUCCESS) {
if (isAsyncSuccess(action)) {
return action.payload
.filter(
(r) => r.types.indexOf(VocabularyUtils.TERM_STATE_TERMINAL) !== -1
Expand All @@ -405,9 +410,7 @@ function properties(
switch (action.type) {
case ActionType.GET_PROPERTIES:
const asyncAction = action as AsyncActionSuccess<RdfsResource[]>;
return asyncAction.status === AsyncActionStatus.SUCCESS
? asyncAction.payload
: state;
return isAsyncSuccess(asyncAction) ? asyncAction.payload : state;
case ActionType.CLEAR_PROPERTIES:
return [];
default:
Expand Down Expand Up @@ -489,10 +492,7 @@ function labelCache(
state: { [key: string]: string } = {},
action: AsyncActionSuccess<{ [key: string]: string }>
) {
if (
action.type === ActionType.GET_LABEL &&
action.status === AsyncActionStatus.SUCCESS
) {
if (action.type === ActionType.GET_LABEL && isAsyncSuccess(action)) {
return Object.assign({}, state, action.payload);
}
return state;
Expand Down Expand Up @@ -540,12 +540,12 @@ function annotatorTerms(
): { [key: string]: Term } {
switch (action.type) {
case ActionType.ANNOTATOR_LOAD_TERMS:
if (action.status === AsyncActionStatus.SUCCESS) {
if (isAsyncSuccess(action)) {
return action.payload as { [key: string]: Term };
}
return {};
case ActionType.ANNOTATOR_LOAD_TERM:
if (action.status === AsyncActionStatus.SUCCESS) {
if (isAsyncSuccess(action)) {
const change = {};
const payload = action.payload as Term;
change[payload.iri] = payload;
Expand All @@ -561,10 +561,7 @@ function configuration(
state: Configuration = DEFAULT_CONFIGURATION,
action: AsyncActionSuccess<Configuration>
) {
if (
action.type === ActionType.LOAD_CONFIGURATION &&
action.status === AsyncActionStatus.SUCCESS
) {
if (action.type === ActionType.LOAD_CONFIGURATION && isAsyncSuccess(action)) {
return action.payload;
}
return state;
Expand All @@ -576,7 +573,7 @@ function validationResults(
) {
switch (action.type) {
case ActionType.FETCH_VALIDATION_RESULTS:
if (action.status === AsyncActionStatus.SUCCESS) {
if (isAsyncSuccess(action)) {
return {
...state,
...action.payload,
Expand All @@ -597,13 +594,13 @@ function definitionallyRelatedTerms(
) {
switch (action.type) {
case ActionType.LOAD_DEFINITION_RELATED_TERMS_TARGETING:
if (action.status === AsyncActionStatus.SUCCESS) {
if (isAsyncSuccess(action)) {
return Object.assign({}, state, { targeting: action.payload });
} else {
return state;
}
case ActionType.LOAD_DEFINITION_RELATED_TERMS_OF:
if (action.status === AsyncActionStatus.SUCCESS) {
if (isAsyncSuccess(action)) {
return Object.assign({}, state, { of: action.payload });
} else {
return state;
Expand Down Expand Up @@ -635,7 +632,7 @@ function breadcrumbs(state: Breadcrumb[] = [], action: BreadcrumbAction) {
function users(state: User[] = [], action: AsyncActionSuccess<User[]>) {
switch (action.type) {
case ActionType.LOAD_USERS:
if (action.status === AsyncActionStatus.SUCCESS) {
if (isAsyncSuccess(action)) {
return action.payload;
}
return state;
Expand All @@ -650,10 +647,7 @@ function accessLevels(
state: { [key: string]: RdfsResource } = {},
action: AsyncActionSuccess<RdfsResource[]>
) {
if (
action.type === ActionType.LOAD_ACCESS_LEVELS &&
action.status === AsyncActionStatus.SUCCESS
) {
if (action.type === ActionType.LOAD_ACCESS_LEVELS && isAsyncSuccess(action)) {
const newState = {};
action.payload.forEach((r) => (newState[r.iri] = r));
return newState;
Expand Down
30 changes: 23 additions & 7 deletions src/reducer/__tests__/TermItReducers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ describe("Reducers", () => {
it("sets vocabulary when it was successfully loaded", () => {
const action = { type: ActionType.LOAD_VOCABULARY };
const vocabularyData: VocabularyData = {
label: "Test vocabulary",
label: langString("Test vocabulary"),
iri: Generator.generateUri(),
};
expect(
Expand All @@ -334,7 +334,7 @@ describe("Reducers", () => {
it("sets transitive imports on vocabulary when they are loaded", () => {
const imports = [Generator.generateUri(), Generator.generateUri()];
initialState.vocabulary = new Vocabulary({
label: "Test vocabulary",
label: langString("Test vocabulary"),
iri: Generator.generateUri(),
});
const vocabulary = reducers(
Expand All @@ -352,7 +352,7 @@ describe("Reducers", () => {
// the vocabulary needs to be reloaded
const action = { type: ActionType.REMOVE_RESOURCE };
initialState.vocabulary = new Vocabulary({
label: "Test vocabulary",
label: langString("Test vocabulary"),
iri: Generator.generateUri(),
types: [VocabularyUtils.VOCABULARY],
});
Expand All @@ -367,7 +367,7 @@ describe("Reducers", () => {
// the vocabulary needs to be reloaded
const action = { type: ActionType.CREATE_RESOURCE };
initialState.vocabulary = new Vocabulary({
label: "Test vocabulary",
label: langString("Test vocabulary"),
iri: Generator.generateUri(),
types: [VocabularyUtils.VOCABULARY],
});
Expand All @@ -379,7 +379,7 @@ describe("Reducers", () => {

it("sets term count on vocabulary when it is loaded", () => {
initialState.vocabulary = new Vocabulary({
label: "Test vocabulary",
label: langString("Test vocabulary"),
iri: Generator.generateUri(),
types: [VocabularyUtils.VOCABULARY],
});
Expand All @@ -399,7 +399,7 @@ describe("Reducers", () => {

it("does not set term count on vocabulary when its identifier does not match action", () => {
initialState.vocabulary = new Vocabulary({
label: "Test vocabulary",
label: langString("Test vocabulary"),
iri: Generator.generateUri(),
types: [VocabularyUtils.VOCABULARY],
});
Expand All @@ -420,7 +420,7 @@ describe("Reducers", () => {
it("resets vocabulary to empty when vocabulary loading request is sent", () => {
const action = { type: ActionType.LOAD_VOCABULARY };
initialState.vocabulary = new Vocabulary({
label: "Test vocabulary",
label: langString("Test vocabulary"),
iri: Generator.generateUri(),
types: [VocabularyUtils.VOCABULARY],
});
Expand Down Expand Up @@ -988,4 +988,20 @@ describe("Reducers", () => {
]);
});
});

describe("vocabularies", () => {
it("are reset on successful SKOS import action", () => {
initialState.vocabularies = require("../../rest-mock/vocabularies.json");
expect(
reducers(
stateToPlainObject(initialState),
asyncActionSuccess({ type: ActionType.IMPORT_SKOS })
)
).toEqual(
Object.assign({}, initialState, {
vocabularies: {},
})
);
});
});
});

0 comments on commit 81b4178

Please sign in to comment.