Files
notesnook/apps/mobile/src/components/Dialog/index.js

102 lines
2.6 KiB
JavaScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
2021-06-16 08:16:31 +05:00
import { View } from 'react-native';
import { useTracked } from '../../provider';
2021-06-16 08:16:31 +05:00
import { DDS } from '../../services/DeviceDetection';
import {
eSubscribeEvent,
eUnSubscribeEvent
} from '../../services/EventManager';
import { getElevation } from '../../utils';
import { eCloseSimpleDialog, eOpenSimpleDialog } from '../../utils/Events';
2021-06-16 08:16:31 +05:00
import { ph, pv } from '../../utils/SizeUtils';
2020-11-02 20:50:07 +05:00
import Seperator from '../Seperator';
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
export const Dialog = () => {
const [state] = useTracked();
const colors = state.colors;
const [visible, setVisible] = useState(false);
const [dialogInfo, setDialogInfo] = useState({
title: '',
paragraph: '',
positiveText: 'Done',
negativeText: 'Cancel',
positivePress: () => {},
onClose: () => {},
negativeText: hide,
positiveType: 'transparent',
icon: null,
paragraphColor:colors.pri
});
const show = data => {
setDialogInfo({...dialogInfo, ...data});
setVisible(true);
};
const hide = () => {
setVisible(false);
};
useEffect(() => {
eSubscribeEvent(eOpenSimpleDialog, show);
eSubscribeEvent(eCloseSimpleDialog, hide);
return () => {
eUnSubscribeEvent(eOpenSimpleDialog, show);
eUnSubscribeEvent(eCloseSimpleDialog, hide);
2020-01-17 11:49:31 +05:00
};
}, []);
2020-01-17 11:49:31 +05:00
const onPressPositive = async () => {
if (dialogInfo.positivePress) {
await dialogInfo.positivePress();
2020-01-17 11:49:31 +05:00
}
hide();
2020-01-17 11:49:31 +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
hide();
2020-01-17 11:49:31 +05:00
};
2019-12-04 19:38:19 +05:00
const style = {
...getElevation(5),
width: DDS.isTab ? 400 : '85%',
maxHeight: 450,
borderRadius: 5,
backgroundColor: colors.bg,
paddingHorizontal: ph,
paddingVertical: pv,
};
return (
visible && (
<BaseDialog visible={true} onRequestClose={hide}>
<View style={style}>
2020-09-27 13:14:24 +05:00
<DialogHeader
title={dialogInfo.title}
icon={dialogInfo.icon}
paragraph={dialogInfo.paragraph}
paragraphColor={dialogInfo.paragraphColor}
2020-09-27 13:14:24 +05:00
/>
2020-11-02 20:50:07 +05:00
<Seperator />
2020-09-27 13:05:26 +05:00
<DialogButtons
onPressNegative={onNegativePress}
onPressPositive={onPressPositive}
positiveTitle={dialogInfo.positiveText}
negativeTitle={dialogInfo.negativeText}
positiveType={dialogInfo.positiveType}
/>
2019-12-04 19:38:19 +05:00
</View>
2020-09-27 13:05:26 +05:00
</BaseDialog>
)
);
};