2021-07-12 10:55:10 +05:00
|
|
|
import React, {useEffect, useRef, useState} from 'react';
|
|
|
|
|
import {View} from 'react-native';
|
|
|
|
|
import {useTracked} from '../../provider';
|
|
|
|
|
import {DDS} from '../../services/DeviceDetection';
|
|
|
|
|
import {eSubscribeEvent, eUnSubscribeEvent} from '../../services/EventManager';
|
|
|
|
|
import {getElevation} from '../../utils';
|
|
|
|
|
import {eCloseSimpleDialog, eOpenSimpleDialog} from '../../utils/Events';
|
|
|
|
|
import {ph, pv} from '../../utils/SizeUtils';
|
|
|
|
|
import {sleep} from '../../utils/TimeUtils';
|
|
|
|
|
import Input from '../Input';
|
2020-11-02 20:50:07 +05:00
|
|
|
import Seperator from '../Seperator';
|
2021-11-08 15:05:48 +05:00
|
|
|
import {Toast} from '../Toast';
|
2020-09-27 13:05:26 +05:00
|
|
|
import BaseDialog from './base-dialog';
|
2020-09-27 13:14:24 +05:00
|
|
|
import DialogButtons from './dialog-buttons';
|
|
|
|
|
import DialogHeader from './dialog-header';
|
2020-01-17 11:49:31 +05:00
|
|
|
|
2021-07-24 13:15:18 +05:00
|
|
|
export const Dialog = ({context = 'global'}) => {
|
2021-07-01 22:34:18 +05:00
|
|
|
const [state] = useTracked();
|
|
|
|
|
const colors = state.colors;
|
|
|
|
|
const [visible, setVisible] = useState(false);
|
2021-07-12 10:55:10 +05:00
|
|
|
const [inputValue, setInputValue] = useState(null);
|
|
|
|
|
const inputRef = useRef();
|
2021-07-01 22:34:18 +05:00
|
|
|
const [dialogInfo, setDialogInfo] = useState({
|
|
|
|
|
title: '',
|
|
|
|
|
paragraph: '',
|
|
|
|
|
positiveText: 'Done',
|
|
|
|
|
negativeText: 'Cancel',
|
|
|
|
|
positivePress: () => {},
|
|
|
|
|
onClose: () => {},
|
|
|
|
|
positiveType: 'transparent',
|
|
|
|
|
icon: null,
|
2021-07-12 10:55:10 +05:00
|
|
|
paragraphColor: colors.pri,
|
|
|
|
|
input: false,
|
|
|
|
|
inputPlaceholder: 'Enter some text',
|
2021-07-24 13:15:18 +05:00
|
|
|
defaultValue: ''
|
2021-07-01 22:34:18 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
eSubscribeEvent(eOpenSimpleDialog, show);
|
|
|
|
|
eSubscribeEvent(eCloseSimpleDialog, hide);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
eUnSubscribeEvent(eOpenSimpleDialog, show);
|
|
|
|
|
eUnSubscribeEvent(eCloseSimpleDialog, hide);
|
2020-01-17 11:49:31 +05:00
|
|
|
};
|
2021-07-01 22:34:18 +05:00
|
|
|
}, []);
|
2020-01-17 11:49:31 +05:00
|
|
|
|
2021-07-01 22:34:18 +05:00
|
|
|
const onPressPositive = async () => {
|
|
|
|
|
if (dialogInfo.positivePress) {
|
2021-07-12 10:55:10 +05:00
|
|
|
inputRef.current?.blur();
|
2021-11-08 15:05:48 +05:00
|
|
|
let result = await dialogInfo.positivePress(
|
|
|
|
|
inputValue || dialogInfo.defaultValue
|
|
|
|
|
);
|
2021-07-30 10:26:51 +05:00
|
|
|
if (result === false) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-01-17 11:49:31 +05:00
|
|
|
}
|
2021-07-01 22:34:18 +05:00
|
|
|
hide();
|
2020-01-17 11:49:31 +05:00
|
|
|
};
|
|
|
|
|
|
2021-07-12 10:55:10 +05:00
|
|
|
const show = data => {
|
2021-07-29 14:16:35 +05:00
|
|
|
if (data.context && data.context !== context) return;
|
|
|
|
|
setDialogInfo(data);
|
|
|
|
|
setVisible(true);
|
2021-07-12 10:55:10 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hide = () => {
|
|
|
|
|
setVisible(false);
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-01 22:34:18 +05:00
|
|
|
const onNegativePress = async () => {
|
|
|
|
|
if (dialogInfo.onClose) {
|
|
|
|
|
await dialogInfo.onClose();
|
2020-02-02 16:18:52 +05:00
|
|
|
}
|
2019-12-04 19:38:19 +05:00
|
|
|
|
2021-07-01 22:34:18 +05:00
|
|
|
hide();
|
2020-01-17 11:49:31 +05:00
|
|
|
};
|
2019-12-04 19:38:19 +05:00
|
|
|
|
2021-07-01 22:34:18 +05:00
|
|
|
const style = {
|
|
|
|
|
...getElevation(5),
|
|
|
|
|
width: DDS.isTab ? 400 : '85%',
|
|
|
|
|
maxHeight: 450,
|
|
|
|
|
borderRadius: 5,
|
|
|
|
|
backgroundColor: colors.bg,
|
2021-11-08 15:05:48 +05:00
|
|
|
paddingTop: 12
|
2021-07-01 22:34:18 +05:00
|
|
|
};
|
2021-07-12 10:55:10 +05:00
|
|
|
|
2021-07-01 22:34:18 +05:00
|
|
|
return (
|
|
|
|
|
visible && (
|
2021-07-12 10:55:10 +05:00
|
|
|
<BaseDialog
|
|
|
|
|
statusBarTranslucent={false}
|
|
|
|
|
onShow={async () => {
|
|
|
|
|
if (dialogInfo.input) {
|
|
|
|
|
await sleep(100);
|
|
|
|
|
inputRef.current?.focus();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
visible={true}
|
|
|
|
|
onRequestClose={hide}>
|
2021-07-01 22:34:18 +05:00
|
|
|
<View style={style}>
|
2020-09-27 13:14:24 +05:00
|
|
|
<DialogHeader
|
2021-07-01 22:34:18 +05:00
|
|
|
title={dialogInfo.title}
|
|
|
|
|
icon={dialogInfo.icon}
|
|
|
|
|
paragraph={dialogInfo.paragraph}
|
|
|
|
|
paragraphColor={dialogInfo.paragraphColor}
|
2021-11-08 15:05:48 +05:00
|
|
|
padding={12}
|
2020-09-27 13:14:24 +05:00
|
|
|
/>
|
2021-07-30 10:26:51 +05:00
|
|
|
<Seperator half />
|
2020-09-27 13:05:26 +05:00
|
|
|
|
2021-07-12 10:55:10 +05:00
|
|
|
{dialogInfo.input && (
|
2021-11-08 15:05:48 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
paddingHorizontal: 12
|
|
|
|
|
}}>
|
|
|
|
|
<Input
|
|
|
|
|
fwdRef={inputRef}
|
|
|
|
|
autoCapitalize="none"
|
|
|
|
|
onChangeText={value => {
|
|
|
|
|
setInputValue(value);
|
|
|
|
|
}}
|
|
|
|
|
secureTextEntry={dialogInfo.secureTextEntry}
|
|
|
|
|
defaultValue={dialogInfo.defaultValue}
|
|
|
|
|
onSubmit={onPressPositive}
|
|
|
|
|
returnKeyLabel="Done"
|
|
|
|
|
returnKeyType="done"
|
|
|
|
|
placeholder={dialogInfo.inputPlaceholder}
|
|
|
|
|
/>
|
|
|
|
|
</View>
|
2021-07-12 10:55:10 +05:00
|
|
|
)}
|
|
|
|
|
|
2021-07-01 22:34:18 +05:00
|
|
|
<DialogButtons
|
|
|
|
|
onPressNegative={onNegativePress}
|
2021-11-24 12:30:02 +05:00
|
|
|
onPressPositive={dialogInfo.positivePress && onPressPositive}
|
2021-07-01 22:34:18 +05:00
|
|
|
positiveTitle={dialogInfo.positiveText}
|
|
|
|
|
negativeTitle={dialogInfo.negativeText}
|
|
|
|
|
positiveType={dialogInfo.positiveType}
|
|
|
|
|
/>
|
2019-12-04 19:38:19 +05:00
|
|
|
</View>
|
2021-11-08 15:05:48 +05:00
|
|
|
<Toast context="local" />
|
2020-09-27 13:05:26 +05:00
|
|
|
</BaseDialog>
|
2021-07-01 22:34:18 +05:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
};
|