Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
  • Loading branch information
QuocAnh189 committed Oct 15, 2024
1 parent 259c926 commit ca7cf1a
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VITE_API_URL= http://localhost:8002//api/v1
VITE_API_URL= http://localhost:8002/api/v1
VITE_API_URL_SOCKET= https://eventhubsolutionbackendserverplan.azurewebsites.net
VITE_URL_SOCIAL_PRODUCTION = https://eventhubsolution.vercel.app/organization
VITE_URL_SOCIAL_DEV = http://localhost:3000/organization
Expand Down
7 changes: 4 additions & 3 deletions src/pages/auth/Signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import { withTranslation } from 'react-i18next'
// assets
import authImg from '@assets/images/auth/bg_auth.png'
import logoText_Img from '@assets/images/common/logo_text.png'
import dayjs from 'dayjs'

const SignUp = ({ t }: any) => {
const dispatch = useAppDispatch()
const navigate = useNavigate()
const { width } = useWindowSize()

const [signUp, { isLoading }] = useSignUpMutation()
const [SignUp, { isLoading }] = useSignUpMutation()

const [formDataSessionOne, setFormDataSessionOne] = useState<SignUpPayloadOne>(InitSignUpOne)
const [formDataSessionTwo, setFormDataSessionTwo] = useState<SignUpPayloadTwo>(InitSignUpTwo)
Expand All @@ -54,6 +55,7 @@ const SignUp = ({ t }: any) => {
}, [])

