@open-tender/store
Version:
A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our in-store POS API
42 lines (41 loc) • 1.66 kB
JavaScript
import { useEffect } from 'react';
import { useAppDispatch, useAppSelector } from '../app/hooks';
import { fetchCustomer, openModal, resetCustomer, resetCustomerIdentify, resetMenu, selectCustomer, selectCustomerIdentified, selectKioskConfig } from '../slices';
var endpoints = [
'ACCOUNT',
'DEALS',
'FAVORITES',
'ORDERS',
'GIFT_CARDS',
'LOYALTY',
'REWARDS'
];
var SignInButton = function (_a) {
var navigate = _a.navigate, children = _a.children, pathname = _a.pathname;
var dispatch = useAppDispatch();
var config = useAppSelector(selectKioskConfig).signInButton;
var customer = (useAppSelector(selectCustomerIdentified) || {}).customer;
var account = useAppSelector(selectCustomer).account;
var customerId = (customer || {}).customer_id;
var isAccount = pathname.includes('account');
var shouldFetch = !!(customerId && !account && !isAccount);
var signIn = function () {
dispatch(openModal({ type: 'SIGN_IN' }));
};
var signOut = function () {
dispatch(resetCustomerIdentify());
dispatch(resetCustomer());
dispatch(resetMenu());
};
var goToAccount = function () { return navigate('/account'); };
var handlers = { signIn: signIn, signOut: signOut, goToAccount: goToAccount };
useEffect(function () {
if (shouldFetch) {
dispatch(fetchCustomer({ customerId: customerId, endpoints: endpoints }));
}
}, [dispatch, shouldFetch, customerId]);
if (!config)
return null;
return children({ config: config, handlers: handlers, customer: customer, isAccount: isAccount });
};
export default SignInButton;