Skip to content

Commit

Permalink
useAuth関係、そのたスタイリングの修正
Browse files Browse the repository at this point in the history
  • Loading branch information
hellotksan committed Nov 25, 2024
1 parent 1972b5c commit d708574
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"lint-staged": {
"src/**/*.{js,jsx,ts,tsx,md}": [
""
"eslint --fix"
]
},
"ignorePatterns": [
Expand Down
2 changes: 1 addition & 1 deletion src/app/followers/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const FollowersPage: React.FC = () => {
return (
<div className="w-full flex flex-col">
<Topbar />
<div className="max-w-xl mx-auto">
<div className="max-w-[480px] xl:w-[480px] mx-auto">
<FollowersComponent />
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/app/followings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const FollowingUsersPage: React.FC = () => {
return (
<div className="w-full flex flex-col">
<Topbar />
<div className="max-w-xl mx-auto">
<div className="max-w-[480px] xl:w-[480px] mx-auto">
<FollowingUsersComponent />
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/app/post/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const PostEditPage: React.FC = () => {
return (
<div className="w-full flex flex-col">
<Topbar />
<div className="max-w-xl mx-auto">
<div className="max-w-[480px] xl:w-[480px] mx-auto">
<Suspense fallback={<LoadingSpinner />}>
<PostEditContent />
</Suspense>
Expand Down
2 changes: 1 addition & 1 deletion src/app/profile/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const ProfilePage: React.FC = () => {
return (
<div className="w-full flex flex-col">
<Topbar />
<div className="max-w-md mx-auto">
<div className="max-w-[480px] xl:w-[480px] mx-auto">
<Suspense fallback={<LoadingSpinner />}>
<ProfileContent />
</Suspense>
Expand Down
2 changes: 1 addition & 1 deletion src/app/setting/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const SettingPage: React.FC = () => {
return (
<div className="w-full flex flex-col">
<Topbar />
<div className="min-w-96 max-w-xl mx-auto">
<div className="max-w-[480px] xl:w-[480px] mx-auto">
<SettingComponent />
</div>
</div>
Expand Down
28 changes: 16 additions & 12 deletions src/components/layouts/login/LoginForm.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
"use client";

import React, { useEffect, useRef } from "react";
import { useRouter } from "next/navigation";
// import { useRouter } from "next/navigation";
import { loginCall } from "@/app/actionCalls";
import {
loginStart,
loginSuccess,
loginError,
} from "@/features/auth/authSlice";
// import {
// loginStart,
// loginSuccess,
// loginError,
// } from "@/features/auth/authSlice";
import { useAppDispatch } from "@/hooks/useDispatch";
import { User } from "@/types/user";
import { useAuth } from "@/hooks/useAuth";

const LoginForm: React.FC = () => {
const emailRef = useRef<HTMLInputElement>(null);
const passwordRef = useRef<HTMLInputElement>(null);

const dispatch = useAppDispatch();
const router = useRouter();
const { login } = useAuth();
// const router = useRouter();

useEffect(() => {
// 自動でメールフィールドにフォーカスを当てる
Expand All @@ -25,22 +27,24 @@ const LoginForm: React.FC = () => {

const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

const emailValue = emailRef.current?.value || "";
const passwordValue = passwordRef.current?.value || "";
onSubmit(emailValue, passwordValue);
};

const onSubmit = async (email: string, password: string): Promise<void> => {
dispatch(loginStart());
// dispatch(loginStart());

try {
const userData: User = await loginCall({ email, password, dispatch });
dispatch(loginSuccess(userData));
router.push("/home");
login(userData);
// dispatch(loginSuccess(userData));
// router.push("/home");
} catch (err) {
dispatch(loginError("ログインに失敗しました"));
// dispatch(loginError("ログインに失敗しました"));
alert("エラーが発生しました。");
router.refresh();
// router.refresh();
}
};

Expand Down
13 changes: 8 additions & 5 deletions src/components/layouts/register/RegisterForm.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
"use client";

import React, { useState } from "react";
import { useRouter } from "next/navigation";
import axios from "axios";
import { AUTH_REGISTER_ENDPOINT } from "@/constants/api";
import RegisterButton from "./RegisterButton";
import { User } from "@/types/user";
import { loginCall } from "@/app/actionCalls";
import { useAppDispatch } from "@/hooks/useDispatch";
import { useAuth } from "@/hooks/useAuth";

const RegisterForm: React.FC = () => {
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [passwordConfirmation, setPasswordConfirmation] = useState("");

const router = useRouter();
const dispatch = useAppDispatch();
const { login } = useAuth();

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
Expand All @@ -25,10 +28,10 @@ const RegisterForm: React.FC = () => {
try {
await axios.post(AUTH_REGISTER_ENDPOINT, { username, email, password });
alert("ユーザーを登録しました。");
router.push("/login");
const userData: User = await loginCall({ email, password, dispatch });
login(userData);
} catch (err) {
alert("ユーザー登録に失敗しました。もう一度お試しください。");
router.refresh();
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/layouts/timeline/PostButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function PostButton() {
type="submit"
disabled={pending}
>
{pending ? "投稿" : "投稿中..."}
{pending ? "投稿中..." : "投稿"}
</button>
</div>
);
Expand Down
5 changes: 5 additions & 0 deletions src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
logout,
} from "@/features/auth/authSlice";
import { User } from "@/types/user";
import { useRouter } from "next/navigation";

interface UseAuthReturn {
user: User | null;
Expand All @@ -19,18 +20,22 @@ interface UseAuthReturn {
export const useAuth = (): UseAuthReturn => {
const dispatch = useAppDispatch();
const { user, isLoading, error } = useAppSelector((state) => state.auth);
const router = useRouter();

const login = async (userData: User) => {
dispatch(loginStart());
try {
dispatch(loginSuccess(userData));
router.push("/home");
} catch (err) {
dispatch(loginError("ログインに失敗しました"));
router.refresh();
}
};

const logoutUser = () => {
dispatch(logout());
router.push("/")
};

return { user, isLoading, error, login, logoutUser };
Expand Down
36 changes: 0 additions & 36 deletions src/tsconfig.json

This file was deleted.

0 comments on commit d708574

Please sign in to comment.