-
Notifications
You must be signed in to change notification settings - Fork 4
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
[DEV] 이미지 데이터 전송 api #41
base: main
Are you sure you want to change the base?
Changes from all commits
5833133
a19f962
0a0edb6
743f46e
ab52197
9455ea9
1a54144
7d45c62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
export const POST = async (request: NextRequest) => { | ||
const { headers } = request; | ||
|
||
const body = await request.json(); | ||
|
||
const response = await fetch( | ||
`${process.env.NEXT_PUBLIC_SERVER_ADDRESS}:${process.env.NEXT_PUBLIC_SERVER_PORT}/api/photo-request`, | ||
{ | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify(body), | ||
}, | ||
); | ||
const jsonData = await response.json(); | ||
return NextResponse.json(jsonData, { | ||
status: jsonData.code, | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,85 @@ | ||
'use client'; | ||
|
||
import { useAtom, useSetAtom } from 'jotai'; | ||
import dynamic from 'next/dynamic'; | ||
import Image from 'next/image'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useState } from 'react'; | ||
import { CompoundModal } from '@/components/Modal/ModalMain'; | ||
import PreviousPage from '@/components/PreviousPage'; | ||
import { ENTER_EMAIL_TITLE } from '@/constants'; | ||
import { | ||
ENTER_EMAIL_TITLE, | ||
GENDER_FEMALE, | ||
GENDER_MALE, | ||
IMG_GENERATED_ERROR_CHECK_MSG, | ||
IMG_GENERATED_ERROR_MSG, | ||
} from '@/constants'; | ||
import { ROUTE_TYPES } from '@/interfaces'; | ||
import { | ||
errorCheckMessageAtom, | ||
errorMessageAtom, | ||
} from '@/store/atoms/errorMessageAtom'; | ||
import { imageUrlsAtom } from '@/store/atoms/imageUrlAtom'; | ||
import { selectedBoxAtom } from '@/store/atoms/selectedBoxAtom'; | ||
import useModal from '../hooks/useModal'; | ||
|
||
export default function EmailEnterView() { | ||
export function EmailEnterView() { | ||
const router = useRouter(); | ||
const [email, setEmail] = useState(''); | ||
const [isEmailValid, setIsEmailValid] = useState(false); | ||
const [genderString] = useAtom(selectedBoxAtom); | ||
const [photoOriginUrls] = useAtom(imageUrlsAtom); | ||
const { isOpen, handleOpenModal, handleCloseModal } = useModal(); | ||
const setErrorMessage = useSetAtom(errorMessageAtom); | ||
const setErrorCheckMessage = useSetAtom(errorCheckMessageAtom); | ||
const storedToken = window.sessionStorage.getItem('accessToken') || ''; | ||
const gender = genderString === 'female' ? GENDER_FEMALE : GENDER_MALE; | ||
const handleEmailEntered = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const emailValue = e.target.value; | ||
setEmail(emailValue); | ||
|
||
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
setIsEmailValid(emailPattern.test(emailValue)); | ||
}; | ||
const submitPhotoData = async () => { | ||
if (storedToken !== '') { | ||
try { | ||
const response = await fetch(`/api/email?code=${storedToken}`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${storedToken}`, | ||
}, | ||
body: JSON.stringify({ | ||
email: email || null, | ||
gender, | ||
photoOriginUrls, | ||
}), | ||
}); | ||
|
||
const handleEmailSubmit = (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
if (isEmailValid) { | ||
router.push(ROUTE_TYPES.WAITING); | ||
if (response.status === 200) { | ||
router.push(ROUTE_TYPES.WAITING); | ||
} else { | ||
setErrorMessage(IMG_GENERATED_ERROR_MSG); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 코드에서는 에러 메시지는 잘 작성하셨는데 정작 중요한 에러 페이지로 라우팅이 되지 않는것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어웃 그러네요 감사합니다! |
||
setErrorCheckMessage(IMG_GENERATED_ERROR_CHECK_MSG); | ||
router.push(ROUTE_TYPES.ERROR); | ||
} | ||
} catch (error) { | ||
setErrorMessage(IMG_GENERATED_ERROR_MSG); | ||
setErrorCheckMessage(IMG_GENERATED_ERROR_CHECK_MSG); | ||
router.push(ROUTE_TYPES.ERROR); | ||
} | ||
} | ||
}; | ||
|
||
const handleMovePage = () => { | ||
const handleEmailSubmit = async (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
await submitPhotoData(); | ||
}; | ||
|
||
const handleMovePage = async () => { | ||
router.push(ROUTE_TYPES.WAITING); | ||
await submitPhotoData(); | ||
}; | ||
|
||
return ( | ||
|
@@ -105,3 +154,6 @@ export default function EmailEnterView() { | |
</div> | ||
); | ||
} | ||
export default dynamic(() => Promise.resolve(EmailEnterView), { | ||
ssr: false, | ||
}); | ||
Comment on lines
+157
to
+159
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 동적 import를 위해서 사용한 부분일까요?! 왜 이렇게 하셨는지 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저번 PR 내용에도 있던 부분인데요,
이 코드를 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 계속 의문이였던게 왜 storedToken을 계속 세션 스토리지에서 불러오는지 궁금했습니다. useEffect(() => {
if (storedToken !== '') {
fetch(
`${process.env.NEXT_PUBLIC_CLIENT_ADDRESS}:${process.env.NEXT_PUBLIC_CLIENT_PORT}/api/validate?code=${storedToken}`,
)
.then((response) => response.json())
.then(async (data) => {
if (data.loginSuccess === false) {
const reissueResponse = await fetch(
`${process.env.NEXT_PUBLIC_CLIENT_ADDRESS}:${process.env.NEXT_PUBLIC_CLIENT_PORT}/api/reissue?code=${storedToken}`,
);
if (reissueResponse.ok) {
const reissueData = await reissueResponse.json();
setInsertToken(reissueData.accessToken);
} else {
setInsertToken('');
setErrorMessage(LOGIN_ERROR_MSG);
setErrorCheckMessage(LOGIN_ERROR_CHECK_MSG);
router.push(ROUTE_TYPES.ERROR);
}
}
})
.catch(() => {
setInsertToken('');
setErrorMessage(LOGIN_ERROR_MSG);
setErrorCheckMessage(LOGIN_ERROR_CHECK_MSG);
router.push(ROUTE_TYPES.ERROR);
});
} else {
setInsertToken(storedToken);
}
}, [ 맨 마지막에 storedToken을 atom에 넣기 때문에 이후에는 세션 객체를 직접 접근할 필요가 없어서 lazy loading이 필요없습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 이슈 파두었습니다 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const GENDER_MALE = 0; | ||
export const GENDER_FEMALE = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stringify를 실행하는 이유가 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON.stringify
를 안쓰면 에러가 났었는데 찾아보니까 JavaScript의 fetch API나 다른 네트워크 요청 라이브러리는 요청 본문을 문자열로 전송해야 한다고 하네요!아니면 혹시 다른 방법이 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아아 fetch 메서드에서는 객체를 직접 답을 수 없었네요. 매번 axios를 사용하다보니 잊고있었네요! 그대로 사용하면 될것 같습니다!