2021-08-02 20:51:57 +00:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { UserRemoveIcon, UsersIcon } from "@heroicons/react/outline";
|
|
|
|
import { useSession } from "next-auth/client";
|
2021-06-05 22:53:33 +00:00
|
|
|
|
|
|
|
export default function EditTeamModal(props) {
|
2021-08-02 20:51:57 +00:00
|
|
|
const [session] = useSession();
|
|
|
|
const [members, setMembers] = useState([]);
|
|
|
|
const [checkedDisbandTeam, setCheckedDisbandTeam] = useState(false);
|
2021-06-05 22:53:33 +00:00
|
|
|
|
2021-08-02 20:51:57 +00:00
|
|
|
const loadMembers = () =>
|
|
|
|
fetch("/api/teams/" + props.team.id + "/membership")
|
|
|
|
.then((res: any) => res.json())
|
|
|
|
.then((data) => setMembers(data.members));
|
2021-06-05 22:53:33 +00:00
|
|
|
|
2021-08-02 20:51:57 +00:00
|
|
|
useEffect(() => {
|
2021-06-05 22:53:33 +00:00
|
|
|
loadMembers();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const deleteTeam = (e) => {
|
|
|
|
e.preventDefault();
|
2021-08-02 20:51:57 +00:00
|
|
|
return fetch("/api/teams/" + props.team.id, {
|
|
|
|
method: "DELETE",
|
2021-06-05 22:53:33 +00:00
|
|
|
}).then(props.onExit);
|
2021-08-02 20:51:57 +00:00
|
|
|
};
|
2021-06-05 22:53:33 +00:00
|
|
|
|
|
|
|
const removeMember = (member) => {
|
2021-08-02 20:51:57 +00:00
|
|
|
return fetch("/api/teams/" + props.team.id + "/membership", {
|
|
|
|
method: "DELETE",
|
2021-06-05 22:53:33 +00:00
|
|
|
body: JSON.stringify({ userId: member.id }),
|
|
|
|
headers: {
|
2021-08-02 20:51:57 +00:00
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2021-06-05 22:53:33 +00:00
|
|
|
}).then(loadMembers);
|
2021-08-02 20:51:57 +00:00
|
|
|
};
|
2021-06-05 22:53:33 +00:00
|
|
|
|
2021-08-02 20:51:57 +00:00
|
|
|
return (
|
|
|
|
<div
|
2021-08-08 19:21:33 +00:00
|
|
|
className="fixed z-50 inset-0 overflow-y-auto"
|
2021-08-02 20:51:57 +00:00
|
|
|
aria-labelledby="modal-title"
|
|
|
|
role="dialog"
|
|
|
|
aria-modal="true">
|
|
|
|
<div className="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
|
2021-08-08 19:21:33 +00:00
|
|
|
<div
|
|
|
|
className="fixed inset-0 bg-gray-500 z-0 bg-opacity-75 transition-opacity"
|
|
|
|
aria-hidden="true"></div>
|
2021-06-05 22:53:33 +00:00
|
|
|
|
2021-08-02 20:51:57 +00:00
|
|
|
<span className="hidden sm:inline-block sm:align-middle sm:h-screen" aria-hidden="true">
|
|
|
|
​
|
|
|
|
</span>
|
2021-06-05 22:53:33 +00:00
|
|
|
|
2021-08-02 20:51:57 +00:00
|
|
|
<div className="inline-block align-bottom bg-white rounded-lg px-4 pt-5 pb-4 text-left shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full sm:p-6">
|
|
|
|
<div className="sm:flex sm:items-start mb-4">
|
|
|
|
<div className="mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-black bg-opacity-10 sm:mx-0 sm:h-10 sm:w-10">
|
|
|
|
<UsersIcon className="h-6 w-6 text-black" />
|
2021-06-05 22:53:33 +00:00
|
|
|
</div>
|
2021-08-02 20:51:57 +00:00
|
|
|
<div className="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
|
|
|
|
<h3 className="text-lg leading-6 font-medium text-gray-900" id="modal-title">
|
|
|
|
Edit the {props.team.name} team
|
|
|
|
</h3>
|
|
|
|
<div>
|
|
|
|
<p className="text-sm text-gray-400">Manage and delete your team.</p>
|
|
|
|
</div>
|
2021-06-05 22:53:33 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-08-02 20:51:57 +00:00
|
|
|
<form>
|
|
|
|
<div>
|
|
|
|
<div className="mb-4">
|
|
|
|
{members.length > 0 && (
|
|
|
|
<div>
|
|
|
|
<div className="flex justify-between mb-2">
|
|
|
|
<h2 className="text-lg font-medium text-gray-900">Members</h2>
|
|
|
|
</div>
|
|
|
|
<table className="table-auto mb-2 w-full text-sm">
|
|
|
|
<tbody>
|
|
|
|
{members.map((member) => (
|
|
|
|
<tr key={member.email}>
|
|
|
|
<td className="p-1">
|
|
|
|
{member.name} {member.name && "(" + member.email + ")"}
|
|
|
|
{!member.name && member.email}
|
|
|
|
</td>
|
|
|
|
<td className="capitalize">{member.role.toLowerCase()}</td>
|
|
|
|
<td className="text-right py-2 px-1">
|
|
|
|
{member.email !== session.user.email && (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => removeMember(member)}
|
|
|
|
className="btn-sm text-xs bg-transparent px-3 py-1 rounded ml-2">
|
|
|
|
<UserRemoveIcon className="text-red-400 group-hover:text-gray-500 flex-shrink-0 -mt-1 mr-1 h-4 w-4 inline" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div className="mb-4 border border-red-400 rounded p-2 px-4">
|
|
|
|
<p className="block text-sm font-medium text-gray-700">Tick the box to disband this team.</p>
|
|
|
|
<label className="mt-1">
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
onChange={(e) => setCheckedDisbandTeam(e.target.checked)}
|
|
|
|
className="shadow-sm mr-2 focus:ring-blue-500 focus:border-blue-500 sm:text-sm border-gray-300 rounded-md"
|
|
|
|
/>
|
|
|
|
Disband this team
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse">
|
|
|
|
{/*!checkedDisbandTeam && <button type="submit" className="btn btn-primary">
|
2021-06-05 22:53:33 +00:00
|
|
|
Update
|
2021-06-07 15:12:00 +00:00
|
|
|
</button>*/}
|
2021-08-02 20:51:57 +00:00
|
|
|
{checkedDisbandTeam && (
|
|
|
|
<button
|
|
|
|
onClick={deleteTeam}
|
|
|
|
className="btn bg-red-700 rounded text-white px-2 font-medium text-sm">
|
|
|
|
Disband Team
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
<button onClick={props.onExit} type="button" className="btn btn-white mr-2">
|
|
|
|
Close
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
2021-06-05 22:53:33 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-08-02 20:51:57 +00:00
|
|
|
);
|
|
|
|
}
|