admin.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { defineStore } from "pinia";
  2. import { useMainStore } from "./main";
  3. import type { IUserProfileCreate, IUserProfile, IUserProfileUpdate } from "@/interfaces";
  4. import { api } from "@/api";
  5. interface AdminState {
  6. users: IUserProfile[];
  7. }
  8. const defaultState: AdminState = {
  9. users: [],
  10. };
  11. export const useAdminStore = defineStore("AdminStoreId", {
  12. state: () => defaultState,
  13. getters: {
  14. readAdminUsers: (state) => state.users,
  15. readAdminOneUser: (state) => {
  16. return (userId:number) => state.users.filter((user) => user.id === userId)[0] || undefined;
  17. },
  18. },
  19. actions: {
  20. // setters
  21. setUsers(payload: IUserProfile[]) {
  22. this.users = payload;
  23. },
  24. setUser(payload: IUserProfile) {
  25. const users = this.users.filter((user: IUserProfile) => user.id !== payload.id);
  26. users.push(payload);
  27. this.users = users;
  28. },
  29. // actions
  30. async actionGetUsers(){
  31. const mainStore = useMainStore();
  32. try {
  33. const response = await api.getUsers(mainStore.token)
  34. if (response) {
  35. this.setUsers(response.data);
  36. }
  37. } catch (error) {
  38. await mainStore.checkApiError(error);
  39. }
  40. },
  41. async actionUpdateUser(payload: { id: number; user: IUserProfileUpdate }) {
  42. const mainStore = useMainStore();
  43. try {
  44. const loadingNotification = { content: "saving", showProgress: true };
  45. mainStore.addNotification(loadingNotification);
  46. const response = (
  47. await Promise.all([
  48. api.updateUser(mainStore.token, payload.id, payload.user),
  49. await new Promise<void>((resolve, _) => setTimeout(() => resolve(), 500)),
  50. ])
  51. )[0];
  52. this.setUser(response.data);
  53. mainStore.removeNotification(loadingNotification);
  54. mainStore.addNotification({
  55. content: "User successfully updated",
  56. color: "success",
  57. });
  58. } catch (error) {
  59. await mainStore.checkApiError(error);
  60. }
  61. },
  62. async actionCreateUser(payload: IUserProfileCreate) {
  63. const mainStore = useMainStore();
  64. try {
  65. const loadingNotification = { content: "saving", showProgress: true };
  66. mainStore.addNotification(loadingNotification);
  67. const response = (
  68. await Promise.all([
  69. api.createUser(mainStore.token, payload),
  70. await new Promise<void>((resolve, _) => setTimeout(() => resolve(), 500)),
  71. ])
  72. )[0];
  73. this.setUser(response.data);
  74. mainStore.removeNotification(loadingNotification);
  75. mainStore.addNotification({
  76. content: "User successfully created",
  77. color: "success",
  78. });
  79. } catch (error) {
  80. await mainStore.checkApiError(error);
  81. }
  82. },
  83. async actionTestCeleryMsg(payload: {msg:string}) {
  84. const mainStore = useMainStore();
  85. try {
  86. const loadingNotification = { content: "sending", showProgress: true };
  87. mainStore.addNotification(loadingNotification);
  88. const response = (
  89. await Promise.all([
  90. api.testCeleryMsg(mainStore.token, payload),
  91. await new Promise<void>((resolve, _) => setTimeout(() => resolve(), 500)),
  92. ])
  93. );
  94. mainStore.removeNotification(loadingNotification);
  95. mainStore.addNotification({
  96. content: "Word received",
  97. color: "success",
  98. })
  99. } catch (error) {
  100. await mainStore.checkApiError(error);
  101. }
  102. },
  103. async actionTestCeleryFile(payload: File) {
  104. const mainStore = useMainStore();
  105. try {
  106. const loadingNotification = { content: "sending", showProgress: true };
  107. mainStore.addNotification(loadingNotification);
  108. const response = (
  109. await Promise.all([
  110. api.testCeleryFile(mainStore.token, payload),
  111. await new Promise<void>((resolve, _) => setTimeout(() => resolve(), 500)),
  112. ])
  113. );
  114. mainStore.removeNotification(loadingNotification);
  115. mainStore.addNotification({
  116. content: "File received",
  117. color: "success",
  118. })
  119. } catch (error) {
  120. await mainStore.checkApiError(error);
  121. }
  122. }
  123. },
  124. });