Skip to content

Commit

Permalink
error handled
Browse files Browse the repository at this point in the history
  • Loading branch information
Abh1noob committed Mar 14, 2024
1 parent b7945e4 commit e5ae211
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
35 changes: 25 additions & 10 deletions devsoc24-portal-fe/src/components/team/joinTeam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Label } from "@/components/ui/label";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { useRef } from "react";
import axios from "axios";
import axios, { AxiosError } from "axios";
import {
useTeamDataStore,
useTeamStore,
Expand All @@ -19,6 +19,7 @@ import {
} from "@/store/store";
import { useRouter } from "next/navigation";
import { type APIResponse } from "@/schemas/api";
import toast from "react-hot-toast";

function JoinTeam() {
const inputRef = useRef<HTMLInputElement>(null);
Expand All @@ -29,7 +30,7 @@ function JoinTeam() {
const { edit, setEdit } = useTeamEditStore();
const router = useRouter();
const handleClick = async () => {
try {
const handleSubmit = async () => {
await axios.post(
`${process.env.NEXT_PUBLIC_API_URL}/team/join`,
{
Expand All @@ -44,16 +45,30 @@ function JoinTeam() {
SetIdea("idea found");
setIsLeader(false);
setTeam(false);
} catch (e) {
if (axios.isAxiosError(e)) {
switch (e.response?.status) {
case 202:
// console.log("Accepted");
};
void toast.promise(handleSubmit(), {
loading: "Cooking...",
success: () => {
void router.push("/home");
return `Team joined successfully!`;
},
error: (err: AxiosError) => {
switch (err.response?.status) {
case 404:
return `Account not found!`;
case 417:
return `User is already in a team!`;
case 409:
return `Invalid Team Code!`;
case 424:
return `Team is full!`;
case 400:
return `Please check your input and try again!`;
default:
// console.log(e);
return `Something went wrong!`;
}
}
}
},
});
};
const fetchTeam = async () => {
try {
Expand Down
4 changes: 1 addition & 3 deletions devsoc24-portal-fe/src/components/team/leaveTeam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const LeaveTeam = () => {
},
);
setTeamData(response.data);

} catch (e) {
if (axios.isAxiosError(e)) {
switch (e.response?.status) {
Expand All @@ -63,13 +62,12 @@ const LeaveTeam = () => {
}
};


const leaveTeam = async () => {
const handleClick = async () => {
await axios.delete(`${process.env.NEXT_PUBLIC_API_URL}/team/leave`, {
withCredentials: true,
});
console.log("left team");
// console.log("left team");
SetIdea("left team");
void fetchTeam();
setTeam(true);
Expand Down

0 comments on commit e5ae211

Please sign in to comment.