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

✨ feat: RSS등록 API 연동 #108

Merged
merged 4 commits into from
Nov 19, 2024
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
1 change: 1 addition & 0 deletions client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ dist-ssr
*.njsproj
*.sln
*.sw?
.env
5 changes: 5 additions & 0 deletions client/src/api/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ export const api = axios.create({
baseURL: "/api",
timeout: 10000,
});

export const axiosInstance = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL,
timeout: 10000,
});
jungmyunggi marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions client/src/api/queries/rssApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { axiosInstance } from "@/api/instance";
import { RegisterRss } from "@/types/rss";
import { RegisterResponse } from "@/types/rss";

export const registerRss = async (data: RegisterRss): Promise<RegisterResponse> => {
const response = await axiosInstance.post<RegisterResponse>("/api/rss", data);
return response.data;
};
28 changes: 25 additions & 3 deletions client/src/components/RssRegistration/RssRegistrationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import {
DialogTitle,
} from "@/components/ui/dialog";

import { useRegisterRss } from "@/hooks/useRegisterRss";

import { validateRssUrl, validateName, validateEmail, validateBlogger } from "./RssValidation";
import { useRegisterModalStore } from "@/store/useRegisterModalStrore";
import { RegisterRss } from "@/types/rss";

export default function RssRegistrationModal({ onClose, rssOpen }: { onClose: () => void; rssOpen: boolean }) {
const {
Expand All @@ -30,7 +33,28 @@ export default function RssRegistrationModal({ onClose, rssOpen }: { onClose: ()
handleInputChange,
isFormValid,
} = useRegisterModalStore();
const onSuccess = () => {
alert("RSS 등록이 성공적으로 완료되었습니다.");
resetInputs();
onClose();
};

const onError = (error: any) => {
alert(`RSS 등록에 실패했습니다: ${error.message}`);
};
jungmyunggi marked this conversation as resolved.
Show resolved Hide resolved

const { mutate } = useRegisterRss(onSuccess, onError);

const handleRegister = () => {
const data: RegisterRss = {
rssURL: rssUrl,
blog: bloggerName,
name: userName,
email: email,
};

mutate(data);
};
return (
<Dialog open={rssOpen} onOpenChange={onClose}>
<DialogContent className="sm:max-w-[425px]">
Expand Down Expand Up @@ -74,9 +98,7 @@ export default function RssRegistrationModal({ onClose, rssOpen }: { onClose: ()
<DialogFooter>
<Button
type="submit"
onClick={() => {
resetInputs();
}}
onClick={handleRegister}
disabled={!isFormValid()}
className="bg-black hover:bg-gray-800 text-white"
>
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/RssRegistration/RssValidation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const validateRssUrl = (url: string) => {
const urlRegex = /^(https?:\/\/)([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(:\d+)?(\/[\w./?=%&-]*)?$/;
const urlRegex = /^(https?:\/\/)([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(\/[^\s]*)?$/;
jungmyunggi marked this conversation as resolved.
Show resolved Hide resolved
return urlRegex.test(url) || false;
};

Expand Down
16 changes: 16 additions & 0 deletions client/src/hooks/useRegisterRss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { AxiosError } from "axios";

import { registerRss } from "@/api/queries/rssApi";
import { RegisterRss, RegisterResponse } from "@/types/rss";
import { useMutation, UseMutationResult } from "@tanstack/react-query";

export const useRegisterRss = (
onSuccess: (data: RegisterResponse) => void,
onError: (error: AxiosError<unknown, any>) => void
): UseMutationResult<RegisterResponse, AxiosError<unknown, any>, RegisterRss, unknown> => {
return useMutation<RegisterResponse, AxiosError<unknown, any>, RegisterRss>({
mutationFn: registerRss,
onSuccess,
onError,
});
jungmyunggi marked this conversation as resolved.
Show resolved Hide resolved
};
10 changes: 10 additions & 0 deletions client/src/types/rss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ export interface RssRequest {
approvedAt?: string; // 승인 시간
rejectedAt?: string; // 거부 시간
}
export interface RegisterRss {
blog: string; // 블로거명
name: string; // 신청자 이름
email: string; // 신청자 이메일
rssURL: string; // 블로그 Rss URL
}

export interface RegisterResponse {
message: string; // 응답 메시지
}