diff --git a/components/modal/ModalProvider.tsx b/components/modal/ModalProvider.tsx index 25c925435..a38cbed0d 100644 --- a/components/modal/ModalProvider.tsx +++ b/components/modal/ModalProvider.tsx @@ -21,7 +21,7 @@ import styles from 'styles/modal/Modal.module.scss'; export default function ModalProvider() { const [ - { modalName, cancel, enroll, manual, announcement, exp, intraId }, + { modalName, cancel, enroll, manual, announcement, exp, intraId, userId }, setModal, ] = useRecoilState(modalState); const setReloadMatch = useSetRecoilState(reloadMatchState); @@ -38,7 +38,7 @@ export default function ModalProvider() { 'USER-PROFILE_EDIT': , 'FIXED-AFTER_GAME': , 'FIXED-STAT': , - 'ADMIN-PROFILE': intraId ? : null, + 'ADMIN-PROFILE': userId ? : null, 'ADMIN-PENALTY': intraId ? : null, 'ADMIN-NOTI_ALL': intraId ? : null, 'ADMIN-NOTI_USER': intraId ? : null, diff --git a/components/modal/admin/AdminProfileModal.tsx b/components/modal/admin/AdminProfileModal.tsx index 5c859e734..f80613286 100644 --- a/components/modal/admin/AdminProfileModal.tsx +++ b/components/modal/admin/AdminProfileModal.tsx @@ -1,83 +1,105 @@ -import { useSetRecoilState } from 'recoil'; +import Image from 'next/image'; import { useEffect, useState } from 'react'; -import styles from 'styles/admin/modal/AdminProfile.module.scss'; +import { useSetRecoilState } from 'recoil'; +import { + AdminProfileProps, + roleTypes, + UserInfo, +} from 'types/admin/adminUserTypes'; +import { racketTypes } from 'types/userTypes'; import { modalState } from 'utils/recoil/modal'; import instance from 'utils/axios'; -import Image from 'next/image'; -import axios from 'axios'; import useUploadImg from 'hooks/useUploadImg'; -import { Props, roleTypes, UserInfo } from 'types/admin/adminUserTypes'; -import { racketTypes } from 'types/userTypes'; +import styles from 'styles/admin/modal/AdminProfile.module.scss'; +import { errorState } from 'utils/recoil/error'; + +const STAT_MSG_LIMIT = 30; + +export default function AdminProfileModal(props: AdminProfileProps) { + const [userInfo, setUserInfo] = useState({ + intraId: '', + userImageUri: null, + statusMessage: '', + racketType: 'shakeHand', + wins: 0, + losses: 0, + ppp: 0, + eMail: '', + roleType: 'ROLE_USER', + }); -export default function AdminProfileModal(props: Props) { - const [userInfo, setUserInfo] = useState(); const { imgData, imgPreview, uploadImg } = useUploadImg(); const setModal = useSetRecoilState(modalState); + const setError = useSetRecoilState(errorState); useEffect(() => { getBasicProfileHandler(); }, []); const getBasicProfileHandler = async () => { - const res = await fetch(`http://localhost:3000/api/admin/users`); //admin/users/{props.intraId}/detail - const data = await res.json(); - setUserInfo(data[0]); + try { + const res = await instance.get( + `/pingpong/admin/users/${props.value}/detail` + ); + setUserInfo(res?.data); + } catch (e) { + setError('SW01'); + } }; const inputHandler = ({ target: { name, value }, + }: + | React.ChangeEvent + | React.ChangeEvent) => { + if (name === 'statusMessage' && value.length > STAT_MSG_LIMIT) return; + setUserInfo({ ...userInfo, [name]: value }); + }; + + const inputNumHandler = ({ + target: { name, value }, }: React.ChangeEvent) => { - if (name === 'wins' || name === 'losses' || name === 'ppp') { - const intValue = parseInt(value); - setUserInfo((prev: any) => ({ - ...prev, - [name]: intValue, - })); - } else { - setUserInfo((prev: any) => ({ - ...prev, - [name]: value, - })); - } + const intValue = parseInt(value); + setUserInfo({ ...userInfo, [name]: intValue }); }; const submitHandler = async () => { - //const formData = new FormData(); - console.log(userInfo); - // const data = { - // userId: userInfo.userId, - // intraId: userInfo.intraId, - // statusMessage: userInfo.statusMessage, - // racketType: userInfo.racketType, - // wins: userInfo.wins, - // losses: userInfo.losses, - // ppp: userInfo.ppp, - // eMail: userInfo.eMail, - // roleType: userInfo.roleType, - // }; - //formData.append('files', imgData); - // formData.append( - // 'data', - // new Blob([JSON.stringify(data)], { - // type: 'application/json', - // }) - // ); - // await instance.put(`/admin/users/${userInfo.userId}`, e); - // try { - // const result = await axios.put(`/admin/users/daijeong/detail`); - // console.log(result); - // } catch (error) { - // console.log(error); - // } + const formData = new FormData(); + const data = { + userId: props.value, + intraId: userInfo.intraId, + statusMessage: userInfo.statusMessage, + racketType: userInfo.racketType, + wins: userInfo.wins, + losses: userInfo.losses, + ppp: userInfo.ppp, + eMail: userInfo.eMail, + roleType: userInfo.roleType, + }; + formData.append( + 'updateRequestDto', + new Blob([JSON.stringify(data)], { + type: 'application/json', + }) + ); + formData.append( + 'multipartFile', + new Blob([imgData as File], { type: 'image/jpeg' }) + ); + + try { + await instance.put( + `/pingpong/admin/users/${props.value}/detail`, + formData + ); + } catch (e) { + setError('SW02'); + } }; return ( <> -
+
회원 정보 수정
@@ -87,8 +109,9 @@ export default function AdminProfileModal(props: Props) {