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

133 lines
3.5 KiB
TypeScript
Raw Normal View History

import React, { useState } from 'react';
2021-08-06 03:09:14 +02:00
import { SessionButton, SessionButtonColor } from '../session/SessionButton';
import { SessionHtmlRenderer } from '../session/SessionHTMLRenderer';
import { SessionIcon, SessionIconSize, SessionIconType } from '../session/icon';
2021-06-07 00:36:48 +02:00
import { DefaultTheme, useTheme, withTheme } from 'styled-components';
2021-08-06 03:09:14 +02:00
import { SessionWrapperModal } from '../session/SessionWrapperModal';
import { updateConfirmModal } from '../../state/ducks/modalDialog';
2021-06-17 08:51:59 +02:00
import { SpacerLG } from '../basic/Text';
2021-08-06 03:09:14 +02:00
import { SessionSpinner } from '../session/SessionSpinner';
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?: () => Promise<void> | void;
onClickClose?: () => any;
onClickCancel?: () => 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;
2021-07-05 09:06:34 +02:00
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,
onClickCancel,
} = props;
2019-12-30 07:38:28 +01:00
const [isLoading, setIsLoading] = useState(false);
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 = async () => {
if (onClickOk) {
setIsLoading(true);
try {
await onClickOk();
} catch (e) {
window.log.warn(e);
} finally {
setIsLoading(false);
}
}
window.inboxStore?.dispatch(updateConfirmModal(null));
2021-06-17 08:51:59 +02:00
};
2021-06-07 00:36:48 +02:00
2021-07-05 09:06:34 +02:00
if (shouldShowConfirm && !shouldShowConfirm) {
return null;
}
/**
* Performs specified on close action then removes the modal.
*/
const onClickCancelHandler = () => {
if (onClickCancel) {
onClickCancel();
}
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}
/>
<SessionSpinner loading={isLoading} />
</div>
<div className="session-modal__button-group">
{!hideCancel && (
2021-06-17 08:51:59 +02:00
<SessionButton
text={cancelText}
buttonColor={closeTheme}
onClick={onClickCancelHandler}
/>
)}
2021-06-22 07:58:37 +02:00
<SessionButton text={okText} buttonColor={okTheme} onClick={onClickOkHandler} />
</div>
2021-06-07 09:12:21 +02:00
</SessionWrapperModal>
);
};
export const SessionConfirm = withTheme(SessionConfirmInner);