2022-08-27 15:23:11 +05:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
2022-08-26 16:19:39 +05:00
|
|
|
import { View } from "react-native";
|
|
|
|
|
import { useThemeStore } from "../../stores/use-theme-store";
|
|
|
|
|
import { DDS } from "../../services/device-detection";
|
|
|
|
|
import {
|
|
|
|
|
eSubscribeEvent,
|
|
|
|
|
eUnSubscribeEvent
|
|
|
|
|
} from "../../services/event-manager";
|
|
|
|
|
import { getElevation } from "../../utils";
|
|
|
|
|
import { eCloseSimpleDialog, eOpenSimpleDialog } from "../../utils/events";
|
|
|
|
|
import { sleep } from "../../utils/time";
|
|
|
|
|
import Input from "../ui/input";
|
|
|
|
|
import Seperator from "../ui/seperator";
|
|
|
|
|
import { Toast } from "../toast";
|
|
|
|
|
import BaseDialog from "./base-dialog";
|
|
|
|
|
import DialogButtons from "./dialog-buttons";
|
|
|
|
|
import DialogHeader from "./dialog-header";
|
2020-01-17 11:49:31 +05:00
|
|
|
|
2022-08-26 16:19:39 +05:00
|
|
|
export const Dialog = ({ context = "global" }) => {
|
|
|
|
|
const colors = useThemeStore((state) => state.colors);
|
2021-07-01 22:34:18 +05:00
|
|
|
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({
|
2022-08-26 16:19:39 +05:00
|
|
|
title: "",
|
|
|
|
|
paragraph: "",
|
|
|
|
|
positiveText: "Done",
|
|
|
|
|
negativeText: "Cancel",
|
2021-07-01 22:34:18 +05:00
|
|
|
positivePress: () => {},
|
|
|
|
|
onClose: () => {},
|
2022-08-26 16:19:39 +05:00
|
|
|
positiveType: "transparent",
|
2021-07-01 22:34:18 +05:00
|
|
|
icon: null,
|
2021-07-12 10:55:10 +05:00
|
|
|
paragraphColor: colors.pri,
|
|
|
|
|
input: false,
|
2022-08-26 16:19:39 +05:00
|
|
|
inputPlaceholder: "Enter some text",
|
|
|
|
|
defaultValue: "",
|
2022-01-22 12:57:05 +05:00
|
|
|
disableBackdropClosing: false
|
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();
|
2022-08-26 16:19:39 +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
|
|
|
}
|
2022-08-15 21:56:22 +05:00
|
|
|
|
2021-07-01 22:34:18 +05:00
|
|
|
hide();
|
2020-01-17 11:49:31 +05:00
|
|
|
};
|
|
|
|
|
|
2022-08-26 16:19:39 +05:00
|
|
|
const show = (data) => {
|
|
|
|
|
if (!data.context) data.context = "global";
|
2022-01-31 10:40:21 +05:00
|
|
|
if (data.context !== context) return;
|
2021-07-29 14:16:35 +05:00
|
|
|
setDialogInfo(data);
|
|
|
|
|
setVisible(true);
|
2021-07-12 10:55:10 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hide = () => {
|
2022-08-15 21:56:22 +05:00
|
|
|
setInputValue(null);
|
2021-07-12 10:55:10 +05:00
|
|
|
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),
|
2022-08-26 16:19:39 +05:00
|
|
|
width: DDS.isTab ? 400 : "85%",
|
2021-07-01 22:34:18 +05:00
|
|
|
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
|
|
|
|
2022-02-28 13:48:59 +05:00
|
|
|
return visible ? (
|
|
|
|
|
<BaseDialog
|
|
|
|
|
statusBarTranslucent={false}
|
|
|
|
|
bounce={!dialogInfo.input}
|
|
|
|
|
closeOnTouch={!dialogInfo.disableBackdropClosing}
|
|
|
|
|
onShow={async () => {
|
|
|
|
|
if (dialogInfo.input) {
|
|
|
|
|
inputRef.current?.setNativeProps({
|
|
|
|
|
text: dialogInfo.defaultValue
|
|
|
|
|
});
|
|
|
|
|
await sleep(300);
|
|
|
|
|
inputRef.current?.focus();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
visible={true}
|
|
|
|
|
onRequestClose={hide}
|
|
|
|
|
>
|
|
|
|
|
<View style={style}>
|
|
|
|
|
<DialogHeader
|
|
|
|
|
title={dialogInfo.title}
|
|
|
|
|
icon={dialogInfo.icon}
|
|
|
|
|
paragraph={dialogInfo.paragraph}
|
|
|
|
|
paragraphColor={dialogInfo.paragraphColor}
|
|
|
|
|
padding={12}
|
|
|
|
|
/>
|
|
|
|
|
<Seperator half />
|
2020-09-27 13:05:26 +05:00
|
|
|
|
2022-02-28 13:48:59 +05:00
|
|
|
{dialogInfo.input ? (
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
paddingHorizontal: 12
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Input
|
|
|
|
|
fwdRef={inputRef}
|
|
|
|
|
autoCapitalize="none"
|
2022-08-26 16:19:39 +05:00
|
|
|
onChangeText={(value) => {
|
2022-02-28 13:48:59 +05:00
|
|
|
setInputValue(value);
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
2022-04-03 03:35:01 +05:00
|
|
|
testID="input-value"
|
2022-02-28 13:48:59 +05:00
|
|
|
secureTextEntry={dialogInfo.secureTextEntry}
|
|
|
|
|
//defaultValue={dialogInfo.defaultValue}
|
|
|
|
|
onSubmit={onPressPositive}
|
|
|
|
|
returnKeyLabel="Done"
|
|
|
|
|
returnKeyType="done"
|
|
|
|
|
placeholder={dialogInfo.inputPlaceholder}
|
|
|
|
|
/>
|
|
|
|
|
</View>
|
|
|
|
|
) : null}
|
2021-07-12 10:55:10 +05:00
|
|
|
|
2022-02-28 13:48:59 +05:00
|
|
|
<DialogButtons
|
|
|
|
|
onPressNegative={onNegativePress}
|
|
|
|
|
onPressPositive={dialogInfo.positivePress && onPressPositive}
|
|
|
|
|
positiveTitle={dialogInfo.positiveText}
|
|
|
|
|
negativeTitle={dialogInfo.negativeText}
|
|
|
|
|
positiveType={dialogInfo.positiveType}
|
|
|
|
|
/>
|
|
|
|
|
</View>
|
|
|
|
|
<Toast context="local" />
|
|
|
|
|
</BaseDialog>
|
|
|
|
|
) : null;
|
2021-07-01 22:34:18 +05:00
|
|
|
};
|