-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Team-INSERT/refactor/api
Refactor/api
- Loading branch information
Showing
34 changed files
with
338 additions
and
252 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const queryKey = { | ||
getLastModify: 'getLastModify', | ||
} | ||
|
||
export default queryKey |
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,24 @@ | ||
import httpClient from '@/lib/httpClient' | ||
import { useMutation } from 'react-query' | ||
import Swal from 'sweetalert2' | ||
|
||
interface AuthorityApiProps { | ||
email: string | ||
authority: string | ||
} | ||
|
||
const onAuthority = async ({ email, authority }: AuthorityApiProps) => { | ||
return (await httpClient.authority.put({ email, authority })).data | ||
} | ||
|
||
const useAuthorityMutation = () => { | ||
return useMutation(onAuthority, { | ||
onSuccess: () => | ||
Swal.fire({ | ||
icon: 'success', | ||
title: '유저 권한이 변경되었습니다!', | ||
}), | ||
}) | ||
} | ||
|
||
export default useAuthorityMutation |
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,47 @@ | ||
import { IFileTypes } from '@/components/DragDrop' | ||
import httpClient from '@/lib/httpClient' | ||
import { encodeContents } from '@/utils/document/requestContents' | ||
import { useRouter } from 'next/router' | ||
import { useMutation } from 'react-query' | ||
import Swal from 'sweetalert2' | ||
|
||
interface CreateDocsFormType { | ||
title: string | ||
enroll: number | ||
contents: string | ||
docsType: string | ||
files: IFileTypes[] | ||
} | ||
|
||
const createDocsForm = ({ title, enroll, contents, docsType, files }: CreateDocsFormType) => { | ||
const data = new FormData() | ||
data.append( | ||
'request', | ||
new Blob([`{ "title": "${title}", "enroll":"${enroll}", "contents":"${encodeContents(contents)}", "docsType":"${docsType}"}`], { | ||
type: 'application/json', | ||
}) | ||
) | ||
files.reverse().forEach((file) => data.append('files', file.object, file.object.name)) | ||
return data | ||
} | ||
|
||
const onCreateDocs = async (data: CreateDocsFormType) => { | ||
const formData = createDocsForm(data) | ||
return (await httpClient.create.post(formData)).data | ||
} | ||
|
||
const useCreateDocsMutation = () => { | ||
const router = useRouter() | ||
|
||
return useMutation(onCreateDocs, { | ||
onSuccess: (data) => { | ||
Swal.fire({ | ||
icon: 'success', | ||
title: '문서 생성 완료!', | ||
}) | ||
router.push(`/docs/${data.title}`) | ||
}, | ||
}) | ||
} | ||
|
||
export default useCreateDocsMutation |
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,20 @@ | ||
import httpClient from '@/lib/httpClient' | ||
import { useRouter } from 'next/router' | ||
import { useMutation } from 'react-query' | ||
import Swal from 'sweetalert2' | ||
|
||
const useDeleteDocsMutation = (docsId: number) => { | ||
const router = useRouter() | ||
|
||
return useMutation(async () => (await httpClient.deleteDocs.deleteById(docsId, {})).data, { | ||
onSuccess: () => { | ||
Swal.fire({ | ||
icon: 'success', | ||
title: '문서 삭제 완료!', | ||
}) | ||
router.push('/') | ||
}, | ||
}) | ||
} | ||
|
||
export default useDeleteDocsMutation |
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,31 @@ | ||
import httpClient from '@/lib/httpClient' | ||
import { Storage } from '@/lib/storage' | ||
import { useRouter } from 'next/router' | ||
import { useMutation, useQueryClient } from 'react-query' | ||
|
||
const onLogin = async (authCode: string | string[] | undefined) => { | ||
return ( | ||
await httpClient.oauth.post(null, { | ||
headers: { authCode }, | ||
}) | ||
).data | ||
} | ||
|
||
const useLoginMutation = () => { | ||
const router = useRouter() | ||
const queryClient = useQueryClient() | ||
|
||
return useMutation(() => onLogin(router.query.code), { | ||
onSuccess: (data) => { | ||
Storage.setItem('access_token', data.accessToken) | ||
Storage.setItem('refresh_token', data.refreshToken) | ||
queryClient.invalidateQueries('getUser') | ||
router.back() | ||
router.back() | ||
// window.history.go(-2) | ||
}, | ||
onError: () => window.history.go(-2), | ||
}) | ||
} | ||
|
||
export default useLoginMutation |
Oops, something went wrong.