diff --git a/src/pages/AnswerForm.js b/src/pages/AnswerForm.js index 08e07953ef446adb13a8462d9141de48cfe6bdc9..0887a12a74c11523f27eeed50bb0466ee5d3ae18 100644 --- a/src/pages/AnswerForm.js +++ b/src/pages/AnswerForm.js @@ -153,6 +153,7 @@ function AnwserForm() { const res = await api .post(`/answer/${id}`, backendTranslation()) .then(function(res) { + setCookie("answerForm", id, 92); alert("Formulário respondido!"); }) .catch(error => { @@ -205,6 +206,7 @@ function AnwserForm() { const res = await api .get(`/form/${id}`) .then(function(res) { + checkCookie(id); setFormData(res.data); mapInputs(res.data); }) @@ -223,6 +225,38 @@ function AnwserForm() { }); } + function setCookie(cookieName, cookieValue, expirationsDays) { + var date = new Date(); + date.setTime(date.getTime() + expirationsDays * 60 * 60 * 24 * 1000); + var expires = "expires=" + date.toUTCString(); + document.cookie = + cookieName + "=" + cookieValue + ";" + expires + ";path=/"; + } + + function getCookie(cookieName) { + var name = cookieName + "="; + var ca = document.cookie.split(";"); + for (var i = 0; i < ca.length; i++) { + var c = ca[i]; + while (c.charAt(0) == " ") { + c = c.substring(1); + } + if (c.indexOf(name) == 0) { + return c.substring(name.length, c.length); + } + } + return ""; + } + + function checkCookie(id) { + var answer = getCookie("answerForm"); + if (answer == id) { + alert("Você já respondeu esse formulário! "); + let path = `/signin`; + history.push(path); + } + } + /** First thing the page does is getting the form from the API. */ useEffect(() => { getForm(id);