Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/auth architecture pt3 #278

Merged
merged 7 commits into from
Nov 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
connect usemutation hook to logout function
Dan-Y-Ko committed Nov 7, 2024
commit 534ac760672371d4afd04b46d9714d3a914dd858
38 changes: 23 additions & 15 deletions src/components/navbar/DropDown.tsx
Original file line number Diff line number Diff line change
@@ -4,13 +4,16 @@ import "reflect-metadata";
import { ChevronDownIcon } from "@heroicons/react/24/outline";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useMutation } from "@tanstack/react-query";
import Button from "@/components/Button";
import { useAppDispatch, useUser } from "@/store/hooks";
import { clientSignOut } from "@/store/features/auth/authSlice";
import { TYPES } from "@/di/types";
import { resolve } from "@/di/resolver";
import { type AuthClientAdapter } from "@/modules/auth/adapters/primary/authClientAdapter";
import routePaths from "@/utils/routePaths";
import type { LogoutResponseDto } from "@/modules/auth/application/dtos/response.dto";
import { onOpenModal } from "@/store/features/modal/modalSlice";

export default function DropDown({ openState }: { openState?: boolean }) {
const router = useRouter();
@@ -36,24 +39,29 @@ export default function DropDown({ openState }: { openState?: boolean }) {
const open =
"absolute flex flex-col gap-5 z-[1] w-[250px] p-5 bottom-100 translate-y-[15%] shadow-md bg-base-200 right-0 border border-base-100 rounded-2xl";

// TODO: update error handling
async function handleClick() {
const authAdapter = resolve<AuthClientAdapter>(TYPES.AuthClientAdapter);

await authAdapter.logout();
dispatch(clientSignOut());
router.replace(routePaths.signIn());
// if (res) {
// dispatch(clientSignOut());
// }
function handleClick() {
mutate();
}

// if (error) {
// dispatch(
// onOpenModal({ type: "error", content: { message: error.message } }),
// );
// }
async function logoutMutation(): Promise<LogoutResponseDto> {
const authAdapter = resolve<AuthClientAdapter>(TYPES.AuthClientAdapter);
return await authAdapter.logout();
}

const { mutate } = useMutation<LogoutResponseDto, Error, void>({
mutationFn: logoutMutation,
onSuccess: () => {
dispatch(clientSignOut());
router.replace(routePaths.signIn());
},
// TODO: update error handling
onError: (error: Error) => {
dispatch(
onOpenModal({ type: "error", content: { message: error.message } }),
);
},
});

const handleDropDownClick = (event: React.MouseEvent<HTMLDivElement>) => {
event.stopPropagation();
};