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

サインアウトモーダルの作成 #76

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
7 changes: 7 additions & 0 deletions backend/api/controller/auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func (ac AuthController) SignIn(c *gin.Context) {
})
}

func (ac AuthController) SignUp(c *gin.Context) {
c.JSON(200, map[string]string{
"status": "success",
})
}


func (ac AuthController) SignOut(c *gin.Context) {
c.SetCookie("token", "", -1, "/", config.GetDomainName(), config.GetIsSecured(), true)
c.Status(200)
Expand Down
14 changes: 14 additions & 0 deletions backend/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ func SetupRoutes(i *do.Injector, app *gin.Engine) error {
return err
}

// signUpOpe /signup POST
app.POST("/signup", authController.SignUp)
signUpOpe, _ := appDoc.NewOperationContext(http.MethodPost, "/signup")
signUpOpe.SetID("signUp")
signUpOpe.SetSummary("ユーザのアカウント登録を実行")
signUpOpe.SetTags("auth")
signUpOpe.AddReqStructure(new(schema.SignupRequest))
signUpOpe.AddRespStructure(new(schema.UserResponse), openapi.WithHTTPStatus(http.StatusOK))
signUpOpe.AddRespStructure(new(schema.ErrorResponse), openapi.WithHTTPStatus(http.StatusBadRequest))
signUpOpe.AddRespStructure(new(schema.ErrorResponse), openapi.WithHTTPStatus(http.StatusInternalServerError))
if err := appDoc.AddOperation(signUpOpe); err != nil {
return err
}

// createPostComments /posts/{postId}/comments POST
authRequired.PUT("/posts/:postid/comments/:commentId", postController.UpdateComment)
putCommnetsOpe, _ := appDoc.NewOperationContext(http.MethodPost, "posts/{postId}/comments/{commentId}")
Expand Down
5 changes: 5 additions & 0 deletions backend/api/schema/auth_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ type LoginRequest struct {
UserName string `json:"user_name"`
Password string `json:"password"`
}

type SignupRequest struct {
UserName string `json:"user_name"`
Password string `json:"password"`
}
37 changes: 37 additions & 0 deletions docs/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,36 @@ paths:
summary: ユーザのログアウトを実行
tags:
- auth
/signup:
post:
operationId: signUp
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/SchemaSignupRequest'
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/SchemaUserResponse'
description: OK
"400":
content:
application/json:
schema:
$ref: '#/components/schemas/SchemaErrorResponse'
description: Bad Request
"500":
content:
application/json:
schema:
$ref: '#/components/schemas/SchemaErrorResponse'
description: Internal Server Error
summary: ユーザのアカウント登録を実行
tags:
- auth
/user:
get:
operationId: getCurrentUser
Expand Down Expand Up @@ -576,6 +606,13 @@ components:
user_name:
type: string
type: object
SchemaSignupRequest:
properties:
password:
type: string
user_name:
type: string
type: object
SchemaUpdateCommentRequest:
properties:
body:
Expand Down
1 change: 1 addition & 0 deletions frontend/src/api/hooks.msw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ export const getWeb6APIMock = () => [
getCreatePostMockHandler(),
getSignInMockHandler(),
getSignOutMockHandler(),
getSignUpMockHandler(),
getGetCurrentUserMockHandler(),
getGetPostMockHandler(),
getListPostCommentsMockHandler(),
Expand Down
59 changes: 59 additions & 0 deletions frontend/src/api/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
SchemaLoginRequest,
SchemaMutationSchema,
SchemaPostResponse,
SchemaSignupRequest,
SchemaUpdateCommentRequest,
SchemaUpdatePostRequest,
SchemaUserResponse
Expand Down Expand Up @@ -266,6 +267,64 @@ export const useSignOut = <TError = SchemaErrorResponse, TContext = unknown>(opt
return useMutation(mutationOptions)
}

/**
* @summary ユーザのアカウント登録を実行
*/
export const signUp = (
schemaSignupRequest: SchemaSignupRequest,
options?: SecondParameter<typeof customInstance>,) => {


return customInstance<SchemaUserResponse>(
{url: `/signup`, method: 'POST',
headers: {'Content-Type': 'application/json', },
data: schemaSignupRequest
},
options);
}



export const getSignUpMutationOptions = <TError = SchemaErrorResponse,
TContext = unknown>(options?: { mutation?:UseMutationOptions<Awaited<ReturnType<typeof signUp>>, TError,{data: SchemaSignupRequest}, TContext>, request?: SecondParameter<typeof customInstance>}
): UseMutationOptions<Awaited<ReturnType<typeof signUp>>, TError,{data: SchemaSignupRequest}, TContext> => {
const {mutation: mutationOptions, request: requestOptions} = options ?? {};




const mutationFn: MutationFunction<Awaited<ReturnType<typeof signUp>>, {data: SchemaSignupRequest}> = (props) => {
const {data} = props ?? {};

return signUp(data,requestOptions)
}




return { mutationFn, ...mutationOptions }}

export type SignUpMutationResult = NonNullable<Awaited<ReturnType<typeof signUp>>>
export type SignUpMutationBody = SchemaSignupRequest
export type SignUpMutationError = SchemaErrorResponse

/**
* @summary ユーザのアカウント登録を実行
*/
export const useSignUp = <TError = SchemaErrorResponse,
TContext = unknown>(options?: { mutation?:UseMutationOptions<Awaited<ReturnType<typeof signUp>>, TError,{data: SchemaSignupRequest}, TContext>, request?: SecondParameter<typeof customInstance>}
): UseMutationResult<
Awaited<ReturnType<typeof signUp>>,
TError,
{data: SchemaSignupRequest},
TContext
> => {

const mutationOptions = getSignUpMutationOptions(options);

return useMutation(mutationOptions);
}

/**
* @summary 現在ログインしているユーザを取得
*/
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/api/model/schemaSignupRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Generated by orval v6.30.2 🍺
* Do not edit manually.
* Web開発研修6班 API
* FY24卒Web開発研修6班のAPI仕様書です
* OpenAPI spec version: 1.0.0
*/

export interface SchemaSignupRequest {
password?: string;
user_name?: string;
}
50 changes: 50 additions & 0 deletions frontend/src/app/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useEffect, useState } from 'react'
import { Box, Button, Loading, Text, useDisclosure } from '@yamada-ui/react'
import { useNavigate } from 'react-router-dom'
import { usePostSignout, useGetUser } from '../../api/api'
import { SignoutModal } from './SignoutModal'

export const Header = () => {
const navigate = useNavigate()
const { data: user, isError, refetch, isFetching } = useGetUser()
const [currentUser, setCurrentUser] = useState(user)
const { isOpen, onOpen, onClose } = useDisclosure()

const { mutate: signoutMutate } = usePostSignout({
mutation: {
onSuccess: () => {
setCurrentUser(undefined)
navigate('/signin')
}
}
})

useEffect(() => {
if (isError) {
setCurrentUser(undefined)
} else {
setCurrentUser(user)
}
}, [user, isError])

return (
<>
<header className="app-header">
サンプルアプリケーション
<Box display="flex" alignItems="center">
{isFetching ? (
<Loading variant="circles" size="6xl" color="cyan.500" />
) : isError || !currentUser ? (
<Button onClick={() => signoutMutate()}>サインイン</Button>
) : (
<>
<Text mr={2}>{currentUser.user_name}</Text>
<Button onClick={onOpen}>サインアウト</Button>
</>
)}
</Box>
</header>
<SignoutModal isOpen={isOpen} onClose={onClose} signoutMutate={signoutMutate} refetch={refetch} />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SignoutModalとして切り出し

</>
)
}
39 changes: 39 additions & 0 deletions frontend/src/app/Header/SignoutModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Button, Center, Divider, Modal, ModalBody, ModalFooter, ModalHeader } from '@yamada-ui/react'
import { Link } from 'react-router-dom'

