diff --git a/.gitignore b/.gitignore index 00bba9b..d61ce4e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ /.pnp .pnp.js .yarn/install-state.gz +pnpm-lock.yaml # testing /coverage diff --git a/app/api/sub/post/create/route.ts b/app/api/sub/post/create/route.ts index c8f4c07..5c962f2 100644 --- a/app/api/sub/post/create/route.ts +++ b/app/api/sub/post/create/route.ts @@ -1,7 +1,6 @@ import { getAuthSession } from "@/lib/auth"; import { db } from "@/lib/db"; import { PostValidator } from "@/lib/validators/post"; -import { SubgroupSubscriptionValidator } from "@/lib/validators/sub"; import { z } from "zod"; export async function POST(req: Request) { diff --git a/app/sub/create/page.tsx b/app/sub/create/page.tsx index b3fefa3..c6c7811 100644 --- a/app/sub/create/page.tsx +++ b/app/sub/create/page.tsx @@ -1,19 +1,19 @@ "use client"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; -import { useRouter } from "next/navigation"; -import { useState } from "react"; +import { ToastAction } from "@/components/ui/toast"; +import { toast } from "@/hooks/use-toast"; +import useCustomLoginToast from "@/hooks/useCustomToast"; +import { CreateSubgroupPayload } from "@/lib/validators/sub"; import { useMutation } from "@tanstack/react-query"; import axios, { AxiosError } from "axios"; -import { CreateSubgroupPayload } from "@/lib/validators/sub"; -import { toast } from "@/hooks/use-toast"; -import useCustomToast from "@/hooks/useCustomToast"; -import { ToastAction } from "@/components/ui/toast"; +import { useRouter } from "next/navigation"; +import { useState } from "react"; export default function Create() { const [input, setInput] = useState(""); const router = useRouter(); - const { loginToast } = useCustomToast(); + const { loginToast } = useCustomLoginToast(); const { mutate: createCommunity, isPending } = useMutation({ mutationFn: async () => { @@ -64,9 +64,7 @@ export default function Create() { variant: "destructive", action: Try again, }); - }, - }); return ( diff --git a/components/SubscribeLeaveToggle.tsx b/components/SubscribeLeaveToggle.tsx index 3baa333..0e257ac 100644 --- a/components/SubscribeLeaveToggle.tsx +++ b/components/SubscribeLeaveToggle.tsx @@ -6,7 +6,7 @@ import axios, { AxiosError } from "axios"; import { ToastAction } from "./ui/toast"; import { toast } from "@/hooks/use-toast"; import { useRouter } from "next/navigation"; -import useCustomToast from "@/hooks/useCustomToast"; +import useCustomLoginToast from "@/hooks/useCustomToast"; import { startTransition } from "react"; interface SubscribeLeaveToggleProps { isSubscribed: boolean; @@ -19,7 +19,7 @@ export default function SubscribeLeaveToggle({ subgroupName, }: SubscribeLeaveToggleProps) { const router = useRouter(); - const { loginToast } = useCustomToast(); + const { loginToast } = useCustomLoginToast(); // Subscribe const { mutate: subscribe, isPending: isSubscribing } = useMutation({ diff --git a/components/pages/Editor.tsx b/components/pages/Editor.tsx index 0e04ef5..c88e655 100644 --- a/components/pages/Editor.tsx +++ b/components/pages/Editor.tsx @@ -1,15 +1,16 @@ "use client"; +import { toast } from "@/hooks/use-toast"; +import useCustomLoginToast from "@/hooks/useCustomToast"; import { uploadFiles } from "@/lib/uploadthing"; import { PostCreationRequest, PostValidator } from "@/lib/validators/post"; import type EditorJS from "@editorjs/editorjs"; import { zodResolver } from "@hookform/resolvers/zod"; import { useMutation } from "@tanstack/react-query"; -import axios from "axios"; +import axios, { AxiosError } from "axios"; import { usePathname, useRouter } from "next/navigation"; import { useCallback, useEffect, useRef, useState } from "react"; import { useForm } from "react-hook-form"; import TextAreaAutoSize from "react-textarea-autosize"; -import { toast } from "../hooks/use-toast"; export default function Editor({ subgroupId }: { subgroupId: string }) { const { @@ -25,6 +26,8 @@ export default function Editor({ subgroupId }: { subgroupId: string }) { }, }); + const { loginToast } = useCustomLoginToast(); + const ref = useRef(); const [isMounted, setIsMounted] = useState(false); const _titleRef = useRef(null); @@ -134,7 +137,13 @@ export default function Editor({ subgroupId }: { subgroupId: string }) { return data; }, - onError: () => { + onError: (err) => { + if (err instanceof AxiosError) { + if (err.response?.status === 401) { + return loginToast(); + } + } + return toast({ title: "You're post wasn't published 😢", description: "Please try again later.", diff --git a/components/pages/UserAccountNav.tsx b/components/pages/UserAccountNav.tsx index 80b50c3..b3c4c35 100644 --- a/components/pages/UserAccountNav.tsx +++ b/components/pages/UserAccountNav.tsx @@ -29,7 +29,7 @@ export default function UserAccountNav({ user }: UserAccountNavProps) { /> - +
{user.name &&

{user.name}

} diff --git a/hooks/useCustomToast.tsx b/hooks/useCustomToast.tsx index 33718e5..0c067f1 100644 --- a/hooks/useCustomToast.tsx +++ b/hooks/useCustomToast.tsx @@ -2,7 +2,7 @@ import Link from "next/link"; import { toast } from "./use-toast"; import { buttonVariants } from "@/components/ui/button"; -export default function useCustomToast() { +export default function useCustomLoginToast() { const loginToast = () => { const { dismiss } = toast({ title: "Login required.",