Skip to content
Snippets Groups Projects
Commit b7da8d4d authored by Lars Kollmann's avatar Lars Kollmann
Browse files

#156 finalize validation and error handling, add alert if selected transect has no sections

parent cb480254
No related branches found
No related tags found
2 merge requests!174Merge develop into master,!150Resolve "Begehung: Refactor Error handling (create/update)"
Pipeline #420072 passed
......@@ -121,7 +121,8 @@
"invalidTime": "Es muss eine gültige Uhrzeit ausgewählt werden",
"requiredTime": "Es muss eine Uhrzeit ausgewählt werden",
"requiredDate": "Es muss ein Datum ausgewählt werden",
"notAccessible": "Nicht begehbar"
"notAccessible": "Nicht begehbar",
"noSectionsForTransect": "Das Transekt enthält keine Abschnitte"
}
},
"inspections": {
......
......@@ -121,7 +121,8 @@
"invalidTime": "A valid time must be entered",
"requiredTime": "Time must be entered",
"requiredDate": "Date must be entered",
"notAccessible": "Not accessible"
"notAccessible": "Not accessible",
"noSectionsForTransect": "The transect does not contain any sections"
}
},
"inspections": {
......
......@@ -65,46 +65,55 @@ export default class TransectInspection extends Vue {
@Watch("inspectionData.headerData.transect")
private transectWatcher() {
this.hideAlerts();
this.inspectionData.mainData.sections = [];
// Did this to avoid errors when toggling transect selection
this.getSections(this.inspectionData.headerData.transect, this.inspectionData.mainData.sections)
.then();
.then((data: any) => {
if (data && data.errors) {
let message = "";
for (const error of data.errors) {
message = message.concat(error.message);
}
this.showErrorAlert(message);
} else if (this.inspectionData.mainData.sections.length === 0) {
this.showErrorAlert(this.$i18n.t("transektbegehung.errors.noSectionsForTransect") as string);
}
});
}
private getSections = async (transect: string, sectionsArray: Section[]) => {
const data = await this.getSectionsForTransect(transect);
if (data.errors || !this.validSectionData(data)) {
// todo: add error handling
return sectionsArray;
}
const sections = data.data.transect.sections.edges;
for (let i = 0; i < sections.length; i++) {
const sectionNode = sections[i].node;
const newSection: Section = {
accessible: sectionNode.accessible,
id: sectionNode.id,
label: sectionNode.label,
location: sectionNode.location,
name: sectionNode.name,
sequenceNumber: sectionNode.sequenceNumber,
sectionEvent: {
id: "",
comment: "",
inspectionId: this.inspectionData.id,
sectionId: sectionNode.id,
observationTableRows: [],
imageRefs: [],
},
};
sectionsArray.push(newSection);
}
sectionsArray.sort((a, b) => (a.sequenceNumber > b.sequenceNumber) ? 1 : -1);
return sectionsArray;
if (data.errors || !this.isValidSectionData(data)) {
return data;
}
const sections = data.data.transect.sections.edges;
for (let i = 0; i < sections.length; i++) {
const sectionNode = sections[i].node;
const newSection: Section = {
accessible: sectionNode.accessible,
id: sectionNode.id,
label: sectionNode.label,
location: sectionNode.location,
name: sectionNode.name,
sequenceNumber: sectionNode.sequenceNumber,
sectionEvent: {
id: "",
comment: "",
inspectionId: this.inspectionData.id,
sectionId: sectionNode.id,
observationTableRows: [],
imageRefs: [],
},
};
sectionsArray.push(newSection);
}
sectionsArray.sort((a, b) => (a.sequenceNumber > b.sequenceNumber) ? 1 : -1);
return sectionsArray;
}
private validSectionData(data: any) {
private isValidSectionData(data: any) {
return (data && data.data && data.data.transect && data.data.transect.sections && data.data.transect.sections.edges);
}
......@@ -185,37 +194,11 @@ export default class TransectInspection extends Vue {
});
}
private getResponseErrors(response: any) {
// todo: collect all the errors and try to display in alert
for (const item of response) {
if (item.errors) {
return item.errors[0].message;
}
for (const itemChild of item) {
if (itemChild.errors) {
return itemChild.errors[0].message;
}
for (const itemChildChild of itemChild) {
if (itemChildChild.errors) {
return itemChildChild.errors[0].message;
}
for (const itemChildChildChild of itemChildChild) {
if (itemChildChildChild.errors) {
return itemChildChildChild.errors[0].message;
}
}
}
}
}
return "";
}
private persistMainDataAndGoToStepper = async (step: number) => {
this.hideAlerts();
this.showLoadingCircle(true, step);
if (this.previousUsedTransect !== this.inspectionData.headerData.transect) {
// init create or Transect has changed
this.previousUsedTransect = this.inspectionData.headerData.transect;
const sectionEventsResponse = await this.createSectionEvents(this.inspectionData.id, this.inspectionData.mainData.sections);
const sectionEventErrorMessage = this.getResponseErrors(sectionEventsResponse);
if (sectionEventErrorMessage !== "") {
......@@ -223,13 +206,12 @@ export default class TransectInspection extends Vue {
return;
}
const observationTableRowsResponse = await this.createObservationTableRows(this.inspectionData.mainData.sections);
console.log(observationTableRowsResponse)
const observationTableRowsErrorMessage = this.getResponseErrors(observationTableRowsResponse);
console.log(observationTableRowsErrorMessage)
if (observationTableRowsErrorMessage !== "") {
this.showErrorAlert(observationTableRowsErrorMessage);
return;
}
this.previousUsedTransect = this.inspectionData.headerData.transect;
this.previousMainDataMd5Hash = Md5.hashStr(JSON.stringify(this.inspectionData.mainData));
} else {
const actualMainDataMd5Hash = Md5.hashStr(JSON.stringify(this.inspectionData.mainData));
......@@ -242,7 +224,6 @@ export default class TransectInspection extends Vue {
}
const observationTableRowsResponse = await this.updateObservationTableRows(this.inspectionData.mainData.sections);
console.log(observationTableRowsResponse)
const observationTableRowsErrorMessage = this.getResponseErrors(observationTableRowsResponse);
if (observationTableRowsErrorMessage !== "") {
this.showErrorAlert(observationTableRowsErrorMessage);
......@@ -254,6 +235,39 @@ export default class TransectInspection extends Vue {
this.finishStepperChange(step);
}
private getResponseErrors(response: any) {
// todo: collect all the errors and try to display in alert
// todo: refactor loop style
try {
for (const item of response) {
if (item.errors) {
return item.errors[0].message;
} else if (item.length > 0) {
for (const itemChild of item) {
if (itemChild.errors) {
return itemChild.errors[0].message;
} else if (itemChild.length > 0) {
for (const itemChildChild of itemChild) {
if (itemChildChild.errors) {
return itemChildChild.errors[0].message;
} else if (itemChildChild.length > 0) {
for (const itemChildChildChild of itemChildChild) {
if (itemChildChildChild.errors) {
return itemChildChildChild.errors[0].message;
}
}
}
}
}
}
}
}
} catch (error) {
return error;
}
return "";
}
private createObservationTableRows = async (sections: Section[]) => {
try {
return await Promise.all(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment