Skip to content

Commit

Permalink
chore: Update custom hook for loginToast
Browse files Browse the repository at this point in the history
  • Loading branch information
gupta-soham committed Nov 4, 2024
1 parent 5a32be2 commit 739230b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/.pnp
.pnp.js
.yarn/install-state.gz
pnpm-lock.yaml

# testing
/coverage
Expand Down
1 change: 0 additions & 1 deletion app/api/sub/post/create/route.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
16 changes: 7 additions & 9 deletions app/sub/create/page.tsx
Original file line number Diff line number Diff line change
@@ -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<string>("");
const router = useRouter();
const { loginToast } = useCustomToast();
const { loginToast } = useCustomLoginToast();

const { mutate: createCommunity, isPending } = useMutation({
mutationFn: async () => {
Expand Down Expand Up @@ -64,9 +64,7 @@ export default function Create() {
variant: "destructive",
action: <ToastAction altText="Try again">Try again</ToastAction>,
});

},

});

return (
Expand Down
4 changes: 2 additions & 2 deletions components/SubscribeLeaveToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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({
Expand Down
15 changes: 12 additions & 3 deletions components/pages/Editor.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -25,6 +26,8 @@ export default function Editor({ subgroupId }: { subgroupId: string }) {
},
});

const { loginToast } = useCustomLoginToast();

const ref = useRef<EditorJS>();
const [isMounted, setIsMounted] = useState<boolean>(false);
const _titleRef = useRef<HTMLTextAreaElement>(null);
Expand Down Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion components/pages/UserAccountNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function UserAccountNav({ user }: UserAccountNavProps) {
/>
</DropdownMenuTrigger>

<DropdownMenuContent className="bg-white" align="end">
<DropdownMenuContent className="" align="end">
<div className="flex items-center justify-start gap-2 p-2">
<div className="flex flex-col space-y-1 leading-none">
{user.name && <p className="font-medium">{user.name}</p>}
Expand Down
2 changes: 1 addition & 1 deletion hooks/useCustomToast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down

0 comments on commit 739230b

Please sign in to comment.