diff --git a/compendium_v2/routes/survey.py b/compendium_v2/routes/survey.py index 898a6b9ea527636934438190b0b41e739ddd16d8..a9b26d34b44ce0a3f0b14cb9926d190b14d1ac7f 100644 --- a/compendium_v2/routes/survey.py +++ b/compendium_v2/routes/survey.py @@ -257,7 +257,7 @@ def try_survey(year) -> Any: """ survey = db.session.scalar(select(Survey).where(Survey.year == year)) if not survey: - return "Survey not found", 404 + return jsonify({'success': False, 'message': 'Survey not found'}), 404 return jsonify({ "model": survey.survey, @@ -283,7 +283,7 @@ def inspect_survey(year) -> Any: """ survey = db.session.scalar(select(Survey).where(Survey.year == year)) if not survey: - return "Survey not found", 404 + return jsonify({'success': False, 'message': 'Survey not found'}), 404 def visible_visitor(object, items): for key, value in items: @@ -323,11 +323,11 @@ def load_survey(year, nren_name) -> Any: """ nren = db.session.scalar(select(NREN).filter(NREN.name == nren_name)) if not nren: - return "NREN not found", 404 + return jsonify({'success': False, 'message': 'Survey not found'}), 404 survey = db.session.scalar(select(Survey).where(Survey.year == year)) if not survey: - return "Survey not found", 404 + return jsonify({'success': False, 'message': 'Survey not found'}), 404 if not check_access_nren(current_user, nren): return jsonify({'success': False, 'message': 'You do not have permissions to access this survey.'}), 403 @@ -373,11 +373,11 @@ def save_survey(year, nren_name) -> Any: nren = db.session.scalar(select(NREN).filter(NREN.name == nren_name)) if nren is None: - return "NREN not found", 404 + return jsonify({'success': False, 'message': 'Survey not found'}), 404 survey = db.session.scalar(select(Survey).where(Survey.year == year)) if survey is None: - return "Survey not found", 404 + return jsonify({'success': False, 'message': 'Survey not found'}), 404 if not check_access_nren(current_user, nren): return jsonify({'success': False, 'message': 'You do not have permission to edit this survey.'}), 403 diff --git a/survey-frontend/src/SurveyContainerComponent.tsx b/survey-frontend/src/SurveyContainerComponent.tsx index c419e97113f61234846d1e7c625b4ee699d589ac..ccebe300cf00596225aa99a10b2643f95ddbb6bb 100644 --- a/survey-frontend/src/SurveyContainerComponent.tsx +++ b/survey-frontend/src/SurveyContainerComponent.tsx @@ -16,39 +16,53 @@ function SurveyContainerComponent({ loadFrom, saveTo = '', readonly = false }) { const [surveyModel, setSurveyModel] = useState<Model>(); const verificationStatus = useRef<Map<string, VerificationStatus>>(new Map()); const { year, nren } = useParams(); + const [error, setError] = useState<string>('loading survey...'); useEffect(() => { - getModel(); - }, []); + async function getModel() { + const response = await fetch(loadFrom + year + (nren ? '/' + nren : '')) // year is always set, nren stays empty for inspect and try + const json = await response.json(); - if (surveyModel === undefined) { - return 'loading survey...' - } + if (!response.ok) { - async function getModel() { - const response = await fetch(loadFrom + year + (nren ? '/' + nren : '')) // year is always set, nren stays empty for inspect and try - const json = await response.json(); + if ('message' in json) { + throw new Error(json.message); + } else { + throw new Error(`Request failed with status ${response.status}`); + } + } - for (const questionName in json["verification_status"]) { - verificationStatus.current.set(questionName, json["verification_status"][questionName]); - } - const survey = new Model(json['model']); - survey.setVariable('surveyyear', year); - survey.setVariable('previousyear', parseInt(year!) - 1); - - survey.data = json['data']; - survey.clearIncorrectValues(true); // TODO test if this really removes all old values and such - survey.currentPageNo = json['page']; - survey.showNavigationButtons = false; - survey.showTOC = false; - if (readonly) { - survey.mode = 'display'; + for (const questionName in json["verification_status"]) { + verificationStatus.current.set(questionName, json["verification_status"][questionName]); + } + + const survey = new Model(json['model']); + survey.setVariable('surveyyear', year); + survey.setVariable('previousyear', parseInt(year!) - 1); + + survey.data = json['data']; + survey.clearIncorrectValues(true); // TODO test if this really removes all old values and such + survey.currentPageNo = json['page']; + survey.showNavigationButtons = false; + survey.showTOC = false; + if (readonly) { + survey.mode = 'display'; + } + + setSurveyModel(survey); } - setSurveyModel(survey); + getModel().catch(error => setError('Error when loading survey: ' + error.message)) + + }); + console.log(surveyModel) + if (surveyModel === undefined) { + return error } + + function saveSurveyData(survey, success?, failure?) { if (saveTo == '') { return;