-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41b233d
commit 0aaedec
Showing
6 changed files
with
125 additions
and
27 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 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
79 changes: 79 additions & 0 deletions
79
apps/storefront/src/pages/orderDetail/context/OrderDetailsContext.tsx
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,79 @@ | ||
import { | ||
useReducer, | ||
createContext, | ||
Dispatch, | ||
ReactNode, | ||
useMemo, | ||
} from 'react' | ||
|
||
interface OrderDetailsState { | ||
shippings: any, | ||
history: any, | ||
poNumber?: string, | ||
status?: string, | ||
statusCode?: string, | ||
currencyCode?: string, | ||
currency?: string, | ||
orderSummary: any, | ||
customStatus?: string, | ||
} | ||
interface OrderDetailsAction { | ||
type: string, | ||
payload: OrderDetailsState | ||
} | ||
export interface OrderDetailsContextType { | ||
state: OrderDetailsState, | ||
dispatch: Dispatch<OrderDetailsAction>, | ||
} | ||
|
||
interface OrderDetailsProviderProps { | ||
children: ReactNode | ||
} | ||
|
||
const initState = { | ||
shippings: [], | ||
history: [], | ||
poNumber: '', | ||
status: '', | ||
statusCode: '', | ||
currencyCode: '', | ||
currency: '', | ||
orderSummary: {}, | ||
customStatus: '', | ||
} | ||
|
||
export const OrderDetailsContext = createContext<OrderDetailsContextType>({ | ||
state: initState, | ||
dispatch: () => {}, | ||
}) | ||
|
||
const reducer = (state: OrderDetailsState, action: OrderDetailsAction) => { | ||
switch (action.type) { | ||
case 'all': | ||
return { | ||
...state, | ||
...action.payload, | ||
} | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
export function OrderDetailsProvider(props: OrderDetailsProviderProps) { | ||
const [state, dispatch] = useReducer(reducer, initState) | ||
|
||
const { | ||
children, | ||
} = props | ||
|
||
const OrderDetailsValue = useMemo(() => ({ | ||
state, | ||
dispatch, | ||
}), [state]) | ||
|
||
return ( | ||
<OrderDetailsContext.Provider value={OrderDetailsValue}> | ||
{children} | ||
</OrderDetailsContext.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