Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GraphQL for e-mail template table and update form #354

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion imports/plugins/core/layout/lib/index.js

This file was deleted.

149 changes: 0 additions & 149 deletions imports/plugins/core/layout/lib/reactionLayout.js

This file was deleted.

1 change: 0 additions & 1 deletion imports/plugins/core/layout/server/index.js

This file was deleted.

16 changes: 0 additions & 16 deletions imports/plugins/core/layout/server/publications/templates.js

This file was deleted.

3 changes: 2 additions & 1 deletion imports/plugins/core/orders/client/components/OrderAppBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ OrderAppBar.propTypes = {
classes: PropTypes.object,
order: PropTypes.shape({
_id: PropTypes.string.isRequired,
status: PropTypes.string
status: PropTypes.string,
fulfillmentGroups: PropTypes.array
})
};

Expand Down
212 changes: 212 additions & 0 deletions imports/plugins/core/templates/client/components/EmailTemplateForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import React, { useState } from "react";
import PropTypes from "prop-types";
import i18next from "i18next";
import { useMutation } from "@apollo/react-hooks";
import { useSnackbar } from "notistack";
import SimpleSchema from "simpl-schema";
import { Button, TextField } from "@reactioncommerce/catalyst";
import {
Dialog,
DialogActions,
DialogContent,
DialogTitle,
Grid,
makeStyles
} from "@material-ui/core";
import muiOptions from "reacto-form/cjs/muiOptions";
import useReactoForm from "reacto-form/cjs/useReactoForm";
import updateEmailTemplateGQL from "../graphql/mutations/updateEmailTemplate";

const useStyles = makeStyles((theme) => ({
deleteButton: {
marginRight: "auto"
},
dialogTitle: {
fontSize: 18,
fontWeight: 500
},
legend: {
marginBottom: theme.spacing(1)
}
}));

const emailTemplateSchema = new SimpleSchema({
title: {
type: String
},
subject: {
type: String
},
language: {
type: String,
defaultValue: "en"
},
template: {
type: String
}
});
const validator = emailTemplateSchema.getFormValidator();

/**
* @summary React component that renders the form for updating an e-mail template record.
* @param {Object} props React props
* @return {React.Node} React node
*/
export default function EmailTemplateForm(props) {
const [isSubmitting, setIsSubmitting] = useState(false);
const { enqueueSnackbar } = useSnackbar();

const { emailTemplate, isOpen, onCloseDialog, refetch, shopId } = props;

const onSuccess = () => {
setIsSubmitting(false);
refetch();
onCloseDialog();
};

const onFailure = () => {
setIsSubmitting(false);
onCloseDialog();
enqueueSnackbar(i18next.t("admin.emailTemplate.failure"), { variant: "warning" });
};

const [updateEmailTemplate] = useMutation(updateEmailTemplateGQL, {
ignoreResults: true,
onCompleted() {
onSuccess();
enqueueSnackbar(i18next.t("admin.emailTemplate.updateSuccess"), { variant: "success" });
},
onError() {
onFailure();
}
});

const {
getFirstErrorMessage,
getInputProps,
isDirty,
hasErrors,
submitForm
} = useReactoForm({
async onSubmit(formData) {
setIsSubmitting(true);

if (emailTemplate) {
const emailTemplateInput = emailTemplateSchema.clean(formData);

await updateEmailTemplate({
variables: {
input: {
id: emailTemplate._id,
subject: emailTemplateInput.subject,
title: emailTemplateInput.title,
template: emailTemplateInput.template,
shopId
}
}
});
}
},
validator(formData) {
return validator(emailTemplateSchema.clean(formData));
},
value: emailTemplate,
logErrorsOnSubmit: true
});

const classes = useStyles();

return (
<div>
<Dialog open={isOpen} onClose={onCloseDialog} aria-labelledby="form-dialog-title">
<DialogTitle id="form-dialog-title">
<span className={classes.dialogTitle}>
{i18next.t("templateUpdateForm.emailTemplates.updateEmailTemplate")}
</span>
</DialogTitle>
<DialogContent>
<Grid container spacing={2}>
<Grid item xs={12}>
<TextField
error={hasErrors(["title"])}
fullWidth
helperText={getFirstErrorMessage(["title"])}
label={i18next.t("templateGrid.columns.title")}
placeholder={i18next.t("templateUpdateForm.emailTemplates.placeholder.title")}
{...getInputProps("title", muiOptions)}
/>
</Grid>
<Grid item xs={12}>
<TextField
error={hasErrors(["subject"])}
fullWidth
helperText={getFirstErrorMessage(["subject"])}
label={i18next.t("templateGrid.columns.subject")}
placeholder={i18next.t("templateUpdateForm.emailTemplates.placeholder.subject")}
{...getInputProps("subject", muiOptions)}
/>
</Grid>
<Grid item xs={12}>
<TextField
error={hasErrors(["language"])}
fullWidth
helperText={getFirstErrorMessage(["language"])}
label={i18next.t("templateGrid.columns.language")}
placeholder={i18next.t("templateUpdateForm.emailTemplates.placeholder.language")}
{...getInputProps("language", muiOptions)}
disabled
/>
</Grid>
<Grid item xs={12}>
<TextField
error={hasErrors(["template"])}
fullWidth
helperText={getFirstErrorMessage(["template"])}
label={i18next.t("templateGrid.columns.template")}
placeholder={i18next.t("templateUpdateForm.emailTemplates.placeholder.template")}
multiline
{...getInputProps("template", muiOptions)}
/>
</Grid>
</Grid>
</DialogContent>
<DialogActions>
<Button variant="outlined" onClick={onCloseDialog} color="primary">
{i18next.t("app.cancel")}
</Button>
<Button
disabled={isSubmitting || !isDirty}
onClick={submitForm}
variant="contained"
color="primary"
>
{i18next.t("app.save")}
</Button>
</DialogActions>
</Dialog>
</div>
);
}

EmailTemplateForm.propTypes = {
/**
* An e-mail template code record
*/
emailTemplate: PropTypes.object,
/**
* Determines whether the form dialog is open or not
*/
isOpen: PropTypes.bool,
/**
* Function that closes the form dialog
*/
onCloseDialog: PropTypes.func,
/**
* Function to call after form is successfully submitted
*/
refetch: PropTypes.func,
/**
* Shop ID to create/edit tax rate for
*/
shopId: PropTypes.string
};
Loading