Skip to content

Commit

Permalink
refactor: 멤버 속성 받기 (#60)
Browse files Browse the repository at this point in the history
Co-authored-by: 조준호 <[email protected]>
  • Loading branch information
dassasa and 조준호 authored May 26, 2024
1 parent cbf6a0d commit 26f376c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
62 changes: 47 additions & 15 deletions src/Components/Buttons/AddMemberButton.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react'
import { addMember } from '../../apis/members'
import React, { useState, useEffect } from 'react'
import { addMember, getMember } from '../../apis/members'
import { Button, Modal, Input } from 'antd'
import styled from 'styled-components'
import PropTypes from 'prop-types'
Expand All @@ -19,19 +19,50 @@ const StyledAddButton = styled(Button)`

const AddMember = ({ groupId }) => {
const [isModalOpen, setIsModalOpen] = useState(false)
const [name, setName] = useState('')
const [studentId, setStudentId] = useState('')
const [email, setEmail] = useState('')
const [phoneNumber, setPhoneNumber] = useState('')
const [loading, setLoading] = useState(false)
const [formData, setFormData] = useState({})
const [propNames, setPropNames] = useState([])
const [name, setName] = useState('')

console.log('propNames:', propNames)

useEffect(() => {
const fetchMemberData = async () => {
try {
const data = await getMember(groupId)
const memberInfo = data.memberInfo || {}
console.log('memberInfo:', memberInfo)
const initialFormData = {}
const propNamesArray = [...memberInfo]
propNamesArray.forEach((propName) => {
initialFormData[propName] = memberInfo[propName] || ''
})

setFormData(initialFormData)
setPropNames(propNamesArray)
} catch (error) {
console.error('Failed to fetch member data:', error)
}
}
fetchMemberData()
}, [groupId])

const isAnyFieldEmpty = () => {
return !name || !studentId || !email || !phoneNumber
return propNames.some((propName) => !formData[propName])
}

const handleInputChange = (e, propName) => {
setFormData({
...formData,
[propName]: e.target.value,
})
}

console.log('formData:', formData)

const handleAddMember = async () => {
setLoading(true)
await addMember(groupId, name, { studentId, email, phoneNumber })
await addMember(groupId, name, formData)
setLoading(false)
setIsModalOpen(false)
alert('회원 추가가 완료되었습니다.')
Expand All @@ -52,13 +83,14 @@ const AddMember = ({ groupId }) => {
okButtonProps={{ disabled: isAnyFieldEmpty() }}
>
<StyledInput placeholder="이름" value={name} onChange={(e) => setName(e.target.value)} />
<StyledInput placeholder="학번" value={studentId} onChange={(e) => setStudentId(e.target.value)} />
<StyledInput placeholder="이메일" value={email} onChange={(e) => setEmail(e.target.value)} />
<StyledInput
placeholder="전화번호"
value={phoneNumber}
onChange={(e) => setPhoneNumber(e.target.value)}
/>
{propNames.map((propName) => (
<StyledInput
key={propName}
placeholder={propName}
value={formData[propName]}
onChange={(e) => handleInputChange(e, propName)}
/>
))}
</Modal>
</>
)
Expand Down
2 changes: 0 additions & 2 deletions src/Components/Tables/MemberList.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ const MemberList = ({ groupId }) => {
})
}, [groupId])


useEffect(() => {
const keys = Object.keys(members[0]?.memberInfo || {})
setMemberKeys(keys)
console.log(keys)
}, [members])

const columns = [
Expand Down
2 changes: 0 additions & 2 deletions src/apis/members.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,3 @@ export const uploadMember = async (groupId, file) => {
const response = await formApi.post(`group/${groupId}/member/excel`, formData)
return response.data
}


0 comments on commit 26f376c

Please sign in to comment.