-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Weekly' into Feat/issue-#60
- Loading branch information
Showing
5 changed files
with
142 additions
and
2 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
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; |
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 |
---|---|---|
@@ -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'; | ||
|
@@ -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 ( | ||
|
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,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('νμκ°μ μ€ν¨'); | ||
} | ||
}; |
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,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); |