getQuestionnaire.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { Question } from "@/models/Questionnaire";
  2. import Toast from "@/utils/Toast";
  3. import { defineComponent } from "vue";
  4. const API_URL = process.env.VUE_APP_API_URL;
  5. export default defineComponent({
  6. data() {
  7. return { introduction: "", questions: [] as Array<Question>, opened: false };
  8. },
  9. methods: {
  10. getQuestionnaire(uuid: string) {
  11. // Get previously saved introduction
  12. fetch(`${API_URL}api/questionnaire/${uuid}`, {
  13. method: "GET",
  14. })
  15. .then((res) => {
  16. if (res.status == 200) {
  17. return res.json();
  18. }
  19. if (res.status == 404) {
  20. Toast({
  21. html: "Pas de donnée existante pour l'introduction du questionnaire",
  22. classes: "error",
  23. });
  24. }
  25. throw new Error("Failed");
  26. })
  27. .then((t) => {
  28. this.introduction = t.introduction ?? "";
  29. this.opened = t.opened ?? false;
  30. });
  31. // Get previously saved questions
  32. fetch(`${API_URL}api/questionnaire/${uuid}/questions`, {
  33. method: "GET",
  34. })
  35. .then((res) => {
  36. if (res.status == 200) {
  37. return res.json();
  38. }
  39. if (res.status == 404) {
  40. Toast({
  41. html: "Pas de question associeé à ce questionnaire",
  42. classes: "error",
  43. });
  44. }
  45. throw new Error("Failed");
  46. })
  47. .then((data: Array<Question>) => (this.questions = data.sort((a, b) => a.order - b.order)));
  48. },
  49. },
  50. });