session-desktop/ts/components/session/SessionConfirm.tsx

120 lines
3.2 KiB
TypeScript
Raw Normal View History

2019-12-30 07:38:28 +01:00
import React from 'react';
import { SessionModal } from './SessionModal';
2020-01-08 07:21:39 +01:00
import { SessionButton, SessionButtonColor } from './SessionButton';
2020-07-30 07:01:11 +02:00
import { SessionHtmlRenderer } from './SessionHTMLRenderer';
import { SessionIcon, SessionIconSize, SessionIconType } from './icon';
2021-06-07 00:36:48 +02:00
import { DefaultTheme, useTheme, withTheme } from 'styled-components';
import { SessionWrapperModal } from './SessionWrapperModal';
import { useDispatch } from 'react-redux';
import { updateConfirmModal } from '../../state/ducks/modalDialog';
import { update } from 'lodash';
2021-06-17 08:51:59 +02:00
import { SpacerLG } from '../basic/Text';
2019-12-30 07:38:28 +01:00
export interface SessionConfirmDialogProps {
message?: string;
2021-06-07 00:36:48 +02:00
messageSub?: string;
title?: string;
2019-12-30 07:38:28 +01:00
onOk?: any;
onClose?: any;
onClickOk?: () => any;
onClickClose?: () => any;
2019-12-30 07:38:28 +01:00
okText?: string;
cancelText?: string;
2021-06-07 00:36:48 +02:00
hideCancel?: boolean;
2021-06-08 06:04:09 +02:00
okTheme?: SessionButtonColor;
2021-06-07 00:36:48 +02:00
closeTheme?: SessionButtonColor;
sessionIcon?: SessionIconType;
iconSize?: SessionIconSize;
2021-06-07 00:36:48 +02:00
theme?: DefaultTheme;
closeAfterClickOk?: boolean;
shouldShowConfirm?: () => boolean | undefined;
2021-06-17 08:51:59 +02:00
}
2019-12-30 07:38:28 +01:00
const SessionConfirmInner = (props: SessionConfirmDialogProps) => {
const {
title = '',
message = '',
messageSub = '',
okTheme = SessionButtonColor.Primary,
closeTheme = SessionButtonColor.Primary,
onClickOk,
onClickClose,
hideCancel = false,
sessionIcon,
iconSize,
shouldShowConfirm,
} = props;
2019-12-30 07:38:28 +01:00
const okText = props.okText || window.i18n('ok');
const cancelText = props.cancelText || window.i18n('cancel');
const showHeader = !!props.title;
2019-12-30 07:38:28 +01:00
2021-06-07 00:36:48 +02:00
const theme = useTheme();
const messageSubText = messageSub ? 'session-confirm-main-message' : 'subtle';
2019-12-30 07:38:28 +01:00
const onClickOkHandler = () => {
if (onClickOk) {
onClickOk();
}
window.inboxStore?.dispatch(updateConfirmModal(null));
2021-06-17 08:51:59 +02:00
};
2021-06-07 00:36:48 +02:00
if (shouldShowConfirm && !shouldShowConfirm()) {
return null;
}
/**
* Performs specified on close action then removes the modal.
*/
const onClickCancelHandler = () => {
if (onClickClose) {
onClickClose();
}
window.inboxStore?.dispatch(updateConfirmModal(null));
2021-06-17 08:51:59 +02:00
};
return (
2021-06-07 09:12:21 +02:00
<SessionWrapperModal
2021-06-07 00:36:48 +02:00
title={title}
onClose={onClickClose}
showExitIcon={false}
showHeader={showHeader}
2021-06-07 09:12:21 +02:00
>
2021-06-17 08:51:59 +02:00
{!showHeader && <SpacerLG />}
2019-12-30 07:38:28 +01:00
<div className="session-modal__centered">
{sessionIcon && iconSize && (
<>
2021-06-07 00:36:48 +02:00
<SessionIcon iconType={sessionIcon} iconSize={iconSize} theme={theme} />
2021-06-17 08:51:59 +02:00
<SpacerLG />
</>
)}
2020-01-03 01:48:55 +01:00
2021-04-22 10:03:58 +02:00
<SessionHtmlRenderer tag="span" className={messageSubText} html={message} />
<SessionHtmlRenderer
tag="span"
className="session-confirm-sub-message subtle"
html={messageSub}
/>
</div>
<div className="session-modal__button-group">
<SessionButton text={okText} buttonColor={okTheme} onClick={onClickOkHandler} />
2020-01-03 01:48:55 +01:00
{!hideCancel && (
2021-06-17 08:51:59 +02:00
<SessionButton
text={cancelText}
buttonColor={closeTheme}
onClick={onClickCancelHandler}
/>
)}
</div>
2021-06-07 09:12:21 +02:00
</SessionWrapperModal>
);
};
export const SessionConfirm = withTheme(SessionConfirmInner);