'use client' import { useState, useRef, useEffect } from 'react' import { useAxios } from '@/hooks/useAxios' import { useMessage } from '@/hooks/useMessage' export default function UserInfoModal({ userId, userInfoModal, setUserInfoModal }) { const { getMessage } = useMessage() // api 조회 관련 const { get, promisePatch, promisePost } = useAxios() const [info, setInfo] = useState({ userId: '', name: '', nameKana: '', category: '', tel: '', fax: '', mail: '', groupId: '', groupName: '', }) const [password, setPassword] = useState('') const [showPwd, setShowPwd] = useState(false) const [chkChgPwd, setChkChgPwd] = useState(false) const [chgPwd, setChgPwd] = useState('') const pwdInput = useRef() const chgPwdInput = useRef() useEffect(() => { async function fetchData() { const url = `/api/my-info/my-profile` const params = new URLSearchParams({ userId: userId }) const apiUrl = `${url}?${params.toString()}` const resultData = await get({ url: apiUrl }) if (resultData) { setInfo(resultData) } else { alert(getMessage('common.message.no.data')) } } if (userId) { fetchData() } }, []) const pattern = /^[\u0020-\u007E]{1,10}$/ // 비밀번호 변경 const handleChangePassword = async () => { if (chgPwd === '') { chgPwdInput.current.focus() return alert(getMessage('myinfo.message.validation.password4')) } if (password === chgPwd) { chgPwdInput.current.focus() return alert(getMessage('myinfo.message.validation.password2')) } if (!pattern.test(chgPwd)) { chgPwdInput.current.focus() return alert(getMessage('myinfo.message.validation.password3')) } const params = { loginId: info.userId, chgType: 'C', pwd: password, chgPwd: chgPwd } await promisePatch({ url: '/api/login/v1.0/user/change-password', data: params }) .then((res) => { if (res) { if (res.data.result.resultCode === 'S') { alert(getMessage('myinfo.message.save')) setChkChgPwd(false) setPassword(chgPwd) setChgPwd('') } else { alert(res.data.result.resultMsg) } } }) .catch((error) => { alert(error.response.data.message) }) } // 비밀번호 변경 버튼 클릭 시, const checkPasswordProcess = async (e) => { if (password === '') { pwdInput.current.focus() return alert(getMessage('myinfo.message.validation.password1')) } const param = { loginId: userId, pwd: password, } await promisePost({ url: '/api/login/v1.0/login', data: param }) .then((res) => { if (res) { if (res.data.result.resultCode === 'S') { setChkChgPwd(true) setTimeout(() => { chgPwdInput.current.focus() }, 10) } else { alert(getMessage('myinfo.message.password.error')) setChkChgPwd(false) setChgPwd('') setTimeout(() => { pwdInput.current.focus() }, 10) } } }) .catch((error) => { alert(error.response.data.message) }) } return ( <>
| {getMessage('myinfo.info.userId')} |
|
|---|---|
| {getMessage('myinfo.info.nameKana')} |
|
| {getMessage('myinfo.info.name')} |
|
| {getMessage('myinfo.info.password')} |
{
setPassword(e.target.value)
}}
/>
{getMessage('myinfo.sub.validation.password')}
|
| {getMessage('myinfo.info.chg.password')} * |
{
setChgPwd(e.target.value)
}}
/>
|
| {getMessage('myinfo.info.category')} |
|
| {getMessage('myinfo.info.tel')} |
|
| {getMessage('myinfo.info.fax')} |
|
| {getMessage('myinfo.info.mail')} |
|