-
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.
refactor: query and mutation, get zod schema defaults helper
- Loading branch information
Showing
15 changed files
with
473 additions
and
115 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
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 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,117 @@ | ||
import { authLoginResponseSchema } from '#auth/apis/auth.api'; | ||
import { isFunction } from '@rifandani/nxact-yutiriti'; | ||
import React from 'react'; | ||
import { z } from 'zod'; | ||
import { create, createStore, useStore } from 'zustand'; | ||
import { createJSONStorage, devtools, persist } from 'zustand/middleware'; | ||
|
||
export type UserStoreState = z.infer<typeof userStoreStateSchema>; | ||
export type UserStore = z.infer<typeof userStoreSchema>; | ||
export type UserStoreLocalStorage = z.infer<typeof userStoreLocalStorageSchema>; | ||
|
||
export const userStoreName = 'app-user' as const; | ||
const userStoreStateSchema = z.object({ | ||
user: authLoginResponseSchema.nullable(), | ||
}); | ||
const userStoreActionSchema = z.object({ | ||
setUser: z.function().args(authLoginResponseSchema).returns(z.void()), | ||
clearUser: z.function().args(z.void()).returns(z.void()), | ||
}); | ||
export const userStoreSchema = userStoreStateSchema.merge( | ||
userStoreActionSchema, | ||
); | ||
export const userStoreLocalStorageSchema = z.object({ | ||
state: userStoreStateSchema, | ||
version: z.number(), | ||
}); | ||
|
||
/** | ||
* Hooks to manipulate user store | ||
* | ||
* @example | ||
* | ||
* ```tsx | ||
* const { user, setUser, clearUser } = useUserStore() | ||
* ``` | ||
*/ | ||
export const useAuthUserStore = create<UserStore>()( | ||
devtools( | ||
persist( | ||
(set) => ({ | ||
user: null, | ||
|
||
setUser: (user) => { | ||
set({ user }); | ||
}, | ||
clearUser: () => { | ||
set({ user: null }); | ||
}, | ||
}), | ||
{ | ||
name: userStoreName, // name of the item in the storage (must be unique) | ||
version: 0, // a migration will be triggered if the version in the storage mismatches this one | ||
storage: createJSONStorage(() => localStorage), // by default, 'localStorage' is used | ||
}, | ||
), | ||
), | ||
); | ||
|
||
/** | ||
* for use with react context to initialize the store with props (default state) | ||
* | ||
* @link https://docs.pmnd.rs/zustand/guides/initialize-state-with-props | ||
*/ | ||
const createUserStore = (initialState?: Partial<UserStoreState>) => { | ||
return createStore<UserStore>()( | ||
devtools((set) => ({ | ||
user: null, | ||
...initialState, | ||
|
||
setUser: (user) => { | ||
set({ user }); | ||
}, | ||
clearUser: () => { | ||
set({ user: null }); | ||
}, | ||
})), | ||
); | ||
}; | ||
|
||
export const UserContext = React.createContext<ReturnType< | ||
typeof createUserStore | ||
> | null>(null); | ||
|
||
export function useUserContext<T>(selector: (_store: UserStore) => T): T { | ||
const store = React.useContext(UserContext); | ||
if (!store) throw new Error('Missing UserContext.Provider in the tree'); | ||
|
||
return useStore(store, selector); | ||
} | ||
|
||
/** | ||
* for use with react context to initialize the store with props (default state) | ||
* | ||
* @link https://docs.pmnd.rs/zustand/guides/initialize-state-with-props | ||
*/ | ||
export function UserProvider({ | ||
children, | ||
initialState, | ||
}: { | ||
children: | ||
| React.ReactNode | ||
| ((context: ReturnType<typeof createUserStore>) => JSX.Element); | ||
initialState?: Parameters<typeof createUserStore>[0]; | ||
}) { | ||
const storeRef = React.useRef<ReturnType<typeof createUserStore> | null>( | ||
null, | ||
); | ||
if (!storeRef.current) { | ||
storeRef.current = createUserStore(initialState); | ||
} | ||
|
||
return ( | ||
<UserContext.Provider value={storeRef.current}> | ||
{isFunction(children) ? children(storeRef.current) : children} | ||
</UserContext.Provider> | ||
); | ||
} |
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
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,75 @@ | ||
import { getSchemaDefaults } from '#shared/utils/helper.util'; | ||
import { z } from 'zod'; | ||
import { create } from 'zustand'; | ||
import { createJSONStorage, devtools, persist } from 'zustand/middleware'; | ||
|
||
const defaultValue = import.meta.env.VITE_MODE === 'development'; | ||
const featureFlagStoreName = 'react-app-feature-flag' as const; | ||
const featureFlagStoreStateSchema = z.object({ | ||
auth: z.object({ | ||
condition: z.object({ | ||
service_checking: z.boolean().default(defaultValue), | ||
}), | ||
endpoint: z.object({ | ||
list_user_access: z.boolean().default(defaultValue), | ||
}), | ||
route: z.object({ | ||
'/user-access/role': z.boolean().default(defaultValue), | ||
}), | ||
ui: z.object({ | ||
logout_button: z.boolean().default(defaultValue), | ||
}), | ||
}), | ||
}); | ||
|
||
export type FeatureFlagStoreStateSchema = z.infer< | ||
typeof featureFlagStoreStateSchema | ||
>; | ||
export type FeatureFlagStoreActionSchema = { | ||
reset: () => void; | ||
}; | ||
export type FeatureFlagStoreSchema = z.infer< | ||
typeof featureFlagStoreStateSchema | ||
> & | ||
FeatureFlagStoreActionSchema; | ||
|
||
export const featureFlagStoreInitialState = getSchemaDefaults< | ||
typeof featureFlagStoreStateSchema | ||
>(featureFlagStoreStateSchema); | ||
|
||
/** | ||
* Persisted react-app-feature-flag store. | ||
* | ||
* @example | ||
* | ||
* ```tsx | ||
* const reset = useFeatureFlagStore(state => state.reset) | ||
* ``` | ||
*/ | ||
export const useFeatureFlagStore = create<FeatureFlagStoreSchema>()( | ||
devtools( | ||
persist( | ||
(set) => ({ | ||
...featureFlagStoreInitialState, | ||
|
||
reset: () => { | ||
set(featureFlagStoreInitialState); | ||
}, | ||
}), | ||
{ | ||
name: featureFlagStoreName, // name of the item in the storage (must be unique) | ||
storage: createJSONStorage(() => localStorage), // by default, 'localStorage' is used | ||
version: 0, // a migration will be triggered if the version in the storage mismatches this one | ||
// migrate: (persistedState, version) => { | ||
// if (version === 0) { | ||
// // if the stored value is in version 0, we rename the field to the new name | ||
// persistedState.newField = persistedState.oldField; | ||
// delete persistedState.oldField; | ||
// } | ||
|
||
// return persistedState; | ||
// }, | ||
}, | ||
), | ||
), | ||
); |
Oops, something went wrong.