mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
feat: Add SMS Integration & SMS Message Form.
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import React from 'react';
|
||||
|
||||
import '../../../style/pages/SMSMessage/SMSMessage.scss';
|
||||
import { SMSMessageDialogProvider } from './SMSMessageDialogProvider';
|
||||
import SMSMessageForm from './SMSMessageForm';
|
||||
|
||||
export default function SMSMessageDialogContent({
|
||||
// #ownProps
|
||||
dialogName,
|
||||
notificationkey,
|
||||
}) {
|
||||
return (
|
||||
<SMSMessageDialogProvider
|
||||
dialogName={dialogName}
|
||||
notificationkey={notificationkey}
|
||||
>
|
||||
<SMSMessageForm />
|
||||
</SMSMessageDialogProvider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
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();
|
||||
|
||||
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 };
|
||||
79
src/containers/Dialogs/SMSMessageDialog/SMSMessageForm.js
Normal file
79
src/containers/Dialogs/SMSMessageDialog/SMSMessageForm.js
Normal file
@@ -0,0 +1,79 @@
|
||||
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 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 },
|
||||
},
|
||||
}) => {
|
||||
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,11 @@
|
||||
import * as Yup from 'yup';
|
||||
import intl from 'react-intl-universal';
|
||||
import { DATATYPES_LENGTH } from 'common/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,17 @@
|
||||
import React from 'react';
|
||||
import { Form } from 'formik';
|
||||
|
||||
import SMSMessageFormFields from './SMSMessageFormFields';
|
||||
import SMSMessageFormFloatingActions from './SMSMessageFormFloatingActions';
|
||||
|
||||
/**
|
||||
* SMS message form content.
|
||||
*/
|
||||
export default function SMSMessageFormContent() {
|
||||
return (
|
||||
<Form>
|
||||
<SMSMessageFormFields />
|
||||
<SMSMessageFormFloatingActions />
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import React from 'react';
|
||||
|
||||
import { FastField, Field, ErrorMessage } from 'formik';
|
||||
import { Classes, FormGroup, TextArea } from '@blueprintjs/core';
|
||||
import { FormattedMessage as T, FieldRequiredHint } from 'components';
|
||||
import { inputIntent } from 'utils';
|
||||
|
||||
export default function SMSMessageFormFields() {
|
||||
return (
|
||||
<div className={Classes.DIALOG_BODY}>
|
||||
{/* ----------- 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'} />}
|
||||
>
|
||||
<TextArea
|
||||
growVertically={true}
|
||||
large={true}
|
||||
intent={inputIntent({ error, touched })}
|
||||
{...field}
|
||||
/>
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import React from 'react';
|
||||
import { Intent, Button, Classes } from '@blueprintjs/core';
|
||||
import { useFormikContext } from 'formik';
|
||||
import { 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();
|
||||
|
||||
const { dialogName } = useSMSMessageDialogContext();
|
||||
|
||||
// Handle close button click.
|
||||
const handleCancelBtnClick = () => {
|
||||
closeDialog(dialogName);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={Classes.DIALOG_FOOTER}>
|
||||
<div className={Classes.DIALOG_FOOTER_ACTIONS}>
|
||||
<Button onClick={handleCancelBtnClick} style={{ minWidth: '75px' }}>
|
||||
<T id={'cancel'} />
|
||||
</Button>
|
||||
<Button
|
||||
intent={Intent.PRIMARY}
|
||||
loading={isSubmitting}
|
||||
style={{ minWidth: '75px' }}
|
||||
type="submit"
|
||||
>
|
||||
{<T id={'save'} />}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(withDialogActions)(SMSMessageFormFloatingActions);
|
||||
39
src/containers/Dialogs/SMSMessageDialog/index.js
Normal file
39
src/containers/Dialogs/SMSMessageDialog/index.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import React from 'react';
|
||||
import intl from 'react-intl-universal';
|
||||
import { Dialog, DialogSuspense } from 'components';
|
||||
import withDialogRedux from 'components/DialogReduxConnect';
|
||||
|
||||
import { compose } from 'redux';
|
||||
|
||||
const SMSMessageDialogContent = React.lazy(() =>
|
||||
import('./SMSMessageDialogContent'),
|
||||
);
|
||||
|
||||
/**
|
||||
* SMS Message dialog.
|
||||
*/
|
||||
function SMSMessageDialog({
|
||||
dialogName,
|
||||
payload: { notificationkey },
|
||||
isOpen,
|
||||
}) {
|
||||
return (
|
||||
<Dialog
|
||||
name={dialogName}
|
||||
title={intl.get('sms_message')}
|
||||
isOpen={isOpen}
|
||||
canEscapeJeyClose={true}
|
||||
autoFocus={true}
|
||||
className={'dialog--sms-message'}
|
||||
>
|
||||
<DialogSuspense>
|
||||
<SMSMessageDialogContent
|
||||
dialogName={dialogName}
|
||||
notificationkey={notificationkey}
|
||||
/>
|
||||
</DialogSuspense>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(withDialogRedux())(SMSMessageDialog);
|
||||
Reference in New Issue
Block a user