Skip to content

Commit

Permalink
Merge pull request #1349 from cassproject/kg-fixAddRelations
Browse files Browse the repository at this point in the history
Error handling when adding relations by url
  • Loading branch information
gloverkari authored Mar 5, 2024
2 parents 0f24dbd + c843fe7 commit 03a75a0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/lode/components/AddProperty.vue
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ export default {
this.selectedPropertyToAddIsLangString = false;
this.limitedTypes = [];
this.limitedConcepts = [];
this.$store.commit('lode/setAddingValues', []);
if (this.profile && this.profile[this.selectedPropertyToAdd.value]) {
var range = [];
var ary = this.profile[this.selectedPropertyToAdd.value]["http://schema.org/rangeIncludes"];
Expand Down
5 changes: 2 additions & 3 deletions src/lode/components/ThingEditing.vue
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,8 @@ export default {
saveNewProperty: async function() {
// Validate input
var property = this.addingProperty;
// Remove unnecessary slash from end of url. github #827
var value = (this.addingValues.length > 0) ? this.addingValues[0] : undefined;
value = value.endsWith("/") ? value.slice(0, value.length - 1) : value;
// It's possible to accumulate more than one value if user deleted values in textbox. Use the last added value.
var value = (this.addingValues.length > 0) ? this.addingValues[this.addingValues.length - 1] : undefined;
var range = this.addingRange;
this.errorMessage = [];
this.errorMessage = [];
Expand Down
48 changes: 43 additions & 5 deletions src/store/modules/lode.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,30 @@ const mutations = {
setAddingProperty(state, value) {
state.addingProperty = value;
},
setAddingValues(state, value) {
state.addingValues = value;
setAddingValues(state, values) {
let newValues = [];
if (Array.isArray(values)) {
values.forEach((value) => {
if (value) {
let newValue = trimUrl(value);
if (newValue) newValues.push(newValue);
}
});
} else {
if (values) {
let newValue = trimUrl(values);
if (newValue) newValues.push(newValue);
}
}
state.addingValues = newValues;
},
addToAddingValues(state, value) {
let newValue = trimUrl(value);
if (!state.addingValues) {
state.addingValues = [];
state.addingValues.push(value);
} else {
state.addingValues.push(value);
}
if (newValue) {
state.addingValues.push(newValue);
}
},
setAddingRange(state, value) {
Expand Down Expand Up @@ -164,6 +179,29 @@ const getters = {
}
};

function trimUrl(url) {
if (!url) {
return undefined;
}
if (url === "" || (url["@value"] && url["@value"] === "")) {
return undefined;
}
let trimmed = url;
if (trimmed["@value"] && typeof trimmed["@value"] === "string" && trimmed["@value"].endsWith("/")) {
trimmed["@value"] = trimmed["@value"].slice(0, trimmed["@value"].length - 2);
}
if (trimmed["@value"] && typeof trimmed["@value"] === "string" && trimmed["@value"].startsWith("/")) {
trimmed["@value"] = trimmed["@value"].slice(1);
}
if (trimmed && typeof trimmed === "string" && trimmed.endsWith("/")) {
trimmed = trimmed.slice(0, trimmed.length - 2);
}
if (trimmed && typeof trimmed === "string" && trimmed.startsWith("/")) {
trimmed = trimmed.slice(1);
}
return trimmed;
}

jsonld.documentLoader = async function(url) {
if (url in state.rawSchemata) {
return {
Expand Down

0 comments on commit 03a75a0

Please sign in to comment.