import React, { useState } from 'react'; import { Modal } from 'react-native'; import { useLanguage, LogoutAction, useApi, useSession, useBusiness, useOrder } from 'ordering-components/native'; import { useForm, Controller } from 'react-hook-form'; import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons' import { useTheme } from 'styled-components/native'; import NavBar from '../NavBar'; import { OSBody, OSContainer, OSContent } from './styles'; import { OButton, OInput, OText } from '../shared'; import { useDeviceOrientation, PORTRAIT } from '../../../../../src/hooks/DeviceOrientation'; import { _clearStoreData, _retrieveStoreData } from '../../../../../src/providers/StoreUtil' const LogoutPopupUI = (props: Props) => { const { open, onClose, handleLogoutClick } = props; const theme = useTheme(); const [, { setStateValues }] = useOrder(); const [ordering] = useApi(); const [{ token }] = useSession(); const [, t] = useLanguage(); const [, { setBusiness }] = useBusiness(); const [orientationState] = useDeviceOrientation(); const { control, handleSubmit, errors } = useForm(); const [passwordSee, setPasswordSee] = useState(false); const [logoutState, setLogoutState] = useState({ loading: false, error: false, result: [] }) const fetchPassword = async (body: any) => { try { setLogoutState({ ...logoutState, loading: true }) const response = await fetch(`${ordering.root}/users/check_password`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify(body) }) const { error, result } = await response.json() setLogoutState({ ...logoutState, loading: false, error, result }) return { error, result } } catch (e: any) { setLogoutState({ ...logoutState, loading: false, error: true, result: [e?.message] }) } } const onSubmit = async (values: any) => { const result = await fetchPassword(values) if (result?.result === 'OK') { const res: any = await handleLogoutClick(); if (res) { _clearStoreData({ excludedKeys: ['isTutorial'] }) setBusiness({}) setStateValues({ options: { moment: null }, carts: {}, }) } onClose(); } } return ( } /> {t('ONLY_MANAGER_LOGOUT', 'Only the manager has the password to sign out this App.')} ( setPasswordSee(!passwordSee)} /> : setPasswordSee(!passwordSee)} /> } value={value} onChange={(val: any) => onChange(val)} autoCapitalize='none' returnKeyType='done' /> )} name="password" rules={{ required: t('VALIDATION_ERROR_PASSWORD_REQUIRED', 'The field Password is required').replace('_attribute_', t('PASSWORD', 'Password')) }} defaultValue="" /> {errors?.password?.message && ( {errors?.password?.message} )} {logoutState.error && logoutState.result && ( {logoutState.result[0]} )} ); } interface Props { open: boolean; formState: { loading: boolean, result: { error: boolean, result: any } }, handleLogoutClick: () => void; onClose: () => void; onLogoutDone?: () => void; } export const LogoutPopup = (props: any) => { const logoutProps = { ...props, isNative: true, UIComponent: LogoutPopupUI } return ( ) }