-
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 pull request #47 from BacPacNet/zustand-implementation
Zustand implementation and useCookie hook
- Loading branch information
Showing
14 changed files
with
172 additions
and
121 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,23 @@ | ||
import { useState, useEffect } from 'react' | ||
|
||
const useCookie = (cookieName: string): [string, (value: string, expirationDate: string) => void, () => void] => { | ||
const [cookieValue, setCookieValue] = useState<string>('') | ||
|
||
useEffect(() => { | ||
const cookie = document.cookie.split('; ').find((row) => row.startsWith(`${cookieName}=`)) | ||
|
||
setCookieValue(cookie ? cookie.split('=')[1] : '') | ||
}, [cookieName]) | ||
|
||
const setCookie = (value: string, expirationDate: string): void => { | ||
document.cookie = `${cookieName}=${value}; expires=${new Date(expirationDate).toUTCString()}; path=/` | ||
} | ||
|
||
const deleteCookie = (): void => { | ||
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/` | ||
} | ||
|
||
return [cookieValue, setCookie, deleteCookie] | ||
} | ||
|
||
export default useCookie |
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 |
---|---|---|
@@ -1,16 +1,30 @@ | ||
import { LoginForm, UserResponseType } from '@/models/auth' | ||
import { useMutation } from '@tanstack/react-query' | ||
import { client } from './api-Client' | ||
import { useUniStore } from '@/store/store' | ||
import useCookie from '@/hooks/useCookie' | ||
|
||
const login = async (data: LoginForm): Promise<UserResponseType> => { | ||
const result = await client<UserResponseType, LoginForm>('auth/login', { data }) | ||
return result | ||
} | ||
|
||
export const useHandleLogin = () => | ||
useMutation({ | ||
export const useHandleLogin = () => { | ||
const setUserData = useUniStore((state) => state.setUserData) | ||
// const setToken = useUniStore((state) => state.setToken) | ||
const [_, setCookieValue] = useCookie('uni_user_token') | ||
const [__, setRefreshCookieValue] = useCookie('uni_user_refresh_token') | ||
|
||
return useMutation({ | ||
mutationFn: (data: LoginForm) => login(data), | ||
onSuccess: (response) => { | ||
console.log(response, 'response') | ||
console.log(_, __) | ||
|
||
setUserData(response.user) | ||
// setToken(response.tokens) | ||
setCookieValue(response.tokens.access.token, response.tokens.access.expires) | ||
setRefreshCookieValue(response.tokens.refresh.token, response.tokens.refresh.expires) | ||
}, | ||
}) | ||
} |
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,19 @@ | ||
import { create } from 'zustand' | ||
import { devtools, persist } from 'zustand/middleware' | ||
import { createUserSlice } from './userSlice/userSlice' | ||
import { storeType } from './storeType' | ||
|
||
export const useUniStore = create<storeType>()( | ||
devtools( | ||
persist( | ||
(...a) => ({ | ||
...createUserSlice(...a), | ||
}), | ||
{ | ||
name: 'UniStore', | ||
// partialize: (state) => ({ products: state.products,userName:state.userName }), | ||
// skipHydration: true | ||
} | ||
) | ||
) | ||
) |
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,3 @@ | ||
import { userSlice } from './userSlice/userSlice' | ||
|
||
export type storeType = userSlice |
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,35 @@ | ||
import { StateCreator } from 'zustand' | ||
import { userType } from './userType' | ||
|
||
type userState = { | ||
userData: userType | ||
} | ||
|
||
type userAction = { | ||
setUserData: (userData: userType) => void | ||
resetUserData: () => void | ||
} | ||
|
||
const initialState: userState = { | ||
userData: { | ||
firstName: '', | ||
lastName: '', | ||
email: '', | ||
// password: '', | ||
gender: '', | ||
dob: '', | ||
role: '', | ||
isEmailVerified: false, | ||
// createdAt: '', | ||
userVerifiedCommunities: [], | ||
userUnVerifiedCommunities: [], | ||
}, | ||
} | ||
|
||
export type userSlice = userState & userAction | ||
|
||
export const createUserSlice: StateCreator<userSlice> = (set) => ({ | ||
userData: initialState.userData, | ||
setUserData: (userData: userType) => set({ userData }), | ||
resetUserData: () => set(initialState), | ||
}) |
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,22 @@ | ||
interface verifiedInterface { | ||
communityId: string | ||
communityName: string | ||
} | ||
interface unverifiedInterface { | ||
communityId: string | ||
communityName: string | ||
} | ||
|
||
export interface userType { | ||
firstName: string | ||
lastName: string | ||
email: string | ||
// password: string | ||
gender: string | ||
dob: string | ||
role: string | ||
isEmailVerified: boolean | ||
// createdAt: Date | string | ||
userVerifiedCommunities: verifiedInterface[] | ||
userUnVerifiedCommunities: unverifiedInterface[] | ||
} |
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