cal.pub0.org/pages/settings/teams.tsx

203 lines
7.8 KiB
TypeScript
Raw Normal View History

import { GetServerSideProps } from "next";
2021-07-07 10:43:13 +00:00
import Head from "next/head";
import Shell from "../../components/Shell";
import SettingsShell from "../../components/Settings";
import { useEffect, useState } from "react";
import type { Session } from "next-auth";
2021-07-07 10:43:13 +00:00
import { getSession, useSession } from "next-auth/client";
import { UsersIcon } from "@heroicons/react/outline";
import TeamList from "../../components/team/TeamList";
import TeamListItem from "../../components/team/TeamListItem";
import Loader from "@components/Loader";
2021-06-03 20:55:34 +00:00
2021-07-01 08:47:27 +00:00
export default function Teams() {
2021-07-07 10:43:13 +00:00
const [, loading] = useSession();
2021-06-08 16:00:06 +00:00
const [teams, setTeams] = useState([]);
const [invites, setInvites] = useState([]);
const [showCreateTeamModal, setShowCreateTeamModal] = useState(false);
2021-07-01 08:47:27 +00:00
const handleErrors = async (resp) => {
if (!resp.ok) {
const err = await resp.json();
throw new Error(err.message);
}
2021-07-01 08:47:27 +00:00
return resp.json();
2021-07-07 10:43:13 +00:00
};
2021-07-01 08:47:27 +00:00
const loadData = () => {
fetch("/api/user/membership")
2021-07-07 10:43:13 +00:00
.then(handleErrors)
.then((data) => {
setTeams(data.membership.filter((m) => m.role !== "INVITEE"));
setInvites(data.membership.filter((m) => m.role === "INVITEE"));
})
.catch(console.log);
};
useEffect(() => {
loadData();
}, []);
2021-06-03 20:55:34 +00:00
if (loading) {
return <Loader />;
2021-06-03 20:55:34 +00:00
}
const createTeam = (e) => {
e.preventDefault();
2021-07-01 08:47:27 +00:00
2021-07-07 10:43:13 +00:00
return fetch("/api/teams", {
method: "POST",
body: JSON.stringify({ name: e.target.elements["name"].value }),
headers: {
2021-07-07 10:43:13 +00:00
"Content-Type": "application/json",
},
2021-06-08 16:00:06 +00:00
}).then(() => {
loadData();
setShowCreateTeamModal(false);
});
2021-07-07 10:43:13 +00:00
};
2021-06-03 20:55:34 +00:00
2021-06-08 16:00:06 +00:00
return (
<Shell heading="Teams" subtitle="Create and manage teams to use collaborative features.">
2021-06-03 20:55:34 +00:00
<Head>
<title>Teams | Calendso</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<SettingsShell>
<div className="divide-y divide-gray-200 lg:col-span-9">
2021-07-30 23:05:38 +00:00
<div className="py-6 lg:pb-8">
2021-06-03 20:55:34 +00:00
<div className="flex justify-between">
<div>
2021-07-07 10:43:13 +00:00
{!(invites.length || teams.length) && (
2021-07-30 23:05:38 +00:00
<div className="bg-gray-50 sm:rounded-sm">
2021-06-08 16:00:06 +00:00
<div className="px-4 py-5 sm:p-6">
2021-07-07 10:43:13 +00:00
<h3 className="text-lg leading-6 font-medium text-gray-900">
Create a team to get started
</h3>
2021-06-08 16:00:06 +00:00
<div className="mt-2 max-w-xl text-sm text-gray-500">
<p>Create your first team and invite other users to work together with you.</p>
</div>
<div className="mt-5">
<button
type="button"
onClick={() => setShowCreateTeamModal(true)}
2021-07-07 10:43:13 +00:00
className="btn btn-primary">
2021-06-08 16:00:06 +00:00
Create new team
</button>
</div>
</div>
</div>
2021-07-07 10:43:13 +00:00
)}
2021-06-03 20:55:34 +00:00
</div>
2021-07-07 10:43:13 +00:00
{!!(invites.length || teams.length) && (
<div>
2021-07-30 23:05:38 +00:00
<button className="btn-sm btn-primary mb-4" onClick={() => setShowCreateTeamModal(true)}>
2021-07-07 10:43:13 +00:00
Create new team
</button>
</div>
)}
2021-06-03 20:55:34 +00:00
</div>
<div>
2021-07-07 10:43:13 +00:00
{!!teams.length && <TeamList teams={teams} onChange={loadData}></TeamList>}
2021-07-07 10:43:13 +00:00
{!!invites.length && (
<div>
<h2 className="text-lg leading-6 font-medium text-gray-900">Open Invitations</h2>
<ul className="border px-2 rounded mt-2 mb-2 divide-y divide-gray-200">
{invites.map((team) => (
<TeamListItem onChange={loadData} key={team.id} team={team}></TeamListItem>
))}
</ul>
</div>
)}
2021-06-03 20:55:34 +00:00
</div>
{/*{teamsLoaded && <div className="flex justify-between">
<div>
<h2 className="text-lg leading-6 font-medium text-gray-900 mb-1">Transform account</h2>
<p className="text-sm text-gray-500 mb-1">
{membership.length !== 0 && "You cannot convert this account into a team until you leave all teams that youre a member of."}
{membership.length === 0 && "A user account can be turned into a team, as a team ...."}
</p>
</div>
<div>
<button className="mt-2 btn-sm btn-primary opacity-50 cursor-not-allowed" disabled>Convert {session.user.username} into a team</button>
</div>
</div>}*/}
2021-06-03 20:55:34 +00:00
</div>
</div>
2021-07-07 10:43:13 +00:00
{showCreateTeamModal && (
<div
2021-08-03 11:19:32 +00:00
className="fixed z-50 inset-0 overflow-y-auto"
2021-07-07 10:43:13 +00:00
aria-labelledby="modal-title"
role="dialog"
aria-modal="true">
2021-06-08 16:00:06 +00:00
<div className="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
2021-07-07 10:43:13 +00:00
<div
2021-08-03 11:23:19 +00:00
className="fixed inset-0 bg-gray-500 z-0 bg-opacity-75 transition-opacity"
2021-07-07 10:43:13 +00:00
aria-hidden="true"></div>
2021-06-03 20:55:34 +00:00
2021-07-07 10:43:13 +00:00
<span className="hidden sm:inline-block sm:align-middle sm:h-screen" aria-hidden="true">
&#8203;
</span>
2021-06-03 20:55:34 +00:00
2021-07-30 23:05:38 +00:00
<div className="inline-block align-bottom bg-white rounded-sm 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">
2021-06-08 16:00:06 +00:00
<div className="sm:flex sm:items-start mb-4">
2021-07-30 23:05:38 +00:00
<div className="mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-neutral-100 sm:mx-0 sm:h-10 sm:w-10">
<UsersIcon className="h-6 w-6 text-neutral-900" />
2021-06-08 16:00:06 +00:00
</div>
<div className="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
2021-07-07 10:43:13 +00:00
<h3 className="text-lg leading-6 font-medium text-gray-900" id="modal-title">
Create a new team
</h3>
2021-06-08 16:00:06 +00:00
<div>
2021-07-07 10:43:13 +00:00
<p className="text-sm text-gray-400">Create a new team to collaborate with users.</p>
2021-06-08 16:00:06 +00:00
</div>
</div>
</div>
2021-06-08 16:00:06 +00:00
<form onSubmit={createTeam}>
<div className="mb-4">
2021-07-07 10:43:13 +00:00
<label htmlFor="name" className="block text-sm font-medium text-gray-700">
Name
</label>
<input
type="text"
name="name"
id="name"
placeholder="Acme Inc."
required
2021-07-30 23:05:38 +00:00
className="mt-1 block w-full border border-gray-300 rounded-sm shadow-sm py-2 px-3 focus:outline-none focus:ring-neutral-500 focus:border-neutral-500 sm:text-sm"
2021-07-07 10:43:13 +00:00
/>
2021-06-08 16:00:06 +00:00
</div>
<div className="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse">
<button type="submit" className="btn btn-primary">
Create team
2021-07-07 10:43:13 +00:00
</button>
<button
onClick={() => setShowCreateTeamModal(false)}
type="button"
className="btn btn-white mr-2">
2021-06-08 16:00:06 +00:00
Cancel
2021-07-07 10:43:13 +00:00
</button>
2021-06-08 16:00:06 +00:00
</div>
</form>
</div>
2021-06-03 20:55:34 +00:00
</div>
</div>
2021-07-07 10:43:13 +00:00
)}
2021-06-03 20:55:34 +00:00
</SettingsShell>
</Shell>
);
}
2021-07-02 10:28:33 +00:00
// Export the `session` prop to use sessions with Server Side Rendering
export const getServerSideProps: GetServerSideProps<{ session: Session | null }> = async (context) => {
2021-07-07 10:43:13 +00:00
const session = await getSession(context);
if (!session) {
return { redirect: { permanent: false, destination: "/auth/login" } };
}
2021-07-02 10:28:33 +00:00
2021-07-07 10:43:13 +00:00
return {
props: { session },
};
};