ScheduleStore.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import { observable, action, runInAction } from "mobx";
  15. import type { Schedule, ScheduleBulkItem } from "@src/@types/Schedule";
  16. import Source from "@src/sources/ScheduleSource";
  17. const updateSchedule = (schedules: any[], id: any, data: any) =>
  18. schedules.map(schedule => {
  19. if (schedule.id === id) {
  20. const newSchedule = { ...schedule, ...data };
  21. if (data.schedule != null && Object.keys(data.schedule).length) {
  22. newSchedule.schedule = {
  23. ...schedule.schedule,
  24. ...(data.schedule || {}),
  25. };
  26. }
  27. return newSchedule;
  28. }
  29. return { ...schedule };
  30. });
  31. class ScheduleStore {
  32. @observable loading = false;
  33. @observable schedules: Schedule[] = [];
  34. @observable bulkSchedules: ScheduleBulkItem[] = [];
  35. @observable unsavedSchedules: Schedule[] = [];
  36. @observable scheduling = false;
  37. @observable adding = false;
  38. @observable savingIds: string[] = [];
  39. @observable enablingIds: string[] = [];
  40. @observable deletingIds: string[] = [];
  41. @action async scheduleMultiple(
  42. transferId: string,
  43. schedules: Schedule[],
  44. ): Promise<void> {
  45. this.scheduling = true;
  46. try {
  47. const scheduledSchedules: Schedule[] = await Source.scheduleMultiple(
  48. transferId,
  49. schedules,
  50. );
  51. runInAction(() => {
  52. this.schedules = scheduledSchedules;
  53. });
  54. } finally {
  55. runInAction(() => {
  56. this.scheduling = false;
  57. });
  58. }
  59. }
  60. @action async getSchedules(transferId: string): Promise<void> {
  61. this.loading = true;
  62. try {
  63. const schedules: Schedule[] = await Source.getSchedules(transferId);
  64. runInAction(() => {
  65. this.schedules = schedules;
  66. });
  67. } finally {
  68. runInAction(() => {
  69. this.loading = false;
  70. });
  71. }
  72. }
  73. async getSchedulesBulk(transferIds: string[]): Promise<void> {
  74. const bulkSchedules: ScheduleBulkItem[] = await Promise.all(
  75. transferIds.map(async transferId => {
  76. const schedules: Schedule[] = await Source.getSchedules(transferId, {
  77. skipLog: true,
  78. });
  79. return { transferId: transferId, schedules };
  80. }),
  81. );
  82. runInAction(() => {
  83. this.bulkSchedules = bulkSchedules;
  84. });
  85. }
  86. @action async addSchedule(
  87. transferId: string,
  88. schedule: Schedule,
  89. ): Promise<void> {
  90. this.adding = true;
  91. try {
  92. const addedSchedule: Schedule = await Source.addSchedule(
  93. transferId,
  94. schedule,
  95. );
  96. runInAction(() => {
  97. this.schedules = [...this.schedules, addedSchedule];
  98. });
  99. } finally {
  100. runInAction(() => {
  101. this.adding = false;
  102. });
  103. }
  104. }
  105. @action async removeSchedule(
  106. transferId: string,
  107. scheduleId: string,
  108. ): Promise<void> {
  109. this.deletingIds.push(scheduleId);
  110. try {
  111. await Source.removeSchedule(transferId, scheduleId);
  112. runInAction(() => {
  113. this.schedules = this.schedules.filter(s => s.id !== scheduleId);
  114. this.unsavedSchedules = this.unsavedSchedules.filter(
  115. s => s.id !== scheduleId,
  116. );
  117. });
  118. } finally {
  119. runInAction(() => {
  120. this.deletingIds = this.deletingIds.filter(id => id !== scheduleId);
  121. });
  122. }
  123. }
  124. @action async updateSchedule(opts: {
  125. transferId: string;
  126. scheduleId: string;
  127. data: Schedule;
  128. oldData?: Schedule | null;
  129. unsavedData?: Schedule | null;
  130. forceSave?: boolean;
  131. }): Promise<void> {
  132. const {
  133. transferId: transferId,
  134. scheduleId,
  135. data,
  136. oldData,
  137. unsavedData,
  138. forceSave,
  139. } = opts;
  140. if (!forceSave) {
  141. this.schedules = updateSchedule(this.schedules, scheduleId, data);
  142. const unsavedSchedule = this.unsavedSchedules.find(
  143. s => s.id === scheduleId,
  144. );
  145. if (unsavedSchedule) {
  146. this.unsavedSchedules = updateSchedule(
  147. this.unsavedSchedules,
  148. scheduleId,
  149. data,
  150. );
  151. } else {
  152. this.unsavedSchedules.push({ id: scheduleId, ...data });
  153. }
  154. return;
  155. }
  156. this.savingIds.push(scheduleId);
  157. if (data.enabled !== oldData?.enabled) {
  158. this.enablingIds.push(scheduleId);
  159. }
  160. try {
  161. const schedule: Schedule = await Source.updateSchedule({
  162. transferId: transferId,
  163. scheduleId,
  164. scheduleData: data,
  165. scheduleOldData: oldData,
  166. unsavedData,
  167. });
  168. runInAction(() => {
  169. this.schedules = this.schedules.map(s => {
  170. if (s.id === schedule.id) {
  171. return { ...schedule };
  172. }
  173. return { ...s };
  174. });
  175. this.unsavedSchedules = this.unsavedSchedules.filter(
  176. s => s.id !== schedule.id,
  177. );
  178. });
  179. } finally {
  180. runInAction(() => {
  181. this.savingIds = this.savingIds.filter(id => id !== scheduleId);
  182. this.enablingIds = this.enablingIds.filter(id => id !== scheduleId);
  183. });
  184. }
  185. }
  186. @action clearUnsavedSchedules() {
  187. this.unsavedSchedules = [];
  188. }
  189. }
  190. export default new ScheduleStore();