interface props {
isOpen: boolean
onClose: () => void
signoutMutate: () => void
refetch: () => void
}

export const SignoutModal = ({ isOpen, onClose, signoutMutate, refetch }: props) => {
return (
<Modal isOpen={isOpen} onClose={onClose}>
<Center>
<ModalHeader>警告</ModalHeader>
</Center>
<Divider variant="solid" my={2} />
<ModalBody>サインアウトを行います。よろしいですか?</ModalBody>
<Divider variant="solid" my={2} />
<ModalFooter>
<Button variant="ghost" onClick={onClose}>
とじる
</Button>
<Link to="/">
<Button
onClick={() => {
signoutMutate()
refetch()
onClose()
}}
colorScheme="primary"
>
サインアウト
</Button>
</Link>
</ModalFooter>
</Modal>
)
}
1 change: 1 addition & 0 deletions frontend/src/app/Header/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Header } from './Header'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Headerをインポートするときに ./Header/Header ってなるのが嫌だったので。

38 changes: 17 additions & 21 deletions frontend/src/pages/posts/detail/PostDetailRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,24 @@ export const PostDetailRoute = () => {
<Button onClick={onOpen} colorScheme="primary">
削除
</Button>
<>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここの<>が要らないので削除

<Modal isOpen={isOpen} onClose={onClose}>
<Center>
<ModalHeader>警告</ModalHeader>
</Center>
<Divider variant="solid" />
<ModalBody>削除したら元に戻せません。削除しますか?</ModalBody>
<Divider variant="solid" />
<ModalFooter>
<Button variant="ghost" onClick={onClose}>
とじる
<Modal isOpen={isOpen} onClose={onClose}>
<Center>
<ModalHeader>警告</ModalHeader>
</Center>
<Divider variant="solid" my={2} />
<ModalBody>削除したら元に戻せません。削除しますか?</ModalBody>
<Divider variant="solid" my={2} />
<ModalFooter>
<Button variant="ghost" onClick={onClose}>
とじる
</Button>
<Link to="/">
<Button onClick={handleDelete} colorScheme="primary">
削除
</Button>
<>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここも

<Link to="/">
<Button onClick={handleDelete} colorScheme="primary">
削除
</Button>
</Link>
</>
</ModalFooter>
</Modal>
</>
</Link>
</ModalFooter>
</Modal>
</>
) : null}
</HStack>
Expand Down
Loading