Skip to content

Commit

Permalink
Merge pull request #367 from ut-code/task/#361/use-openapi-schema
Browse files Browse the repository at this point in the history
ユーザーネーム関連をフロントエンドから消し去った
  • Loading branch information
bvv-1 authored Jun 12, 2024
2 parents 753e9ce + bbb2f7a commit d7c4523
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 112 deletions.
11 changes: 2 additions & 9 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Routes, Route, Navigate, useLocation } from "react-router-dom"
import { useState, useEffect } from "react"
import { Session } from "@supabase/supabase-js"
import { useLocalStorage } from "react-use"

import { User, updateUsername } from "@/utils/users"
import { User } from "@/utils/users"
import { getUserApi } from "@/utils/apiUtils"
import { UserContext } from "@/utils/context"
import { HowTo } from "@/pages/HowTo"
Expand All @@ -26,7 +25,6 @@ export default function App() {
const [user, setUser] = useState<User | null>(null)
const [session, setSession] = useState<Session | null>(null)
const [isLoading, setIsLoading] = useState<boolean>(true)
const [inputUsername, setInputUsername] = useLocalStorage("inputUsername", "")
const location = useLocation()

useEffect(() => {
Expand Down Expand Up @@ -54,11 +52,6 @@ export default function App() {
if (fetchedUser) {
setUser(fetchedUser)
}
const username = await updateUsername({ user, session }, inputUsername)
if (username && fetchedUser) {
setInputUsername(username)
setUser({ ...fetchedUser, username: username })
}
})
setIsLoading(false)
}
Expand All @@ -77,7 +70,7 @@ export default function App() {
/>
<Route path="/result" element={<Result />} />
<Route path="/setting" element={<Setting />} />
<Route path="/auth" element={<Auth inputUsername={inputUsername} setInputUsername={setInputUsername} />} />
<Route path="/auth" element={<Auth />} />
<Route path="/new" element={<NewRecipe />} />
<Route path="*" element={<NotFound />} />
</Routes>
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/pages/Auth/Auth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ beforeAll(() => {

describe("SignIn component", () => {
it("should handle form submission and show success alert", async () => {
const mockSetInputUsername = vi.fn()
const { getByLabelText, getByText } = render(
<MemoryRouter>
<Auth inputUsername="" setInputUsername={mockSetInputUsername} />
<Auth />
</MemoryRouter>
)
const emailInput = getByLabelText("メールアドレス")
Expand All @@ -36,10 +35,9 @@ describe("SignIn component", () => {
})

it("should handle Google sign-in button click and show error alert", async () => {
const mockSetInputUsername = vi.fn()
const { getByText } = render(
<MemoryRouter>
<Auth inputUsername="" setInputUsername={mockSetInputUsername} />
<Auth />
</MemoryRouter>
)
const googleSignInButton = getByText("Googleアカウントで続ける")
Expand Down
13 changes: 2 additions & 11 deletions frontend/src/pages/Auth/Auth.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, useContext } from "react"
import { useState, useContext } from "react"
import { useNavigate } from "react-router-dom"

import { UserContext } from "@/utils/context"
Expand All @@ -12,22 +12,13 @@ import { TextField } from "@mui/material"

import styles from "./Auth.module.css"

interface Props {
inputUsername: string | undefined
setInputUsername: (inputUsername: string) => void
}

export const Auth = ({ setInputUsername }: Props) => {
export const Auth = () => {
const navigate = useNavigate()
const { session } = useContext(UserContext)
const [loading, setLoading] = useState(false)
const [hasAccount, setHasAccount] = useState<boolean>(false)
const [email, setEmail] = useState("")

useEffect(() => {
setInputUsername("")
}, [])

const handleSignIn = async () => {
setLoading(true)
const { error } = await supabase.auth.signInWithOtp({ email })
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/utils/apiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ export const deleteUserFavoritesApi = (id: number): string => {
return `${API_BASE_URL}/api/users/favorites/${id}`
}

export const updateUsernameApi = (): string => {
return `${API_BASE_URL}/api/users/username`
}

export const getUserApi = (): string => {
return `${API_BASE_URL}/api/users`
}
Expand Down
21 changes: 0 additions & 21 deletions frontend/src/utils/users.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Session } from "@supabase/supabase-js"

import { updateUsernameApi } from "./apiUtils"

export type User = {
id: string | null
email: string | null
Expand All @@ -12,22 +10,3 @@ export type UserInfo = {
user: User | null
session: Session | null
}

export const updateUsername = async (
{ user, session }: UserInfo,
username: string | undefined
): Promise<string | null> => {
if (!session?.access_token || !user) return null
if (!username) return null

const response = await fetch(updateUsernameApi(), {
method: "PUT",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${session?.access_token}` },
body: JSON.stringify({ username: username }),
})
if (!response.ok) {
alert("ユーザーネームの更新に失敗しました")
return null
}
return username
}
File renamed without changes.
33 changes: 0 additions & 33 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -410,39 +410,6 @@
}
}
},
"/api/users/username": {
"put": {
"description": "",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"username": {
"example": "any"
}
}
}
}
}
},
"responses": {
"200": {
"description": "OK"
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
},
"500": {
"description": "Internal Server Error"
}
}
}
},
"/api/recipes/new": {
"post": {
"description": "",
Expand Down
30 changes: 0 additions & 30 deletions types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,36 +286,6 @@ export interface paths {
}
}
}
"/api/users/username": {
put: {
requestBody?: {
content: {
"application/json": {
/** @example any */
username?: unknown
}
}
}
responses: {
/** @description OK */
200: {
content: never
}
/** @description Bad Request */
400: {
content: never
}
/** @description Unauthorized */
401: {
content: never
}
/** @description Internal Server Error */
500: {
content: never
}
}
}
}
"/api/recipes/new": {
post: {
requestBody?: {
Expand Down

0 comments on commit d7c4523

Please sign in to comment.