2020-02-18 07:57:00 +00:00
|
|
|
import { mixins } from "vue-class-component";
|
|
|
|
import { Component, Vue } from "vue-property-decorator";
|
2020-11-06 10:34:32 +00:00
|
|
|
import { SnackbarProgrammatic as Snackbar } from "buefy";
|
2020-11-27 18:27:44 +00:00
|
|
|
import { ParticipantRole } from "@/types/enums";
|
|
|
|
import { IParticipant } from "../types/participant.model";
|
2020-11-06 10:34:32 +00:00
|
|
|
import { IEvent } from "../types/event.model";
|
2020-02-18 07:57:00 +00:00
|
|
|
import {
|
|
|
|
DELETE_EVENT,
|
|
|
|
EVENT_PERSON_PARTICIPATION,
|
|
|
|
FETCH_EVENT,
|
|
|
|
LEAVE_EVENT,
|
|
|
|
} from "../graphql/event";
|
|
|
|
import { IPerson } from "../types/actor";
|
2021-05-12 16:10:07 +00:00
|
|
|
import { ApolloCache, FetchResult, InMemoryCache } from "@apollo/client/core";
|
2019-09-18 15:32:37 +00:00
|
|
|
|
|
|
|
@Component
|
|
|
|
export default class EventMixin extends mixins(Vue) {
|
2019-12-20 12:04:34 +00:00
|
|
|
protected async leaveEvent(
|
2020-02-18 07:57:00 +00:00
|
|
|
event: IEvent,
|
|
|
|
actorId: string,
|
|
|
|
token: string | null = null,
|
|
|
|
anonymousParticipationConfirmed: boolean | null = null
|
2020-11-06 10:34:32 +00:00
|
|
|
): Promise<void> {
|
2019-12-20 12:04:34 +00:00
|
|
|
try {
|
2020-11-30 09:24:11 +00:00
|
|
|
const { data: resultData } = await this.$apollo.mutate<{
|
|
|
|
leaveEvent: IParticipant;
|
|
|
|
}>({
|
2019-12-20 12:04:34 +00:00
|
|
|
mutation: LEAVE_EVENT,
|
|
|
|
variables: {
|
|
|
|
eventId: event.id,
|
|
|
|
actorId,
|
|
|
|
token,
|
|
|
|
},
|
2021-06-11 12:21:27 +00:00
|
|
|
update: (
|
|
|
|
store: ApolloCache<{
|
|
|
|
leaveEvent: IParticipant;
|
|
|
|
}>,
|
|
|
|
{ data }: FetchResult
|
|
|
|
) => {
|
2019-12-20 12:04:34 +00:00
|
|
|
if (data == null) return;
|
|
|
|
let participation;
|
|
|
|
|
|
|
|
if (!token) {
|
2020-11-30 09:24:11 +00:00
|
|
|
const participationCachedData = store.readQuery<{
|
|
|
|
person: IPerson;
|
|
|
|
}>({
|
2019-12-20 12:04:34 +00:00
|
|
|
query: EVENT_PERSON_PARTICIPATION,
|
|
|
|
variables: { eventId: event.id, actorId },
|
|
|
|
});
|
|
|
|
if (participationCachedData == null) return;
|
|
|
|
const { person } = participationCachedData;
|
2020-06-15 16:12:49 +00:00
|
|
|
[participation] = person.participations.elements;
|
2021-05-17 16:20:25 +00:00
|
|
|
|
|
|
|
store.modify({
|
|
|
|
id: `Person:${actorId}`,
|
|
|
|
fields: {
|
|
|
|
participations() {
|
|
|
|
return {
|
|
|
|
elements: [],
|
|
|
|
total: 0,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
2019-12-20 12:04:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
const eventCachedData = store.readQuery<{ event: IEvent }>({
|
|
|
|
query: FETCH_EVENT,
|
|
|
|
variables: { uuid: event.uuid },
|
|
|
|
});
|
2019-12-20 12:04:34 +00:00
|
|
|
if (eventCachedData == null) return;
|
|
|
|
const { event: eventCached } = eventCachedData;
|
|
|
|
if (eventCached === null) {
|
2020-02-18 07:57:00 +00:00
|
|
|
console.error("Cannot update event cache, because of null value.");
|
2019-12-20 12:04:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-05-17 16:20:25 +00:00
|
|
|
const participantStats = { ...eventCached.participantStats };
|
2020-11-30 09:24:11 +00:00
|
|
|
if (
|
|
|
|
participation &&
|
2021-05-17 16:20:25 +00:00
|
|
|
participation?.role === ParticipantRole.NOT_APPROVED
|
2020-11-30 09:24:11 +00:00
|
|
|
) {
|
2021-05-17 16:20:25 +00:00
|
|
|
participantStats.notApproved -= 1;
|
2019-12-20 12:04:34 +00:00
|
|
|
} else if (anonymousParticipationConfirmed === false) {
|
2021-05-17 16:20:25 +00:00
|
|
|
participantStats.notConfirmed -= 1;
|
2019-12-20 12:04:34 +00:00
|
|
|
} else {
|
2021-05-17 16:20:25 +00:00
|
|
|
participantStats.going -= 1;
|
|
|
|
participantStats.participant -= 1;
|
2019-12-20 12:04:34 +00:00
|
|
|
}
|
2020-02-18 07:57:00 +00:00
|
|
|
store.writeQuery({
|
|
|
|
query: FETCH_EVENT,
|
|
|
|
variables: { uuid: event.uuid },
|
2021-05-17 16:20:25 +00:00
|
|
|
data: {
|
|
|
|
event: {
|
|
|
|
...eventCached,
|
|
|
|
participantStats,
|
|
|
|
},
|
|
|
|
},
|
2020-02-18 07:57:00 +00:00
|
|
|
});
|
2019-12-20 12:04:34 +00:00
|
|
|
},
|
|
|
|
});
|
2020-02-18 07:57:00 +00:00
|
|
|
if (resultData) {
|
2019-12-20 12:04:34 +00:00
|
|
|
this.participationCancelledMessage();
|
|
|
|
}
|
2021-09-29 16:20:33 +00:00
|
|
|
} catch (error: any) {
|
2020-11-30 09:24:11 +00:00
|
|
|
Snackbar.open({
|
|
|
|
message: error.message,
|
|
|
|
type: "is-danger",
|
|
|
|
position: "is-bottom",
|
|
|
|
});
|
2019-12-20 12:04:34 +00:00
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private participationCancelledMessage() {
|
2020-11-30 09:24:11 +00:00
|
|
|
this.$notifier.success(
|
|
|
|
this.$t("You have cancelled your participation") as string
|
|
|
|
);
|
2019-12-20 12:04:34 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 16:06:28 +00:00
|
|
|
protected async openDeleteEventModal(event: IEvent): Promise<void> {
|
2020-02-18 07:57:00 +00:00
|
|
|
function escapeRegExp(string: string) {
|
2020-06-15 16:12:49 +00:00
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
|
|
|
}
|
2019-10-25 15:43:37 +00:00
|
|
|
const participantsLength = event.participantStats.participant;
|
2019-09-18 15:32:37 +00:00
|
|
|
const prefix = participantsLength
|
2020-11-30 09:24:11 +00:00
|
|
|
? this.$tc(
|
|
|
|
"There are {participants} participants.",
|
|
|
|
event.participantStats.participant,
|
|
|
|
{
|
|
|
|
participants: event.participantStats.participant,
|
|
|
|
}
|
|
|
|
)
|
2020-02-18 07:57:00 +00:00
|
|
|
: "";
|
2019-09-18 15:32:37 +00:00
|
|
|
|
|
|
|
this.$buefy.dialog.prompt({
|
2020-02-18 07:57:00 +00:00
|
|
|
type: "is-danger",
|
|
|
|
title: this.$t("Delete event") as string,
|
2019-09-18 15:32:37 +00:00
|
|
|
message: `${prefix}
|
2020-11-30 09:24:11 +00:00
|
|
|
${this.$t(
|
|
|
|
"Are you sure you want to delete this event? This action cannot be reverted."
|
|
|
|
)}
|
2019-09-18 15:32:37 +00:00
|
|
|
<br><br>
|
2020-02-18 07:57:00 +00:00
|
|
|
${this.$t('To confirm, type your event title "{eventTitle}"', {
|
|
|
|
eventTitle: event.title,
|
|
|
|
})}`,
|
2020-11-30 09:24:11 +00:00
|
|
|
confirmText: this.$t("Delete {eventTitle}", {
|
|
|
|
eventTitle: event.title,
|
|
|
|
}) as string,
|
2019-09-18 15:32:37 +00:00
|
|
|
inputAttrs: {
|
|
|
|
placeholder: event.title,
|
2020-04-12 15:42:30 +00:00
|
|
|
pattern: escapeRegExp(event.title),
|
2019-09-18 15:32:37 +00:00
|
|
|
},
|
2020-11-19 16:06:28 +00:00
|
|
|
onConfirm: () => this.deleteEvent(event),
|
2019-09-18 15:32:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-19 16:06:28 +00:00
|
|
|
private async deleteEvent(event: IEvent) {
|
2021-06-10 08:32:37 +00:00
|
|
|
const { title: eventTitle, id: eventId } = event;
|
2019-09-18 15:32:37 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await this.$apollo.mutate<IParticipant>({
|
|
|
|
mutation: DELETE_EVENT,
|
|
|
|
variables: {
|
|
|
|
eventId: event.id,
|
|
|
|
},
|
|
|
|
});
|
2021-06-10 08:32:37 +00:00
|
|
|
const cache = this.$apollo.getClient().cache as InMemoryCache;
|
|
|
|
cache.evict({ id: `Event:${eventId}` });
|
|
|
|
cache.gc();
|
2019-10-05 19:17:18 +00:00
|
|
|
/**
|
2020-02-18 07:57:00 +00:00
|
|
|
* When the event corresponding has been deleted (by the organizer).
|
|
|
|
* A notification is already triggered.
|
2019-10-05 19:17:18 +00:00
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2020-11-06 10:34:32 +00:00
|
|
|
this.$emit("event-deleted", event.id);
|
2019-09-18 15:32:37 +00:00
|
|
|
|
|
|
|
this.$buefy.notification.open({
|
2020-11-30 09:24:11 +00:00
|
|
|
message: this.$t("Event {eventTitle} deleted", {
|
|
|
|
eventTitle,
|
|
|
|
}) as string,
|
2020-02-18 07:57:00 +00:00
|
|
|
type: "is-success",
|
|
|
|
position: "is-bottom-right",
|
2019-09-18 15:32:37 +00:00
|
|
|
duration: 5000,
|
|
|
|
});
|
2021-09-29 16:20:33 +00:00
|
|
|
} catch (error: any) {
|
2020-11-30 09:24:11 +00:00
|
|
|
Snackbar.open({
|
|
|
|
message: error.message,
|
|
|
|
type: "is-danger",
|
|
|
|
position: "is-bottom",
|
|
|
|
});
|
2020-09-29 07:53:48 +00:00
|
|
|
|
2019-09-18 15:32:37 +00:00
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|