studenthub.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { defineStore } from "pinia";
  2. import { StudenthubAnmeldung, StudenthubStudent } from "../types";
  3. const STUDENTHUB_STUDENT_DATA = "studenthubStudentData";
  4. const STUDENTHUB_APPLICATIONS_DATA = "studenthubApplicationsData";
  5. export const useStudenthubStore = defineStore("studenthub", {
  6. state: () => {
  7. return {
  8. student: null as StudenthubStudent | null,
  9. applications: null as StudenthubAnmeldung[] | null,
  10. };
  11. },
  12. getters: {
  13. hasSomeStudenthubData(): boolean {
  14. return (
  15. (this.student != null && this.student.studierendeId != null) ||
  16. (this.applications != null && this.applications?.length != null)
  17. );
  18. },
  19. hasAllStudenthubData(): boolean {
  20. return (
  21. this.student != null &&
  22. this.student.studierendeId != null &&
  23. this.applications != null &&
  24. this.applications?.length != null
  25. );
  26. },
  27. passedModuleIds(): number[] {
  28. if (this.applications === null) return [];
  29. return this.applications
  30. .filter((a) => a.bestanden && a.modulanlass?.nummer.endsWith(".HN"))
  31. .map((a) => a.modulanlass?.modulId ?? -1);
  32. },
  33. creditedModuleIds(): number[] {
  34. if (this.student === null) return [];
  35. return this.student.modulAnrechnungen?.map((m) => m.modulId) ?? [];
  36. },
  37. completedModuleIds(): number[] {
  38. return [...this.passedModuleIds, ...this.creditedModuleIds];
  39. },
  40. activeModuleIds(): number[] {
  41. if (this.applications === null) return [];
  42. return this.applications
  43. .filter(
  44. (a) =>
  45. !a.bestanden &&
  46. a.modulanlass &&
  47. a.statusName == "aA.Angemeldet" &&
  48. a.modulanlass.nummer.endsWith(".HN"),
  49. )
  50. .map((a) => a.modulanlass?.modulId ?? -1);
  51. },
  52. failedModuleIds(): number[] {
  53. if (this.applications === null) return [];
  54. return this.applications
  55. .filter(
  56. (a) =>
  57. !a.bestanden &&
  58. a.modulanlass &&
  59. a.statusName == "aA.Nicht erfolgreich teilgenommen" &&
  60. a.modulanlass.nummer.endsWith(".HN"),
  61. )
  62. .map((a) => a.modulanlass?.modulId ?? -1);
  63. },
  64. moduleAttemptCount(): Record<number, number> {
  65. if (this.applications === null) return [];
  66. const attempts = {} as Record<number, number>;
  67. this.applications
  68. .filter((a) => a.modulanlass && a.modulanlass.nummer.endsWith(".HN"))
  69. .forEach((a) => {
  70. const mid = a.modulanlass?.modulId;
  71. if (!mid) return;
  72. if (mid in attempts) attempts[mid]++;
  73. else attempts[mid] = 1;
  74. });
  75. return attempts;
  76. },
  77. },
  78. actions: {
  79. loadData() {
  80. try {
  81. this.student = JSON.parse(localStorage[STUDENTHUB_STUDENT_DATA]);
  82. } catch (e) {
  83. console.info("Failed to parse the studenthub student data!");
  84. console.error(e);
  85. }
  86. try {
  87. this.applications = JSON.parse(
  88. localStorage[STUDENTHUB_APPLICATIONS_DATA],
  89. );
  90. } catch (e) {
  91. console.info("Failed to parse the studenthub applications data!");
  92. console.error(e);
  93. }
  94. },
  95. setStudentData(stud: StudenthubStudent | null) {
  96. if (stud === null) {
  97. localStorage[STUDENTHUB_STUDENT_DATA] = null;
  98. this.student = null;
  99. return;
  100. }
  101. localStorage[STUDENTHUB_STUDENT_DATA] = JSON.stringify(stud);
  102. this.student = stud;
  103. },
  104. setApplicationsData(application: StudenthubAnmeldung[] | null) {
  105. if (application === null) {
  106. localStorage[STUDENTHUB_APPLICATIONS_DATA] = null;
  107. this.applications = null;
  108. return;
  109. }
  110. localStorage[STUDENTHUB_APPLICATIONS_DATA] = JSON.stringify(application);
  111. this.applications = application;
  112. },
  113. hasCompletedModule(moduleID: number | null) {
  114. if (moduleID === null) return false;
  115. return this.completedModuleIds.find((m) => m == moduleID) !== undefined;
  116. },
  117. },
  118. });