mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
re-structure to monorepo.
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
|
||||
import '@/style/pages/SMSMessage/SMSMessage.scss';
|
||||
import { SMSMessageDialogProvider } from './SMSMessageDialogProvider';
|
||||
import SMSMessageForm from './SMSMessageForm';
|
||||
|
||||
/**
|
||||
* SMS message dialog content.
|
||||
*/
|
||||
export default function SMSMessageDialogContent({
|
||||
// #ownProps
|
||||
dialogName,
|
||||
notificationkey,
|
||||
}) {
|
||||
return (
|
||||
<SMSMessageDialogProvider
|
||||
dialogName={dialogName}
|
||||
notificationkey={notificationkey}
|
||||
>
|
||||
<SMSMessageForm />
|
||||
</SMSMessageDialogProvider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { DialogContent } from '@/components';
|
||||
import {
|
||||
useSettingEditSMSNotification,
|
||||
useSettingSMSNotification,
|
||||
} from '@/hooks/query';
|
||||
|
||||
const SMSMessageDialogContext = React.createContext();
|
||||
|
||||
/**
|
||||
* SMS Message dialog provider.
|
||||
*/
|
||||
function SMSMessageDialogProvider({ notificationkey, dialogName, ...props }) {
|
||||
// Edit SMS message notification mutations.
|
||||
const { mutateAsync: editSMSNotificationMutate } =
|
||||
useSettingEditSMSNotification();
|
||||
|
||||
// SMS notificiation details
|
||||
const { data: smsNotification, isLoading: isSMSNotificationLoading } =
|
||||
useSettingSMSNotification(notificationkey);
|
||||
|
||||
// provider.
|
||||
const provider = {
|
||||
dialogName,
|
||||
smsNotification,
|
||||
editSMSNotificationMutate,
|
||||
};
|
||||
|
||||
return (
|
||||
<DialogContent isLoading={isSMSNotificationLoading}>
|
||||
<SMSMessageDialogContext.Provider value={provider} {...props} />
|
||||
</DialogContent>
|
||||
);
|
||||
}
|
||||
|
||||
const useSMSMessageDialogContext = () =>
|
||||
React.useContext(SMSMessageDialogContext);
|
||||
|
||||
export { SMSMessageDialogProvider, useSMSMessageDialogContext };
|
||||
@@ -0,0 +1,12 @@
|
||||
// @ts-nocheck
|
||||
import * as Yup from 'yup';
|
||||
import intl from 'react-intl-universal';
|
||||
import { DATATYPES_LENGTH } from '@/constants/dataTypes';
|
||||
|
||||
const Schema = Yup.object().shape({
|
||||
notification_key: Yup.string().required(),
|
||||
is_notification_enabled: Yup.boolean(),
|
||||
message_text: Yup.string().min(3).max(DATATYPES_LENGTH.TEXT),
|
||||
});
|
||||
|
||||
export const CreateSMSMessageFormSchema = Schema;
|
||||
@@ -0,0 +1,81 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import intl from 'react-intl-universal';
|
||||
import { Formik } from 'formik';
|
||||
import { omit } from 'lodash';
|
||||
import { Intent } from '@blueprintjs/core';
|
||||
|
||||
import { AppToaster } from '@/components';
|
||||
|
||||
import SMSMessageFormContent from './SMSMessageFormContent';
|
||||
import { CreateSMSMessageFormSchema } from './SMSMessageForm.schema';
|
||||
import { useSMSMessageDialogContext } from './SMSMessageDialogProvider';
|
||||
import { transformErrors } from './utils';
|
||||
|
||||
import withDialogActions from '@/containers/Dialog/withDialogActions';
|
||||
|
||||
import { compose, transformToForm } from '@/utils';
|
||||
|
||||
const defaultInitialValues = {
|
||||
notification_key: '',
|
||||
is_notification_enabled: '',
|
||||
message_text: '',
|
||||
};
|
||||
|
||||
/**
|
||||
* SMS Message form.
|
||||
*/
|
||||
function SMSMessageForm({
|
||||
// #withDialogActions
|
||||
closeDialog,
|
||||
}) {
|
||||
const { dialogName, smsNotification, editSMSNotificationMutate } =
|
||||
useSMSMessageDialogContext();
|
||||
|
||||
// Initial form values.
|
||||
const initialValues = {
|
||||
...defaultInitialValues,
|
||||
...transformToForm(smsNotification, defaultInitialValues),
|
||||
notification_key: smsNotification.key,
|
||||
message_text: smsNotification.sms_message,
|
||||
};
|
||||
|
||||
// Handles the form submit.
|
||||
const handleFormSubmit = (values, { setSubmitting, setErrors }) => {
|
||||
const form = {
|
||||
...omit(values, ['is_notification_enabled', 'sms_message']),
|
||||
notification_key: smsNotification.key,
|
||||
};
|
||||
// Handle request response success.
|
||||
const onSuccess = (response) => {
|
||||
AppToaster.show({
|
||||
message: intl.get('sms_message.dialog.success_message'),
|
||||
intent: Intent.SUCCESS,
|
||||
});
|
||||
closeDialog(dialogName);
|
||||
};
|
||||
// Handle request response errors.
|
||||
const onError = ({
|
||||
response: {
|
||||
data: { errors },
|
||||
},
|
||||
}) => {
|
||||
if (errors) {
|
||||
transformErrors(errors, { setErrors });
|
||||
}
|
||||
setSubmitting(false);
|
||||
};
|
||||
editSMSNotificationMutate(form).then(onSuccess).catch(onError);
|
||||
};
|
||||
|
||||
return (
|
||||
<Formik
|
||||
validationSchema={CreateSMSMessageFormSchema}
|
||||
initialValues={initialValues}
|
||||
onSubmit={handleFormSubmit}
|
||||
component={SMSMessageFormContent}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(withDialogActions)(SMSMessageForm);
|
||||
@@ -0,0 +1,112 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import intl from 'react-intl-universal';
|
||||
import styled from 'styled-components';
|
||||
import { Form, useFormikContext } from 'formik';
|
||||
import { Classes } from '@blueprintjs/core';
|
||||
import { castArray } from 'lodash';
|
||||
|
||||
import SMSMessageFormFields from './SMSMessageFormFields';
|
||||
import SMSMessageFormFloatingActions from './SMSMessageFormFloatingActions';
|
||||
|
||||
import { useSMSMessageDialogContext } from './SMSMessageDialogProvider';
|
||||
import { SMSMessagePreview } from '@/components';
|
||||
import { getSMSUnits } from '@/containers/NotifyViaSMS/utils';
|
||||
|
||||
/**
|
||||
* SMS message form content.
|
||||
*/
|
||||
export default function SMSMessageFormContent() {
|
||||
// SMS message dialog context.
|
||||
const { smsNotification } = useSMSMessageDialogContext();
|
||||
|
||||
// Ensure always returns array.
|
||||
const messageVariables = React.useMemo(
|
||||
() => castArray(smsNotification.allowed_variables),
|
||||
[smsNotification.allowed_variables],
|
||||
);
|
||||
|
||||
return (
|
||||
<Form>
|
||||
<div className={Classes.DIALOG_BODY}>
|
||||
<FormContent>
|
||||
<FormFields>
|
||||
<SMSMessageFormFields />
|
||||
<SMSMessageVariables>
|
||||
{messageVariables.map(({ variable, description }) => (
|
||||
<MessageVariable>
|
||||
<strong>{`{${variable}}`}</strong> {description}
|
||||
</MessageVariable>
|
||||
))}
|
||||
</SMSMessageVariables>
|
||||
</FormFields>
|
||||
|
||||
<FormPreview>
|
||||
<SMSMessagePreviewSection />
|
||||
</FormPreview>
|
||||
</FormContent>
|
||||
</div>
|
||||
<SMSMessageFormFloatingActions />
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* SMS Message preview section.
|
||||
* @returns {JSX}
|
||||
*/
|
||||
function SMSMessagePreviewSection() {
|
||||
const {
|
||||
values: { message_text: message },
|
||||
} = useFormikContext();
|
||||
|
||||
const messagesUnits = getSMSUnits(message);
|
||||
|
||||
return (
|
||||
<SMSPreviewSectionRoot>
|
||||
<SMSMessagePreview message={message} />
|
||||
<SMSPreviewSectionNote>
|
||||
{intl.formatHTMLMessage(
|
||||
{ id: 'sms_message.dialog.sms_note' },
|
||||
{
|
||||
value: messagesUnits,
|
||||
},
|
||||
)}
|
||||
</SMSPreviewSectionNote>
|
||||
</SMSPreviewSectionRoot>
|
||||
);
|
||||
}
|
||||
|
||||
const SMSPreviewSectionRoot = styled.div``;
|
||||
|
||||
const SMSPreviewSectionNote = styled.div`
|
||||
font-size: 12px;
|
||||
opacity: 0.7;
|
||||
`;
|
||||
|
||||
const SMSMessageVariables = styled.div`
|
||||
list-style: none;
|
||||
font-size: 12px;
|
||||
opacity: 0.9;
|
||||
`;
|
||||
|
||||
const MessageVariable = styled.div`
|
||||
margin-bottom: 8px;
|
||||
`;
|
||||
|
||||
const FormContent = styled.div`
|
||||
display: flex;
|
||||
`;
|
||||
|
||||
const FormFields = styled.div`
|
||||
width: 55%;
|
||||
`;
|
||||
|
||||
const FormPreview = styled.div`
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
width: 45%;
|
||||
padding-left: 25px;
|
||||
margin-left: 25px;
|
||||
border-left: 1px solid #dcdcdd;
|
||||
`;
|
||||
@@ -0,0 +1,66 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { useFormikContext, FastField, ErrorMessage } from 'formik';
|
||||
import { Intent, Button, FormGroup, TextArea } from '@blueprintjs/core';
|
||||
|
||||
import { FormattedMessage as T } from '@/components';
|
||||
|
||||
import { useSMSMessageDialogContext } from './SMSMessageDialogProvider';
|
||||
|
||||
import { inputIntent } from '@/utils';
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
export default function SMSMessageFormFields() {
|
||||
// SMS message dialog context.
|
||||
const { smsNotification } = useSMSMessageDialogContext();
|
||||
|
||||
// Form formik context.
|
||||
const { setFieldValue } = useFormikContext();
|
||||
|
||||
// Handle the button click.
|
||||
const handleBtnClick = () => {
|
||||
setFieldValue('message_text', smsNotification.default_sms_message);
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
{/* ----------- Message Text ----------- */}
|
||||
<FastField name={'message_text'}>
|
||||
{({ field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={<T id={'notify_via_sms.dialog.message_text'} />}
|
||||
className={'form-group--message_text'}
|
||||
intent={inputIntent({ error, touched })}
|
||||
helperText={
|
||||
<>
|
||||
<ErrorMessage name={'message_text'} />
|
||||
<ResetButton
|
||||
minimal={true}
|
||||
small={true}
|
||||
intent={Intent.PRIMARY}
|
||||
onClick={handleBtnClick}
|
||||
>
|
||||
<T id={'sms_message.edit_form.reset_to_default_message'} />
|
||||
</ResetButton>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<TextArea
|
||||
growVertically={true}
|
||||
large={true}
|
||||
intent={inputIntent({ error, touched })}
|
||||
{...field}
|
||||
/>
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const ResetButton = styled(Button)`
|
||||
font-size: 12px;
|
||||
`;
|
||||
@@ -0,0 +1,53 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { Intent, Button } from '@blueprintjs/core';
|
||||
import { useFormikContext } from 'formik';
|
||||
|
||||
import {
|
||||
DialogFooter,
|
||||
DialogFooterActions,
|
||||
FormattedMessage as T,
|
||||
} from '@/components';
|
||||
import { useSMSMessageDialogContext } from './SMSMessageDialogProvider';
|
||||
import withDialogActions from '@/containers/Dialog/withDialogActions';
|
||||
|
||||
import { compose } from '@/utils';
|
||||
|
||||
/**
|
||||
* SMS Message Form floating actions.
|
||||
*/
|
||||
function SMSMessageFormFloatingActions({
|
||||
// #withDialogActions
|
||||
closeDialog,
|
||||
}) {
|
||||
// Formik context.
|
||||
const { isSubmitting } = useFormikContext();
|
||||
|
||||
// SMS Message dialog contxt.
|
||||
const { dialogName } = useSMSMessageDialogContext();
|
||||
|
||||
// Handle close button click.
|
||||
const handleCancelBtnClick = () => {
|
||||
closeDialog(dialogName);
|
||||
};
|
||||
|
||||
return (
|
||||
<DialogFooter>
|
||||
<DialogFooterActions alignment={'left'}>
|
||||
<Button
|
||||
intent={Intent.PRIMARY}
|
||||
loading={isSubmitting}
|
||||
style={{ minWidth: '75px' }}
|
||||
type="submit"
|
||||
>
|
||||
<T id={'save_sms_message'} />
|
||||
</Button>
|
||||
<Button onClick={handleCancelBtnClick} style={{ minWidth: '75px' }}>
|
||||
<T id={'cancel'} />
|
||||
</Button>
|
||||
</DialogFooterActions>
|
||||
</DialogFooter>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(withDialogActions)(SMSMessageFormFloatingActions);
|
||||
@@ -0,0 +1,40 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import intl from 'react-intl-universal';
|
||||
import { Dialog, DialogSuspense } from '@/components';
|
||||
import withDialogRedux from '@/components/DialogReduxConnect';
|
||||
|
||||
import { compose } from '@/utils';
|
||||
|
||||
const SMSMessageDialogContent = React.lazy(() =>
|
||||
import('./SMSMessageDialogContent'),
|
||||
);
|
||||
|
||||
/**
|
||||
* SMS Message dialog.
|
||||
*/
|
||||
function SMSMessageDialog({
|
||||
dialogName,
|
||||
payload: { notificationkey },
|
||||
isOpen,
|
||||
}) {
|
||||
return (
|
||||
<Dialog
|
||||
name={dialogName}
|
||||
title={intl.get('sms_message.dialog.label')}
|
||||
isOpen={isOpen}
|
||||
canEscapeJeyClose={true}
|
||||
autoFocus={true}
|
||||
className={'dialog--sms-message'}
|
||||
>
|
||||
<DialogSuspense>
|
||||
<SMSMessageDialogContent
|
||||
dialogName={dialogName}
|
||||
notificationkey={notificationkey}
|
||||
/>
|
||||
</DialogSuspense>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(withDialogRedux())(SMSMessageDialog);
|
||||
@@ -0,0 +1,20 @@
|
||||
// @ts-nocheck
|
||||
import { Intent } from '@blueprintjs/core';
|
||||
import { castArray } from 'lodash';
|
||||
|
||||
export const transformErrors = (errors, { setErrors }) => {
|
||||
let unsupportedVariablesError = errors.find(
|
||||
(error) => error.type === 'UNSUPPORTED_SMS_MESSAGE_VARIABLES',
|
||||
);
|
||||
if (unsupportedVariablesError) {
|
||||
const variables = castArray(
|
||||
unsupportedVariablesError.data.unsupported_args,
|
||||
);
|
||||
const stringifiedVariables = variables.join(', ');
|
||||
|
||||
setErrors({
|
||||
message_text: `The SMS message has unsupported variables - ${stringifiedVariables}`,
|
||||
intent: Intent.DANGER,
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user