-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticipationsCard.vue
More file actions
277 lines (249 loc) · 9.65 KB
/
ParticipationsCard.vue
File metadata and controls
277 lines (249 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<template>
<Card class="w-full py-0">
<CardHeader class="w-full pt-6">
<h2 class="text-lg font-semibold mb-4">Participations</h2>
<div v-if="sortedParticipations.length > 0" class="space-y-4">
<div v-if="currentParticipation">
<div class="flex items-center gap-2 mb-3">
<h4 class="text-sm font-medium text-muted-foreground">Current</h4>
</div>
<EditableCompanyParticipation
v-if="isCompanyParticipation(currentParticipation)"
:participation="currentParticipation"
:company-id="entityId"
/>
<EditableSpeakerParticipation
v-else-if="isSpeakerParticipation(currentParticipation)"
:participation="currentParticipation"
:speaker-id="entityId"
/>
</div>
<div v-else-if="getCurrentEvent()">
<div class="flex items-center gap-2 mb-3">
<h4 class="text-sm font-medium text-muted-foreground">Current</h4>
</div>
<EmptyStateCard
title="Create Participation"
:description="`Click to participate in ${getEventName(getCurrentEvent()?.id || 0)}`"
:loading="isCreatingParticipation"
loading-text="Creating..."
@click="createParticipation"
/>
</div>
<div v-if="pastParticipations.length > 0">
<Accordion type="single" collapsible class="w-full">
<AccordionItem value="pastParticipations">
<AccordionTrigger
class="text-sm font-semibold px-0 cursor-pointer"
>
Past Participations
</AccordionTrigger>
<AccordionContent class="px-0 pt-4">
<div class="space-y-4">
<Card
v-for="participation in pastParticipations"
:key="participation.event"
class="p-4"
>
<div class="space-y-3">
<div class="flex items-center justify-between">
<h4 class="font-medium">
{{ getEventName(participation.event) }}
</h4>
<Badge
:class="
participationStatusColor[participation.status]
?.background
"
class="text-xs"
>
{{
humanReadableParticipationStatus[
participation.status
]
}}
</Badge>
</div>
<div
class="grid grid-cols-1 lg:grid-cols-3 gap-4 items-start"
>
<div class="flex items-center gap-2">
<Image
:src="getMember(participation.member)?.img"
:alt="getMember(participation.member)?.name"
class="w-8 h-8 rounded-full object-cover border"
/>
<div>
<div class="text-sm font-medium">
{{ getMember(participation.member)?.name }}
</div>
<div class="text-xs text-muted-foreground">
Responsible
</div>
</div>
</div>
<template v-if="isCompanyParticipation(participation)">
<div
v-if="hasDate(participation.confirmed)"
class="text-sm"
>
<span class="font-medium">Confirmed:</span>
<div class="mt-1 text-muted-foreground">
{{ formatDate(participation.confirmed) }}
</div>
</div>
<div v-if="participation.notes" class="text-sm">
<span class="font-medium">Notes:</span>
<div class="mt-1 text-muted-foreground">
{{ participation.notes }}
</div>
</div>
<div
v-if="participation.partner"
class="flex items-center gap-1"
>
<Badge variant="secondary" class="text-xs"
>Partner</Badge
>
</div>
</template>
<template
v-else-if="isSpeakerParticipation(participation)"
>
</template>
</div>
</div>
</Card>
</div>
</AccordionContent>
</AccordionItem>
</Accordion>
</div>
</div>
<div v-else class="text-muted-foreground p-4">
No participations found.
</div>
</CardHeader>
</Card>
</template>
<script setup lang="ts">
import { getAllEvents } from "@/api/events";
import { getAllMembers } from "@/api/members";
import { useCreateCompanyParticipationMutation } from "@/mutations/companies";
import { useCreateSpeakerParticipationMutation } from "@/mutations/speakers";
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion";
import { Badge } from "@/components/ui/badge";
import { Card, CardHeader } from "@/components/ui/card";
import EditableCompanyParticipation from "./companies/EditableCompanyParticipation.vue";
import EditableSpeakerParticipation from "./speakers/EditableSpeakerParticipation.vue";
import type { CompanyParticipation } from "@/dto/companies";
import type { SpeakerParticipation } from "@/dto/speakers";
import {
humanReadableParticipationStatus,
participationStatusColor,
} from "@/dto";
import { useQuery } from "@pinia/colada";
import { computed } from "vue";
import Image from "./Image.vue";
import EmptyStateCard from "./ui/EmptyStateCard.vue";
type Participation = CompanyParticipation | SpeakerParticipation;
interface Props {
participations: Participation[];
entityId: string;
entityType: "company" | "speaker";
}
const props = defineProps<Props>();
const { data: eventsData } = useQuery({
key: () => ["events"],
query: () => getAllEvents(),
});
const { data: membersData } = useQuery({
key: () => ["members"],
query: () => getAllMembers(),
});
const createCompanyParticipationMutation =
useCreateCompanyParticipationMutation();
const createSpeakerParticipationMutation =
useCreateSpeakerParticipationMutation();
const isCreatingParticipation = computed(
() =>
createCompanyParticipationMutation.isLoading.value ||
createSpeakerParticipationMutation.isLoading.value,
);
const sortedParticipations = computed(() => {
if (!props.participations) return [];
return [...props.participations].sort((a, b) => b.event - a.event); // Most recent first
});
const getCurrentEvent = () => {
if (!eventsData.value?.data) return null;
// Find the current event (the one with the highest ID)
return eventsData.value.data.reduce((latest, event) =>
event.id > latest.id ? event : latest,
);
};
const currentParticipation = computed(() => {
const currentEvent = getCurrentEvent();
if (!currentEvent) return null;
return props.participations.find((p) => p.event === currentEvent.id) || null;
});
const pastParticipations = computed(() => {
const currentEvent = getCurrentEvent();
if (!currentEvent) return sortedParticipations.value;
// All participations except the current one
return sortedParticipations.value.filter((p) => p.event !== currentEvent.id);
});
const getEventName = (eventId: number) => {
if (!eventsData.value?.data) return `Event ${eventId}`;
const event = eventsData.value.data.find((e) => e.id === eventId);
return event?.name || `Event ${eventId}`;
};
const getMember = (memberId: string) => {
if (!membersData.value?.data) return null;
return membersData.value.data.find((m) => m.id === memberId) || null;
};
const hasDate = (dateString: string) => {
return !!dateString && dateString !== "0001-01-01T00:00:00Z";
};
const formatDate = (dateString: string) => {
if (!hasDate(dateString)) return "";
try {
return new Date(dateString).toLocaleString();
} catch {
return dateString;
}
};
const isCompanyParticipation = (
_participation: Participation,
): _participation is CompanyParticipation => {
return props.entityType === "company";
};
const isSpeakerParticipation = (
_participation: Participation,
): _participation is SpeakerParticipation => {
return props.entityType === "speaker";
};
const createParticipation = async () => {
if (isCreatingParticipation.value) return;
const currentEvent = getCurrentEvent();
if (!currentEvent) return;
try {
if (props.entityType === "company") {
createCompanyParticipationMutation.companyId.value = props.entityId;
createCompanyParticipationMutation.data.value = {
partner: false,
};
await createCompanyParticipationMutation.mutate();
} else if (props.entityType === "speaker") {
createSpeakerParticipationMutation.speakerId.value = props.entityId;
await createSpeakerParticipationMutation.mutate();
}
} catch (error) {
console.error("Failed to create participation:", error);
}
};
</script>