Skip to content

Commit

Permalink
Merge branch 'Weekly' into Feat/issue-#60
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobbymin authored Oct 11, 2024
2 parents e9a5a2a + 1b5ca88 commit b0274a0
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 2 deletions.
47 changes: 47 additions & 0 deletions src/pages/common/register/api/hooks/useRegister.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useNavigate } from 'react-router-dom';

import { AxiosError } from 'axios';

import { RouterPath } from '@/app/routes/path';
import {
SignupApiResponse,
registerUser,
} from '@/shared/api/auth/user-register';
import { authLocalStorage } from '@/shared/utils/storage';
import { useMutation } from '@tanstack/react-query';

const useRegister = () => {
const navigate = useNavigate();

const handleSuccess = (data: SignupApiResponse) => {
if ('status' in data && data.status === 207) {
alert(data.detail);
} else {
console.log(data);
if ('accessToken' in data) {
authLocalStorage.set(data.accessToken);
authLocalStorage.set(data.refreshToken);
alert('νšŒμ›κ°€μž…μ΄ μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.');
navigate(data.isSinitto === 'true' ? RouterPath.ROOT : RouterPath.ROOT);
}
}
};

const handleError = (error: AxiosError) => {
if (error.response && error.response.data) {
alert('νšŒμ›κ°€μž… 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.');
} else {
alert('νšŒμ›κ°€μž… 쀑 λ„€νŠΈμ›Œν¬ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.');
}
};

const mutation = useMutation({
mutationFn: registerUser,
onSuccess: handleSuccess,
onError: handleError,
});

return mutation;
};

export default useRegister;
18 changes: 16 additions & 2 deletions src/pages/common/register/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState } from 'react';
import { useForm } from 'react-hook-form';

import useRegister from './api/hooks/useRegister';
import { RegisterFields } from './components/register-fields';
import { RegisterType } from './components/register-type';
import { Tos } from './components/tos';
Expand All @@ -17,13 +18,26 @@ const RegisterPage = () => {
formState: { errors },
} = useForm<FormValues>();

// νšŒμ›κ°€μž… 처리
const mutation = useRegister();

const handleUserType = (id: string) => {
setUserType(id);
};

const onSubmit = (data: FormValues) => {
// νšŒμ›κ°€μž… api
console.log(userType, data);
// request 에 맞게 데이터 병합
const isSinitto = userType === 'sinitto';

const requestData = {
name: data.name,
phoneNumber: data.phoneNumber,
email: '[email protected]', // μž„μ‹œ (카카였 둜그인 ν›„ λ„˜κ²¨λ°›κΈ°)
isSinitto,
};
console.log(requestData);
// νšŒμ›κ°€μž… API 호좜
mutation.mutate(requestData);
};

return (
Expand Down
53 changes: 53 additions & 0 deletions src/shared/api/auth/user-register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { fetchInstance } from '../instance';

// request(μš”μ²­) νƒ€μž…
export type SignupReguestParams = {
name: string;
phoneNumber: string;
email: string;
isSinitto: boolean;
};

// response(응닡) νƒ€μž… - 성곡
export type SignupResponse = {
accessToken: string;
refreshToken: string;
isSinitto?: 'true' | 'false';
};

// μ—λŸ¬(아직 μ •ν™•νžˆ μ—λŸ¬μ½”λ“œ 확인 x) or μ˜ˆμ™Έ
export type SignupErrorResponse = {
status: number;
detail: string;
};

// 곡톡 νƒ€μž… μ •μ˜ (onSuccess λ‚΄λΆ€μ—μ„œ λΆ„κΈ°)
export type SignupApiResponse = SignupResponse | SignupErrorResponse;

export const registerUser = async ({
name,
phoneNumber,
email,
isSinitto,
}: SignupReguestParams): Promise<SignupApiResponse> => {
try {
// μ‹œλ‹ˆλ˜ λ³΄ν˜Έμžμ— 따라 API μ—”λ“œν¬μΈνŠΈ ꡬ뢄
const endpoint = isSinitto ? 'sinitto' : 'guard';

const response = await fetchInstance.post(`/api/members/${endpoint}`, {
name,
phoneNumber,
email,
isSinitto,
});
if (response.status === 207) {
return {
status: response.status,
detail: response.data.detail,
} as SignupErrorResponse; // 207 (μ˜ˆμ™Έ - 쀑볡 이메일)
}
return response.data; // 200
} catch (err) {
throw new Error('νšŒμ›κ°€μž… μ‹€νŒ¨');
}
};
2 changes: 2 additions & 0 deletions src/shared/api/instance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const initInstance = (config: AxiosRequestConfig): AxiosInstance => {
return instance;
};

export const BASE_URL = 'http://43.201.254.198:8080';

export const fetchInstance = initInstance({
baseURL: BASE_URI,
});
Expand Down
24 changes: 24 additions & 0 deletions src/shared/utils/storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
type StorageKey = {
accessToken?: string;
};

const initStorage = <T extends keyof StorageKey>(key: T, storage: Storage) => {
const storageKey = `${key}`;

const get = (): StorageKey[T] => {
const value = storage.getItem(storageKey);
return value as StorageKey[T];
};

const set = (value: StorageKey[T]) => {
if (value == undefined || value == null) {
return storage.removeItem(storageKey);
}
const stringifiedValue = JSON.stringify(value);
storage.setItem(storageKey, stringifiedValue);
};

return { get, set };
};

export const authLocalStorage = initStorage('accessToken', localStorage);

0 comments on commit b0274a0

Please sign in to comment.