Skip to content

Commit

Permalink
fix: requestPost에서 contentType 분기 삭제로 인해 에러 발생한 것을 분기 추가하면서해결
Browse files Browse the repository at this point in the history
  • Loading branch information
soi-ha committed Aug 8, 2024
1 parent 5d959cc commit c18797c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
11 changes: 9 additions & 2 deletions client/src/apis/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,22 @@ export const requestPut = ({headers = {}, ...args}: RequestProps) => {
export const requestPost = async <T>({headers = {}, ...args}: RequestProps): Promise<T> => {
const response = await fetcher({method: 'POST', headers, ...args});

const data: T = await response!.json();
return data;
const contentType = response!.headers.get('Content-Type');
if (contentType && contentType.includes('application/json')) {
const data: T = await response!.json();
return data;
}

return;
};

export const requestDelete = ({headers = {}, ...args}: RequestProps) => {
return fetcher({method: 'DELETE', headers, ...args});
};

const fetcher = ({baseUrl = API_BASE_URL, method, endpoint, headers, body, queryParams}: FetcherProps) => {
console.log('fetcher');
console.log(JSON.stringify(body));
// const token = generateBasicToken(USER_ID, USER_PASSWORD);
const options = {
method,
Expand Down
2 changes: 1 addition & 1 deletion client/src/apis/request/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {TEMP_PREFIX} from '@apis/tempPrefix';
import {requestGet, requestPost} from '@apis/fetcher';
import {WithEventId} from '@apis/withEventId.type';

type RequestPostNewEvent = {
export type RequestPostNewEvent = {
eventName: string;
password: number;
};
Expand Down
6 changes: 3 additions & 3 deletions client/src/hooks/useEvent.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {ResponsePostNewEvent, requestPostNewEvent} from '@apis/request/event';
import {RequestPostNewEvent, ResponsePostNewEvent, requestPostNewEvent} from '@apis/request/event';

import {useFetch} from '@apis/useFetch';

const useEvent = () => {
const {fetch} = useFetch();

const createNewEvent = async ({eventName}: {eventName: string}) => {
return await fetch<ResponsePostNewEvent>(() => requestPostNewEvent({eventName}));
const createNewEvent = async ({eventName, password}: RequestPostNewEvent) => {
return await fetch<ResponsePostNewEvent>(() => requestPostNewEvent({eventName, password}));
};

return {createNewEvent};
Expand Down
1 change: 0 additions & 1 deletion client/src/pages/CreateEventPage/SetEventNamePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const SetEventNamePage = () => {

const submitEventName = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const {eventId} = await createNewEvent({eventName});

navigate(ROUTER_URLS.eventCreatePassword, {state: {eventName}});
};
Expand Down
14 changes: 5 additions & 9 deletions client/src/pages/CreateEventPage/SetEventPasswordPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {FixedButton, MainLayout, LabelInput, Title, TopNav, Back} from 'haengdon
import validateEventPassword from '@utils/validate/validateEventPassword';
import {requestPostNewEvent} from '@apis/request/event';

import useEvent from '@hooks/useEvent';

import RULE from '@constants/rule';
import {ROUTER_URLS} from '@constants/routerUrls';

Expand All @@ -13,7 +15,7 @@ const SetEventPasswordPage = () => {
const [password, setPassword] = useState('');
const [errorMessage, setErrorMessage] = useState('');
const [canSubmit, setCanSubmit] = useState(false);

const {createNewEvent} = useEvent();
const navigate = useNavigate();
const location = useLocation();

Expand All @@ -28,15 +30,9 @@ const SetEventPasswordPage = () => {
const submitPassword = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

const response = await requestPostNewEvent({eventName, password: parseInt(password)});
const {eventId} = await createNewEvent({eventName, password: parseInt(password)});

if (response) {
const {eventId} = response;
navigate(`${ROUTER_URLS.eventCreateComplete}?${new URLSearchParams({eventId})}`);
} else {
// TODO: (@weadie)
alert('오류님');
}
navigate(`${ROUTER_URLS.eventCreateComplete}?${new URLSearchParams({eventId})}`);
};

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand Down

0 comments on commit c18797c

Please sign in to comment.