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";
|
|
|
|
import { IParticipant, ParticipantRole } from "../types/participant.model";
|
|
|
|
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";
|
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-02-18 07:57:00 +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,
|
|
|
|
},
|
|
|
|
update: (store, { data }) => {
|
|
|
|
if (data == null) return;
|
|
|
|
let participation;
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
const participationCachedData = store.readQuery<{ person: IPerson }>({
|
|
|
|
query: EVENT_PERSON_PARTICIPATION,
|
|
|
|
variables: { eventId: event.id, actorId },
|
|
|
|
});
|
|
|
|
if (participationCachedData == null) return;
|
|
|
|
const { person } = participationCachedData;
|
|
|
|
if (person === null) {
|
2020-02-18 07:57:00 +00:00
|
|
|
console.error("Cannot update participation cache, because of null value.");
|
2019-12-20 12:04:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-15 16:12:49 +00:00
|
|
|
[participation] = person.participations.elements;
|
|
|
|
person.participations.elements = [];
|
|
|
|
person.participations.total = 0;
|
2019-12-20 12:04:34 +00:00
|
|
|
store.writeQuery({
|
|
|
|
query: EVENT_PERSON_PARTICIPATION,
|
|
|
|
variables: { eventId: event.id, actorId },
|
|
|
|
data: { person },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (participation && participation.role === ParticipantRole.NOT_APPROVED) {
|
2020-02-18 07:57:00 +00:00
|
|
|
eventCached.participantStats.notApproved -= 1;
|
2019-12-20 12:04:34 +00:00
|
|
|
} else if (anonymousParticipationConfirmed === false) {
|
2020-02-18 07:57:00 +00:00
|
|
|
eventCached.participantStats.notConfirmed -= 1;
|
2019-12-20 12:04:34 +00:00
|
|
|
} else {
|
2020-02-18 07:57:00 +00:00
|
|
|
eventCached.participantStats.going -= 1;
|
|
|
|
eventCached.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 },
|
|
|
|
data: { event: eventCached },
|
|
|
|
});
|
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();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2020-09-29 07:53:48 +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-02-18 07:57:00 +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-02-18 07:57:00 +00:00
|
|
|
? this.$tc("There are {participants} participants.", event.participantStats.participant, {
|
|
|
|
participants: event.participantStats.participant,
|
|
|
|
})
|
|
|
|
: "";
|
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-02-18 07:57:00 +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,
|
|
|
|
})}`,
|
|
|
|
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) {
|
2019-09-18 15:32:37 +00:00
|
|
|
const eventTitle = event.title;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.$apollo.mutate<IParticipant>({
|
|
|
|
mutation: DELETE_EVENT,
|
|
|
|
variables: {
|
|
|
|
eventId: event.id,
|
|
|
|
},
|
|
|
|
});
|
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-02-18 07:57:00 +00:00
|
|
|
message: this.$t("Event {eventTitle} deleted", { eventTitle }) as string,
|
|
|
|
type: "is-success",
|
|
|
|
position: "is-bottom-right",
|
2019-09-18 15:32:37 +00:00
|
|
|
duration: 5000,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2020-09-29 07:53:48 +00:00
|
|
|
Snackbar.open({ message: error.message, type: "is-danger", position: "is-bottom" });
|
|
|
|
|
2019-09-18 15:32:37 +00:00
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2020-11-06 10:34:32 +00:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
2020-02-18 07:57:00 +00:00
|
|
|
urlToHostname(url: string): string | null {
|
|
|
|
try {
|
|
|
|
return new URL(url).hostname;
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 15:32:37 +00:00
|
|
|
}
|