11import * as S from './style'
22
33import React from 'react'
4- import userState from '@/context/userState'
5- import { useMutation , useQueryClient } from 'react-query'
6- import { useRecoilValue } from 'recoil'
74import { useRouter } from 'next/router'
8- import httpClient from '@/lib/httpClient'
9- import { Storage } from '@/lib/storage'
105import { CustomToastContainer } from '@/layout/HomeLayout.style'
116import { toast } from 'react-toastify'
12- import Swal from 'sweetalert2'
137import authority from '@/constants/authority.constants'
8+ import useUpdateTitleMutation from '@/features/UpdateTitleFeature'
9+ import useUpdateTypeMutation from '@/features/UpdateTypeFeature'
10+ import useUser from '@/hooks/useUser'
11+ import useDeleteDocsMutation from '@/features/DeleteDocsFeature'
12+ import Swal from 'sweetalert2'
1413
1514interface DetailBtnProps {
1615 docsId : number
1716}
1817
1918const DetailBtn = ( { docsId } : DetailBtnProps ) => {
2019 const router = useRouter ( )
21- const user = useRecoilValue ( userState )
20+ const { user, isLogined } = useUser ( )
2221 const [ docsName , setDocsName ] = React . useState ( '' )
2322 const [ docsType , setDocsType ] = React . useState ( '' )
24- const queryClient = useQueryClient ( )
25-
26- const updateDocsTitleMutation = useMutation ( ( ) => httpClient . updateTitle . putByTitle ( router . pathname , docsName ) , {
27- onSuccess : ( res ) => {
28- Swal . fire ( {
29- icon : 'success' ,
30- title : '문서 이름 변경 완료!' ,
31- } )
32- queryClient . invalidateQueries ( 'lastModifiedDocs' )
33- router . push ( `/docs/${ res . data . title } ` )
34- } ,
35- } )
36-
37- const onUpdateType = async ( ) => {
38- return (
39- await httpClient . updateType . put (
40- { id : docsId , docsType } ,
41- {
42- headers : {
43- Authorization : Storage . getItem ( 'access_token' ) ,
44- } ,
45- }
46- )
47- ) . data
48- }
49-
50- const updateDocsTypeMutation = useMutation ( onUpdateType , {
51- onSuccess : ( res ) => {
52- Swal . fire ( {
53- icon : 'success' ,
54- title : '문서 타입 변경 완료!' ,
55- } )
56- queryClient . invalidateQueries ( 'lastModifiedDocs' )
57- router . push ( `/docs/${ res . data . title } ` )
58- } ,
59- } )
6023
61- const onClickNavigatePage = ( type : string ) => {
62- if ( type === 'VERSION' ) return router . push ( `/version/${ router . query . title } ` )
63- if ( type === 'UPDATE' && ! user . id ) return alert ( '로그인 후 편집하실 수 있습니다!' )
64- router . push ( `/update/${ router . query . title } ` )
65- }
24+ const updateTitle = useUpdateTitleMutation ( docsName )
25+ const updateType = useUpdateTypeMutation ( docsId , docsType )
26+ const deleteDocs = useDeleteDocsMutation ( docsId )
6627
67- const onDeleteDocsTitle = async ( ) => {
68- return (
69- await httpClient . deleteDocs . deleteById ( docsId , {
70- headers : {
71- Authorization : Storage . getItem ( 'access_token' ) ,
72- } ,
73- } )
74- ) . data
28+ const onChangeTitle = async ( ) => {
29+ if ( ! docsName ) return toast . error ( '내용이 없습니다.' )
30+ updateTitle . mutate ( )
7531 }
7632
77- const deleteDocsTitleMutation = useMutation ( onDeleteDocsTitle , {
78- onSuccess : ( ) => {
79- Swal . fire ( {
80- icon : 'success' ,
81- title : '문서 삭제 완료!' ,
82- } )
83- router . push ( '/' )
84- } ,
85- } )
86-
87- const onClickChangeDocsName = async ( ) => {
88- if ( ! docsName . length ) return toast . error ( '내용이 없습니다.' )
89- updateDocsTitleMutation . mutate ( )
33+ const onChangeType = async ( ) => {
34+ if ( ! docsType ) return toast . error ( '내용이 없습니다.' )
35+ updateType . mutate ( )
9036 }
9137
92- const onClickChangeDocsType = async ( ) => {
93- if ( ! docsType . length ) return toast . error ( '내용이 없습니다.' )
94- updateDocsTypeMutation . mutate ( )
38+ const onDeleteDocs = ( ) => {
39+ Swal . fire ( {
40+ title : '문서를 정말 삭제하시겠습니까?' ,
41+ showDenyButton : true ,
42+ confirmButtonText : '네' ,
43+ denyButtonText : '아니오' ,
44+ } ) . then ( ( result ) => {
45+ if ( result . isConfirmed ) deleteDocs . mutate ( )
46+ } )
9547 }
9648
97- const onClickDeleteDocs = ( ) => {
98- if ( window . confirm ( '정말 삭제하시겠습니까?' ) ) deleteDocsTitleMutation . mutate ( )
49+ const onNavigatePage = ( type : string ) => {
50+ if ( type === 'VERSION' ) return router . push ( `/version/${ router . query . title } ` )
51+ if ( type === 'UPDATE' && ! isLogined ) return alert ( '로그인 후 편집하실 수 있습니다!' )
52+ router . push ( `/update/${ router . query . title } ` )
9953 }
10054
10155 return (
@@ -104,18 +58,18 @@ const DetailBtn = ({ docsId }: DetailBtnProps) => {
10458 { user . authority === authority . ADMIN ? (
10559 < >
10660 < S . DetailInput value = { docsType } onChange = { ( e ) => setDocsType ( e . target . value ) } />
107- < S . DetailWrap onClick = { onClickChangeDocsType } >
61+ < S . DetailWrap onClick = { onChangeType } >
10862 < S . DetailButton >
10963 < S . DetailText > 타입변경</ S . DetailText >
11064 </ S . DetailButton >
11165 </ S . DetailWrap >
112- < S . DetailWrap onClick = { onClickDeleteDocs } >
66+ < S . DetailWrap onClick = { onDeleteDocs } >
11367 < S . DetailButton >
11468 < S . DetailText > 삭제</ S . DetailText >
11569 </ S . DetailButton >
11670 </ S . DetailWrap >
11771 < S . DetailInput value = { docsName } onChange = { ( e ) => setDocsName ( e . target . value ) } />
118- < S . DetailWrap onClick = { onClickChangeDocsName } >
72+ < S . DetailWrap onClick = { onChangeTitle } >
11973 < S . DetailButton >
12074 < S . DetailText > 변경</ S . DetailText >
12175 </ S . DetailButton >
@@ -124,12 +78,12 @@ const DetailBtn = ({ docsId }: DetailBtnProps) => {
12478 ) : (
12579 ''
12680 ) }
127- < S . DetailLinkWrap onClick = { ( ) => onClickNavigatePage ( 'UPDATE' ) } >
81+ < S . DetailLinkWrap onClick = { ( ) => onNavigatePage ( 'UPDATE' ) } >
12882 < S . DetailButton >
12983 < S . DetailText > 편집</ S . DetailText >
13084 </ S . DetailButton >
13185 </ S . DetailLinkWrap >
132- < S . DetailLinkWrap onClick = { ( ) => onClickNavigatePage ( 'VERSION' ) } >
86+ < S . DetailLinkWrap onClick = { ( ) => onNavigatePage ( 'VERSION' ) } >
13387 < S . DetailButton >
13488 < S . DetailText > 기록</ S . DetailText >
13589 </ S . DetailButton >
0 commit comments