-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] QFEED-117 토론방 생성 페이지 api 연동 (#60)
* feat: qfeed-117 취미 카테고리 type 변경 (string->number) * feat: qfeed-117 토론방 생성 페이지 api 연동 * refactor: qfeed-117 formatlastupdated 함수 수정
- Loading branch information
Showing
13 changed files
with
255 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { apiClient } from '@/api/fetch'; | ||
import { UploadResponse } from '@/pages/QSpace/types/group'; | ||
|
||
export const uploadAPI = { | ||
uploadImage: (file: File) => { | ||
const formData = new FormData(); | ||
formData.append('file', file); | ||
|
||
return apiClient.post<UploadResponse>('/upload/image', formData, { | ||
headers: { | ||
'Content-Type': 'multipart/form-data', | ||
}, | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useState } from 'react'; | ||
import { useLocation, useNavigate } from 'react-router-dom'; | ||
import { useToast } from '@chakra-ui/react'; | ||
import { UseGroupFormReturn, GroupFormData } from '../types/group'; | ||
|
||
export const useGroupForm = (): UseGroupFormReturn => { | ||
const location = useLocation(); | ||
const navigate = useNavigate(); | ||
const toast = useToast(); | ||
const { categoryId } = location.state as { categoryId: number }; | ||
|
||
const [title, setTitle] = useState(''); | ||
const [description, setDescription] = useState(''); | ||
const [imageFile, setImageFile] = useState<File | null>(null); | ||
const [isPending, setIsPending] = useState(false); | ||
|
||
const formData: GroupFormData = { | ||
title, | ||
description, | ||
imageFile, | ||
categoryId, | ||
}; | ||
|
||
const formActions = { | ||
setTitle, | ||
setDescription, | ||
setImageFile, | ||
}; | ||
|
||
const formState = { | ||
isPending, | ||
setIsPending, | ||
}; | ||
|
||
return { | ||
formData, | ||
formActions, | ||
formState, | ||
toast, | ||
navigate, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { groupAPI } from '@/pages/QSpace/api/fetchGroups'; | ||
import { CreateGroupParams, CreateGroupRequest } from '@/pages/QSpace/types/group'; | ||
import { showErrorToast, showSuccessToast, uploadImage } from '@/pages/QSpace/utils/uploadImage'; | ||
|
||
export const createGroup = async ({ | ||
formData, | ||
setIsPending, | ||
toast, | ||
navigate, | ||
}: CreateGroupParams) => { | ||
const { title, description, imageFile, categoryId } = formData; | ||
|
||
if (!title || !description) { | ||
toast({ | ||
title: '입력 확인', | ||
description: '방 제목과 설명을 입력해주세요.', | ||
status: 'warning', | ||
duration: 3000, | ||
isClosable: true, | ||
}); | ||
return; | ||
} | ||
|
||
try { | ||
setIsPending(true); | ||
const createGroupData: Partial<CreateGroupRequest> = { | ||
groupName: title, | ||
description, | ||
categoryId, | ||
isOpen: true, | ||
}; | ||
|
||
if (imageFile) { | ||
const imageUrl = await uploadImage(imageFile, toast); | ||
if (imageUrl) createGroupData.url = imageUrl; | ||
} | ||
|
||
await groupAPI.createGroup(createGroupData as CreateGroupRequest); | ||
showSuccessToast(toast); | ||
navigate('/qspace'); | ||
} catch (error) { | ||
showErrorToast(toast, error); | ||
} finally { | ||
setIsPending(false); | ||
} | ||
}; |
Oops, something went wrong.