Skip to content

Commit

Permalink
removed unusual toast
Browse files Browse the repository at this point in the history
  • Loading branch information
Abh1noob committed Mar 12, 2024
1 parent 00c4acc commit 0c3545b
Showing 1 changed file with 39 additions and 48 deletions.
87 changes: 39 additions & 48 deletions devsoc24-portal-fe/src/components/team/createTeam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import axios, { AxiosError } from "axios";
import { useRef } from "react";
import {
useTeamDataStore,
useTeamStore,
} from "@/store/store";
import { useTeamDataStore, useTeamStore } from "@/store/store";
import { useRouter } from "next/navigation";
import { APIResponse } from "@/schemas/api";
import toast from "react-hot-toast";
Expand All @@ -24,39 +21,41 @@ function CreateTeam() {
const { teamData, setTeamData } = useTeamDataStore();

const router = useRouter();
const handleClick = async () => {
await axios.post(
`${process.env.NEXT_PUBLIC_API_URL}/team/create`,
{
name: inputRef.current?.value,
},
{
withCredentials: true,
},
);
};

void toast.promise(handleClick(), {
loading: "Cooking...",
success: () => {
const handleClick = async () => {
try {
await axios.post(
`${process.env.NEXT_PUBLIC_API_URL}/team/create`,
{
name: inputRef.current?.value,
},
{
withCredentials: true,
},
);
toast.success("Team created successfully!");
await fetchTeam();
setTeam(false);
void fetchTeam();
return `Team created successfully!`;
},
error: (err: AxiosError) => {
// console.log("ERR", err);
switch (err.response?.status) {
case 404:
return `Account not found!`;
case 409:
return `Incorrect credentials`;
case 400:
return `Please check your input and try again!`;
default:
return `Something went wrong!`;
} catch (err) {
if (axios.isAxiosError(err)) {
switch (err.response?.status) {
case 404:
toast.error("Account not found!");
break;
case 409:
toast.error("Team name already exists!");
break;
case 400:
toast.error("Please check your input and try again!");
break;
default:
toast.error("Something went wrong!");
break;
}
}
},
});
}
};

const fetchTeam = async () => {
try {
const response = await axios.get<APIResponse>(
Expand All @@ -66,26 +65,24 @@ function CreateTeam() {
},
);
setTeamData(response.data);
setTeam(true);
} catch (e) {
if (axios.isAxiosError(e)) {
switch (e.response?.status) {
case 401:
void router.push("/");
router.push("/");
break;
case 417:
setTeam(true);
// console.log("no team");
break;
case 200:
setTeam(true);
setTeam(false);
break;
default:
console.log(e);
console.error(e);
break;
}
}
}
};

return (
<>
<DialogContent className="sm:max-w-[425px]">
Expand All @@ -106,13 +103,7 @@ function CreateTeam() {
<div className="flex justify-center">
<DialogFooter className="sm:justify-start">
<DialogClose>
<Button
className="bg-[#458B71]"
onClick={async () => {
await handleClick();
await fetchTeam();
}}
>
<Button className="bg-[#458B71]" onClick={handleClick}>
Confirm
</Button>
</DialogClose>
Expand Down

0 comments on commit 0c3545b

Please sign in to comment.