mirror of
https://github.com/vercel/commerce.git
synced 2025-03-16 15:32:32 +00:00
* Moved everything * Figuring out how to make imports work * Updated exports * Added missing exports * Added @vercel/commerce-local to `site` * Updated commerce config * Updated exports and commerce config * Updated commerce hoc * Fixed exports in local * Added publish config * Updated imports in site * It's actually working * Don't use debugger in dev for better speeds * Improved DX when editing packages * Set up eslint with husky * Updated prettier config * Added prettier setup to every package * Moved bigcommerce * Moved Bigcommerce to src and package updates * Updated setup of bigcommerce * Moved definitions script * Moved commercejs * Move to src * Fixed types in commercejs * Moved kibocommerce * Moved kibocommerce to src * Added package/tsconfig to kibocommerce * Fixed imports and other things * Moved ordercloud * Moved ordercloud to src * Fixed imports * Added missing prettier files * Moved Saleor * Moved Saleor to src * Fixed imports * Replaced all imports to @commerce * Added prettierignore/rc to all providers * Moved shopify to src * Build shopify in packages * Moved Spree * Moved spree to src * Updated spree * Moved swell * Moved swell to src * Fixed type imports in swell * Moved Vendure to packages * Moved vendure to src * Fixed imports in vendure * Added codegen to saleor * Updated codegen setup for shopify * Added codegen to vendure * Added codegen to kibocommerce * Added all packages to site's deps * Updated codegen setup in bigcommerce * Minor fixes * Updated providers' names in site * Updated packages based on Bel's changes * Updated turbo to latest * Fixed ts complains * Set npm engine in root * New lockfile install * remove engines * Regen lockfile * Switched from npm to yarn * Updated typesVersions in all packages * Moved dep * Updated SWR to the just released 1.2.0 * Removed "isolatedModules" from packages * Updated list of providers and default * Updated swell declaration * Removed next import from kibocommerce * Added COMMERCE_PROVIDER log * Added another log * Updated turbo config * Updated docs * Removed test logs Co-authored-by: Jared Palmer <jared@jaredpalmer.com>
217 lines
4.3 KiB
TypeScript
217 lines
4.3 KiB
TypeScript
import React, { FC, useCallback, useMemo } from 'react'
|
|
import { ThemeProvider } from 'next-themes'
|
|
|
|
export interface State {
|
|
displaySidebar: boolean
|
|
displayDropdown: boolean
|
|
displayModal: boolean
|
|
sidebarView: string
|
|
modalView: string
|
|
userAvatar: string
|
|
}
|
|
|
|
const initialState = {
|
|
displaySidebar: false,
|
|
displayDropdown: false,
|
|
displayModal: false,
|
|
modalView: 'LOGIN_VIEW',
|
|
sidebarView: 'CART_VIEW',
|
|
userAvatar: '',
|
|
}
|
|
|
|
type Action =
|
|
| {
|
|
type: 'OPEN_SIDEBAR'
|
|
}
|
|
| {
|
|
type: 'CLOSE_SIDEBAR'
|
|
}
|
|
| {
|
|
type: 'OPEN_DROPDOWN'
|
|
}
|
|
| {
|
|
type: 'CLOSE_DROPDOWN'
|
|
}
|
|
| {
|
|
type: 'OPEN_MODAL'
|
|
}
|
|
| {
|
|
type: 'CLOSE_MODAL'
|
|
}
|
|
| {
|
|
type: 'SET_MODAL_VIEW'
|
|
view: MODAL_VIEWS
|
|
}
|
|
| {
|
|
type: 'SET_SIDEBAR_VIEW'
|
|
view: SIDEBAR_VIEWS
|
|
}
|
|
| {
|
|
type: 'SET_USER_AVATAR'
|
|
value: string
|
|
}
|
|
|
|
type MODAL_VIEWS =
|
|
| 'SIGNUP_VIEW'
|
|
| 'LOGIN_VIEW'
|
|
| 'FORGOT_VIEW'
|
|
| 'NEW_SHIPPING_ADDRESS'
|
|
| 'NEW_PAYMENT_METHOD'
|
|
|
|
type SIDEBAR_VIEWS = 'CART_VIEW' | 'CHECKOUT_VIEW' | 'PAYMENT_METHOD_VIEW'
|
|
|
|
export const UIContext = React.createContext<State | any>(initialState)
|
|
|
|
UIContext.displayName = 'UIContext'
|
|
|
|
function uiReducer(state: State, action: Action) {
|
|
switch (action.type) {
|
|
case 'OPEN_SIDEBAR': {
|
|
return {
|
|
...state,
|
|
displaySidebar: true,
|
|
}
|
|
}
|
|
case 'CLOSE_SIDEBAR': {
|
|
return {
|
|
...state,
|
|
displaySidebar: false,
|
|
}
|
|
}
|
|
case 'OPEN_DROPDOWN': {
|
|
return {
|
|
...state,
|
|
displayDropdown: true,
|
|
}
|
|
}
|
|
case 'CLOSE_DROPDOWN': {
|
|
return {
|
|
...state,
|
|
displayDropdown: false,
|
|
}
|
|
}
|
|
case 'OPEN_MODAL': {
|
|
return {
|
|
...state,
|
|
displayModal: true,
|
|
displaySidebar: false,
|
|
}
|
|
}
|
|
case 'CLOSE_MODAL': {
|
|
return {
|
|
...state,
|
|
displayModal: false,
|
|
}
|
|
}
|
|
case 'SET_MODAL_VIEW': {
|
|
return {
|
|
...state,
|
|
modalView: action.view,
|
|
}
|
|
}
|
|
case 'SET_SIDEBAR_VIEW': {
|
|
return {
|
|
...state,
|
|
sidebarView: action.view,
|
|
}
|
|
}
|
|
case 'SET_USER_AVATAR': {
|
|
return {
|
|
...state,
|
|
userAvatar: action.value,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export const UIProvider: FC = (props) => {
|
|
const [state, dispatch] = React.useReducer(uiReducer, initialState)
|
|
|
|
const openSidebar = useCallback(
|
|
() => dispatch({ type: 'OPEN_SIDEBAR' }),
|
|
[dispatch]
|
|
)
|
|
const closeSidebar = useCallback(
|
|
() => dispatch({ type: 'CLOSE_SIDEBAR' }),
|
|
[dispatch]
|
|
)
|
|
const toggleSidebar = useCallback(
|
|
() =>
|
|
state.displaySidebar
|
|
? dispatch({ type: 'CLOSE_SIDEBAR' })
|
|
: dispatch({ type: 'OPEN_SIDEBAR' }),
|
|
[dispatch, state.displaySidebar]
|
|
)
|
|
const closeSidebarIfPresent = useCallback(
|
|
() => state.displaySidebar && dispatch({ type: 'CLOSE_SIDEBAR' }),
|
|
[dispatch, state.displaySidebar]
|
|
)
|
|
|
|
const openDropdown = useCallback(
|
|
() => dispatch({ type: 'OPEN_DROPDOWN' }),
|
|
[dispatch]
|
|
)
|
|
const closeDropdown = useCallback(
|
|
() => dispatch({ type: 'CLOSE_DROPDOWN' }),
|
|
[dispatch]
|
|
)
|
|
|
|
const openModal = useCallback(
|
|
() => dispatch({ type: 'OPEN_MODAL' }),
|
|
[dispatch]
|
|
)
|
|
const closeModal = useCallback(
|
|
() => dispatch({ type: 'CLOSE_MODAL' }),
|
|
[dispatch]
|
|
)
|
|
|
|
const setUserAvatar = useCallback(
|
|
(value: string) => dispatch({ type: 'SET_USER_AVATAR', value }),
|
|
[dispatch]
|
|
)
|
|
|
|
const setModalView = useCallback(
|
|
(view: MODAL_VIEWS) => dispatch({ type: 'SET_MODAL_VIEW', view }),
|
|
[dispatch]
|
|
)
|
|
|
|
const setSidebarView = useCallback(
|
|
(view: SIDEBAR_VIEWS) => dispatch({ type: 'SET_SIDEBAR_VIEW', view }),
|
|
[dispatch]
|
|
)
|
|
|
|
const value = useMemo(
|
|
() => ({
|
|
...state,
|
|
openSidebar,
|
|
closeSidebar,
|
|
toggleSidebar,
|
|
closeSidebarIfPresent,
|
|
openDropdown,
|
|
closeDropdown,
|
|
openModal,
|
|
closeModal,
|
|
setModalView,
|
|
setSidebarView,
|
|
setUserAvatar,
|
|
}),
|
|
[state]
|
|
)
|
|
|
|
return <UIContext.Provider value={value} {...props} />
|
|
}
|
|
|
|
export const useUI = () => {
|
|
const context = React.useContext(UIContext)
|
|
if (context === undefined) {
|
|
throw new Error(`useUI must be used within a UIProvider`)
|
|
}
|
|
return context
|
|
}
|
|
|
|
export const ManagedUIContext: FC = ({ children }) => (
|
|
<UIProvider>
|
|
<ThemeProvider>{children}</ThemeProvider>
|
|
</UIProvider>
|
|
)
|