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

[ Feature/subscription 75 ] 구독 & 구독 취소 API 연결 #78

Merged
merged 11 commits into from
Jun 27, 2024
2 changes: 1 addition & 1 deletion src/subscription/components/UnsubscribeTitle/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UNSUBSCRIBE_TITLES } from "@workbook/constants/unsubscribe";
import { UNSUBSCRIBE_TITLES } from "@subscription/constants/unsubscribe";

export default function UnsubscribeTitle () {
return (
Expand Down
32 changes: 26 additions & 6 deletions src/subscription/hooks/useUnsubscribeForm.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useForm } from "react-hook-form";

import { useMutation } from "@tanstack/react-query";

import { z } from "zod";

import { useToast } from "@shared/components/ui/use-toast";

import { UNSUBSCRIBE_CONFIRM } from "@subscription/constants/unsubscribe";
import { unsubscribeWorkbookOptions } from "@subscription/remotes/postUnsubscriptionQueryOptions";
import { unSubscribeSchema } from "@subscription/schemas";
import { UnsubscribeFormData } from "@subscription/types/subscription";
import { getCookie } from "@subscription/utils";

import { zodResolver } from "@hookform/resolvers/zod";

Expand All @@ -20,16 +24,32 @@ export const useUnsubscribeForm = () => {
},
});

const { mutate: unsubscribeWorkbook } = useMutation(unsubscribeWorkbookOptions());

const onSubmit = (values: UnsubscribeFormData) => {
try {
const result = unSubscribeSchema.safeParse(values);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

근데 이거 제출할때 zod가 유효성 검사 해주지 않나요?! 왜 한번더 진행하는걸까욥..?


const email = getCookie('user-email');

// 폼 제출 성공 로직 추가
if (result.success) {
form.reset();
toast({
title: UNSUBSCRIBE_CONFIRM,
});
if (result.success && typeof email === "string") {
unsubscribeWorkbook(
{ email: decodeURIComponent(email), opinion: values.opinion },
{
onSuccess: () => {
form.reset();
toast({
title: UNSUBSCRIBE_CONFIRM,
});
},
onError: (error) => {
console.error(error);
toast({
title: '구독 취소가 되지 않았습니다.'
});
},
},
);
}
} catch (error) {
if (error instanceof z.ZodError) {
Expand Down
2 changes: 2 additions & 0 deletions src/subscription/remotes/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const API_ROUTE = {
SUBSCRIBE: (workbookId: string) => `/api/v1/workbooks/${workbookId}/subs`,
UNSUBSCRIBE: () => `/api/v1/subscriptions/unsubs`
};
export const QUERY_KEY = {
SUBSCRIBE_WORKBOOK: "sub-workbook",
UNSUBSCRIBE_WORKBOOK: "unsub-workbook"
};
27 changes: 27 additions & 0 deletions src/subscription/remotes/postUnsubscriptionQueryOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { UseMutationOptions } from "@tanstack/react-query";

import { ApiResponse, axiosRequest } from "@api/api-config";

import {
MessageOnlyResponse,
UnsubscribeBody,
} from "@subscription/types/subscription";

import { API_ROUTE, QUERY_KEY } from "./api";

export const unsubscribeWorkbook = (
body: UnsubscribeBody,
): Promise<ApiResponse<MessageOnlyResponse>> => {
return axiosRequest("post", API_ROUTE.UNSUBSCRIBE(), body);
};

export const unsubscribeWorkbookOptions = (): UseMutationOptions<
ApiResponse<MessageOnlyResponse>,
Error,
UnsubscribeBody
> => {
return {
mutationKey: [QUERY_KEY.UNSUBSCRIBE_WORKBOOK],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

body값에 있는 email 키를 넣어서 mutation key를 유효하게 만드는건 어떤가욥...?

mutationFn: (body) => unsubscribeWorkbook(body),
};
};
7 changes: 6 additions & 1 deletion src/subscription/types/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ export type SubscribeBody = {
email: string
}

export interface MessageOnlyResponse extends Pick<ApiResponse<any>, 'message'> {}
export interface MessageOnlyResponse extends Pick<ApiResponse<any>, 'message'> {}

export type UnsubscribeBody = {
email: string
opinion: string
}
5 changes: 5 additions & 0 deletions src/subscription/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const getCookie = (name: string) => {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop()?.split(";").shift();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

조건문에 해당되지 않을때 반환하는게 있다면 더 명시적일것같습니다..!

};
Loading