update fetch to also handle errors
parent
8b00475b27
commit
1122def2c0
|
@ -1,30 +1,37 @@
|
||||||
import { GetServerSideProps } from "next";
|
|
||||||
import Head from 'next/head';
|
import Head from 'next/head';
|
||||||
import prisma from '../../lib/prisma';
|
|
||||||
import Modal from '../../components/Modal';
|
|
||||||
import Shell from '../../components/Shell';
|
import Shell from '../../components/Shell';
|
||||||
import SettingsShell from '../../components/Settings';
|
import SettingsShell from '../../components/Settings';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useSession, getSession } from 'next-auth/client';
|
import { useSession } from 'next-auth/client';
|
||||||
import {
|
import {
|
||||||
UsersIcon,
|
UsersIcon,
|
||||||
} from "@heroicons/react/outline";
|
} from "@heroicons/react/outline";
|
||||||
import TeamList from "../../components/team/TeamList";
|
import TeamList from "../../components/team/TeamList";
|
||||||
import TeamListItem from "../../components/team/TeamListItem";
|
import TeamListItem from "../../components/team/TeamListItem";
|
||||||
|
|
||||||
export default function Teams(props) {
|
export default function Teams() {
|
||||||
|
|
||||||
const [session, loading] = useSession();
|
const [session, loading] = useSession();
|
||||||
const [teams, setTeams] = useState([]);
|
const [teams, setTeams] = useState([]);
|
||||||
const [invites, setInvites] = useState([]);
|
const [invites, setInvites] = useState([]);
|
||||||
const [showCreateTeamModal, setShowCreateTeamModal] = useState(false);
|
const [showCreateTeamModal, setShowCreateTeamModal] = useState(false);
|
||||||
|
|
||||||
const loadData = () => fetch('/api/user/membership').then((res: any) => res.json()).then(
|
const handleErrors = async (resp) => {
|
||||||
(data) => {
|
if (!resp.ok) {
|
||||||
|
const err = await resp.json();
|
||||||
|
throw new Error(err.message);
|
||||||
|
}
|
||||||
|
return resp.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
const loadData = () => {
|
||||||
|
fetch('/api/user/membership')
|
||||||
|
.then(handleErrors)
|
||||||
|
.then((data) => {
|
||||||
setTeams(data.membership.filter((m) => m.role !== "INVITEE"));
|
setTeams(data.membership.filter((m) => m.role !== "INVITEE"));
|
||||||
setInvites(data.membership.filter((m) => m.role === "INVITEE"));
|
setInvites(data.membership.filter((m) => m.role === "INVITEE"));
|
||||||
|
})
|
||||||
|
.catch(console.log);
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadData();
|
loadData();
|
||||||
|
@ -36,6 +43,7 @@ export default function Teams(props) {
|
||||||
|
|
||||||
const createTeam = (e) => {
|
const createTeam = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
return fetch('/api/teams', {
|
return fetch('/api/teams', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ name: e.target.elements['name'].value }),
|
body: JSON.stringify({ name: e.target.elements['name'].value }),
|
||||||
|
@ -157,14 +165,3 @@ export default function Teams(props) {
|
||||||
</Shell>
|
</Shell>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getServerSideProps: GetServerSideProps = async (context) => {
|
|
||||||
const session = await getSession(context);
|
|
||||||
if (!session) {
|
|
||||||
return { redirect: { permanent: false, destination: "/auth/login" } };
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
props: { }, // will be passed to the page component as props
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in New Issue