Skip to content
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

리포지토리가 존재하는지 여부를 체크하는 쿼리훅을 추하고 이를 로직에 적용한다 #124

Merged
merged 3 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/components/modals/SideGithubModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import Input, { ParentRef } from '@src/components/common/Input';
import { GuideText } from '@src/constant/enums';
import { useHistory } from 'react-router-dom';
import { setInitSide, useAppDispatch } from '@src/store';
import { useReadGithubInfo } from '@src/hooks/useGithubQuery';
import {
useIsExistRepository,
useReadGithubInfo,
} from '@src/hooks/useGithubQuery';

export interface SideGithubModalProps {
hideModal: () => void;
Expand All @@ -29,16 +32,19 @@ const Template = ({
const urlRef = useRef({} as ParentRef);

const { isError, data } = useReadGithubInfo(url);
const { data: isExist, isLoading } = useIsExistRepository(data?.id || 0);

useEffect(() => {
if (data) {
if (data && !isLoading) {
if (isExist?.duplicated) return showAlert(GuideText.DUPLICATE_SIDE);

dispatch(setInitSide());
history.push({
pathname: '/side-create',
state: data,
});
}
}, [data]);
}, [data, isExist]);

useEffect(() => {
if (isError) {
Expand Down
1 change: 1 addition & 0 deletions src/constant/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum GuideText {
ERROR = '에러가 발생했습니다',
DELETE_SUCCESS = '삭제를 성공했습니다',
DUPLICATE = '중복된 값 입니다',
DUPLICATE_SIDE = '입력하신 리포지토리로는 SIDE가 이미 생성되어있습니다.',
}

export enum ConfirmText {
Expand Down
9 changes: 6 additions & 3 deletions src/hooks/useGithubQuery.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useQueries, useQuery } from 'react-query';
import { useQueries, useQuery, useQueryClient } from 'react-query';
import axios from 'axios';
import { getGithubApiInstance } from '@src/utils/githubContext';

export type GithubInfoType = {
id: string;
id: number;
homepage: string;
html_url: string;
owner: {
Expand All @@ -18,12 +18,15 @@ export type GithubInfoType = {
};

export type ContributorsType = {
id: string;
id: number;
html_url: string;
avatar_url: string;
login: string;
};

export const useIsExistRepository = (id: number) =>
useQuery<{ duplicated: boolean }>(`/sides/exists/${id}`, { enabled: !!id });

export const useReadGithubInfo = (url: string) =>
useQuery<GithubInfoType>(
`https://api.github.com/repos/${url}`,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/SideCreatePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '@src/hooks/useGithubQuery';

import { setInitSide, setSide, useAppDispatch, useSideState } from '@src/store';
import { GuideText } from '@src/constant/enums';

interface SideCreatePageProps {
handleToTop?: () => void;
Expand Down Expand Up @@ -141,10 +142,9 @@ const SideCreatePage = ({ handleToTop }: SideCreatePageProps) => {

history.push('/side');
},
// TODO: 추후 타입 정의
onError: (e: any) => {
if (e.response.status === 400) {
showAlert('입력하신 리포지토리로는 toy가 이미 생성되어있습니다');
showAlert(GuideText.ERROR);
}
},
});
Expand Down
3 changes: 2 additions & 1 deletion src/pages/SideEditPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from '@src/hooks/useGithubQuery';

import { setInitSide, setSide, useAppDispatch, useSideState } from '@src/store';
import { GuideText } from '@src/constant/enums';

interface SideEditPageProps {
handleToTop?: () => void;
Expand Down Expand Up @@ -149,7 +150,7 @@ const SideEditPage = ({ handleToTop }: SideEditPageProps) => {
// TODO: 추후 타입 정의
onError: (e: any) => {
if (e.response.status === 400) {
showAlert('입력하신 리포지토리로는 toy가 이미 생성되어있습니다');
showAlert(GuideText.ERROR);
}
},
});
Expand Down