Skip to content

Commit

Permalink
Merge pull request #40 from NishantGupt786/master
Browse files Browse the repository at this point in the history
error handling
  • Loading branch information
NishantGupt786 authored Mar 14, 2024
2 parents a87f651 + 340ad4d commit 8439cba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
20 changes: 17 additions & 3 deletions devsoc24-portal-fe/src/app/edit-project/edit-subject-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import { Textarea } from "@/components/ui/textarea";
import { Button } from "@/components/ui/button";
import { ideaSchema } from "@/schemas/idea";
import toast from "react-hot-toast";
import axios from "axios";
import axios, { AxiosError } from "axios";
import { useRouter } from "next/navigation";
import { type APIResponse } from "@/schemas/api";

interface FormValues {
title: string;
Expand Down Expand Up @@ -43,6 +45,7 @@ import ToastContainer from "@/components/ToastContainer";
const tracks = ["Track 1", "Track 2", "Track 3"];

export default function EditProjectForm() {
const router = useRouter();
useEffect(() => {
async function getIdeaSubmission() {
try {
Expand All @@ -54,8 +57,19 @@ export default function EditProjectForm() {
);
// console.log(res.data.data);
form.reset(res.data.data);
} catch (error) {
// console.log("Error getting idea submission:", error);
} catch (e) {
if (axios.isAxiosError(e)) {
const axiosError = e as AxiosError<APIResponse>;
switch (axiosError.response?.status) {
case 409:
void router.push("/home");
break;
case 401:
void router.push("/");
break;

}
}
}
}
void getIdeaSubmission();
Expand Down
27 changes: 14 additions & 13 deletions devsoc24-portal-fe/src/app/submit-idea/submit-idea-form.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm, Controller } from "react-hook-form";
import {IdeaStore} from "@/store/store";
import { IdeaStore } from "@/store/store";
import {
Form,
FormControl,
Expand Down Expand Up @@ -78,6 +78,9 @@ export default function SubmitIdeaForm() {
return `Incorrect credentials`;
case 400:
return `Please check your input and try again!`;
case 401:
void router.push("/");
return `Session Expired`;
default:
return `Something went wrong!`;
}
Expand Down Expand Up @@ -301,18 +304,16 @@ export default function SubmitIdeaForm() {
</div>
</div>
</div>
<div className="w-screen flex items-center justify-center">

<Button
className="my-5 bg-[#0019FF]"
type="submit"
// disabled={isSubmitting}
>
<Image src={send as HTMLImageElement} alt="b" />
<span className="pl-2">Submit Idea</span>
</Button>
</div>

<div className="flex w-screen items-center justify-center">
<Button
className="my-5 bg-[#0019FF]"
type="submit"
// disabled={isSubmitting}
>
<Image src={send as HTMLImageElement} alt="b" />
<span className="pl-2">Submit Idea</span>
</Button>
</div>
</form>
</Form>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Image from "next/image";
import toast from "react-hot-toast";
import axios, { AxiosError } from "axios";
import ToastContainer from "@/components/ToastContainer";
import {useRouter} from "next/navigation";
interface FormValues {
title: string;
track: string;
Expand All @@ -36,7 +37,7 @@ interface SubmitProjectResponse {
const tracks = ["Track 1", "Track 2", "Track 3"];

export default function SubmitProjectForm() {

const router = useRouter();
const form = useForm<FormValues>({
resolver: zodResolver(ideaSchema),
defaultValues: {
Expand All @@ -52,7 +53,7 @@ export default function SubmitProjectForm() {

async function onSubmit(data: FormValues) {
const handleSubmit = async () => {
await axios.post<SubmitProjectResponse>(
await axios.post<SubmitProjectResponse>(
`${process.env.NEXT_PUBLIC_API_URL}/project/create`,
data,
{
Expand All @@ -69,6 +70,9 @@ await axios.post<SubmitProjectResponse>(
switch (err.response?.status) {
case 404:
return `Account not found!`;
case 401:
void router.push("/");
return `Session Expired`;
default:
return `Project Submission failed!`;
}
Expand Down

0 comments on commit 8439cba

Please sign in to comment.