const handleChangeFormSessionOne = (e: ChangeEvent<HTMLInputElement> | any) => {
console.log(e.target.name, e.target.value)
setFormDataSessionOne({
...formDataSessionOne,
[e.target.name]: e.target.value
Expand All @@ -74,8 +76,7 @@ const SignUp = ({ t }: any) => {
}

try {
const result = await signUp(formData).unwrap()

const result = await SignUp({ ...formData, dob: dayjs(formData.dob).format('DD/MM/YYYY') }).unwrap()
if (result) {
localStorage.setItem('token', JSON.stringify(result))
const response = await fetch(`${import.meta.env.VITE_API_URL!}/auth/profile`, {
Expand Down
22 changes: 18 additions & 4 deletions src/pages/auth/components/ForgotPassword.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
//hooks
import { useState } from 'react'

//components
import { Skeleton } from '@mui/material'

//motion
import { motion } from 'framer-motion'

//util
import classNames from 'classnames'

//redux
import { useForgotPasswordMutation } from '@redux/apis/auth.api'

//i18n
import { withTranslation } from 'react-i18next'

Expand All @@ -18,10 +24,18 @@ interface Props {
const FormForgotPassword = (props: Props) => {
const { t, changeSession } = props

const [ForgotPassword, { isLoading }] = useForgotPasswordMutation()
const [email, setEmail] = useState<string>('')

const handleSubmit = () => {
changeSession('verify-opt')
const handleSubmit = async () => {
try {
const result = await ForgotPassword({ email }).unwrap()
if (result) {
changeSession('verify-opt')
}
} catch (error: any) {
console.log(error)
}
}

return (
Expand Down Expand Up @@ -54,10 +68,10 @@ const FormForgotPassword = (props: Props) => {
>
<button
className='flex w-full btn hover:bg-blue-light2 bg-blue-light2 text-white'
// disabled={email ? false : true}
disabled={email ? false : true}
onClick={handleSubmit}
>
{t('submit_btn')}
{isLoading ? <Skeleton /> : t('submit_btn')}
</button>
</motion.div>

Expand Down
25 changes: 23 additions & 2 deletions src/pages/auth/components/FormRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const FormRegister = (props: Props) => {

const navigate = useNavigate()

const [validateUser, { isLoading }] = useValidateUserMutation()
const [ValidateUser, { isLoading }] = useValidateUserMutation()
const {
register,
handleSubmit,
Expand All @@ -66,7 +66,7 @@ const FormRegister = (props: Props) => {

const onSubmit: SubmitHandler<SignUpPayloadOne> = async (data: SignUpPayloadOne) => {
try {
const result = await validateUser(data).unwrap()
const result = await ValidateUser(data).unwrap()
if (result) {
nextSession()
}
Expand Down Expand Up @@ -154,6 +154,27 @@ const FormRegister = (props: Props) => {
{errors.phoneNumber && <p className='mt-1 text-red'>{errors.phoneNumber.message}</p>}
</motion.div>

<motion.div
initial={{ x: -20, opacity: 0 }}
animate={{ x: 0, opacity: 1 }}
transition={{ duration: 0.4, delay: 0.5 }}
className='relative mb-6'
>
<input
className={classNames(
'field-input text-header block min-h-[auto] w-full rounded-2xl border-[2px] px-3 py-6 font-semibold placeholder-gray-400 outline-none placeholder:italic focus:border-[2px]',
{
'field-input--error': errors.dob
}
)}
{...register('dob')}
type='date'
name='dob'
onChange={setFormDataSessionOne}
/>
{errors.phoneNumber && <p className='mt-1 text-red'>{errors.dob?.message}</p>}
</motion.div>

<motion.div
initial={{ x: -20, opacity: 0 }}
animate={{ x: 0, opacity: 1 }}
Expand Down
24 changes: 22 additions & 2 deletions src/pages/auth/components/ResetPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { withTranslation } from 'react-i18next'

//util
import classNames from 'classnames'
import { useResetPasswordMutation } from '@redux/apis/auth.api'
import { toast } from 'react-toastify'
import { Skeleton } from '@mui/material'
import { ResetPassWordPayload } from '@type/auth.type'

interface Props {
t: any
Expand All @@ -22,13 +26,29 @@ interface Props {
const ResetPassword = (props: Props) => {
const { t, changeSession } = props

const [ResetPassword, { isLoading }] = useResetPasswordMutation()

const password = useRef(null)
const confirmPassword = useRef(null)

const [showPassWord, setShowPassWord] = useState<boolean>(false)
const [showConfirmPassWord, setShowConfirmPassWord] = useState<boolean>(false)

const handleSubmit = () => {}
const handleSubmit = async () => {
const data: ResetPassWordPayload = {
newPassword: password.current!,
email: '',
token: ''
}
try {
const result = await ResetPassword(data).unwrap()
if (result) {
toast.success('Reset password successfully')
}
} catch (error: any) {
console.log(error)
}
}

return (
<div className='flex flex-col h-full items-center'>
Expand Down Expand Up @@ -97,7 +117,7 @@ const ResetPassword = (props: Props) => {
transition={{ duration: 0.4, delay: 0.6 }}
>
<button className='flex w-full btn hover:bg-blue-light2 bg-blue-light2 text-white' onClick={handleSubmit}>
{t('submit_btn')}
{isLoading ? <Skeleton /> : t('submit_btn')}
</button>
</motion.div>

Expand Down
15 changes: 11 additions & 4 deletions src/redux/apis/auth.api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'

import { LoginPayload, SignUpPayload, ForgotPassPayload, IParamsExternalLogin } from '@type/auth.type'
import {
LoginPayload,
SignUpPayload,
ForgotPassPayload,
IParamsExternalLogin,
ResetPassWordPayload
} from '@type/auth.type'

//interface
import { IAuth } from '@interfaces/systems/auth.interface'
Expand Down Expand Up @@ -77,10 +83,11 @@ export const apiAuth = createApi({
})
}),

resetPassword: builder.mutation<IAuth, void>({
query: () => ({
resetPassword: builder.mutation<IAuth, ResetPassWordPayload>({
query: (data) => ({
url: '/auth/reset-password',
method: 'POST'
method: 'POST',
body: data
})
}),

Expand Down
2 changes: 1 addition & 1 deletion src/redux/apis/category.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const apiCategory = createApi({
export const {
useCreateCategoryMutation,
useGetCategoriesQuery,
useLazyGetCategoryByIdQuery,
useGetCategoryByIdQuery,
useUpdateCategoryMutation,
useDeleteCategoryMutation
} = apiCategory
11 changes: 10 additions & 1 deletion src/type/auth.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface SignUpPayload {
fullName: string
phoneNumber: string
password: string
dob: string
}

export const InitSignup = {
Expand All @@ -40,12 +41,14 @@ export interface SignUpPayloadOne {
email: string
fullName: string
phoneNumber: string
dob: string
}

export const InitSignUpOne = {
email: '',
fullName: '',
phoneNumber: ''
phoneNumber: '',
dob: ''
} as SignUpPayloadOne

export interface SignUpPayloadTwo {
Expand All @@ -60,6 +63,12 @@ export interface ForgotPassPayload {
email: string
}

export interface ResetPassWordPayload {
newPassword: string | null
email: string
token: string
}

export interface LoginResponse {
accessToken: string
refreshToken: string
Expand Down

0 comments on commit ca7cf1a

Please sign in to comment.