4
0
forked from crowetic/commerce
commerce/components/ui/context.tsx
B 3a7d5e2489
Latest Release (#187)
* Remove duplicated css rules. (#185)

* Fix typo in the Marquee component (#176)

Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>

* Remove duplicated css rules.
Fix invalid JSX props.

Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>

* Fix the body scroll when the sidebar is open (#184)

* Fix typo in the Marquee component (#176)

Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>

* Fix the body scroll when the sidebar is open

Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>

* Remove duplicate class in the I18nWidget comp (#183)

* Fix typo in the Marquee component (#176)

Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>

* Remove duplicate class name in the I18nWidget comp

This PR removes a duplicate class name in the I18nWidget component.

Co-authored-by: Hugo Lopes <hugo.rodrigues.lopes@gmail.com>
Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>

* add horizontal margin to pages when mobile screen (#180)

* Add cart item options like color and size (#177)

Co-authored-by: hlopes <hugo.lopes@present-technologies.com>

* Changes

Co-authored-by: Hugo Lopes <hugo.rodrigues.lopes@gmail.com>
Co-authored-by: Hugo Lopes <hugo.lopes@present-technologies.com>
Co-authored-by: Jamie Isaksen <jamie@jamie.no>
Co-authored-by: Vinicius Zucatti <51221635+vczb@users.noreply.github.com>
2021-01-29 12:00:16 -03:00

187 lines
3.8 KiB
TypeScript

import React, { FC, useMemo } from 'react'
import { ThemeProvider } from 'next-themes'
export interface State {
displaySidebar: boolean
displayDropdown: boolean
displayModal: boolean
displayToast: boolean
modalView: string
toastText: string
}
const initialState = {
displaySidebar: false,
displayDropdown: false,
displayModal: false,
modalView: 'LOGIN_VIEW',
displayToast: false,
toastText: '',
}
type Action =
| {
type: 'OPEN_SIDEBAR'
}
| {
type: 'CLOSE_SIDEBAR'
}
| {
type: 'OPEN_TOAST'
}
| {
type: 'CLOSE_TOAST'
}
| {
type: 'SET_TOAST_TEXT'
text: ToastText
}
| {
type: 'OPEN_DROPDOWN'
}
| {
type: 'CLOSE_DROPDOWN'
}
| {
type: 'OPEN_MODAL'
}
| {
type: 'CLOSE_MODAL'
}
| {
type: 'SET_MODAL_VIEW'
view: MODAL_VIEWS
}
type MODAL_VIEWS = 'SIGNUP_VIEW' | 'LOGIN_VIEW' | 'FORGOT_VIEW'
type ToastText = string
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 'OPEN_TOAST': {
return {
...state,
displayToast: true,
}
}
case 'CLOSE_TOAST': {
return {
...state,
displayToast: false,
}
}
case 'SET_MODAL_VIEW': {
return {
...state,
modalView: action.view,
}
}
case 'SET_TOAST_TEXT': {
return {
...state,
toastText: action.text,
}
}
}
}
export const UIProvider: FC = (props) => {
const [state, dispatch] = React.useReducer(uiReducer, initialState)
const openSidebar = () => dispatch({ type: 'OPEN_SIDEBAR' })
const closeSidebar = () => dispatch({ type: 'CLOSE_SIDEBAR' })
const toggleSidebar = () =>
state.displaySidebar
? dispatch({ type: 'CLOSE_SIDEBAR' })
: dispatch({ type: 'OPEN_SIDEBAR' })
const closeSidebarIfPresent = () =>
state.displaySidebar && dispatch({ type: 'CLOSE_SIDEBAR' })
const openDropdown = () => dispatch({ type: 'OPEN_DROPDOWN' })
const closeDropdown = () => dispatch({ type: 'CLOSE_DROPDOWN' })
const openModal = () => dispatch({ type: 'OPEN_MODAL' })
const closeModal = () => dispatch({ type: 'CLOSE_MODAL' })
const openToast = () => dispatch({ type: 'OPEN_TOAST' })
const closeToast = () => dispatch({ type: 'CLOSE_TOAST' })
const setModalView = (view: MODAL_VIEWS) =>
dispatch({ type: 'SET_MODAL_VIEW', view })
const value = useMemo(
() => ({
...state,
openSidebar,
closeSidebar,
toggleSidebar,
closeSidebarIfPresent,
openDropdown,
closeDropdown,
openModal,
closeModal,
setModalView,
openToast,
closeToast,
}),
[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>
)