|
|
@@ -1,15 +1,38 @@
|
|
|
import { ActionTree, ActionContext } from "vuex";
|
|
|
import { State } from "./State";
|
|
|
import { Mutations, MutationTypes } from "./Mutations";
|
|
|
-import Creneau, { ICreneau } from "@/models/Creneau";
|
|
|
-import SockJS from "sockjs-client";
|
|
|
-import Stomp from "stompjs";
|
|
|
+import Creneau from "@/models/Creneau";
|
|
|
import PlanningUpdateMessage from "@/models/messages/PlanningUpdateMessage";
|
|
|
+import Benevole from "@/models/Benevole";
|
|
|
+import Competence from "@/models/Competence";
|
|
|
+import { IRessource, Ressource } from "jc-timeline";
|
|
|
|
|
|
export enum ActionTypes {
|
|
|
superCommit = "superComit",
|
|
|
+ commitRemote = "commitRemote",
|
|
|
}
|
|
|
|
|
|
+const RAW_PAYLOAD = [
|
|
|
+ MutationTypes.addBenevole2Creneau,
|
|
|
+ MutationTypes.removeBenevole2Creneau,
|
|
|
+ MutationTypes.reorderCreneauGroup,
|
|
|
+ MutationTypes.removeBenevole,
|
|
|
+ MutationTypes.removeConstraint,
|
|
|
+ MutationTypes.removeCreneau,
|
|
|
+ MutationTypes.removeCreneauGroup,
|
|
|
+ MutationTypes.editBenevole,
|
|
|
+ MutationTypes.editConstraint,
|
|
|
+ MutationTypes.editCreneau,
|
|
|
+ MutationTypes.editCreneauGroup,
|
|
|
+ MutationTypes.editEvenement,
|
|
|
+];
|
|
|
+const JSON_PAYLOAD = [
|
|
|
+ MutationTypes.addBenevole,
|
|
|
+ MutationTypes.addConstraint,
|
|
|
+ MutationTypes.addCreneau,
|
|
|
+ MutationTypes.addCreneauGroup,
|
|
|
+];
|
|
|
+
|
|
|
type AugmentedActionContext = {
|
|
|
commit<K extends keyof Mutations>(
|
|
|
key: K,
|
|
|
@@ -22,6 +45,7 @@ export interface Actions {
|
|
|
context: AugmentedActionContext,
|
|
|
payload: { mutation: K; payload: Parameters<Mutations[K]>[1] }
|
|
|
): void;
|
|
|
+ [ActionTypes.commitRemote](context: AugmentedActionContext, payload: PlanningUpdateMessage): void;
|
|
|
}
|
|
|
|
|
|
export const actions: ActionTree<State, State> & Actions = {
|
|
|
@@ -33,9 +57,49 @@ export const actions: ActionTree<State, State> & Actions = {
|
|
|
method: payload.mutation,
|
|
|
user: context.state.username,
|
|
|
uuid: context.state.evenement.uuid,
|
|
|
- payload: JSON.stringify(payload.payload),
|
|
|
+ payload: "",
|
|
|
};
|
|
|
- client.send("/app/notify", {}, JSON.stringify(body));
|
|
|
+ if (RAW_PAYLOAD.includes(payload.mutation)) {
|
|
|
+ body.payload = JSON.stringify(payload.payload);
|
|
|
+ }
|
|
|
+ if (JSON_PAYLOAD.includes(payload.mutation)) {
|
|
|
+ const content = payload.payload as Benevole | Competence | Creneau | Ressource;
|
|
|
+ body.payload = JSON.stringify(content.toJSON());
|
|
|
+ }
|
|
|
+ if (payload.mutation == MutationTypes.addCreneauGroupAt) {
|
|
|
+ const content = payload.payload as { r: Ressource; pos: number };
|
|
|
+ body.payload = JSON.stringify({ r: content.r.toJSON(), pos: content.pos });
|
|
|
+ }
|
|
|
+ if (body.payload != "") client.send("/app/notify", {}, JSON.stringify(body));
|
|
|
+ }
|
|
|
+ },
|
|
|
+ [ActionTypes.commitRemote](context, data) {
|
|
|
+ if (data.method != "toast" && data.uuid == context.state.evenement.uuid) {
|
|
|
+ // Handle mutations that have dedicated JSON parser
|
|
|
+ if (data.method == MutationTypes.addBenevole)
|
|
|
+ context.commit(data.method, Benevole.fromJSON(JSON.parse(data.payload)));
|
|
|
+ if (data.method == MutationTypes.addConstraint)
|
|
|
+ context.commit(data.method, Competence.fromJSON(JSON.parse(data.payload)));
|
|
|
+ if (data.method == MutationTypes.addCreneau)
|
|
|
+ context.commit(data.method, Creneau.fromJSON(JSON.parse(data.payload)));
|
|
|
+ if (data.method == MutationTypes.addCreneauGroup)
|
|
|
+ context.commit(data.method, new Ressource(JSON.parse(data.payload)));
|
|
|
+ if (data.method == MutationTypes.addCreneauGroupAt) {
|
|
|
+ const obj: { r: IRessource; pos: number } = JSON.parse(data.payload);
|
|
|
+ context.commit(data.method, { r: new Ressource(obj.r), pos: obj.pos });
|
|
|
+ }
|
|
|
+ // Handle Mutation that manipulate Dates
|
|
|
+ if ([MutationTypes.editCreneau, MutationTypes.editEvenement].includes(data.method)) {
|
|
|
+ const content: Parameters<
|
|
|
+ Mutations[MutationTypes.editEvenement | MutationTypes.editCreneau]
|
|
|
+ >[1] = JSON.parse(data.payload);
|
|
|
+ if (content.field == "end" || content.field == "start") {
|
|
|
+ content.value = new Date(content.value as string);
|
|
|
+ }
|
|
|
+ context.commit(data.method, content);
|
|
|
+ } else if (RAW_PAYLOAD.includes(data.method)) {
|
|
|
+ context.commit(data.method, JSON.parse(data.payload));
|
|
|
+ }
|
|
|
}
|
|
|
},
|
|
|
};
|