Skip to content

Commit

Permalink
feat : 토큰 사용해서 팀 생성 api 연결 (#269)
Browse files Browse the repository at this point in the history
* feat : 토큰 사용해서 팀 생성 api 연결

#267

* feat : ok false 에러 해결

#267
  • Loading branch information
pipisebastian authored Jun 29, 2024
1 parent 29fa0f4 commit 438f06a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/app/api/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export const fetcher = (options?: FetcherOptions) => {

let fetchHeaders = {
...headers,
...(config?.headers || {}),
};

if (config?.body && typeof config.body === 'object') {
Expand All @@ -52,15 +51,26 @@ export const fetcher = (options?: FetcherOptions) => {
fetchHeaders = {};
}
}

fetchHeaders = {
...fetchHeaders,
...(config?.headers || {}),
};

const fullUrl = url.startsWith('http') ? url : `${baseUrl}${url}`;
let response = await fetch(fullUrl, {
...(config as RequestInit),
headers: fetchHeaders,
});

if (interceptors?.response) {
response = await interceptors.response(response);
}
return { ok: true, body: await response.json() };

if (response.status === 201) {
return { ok: true, body: { message: 'Created' } };
}
return { ok: true, body: await response?.json() };
} catch (error) {
let message = '';
if (error instanceof Error) message = error.message;
Expand Down
5 changes: 4 additions & 1 deletion src/app/api/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { fetcher } from './fetcher';

const teamFetcher = fetcher();

const postCreateTeam = (team: FormData) =>
const postCreateTeam = (token: string, team: FormData) =>
teamFetcher('/teams', {
method: 'POST',
body: team,
headers: {
Authorization: `Bearer ${token}`,
},
});

const putEditTeam = (teamId: number, teamInfo: Pick<Team, 'name' | 'description'>) =>
Expand Down
9 changes: 7 additions & 2 deletions src/containers/team/TeamModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BiEdit, BiFile } from 'react-icons/bi';
import { patchEditTeamImage, postCreateTeam, putEditTeam } from '@/app/api/team';
import IconBox from '@/components/IconBox';
import ActionModal from '@/components/Modal/ActionModal';
import { useMutateWithToken } from '@/hooks/useFetchWithToken';

import { TeamModalProps } from './type';

Expand All @@ -27,6 +28,8 @@ const TeamModal = ({ teamInfo, isOpen, onClose }: TeamModalProps) => {
const [alertName, setAlertName] = useState<boolean>(false);
const [alertDescription, setAlertDescription] = useState<boolean>(false);

const createTeam = useMutateWithToken(postCreateTeam);

const resetState = () => {
setName('');
setDescription('');
Expand Down Expand Up @@ -82,8 +85,10 @@ const TeamModal = ({ teamInfo, isOpen, onClose }: TeamModalProps) => {
teamForm.append('request', requestBlob);
teamForm.append('file', thumbnail as Blob);

postCreateTeam(teamForm).then(() => {
resetAndCloseModal();
createTeam(teamForm).then((res) => {
if (res.ok) {
resetAndCloseModal();
}
});
};

Expand Down

0 comments on commit 438f06a

Please sign in to comment.