|
1 | | -import {MeetingDto} from "../../dtos/MeetingDto"; |
2 | | -import {CuteButton} from "../CuteButton"; |
3 | | -import React from "react"; |
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { MeetingDto } from "../../dtos/MeetingDto"; |
| 3 | +import { UserDto } from "../../dtos/UserDto"; |
| 4 | +import { getUser } from "../../api/UserApi"; |
| 5 | +import { getUserIdsForMeeting, joinStudyGroup, leaveStudyGroup } from "../../api/UserGroupApi"; |
| 6 | +import axiosInstance from "../../AxiosConfig"; |
| 7 | +import { CuteButton } from "../CuteButton"; |
| 8 | +import { deleteMeeting, updateCreator } from '../../api/MeetingApi'; |
| 9 | + |
| 10 | +interface Props { |
| 11 | + meeting: MeetingDto; |
| 12 | +} |
| 13 | + |
| 14 | +export default function MeetingSearchResult({ meeting }: Props) { |
| 15 | + const [userIds, setUserIds] = useState<string[]>([]); |
| 16 | + const [myUser, setMyUser] = useState<UserDto | null>(null); |
| 17 | + const [loading, setLoading] = useState<boolean>(false); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + getUser(axiosInstance) |
| 21 | + .then(setMyUser) |
| 22 | + .catch(err => console.error(err)); |
| 23 | + }, []); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + getUserIdsForMeeting(axiosInstance, meeting.id) |
| 27 | + .then(setUserIds) |
| 28 | + .catch(err => console.error(err)); |
| 29 | + }, [meeting.id]); |
| 30 | + |
| 31 | + if (!myUser) return null; |
| 32 | + |
| 33 | + const myUserId = myUser.uuid; |
| 34 | + |
| 35 | + const joinMeeting = () => { |
| 36 | + setLoading(true); |
| 37 | + joinStudyGroup(axiosInstance, meeting.id) |
| 38 | + .then(() => setUserIds(prev => [...prev, myUserId])) |
| 39 | + .finally(() => setLoading(false)); |
| 40 | + }; |
| 41 | + |
| 42 | + const leaveMeeting = () => { |
| 43 | + setLoading(true); |
| 44 | + leaveStudyGroup(axiosInstance, meeting.id) |
| 45 | + .then(() => setUserIds(prev => prev.filter(id => id !== myUserId))) |
| 46 | + .finally(() => setLoading(false)); |
| 47 | + }; |
| 48 | + |
| 49 | + const leaveMeetingAsCreator = () => { |
| 50 | + if (userIds.length === 0) { |
| 51 | + deleteMeeting(axiosInstance, meeting.id) |
| 52 | + .then(() => console.log("Meeting gelöscht")) |
| 53 | + .catch(err => console.error(err)); |
| 54 | + } else { |
| 55 | + const newCreatorId = userIds[0]; |
| 56 | + updateCreator(axiosInstance, meeting.id, newCreatorId) |
| 57 | + .then(() => console.log("Creator geändert")) |
| 58 | + .catch(err => console.error(err)); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + const isMember = userIds.includes(myUserId); |
| 63 | + const isCreator = meeting.creator === myUserId; |
4 | 64 |
|
5 | | -export default function MeetingSearchResult(props: { meeting: MeetingDto }) { |
6 | 65 | return ( |
7 | 66 | <div className="bg-[#333C4F] p-4 flex flex-col gap-4"> |
8 | 67 | <div> |
9 | | - <div className="text-white"> |
10 | | - {new Date(props.meeting.dateFrom).toLocaleString('de-DE', { |
11 | | - day: '2-digit', |
12 | | - month: '2-digit', |
13 | | - year: 'numeric', |
14 | | - hour: '2-digit', |
15 | | - minute: '2-digit', |
16 | | - })} |
17 | | - {" - "} |
18 | | - {new Date(props.meeting.dateUntil).toLocaleString('de-DE', { |
19 | | - day: '2-digit', |
20 | | - month: '2-digit', |
21 | | - year: 'numeric', |
22 | | - hour: '2-digit', |
23 | | - minute: '2-digit', |
24 | | - })} |
25 | | - </div> |
26 | | - <div className="text-white space-y-1"> |
27 | | - <p>{props.meeting.description}</p> |
28 | | - <p>{props.meeting.member}</p> |
29 | | - <p>{props.meeting.place}</p> |
| 68 | + <h2 className="font-bold text-2xl text-white mb-4">{meeting.title}</h2> |
| 69 | + |
| 70 | + <div className="flex flex-col gap-4 mb-4"> |
| 71 | + <p className="text-bs font-medium text-white"> |
| 72 | + <strong className="text-[#CAE8FF] font-semibold">Start: </strong> |
| 73 | + {new Date(meeting.dateFrom).toLocaleString('de-DE', { |
| 74 | + day: '2-digit', |
| 75 | + month: '2-digit', |
| 76 | + year: 'numeric', |
| 77 | + hour: '2-digit', |
| 78 | + minute: '2-digit', |
| 79 | + })} |
| 80 | + </p> |
| 81 | + <p className="text-bs font-medium text-white"> |
| 82 | + <strong className="text-[#CAE8FF] font-semibold">Ende: </strong> |
| 83 | + {new Date(meeting.dateUntil).toLocaleString('de-DE', { |
| 84 | + day: '2-digit', |
| 85 | + month: '2-digit', |
| 86 | + year: 'numeric', |
| 87 | + hour: '2-digit', |
| 88 | + minute: '2-digit', |
| 89 | + })} |
| 90 | + </p> |
| 91 | + <p className="text-bs font-medium text-white"> |
| 92 | + <strong className="text-[#CAE8FF] font-semibold">Beschreibung:</strong> {meeting.description} |
| 93 | + </p> |
| 94 | + <p className="text-bs font-medium text-white"> |
| 95 | + <strong className="text-[#CAE8FF] font-semibold">Raum:</strong> {meeting.place} |
| 96 | + </p> |
| 97 | + |
| 98 | + <p className="text-bs font-medium text-white"> |
| 99 | + <strong className="text-[#CAE8FF] font-semibold">Teilnehmer:</strong> |
| 100 | + </p> |
| 101 | + <ul className="text-white text-sm list-disc list-inside"> |
| 102 | + <li key={meeting.creator}>{meeting.creator}</li> |
| 103 | + {userIds.map(id => ( |
| 104 | + <li key={id}>{id}</li> |
| 105 | + ))} |
| 106 | + {userIds.length === 0 && <li>-</li>} |
| 107 | + </ul> |
30 | 108 | </div> |
31 | 109 | </div> |
32 | | - <div className="flex flex-col"> |
33 | | - <CuteButton onClick={() => { |
34 | | - }} text={"Teilnehmen"} textColor={"#e8fcf6"} |
35 | | - bgColor={"#56A095"} |
36 | | - classname={"text-sm"}/> |
| 110 | + |
| 111 | + <div className="flex gap-4"> |
| 112 | + {!isMember && !isCreator && !loading && ( |
| 113 | + <CuteButton |
| 114 | + onClick={joinMeeting} |
| 115 | + text={"Teilnehmen"} |
| 116 | + textColor={"#e8fcf6"} |
| 117 | + bgColor={"#56A095"} |
| 118 | + classname={"text-sm w-full"} |
| 119 | + /> |
| 120 | + )} |
| 121 | + {loading && ( |
| 122 | + <CuteButton |
| 123 | + text={"Lade..."} |
| 124 | + textColor={"#e8fcf6"} |
| 125 | + bgColor={"#56A095"} |
| 126 | + classname={"text-sm w-full"} |
| 127 | + /> |
| 128 | + )} |
| 129 | + |
| 130 | + {isCreator && !loading && ( |
| 131 | + <CuteButton |
| 132 | + onClick={leaveMeetingAsCreator} |
| 133 | + text={"Meeting verlassen"} |
| 134 | + textColor={"#e8fcf6"} |
| 135 | + bgColor={"#974242"} |
| 136 | + classname={"text-sm w-full"} |
| 137 | + /> |
| 138 | + )} |
| 139 | + |
| 140 | + {isMember && !isCreator && !loading && ( |
| 141 | + <CuteButton |
| 142 | + onClick={leaveMeeting} |
| 143 | + text={"Meeting verlassen"} |
| 144 | + textColor={"#e8fcf6"} |
| 145 | + bgColor={"#974242"} |
| 146 | + classname={"text-sm w-full"} |
| 147 | + /> |
| 148 | + )} |
37 | 149 | </div> |
38 | 150 | </div> |
39 | | - ) |
| 151 | + ); |
40 | 152 | } |
0 commit comments