2021-05-14 08:15:15 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Dialog,
|
|
|
|
DialogActions,
|
|
|
|
DialogContent,
|
|
|
|
DialogTitle,
|
|
|
|
TextField
|
|
|
|
} from "@material-ui/core";
|
2019-06-19 14:40:52 +00:00
|
|
|
import ConfirmButton, {
|
|
|
|
ConfirmButtonTransitionState
|
|
|
|
} from "@saleor/components/ConfirmButton";
|
|
|
|
import Form from "@saleor/components/Form";
|
|
|
|
import FormSpacer from "@saleor/components/FormSpacer";
|
2020-07-07 10:14:12 +00:00
|
|
|
import { StaffErrorFragment } from "@saleor/fragments/types/StaffErrorFragment";
|
2020-02-24 14:14:48 +00:00
|
|
|
import useModalDialogErrors from "@saleor/hooks/useModalDialogErrors";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { buttonMessages, commonMessages } from "@saleor/intl";
|
2021-07-21 08:59:52 +00:00
|
|
|
import { makeStyles } from "@saleor/macaw-ui";
|
2020-04-23 15:43:08 +00:00
|
|
|
import { SearchPermissionGroups_search_edges_node } from "@saleor/searches/types/SearchPermissionGroups";
|
|
|
|
import { FetchMoreProps, SearchPageProps } from "@saleor/types";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { getFormErrors } from "@saleor/utils/errors";
|
2020-04-20 11:11:07 +00:00
|
|
|
import getStaffErrorMessage from "@saleor/utils/errors/staff";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
|
|
|
import { FormattedMessage, useIntl } from "react-intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-04-23 15:43:08 +00:00
|
|
|
export interface AddMemberFormData {
|
2019-06-19 14:40:52 +00:00
|
|
|
email: string;
|
|
|
|
firstName: string;
|
|
|
|
lastName: string;
|
2020-04-23 15:43:08 +00:00
|
|
|
permissionGroups: string[];
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 15:43:08 +00:00
|
|
|
const initialForm: AddMemberFormData = {
|
2019-06-19 14:40:52 +00:00
|
|
|
email: "",
|
|
|
|
firstName: "",
|
2020-04-23 15:43:08 +00:00
|
|
|
lastName: "",
|
|
|
|
permissionGroups: []
|
2019-06-19 14:40:52 +00:00
|
|
|
};
|
|
|
|
|
2019-12-03 15:28:40 +00:00
|
|
|
const useStyles = makeStyles(
|
|
|
|
theme => ({
|
|
|
|
hr: {
|
|
|
|
backgroundColor: "#eaeaea",
|
|
|
|
border: "none",
|
|
|
|
height: 1,
|
|
|
|
marginBottom: 0
|
|
|
|
},
|
|
|
|
sectionTitle: {
|
|
|
|
fontWeight: 600 as 600,
|
|
|
|
marginBottom: theme.spacing(),
|
|
|
|
marginTop: theme.spacing(2)
|
|
|
|
},
|
|
|
|
textFieldGrid: {
|
|
|
|
display: "grid",
|
|
|
|
gridColumnGap: theme.spacing(2),
|
|
|
|
gridTemplateColumns: "1fr 1fr"
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
{ name: "StaffAddMemberDialog" }
|
|
|
|
);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-04-23 15:43:08 +00:00
|
|
|
interface StaffAddMemberDialogProps extends SearchPageProps {
|
|
|
|
availablePermissionGroups: SearchPermissionGroups_search_edges_node[];
|
2019-06-19 14:40:52 +00:00
|
|
|
confirmButtonState: ConfirmButtonTransitionState;
|
2020-04-23 15:43:08 +00:00
|
|
|
disabled: boolean;
|
2020-04-20 11:11:07 +00:00
|
|
|
errors: StaffErrorFragment[];
|
2020-04-23 15:43:08 +00:00
|
|
|
fetchMorePermissionGroups: FetchMoreProps;
|
2019-06-19 14:40:52 +00:00
|
|
|
open: boolean;
|
|
|
|
onClose: () => void;
|
2020-04-23 15:43:08 +00:00
|
|
|
onConfirm: (data: AddMemberFormData) => void;
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const StaffAddMemberDialog: React.FC<StaffAddMemberDialogProps> = props => {
|
2020-04-23 15:43:08 +00:00
|
|
|
const { confirmButtonState, errors, onClose, onConfirm, open } = props;
|
2020-03-11 09:55:14 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const classes = useStyles(props);
|
2020-02-24 14:14:48 +00:00
|
|
|
const dialogErrors = useModalDialogErrors(errors, open);
|
2019-10-30 14:34:24 +00:00
|
|
|
const intl = useIntl();
|
2020-03-11 09:55:14 +00:00
|
|
|
const formErrors = getFormErrors(
|
|
|
|
["firstName", "lastName", "email"],
|
|
|
|
dialogErrors
|
|
|
|
);
|
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
return (
|
|
|
|
<Dialog onClose={onClose} open={open}>
|
2020-02-24 14:14:48 +00:00
|
|
|
<Form initial={initialForm} onSubmit={onConfirm}>
|
2020-04-23 15:43:08 +00:00
|
|
|
{({ change, data: formData, hasChanged }) => (
|
2019-10-30 14:34:24 +00:00
|
|
|
<>
|
|
|
|
<DialogTitle>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Invite Staff Member"
|
|
|
|
description="dialog header"
|
|
|
|
/>
|
|
|
|
</DialogTitle>
|
|
|
|
<DialogContent>
|
|
|
|
<div className={classes.textFieldGrid}>
|
2019-06-19 14:40:52 +00:00
|
|
|
<TextField
|
2020-03-11 09:55:14 +00:00
|
|
|
error={!!formErrors.firstName}
|
2020-04-23 15:43:08 +00:00
|
|
|
helperText={
|
|
|
|
!!formErrors.firstName &&
|
|
|
|
getStaffErrorMessage(formErrors.firstName, intl)
|
|
|
|
}
|
2019-10-30 14:34:24 +00:00
|
|
|
label={intl.formatMessage(commonMessages.firstName)}
|
|
|
|
name="firstName"
|
|
|
|
type="text"
|
2020-04-23 15:43:08 +00:00
|
|
|
value={formData.firstName}
|
2019-06-19 14:40:52 +00:00
|
|
|
onChange={change}
|
|
|
|
/>
|
2019-10-30 14:34:24 +00:00
|
|
|
<TextField
|
2020-03-11 09:55:14 +00:00
|
|
|
error={!!formErrors.lastName}
|
2020-04-23 15:43:08 +00:00
|
|
|
helperText={
|
|
|
|
!!formErrors.lastName &&
|
|
|
|
getStaffErrorMessage(formErrors.lastName, intl)
|
|
|
|
}
|
2019-10-30 14:34:24 +00:00
|
|
|
label={intl.formatMessage(commonMessages.lastName)}
|
|
|
|
name="lastName"
|
|
|
|
type="text"
|
2020-04-23 15:43:08 +00:00
|
|
|
value={formData.lastName}
|
2019-06-19 14:40:52 +00:00
|
|
|
onChange={change}
|
|
|
|
/>
|
2019-10-30 14:34:24 +00:00
|
|
|
</div>
|
|
|
|
<FormSpacer />
|
|
|
|
<TextField
|
2020-03-11 09:55:14 +00:00
|
|
|
error={!!formErrors.email}
|
2019-10-30 14:34:24 +00:00
|
|
|
fullWidth
|
2020-04-23 15:43:08 +00:00
|
|
|
helperText={
|
|
|
|
!!formErrors.email &&
|
|
|
|
getStaffErrorMessage(formErrors.email, intl)
|
|
|
|
}
|
2019-10-30 14:34:24 +00:00
|
|
|
label={intl.formatMessage(commonMessages.email)}
|
|
|
|
name="email"
|
|
|
|
type="email"
|
2020-04-23 15:43:08 +00:00
|
|
|
value={formData.email}
|
2019-10-30 14:34:24 +00:00
|
|
|
onChange={change}
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
2020-04-23 15:43:08 +00:00
|
|
|
<hr className={classes.hr} />
|
2019-10-30 14:34:24 +00:00
|
|
|
<DialogActions>
|
|
|
|
<Button onClick={onClose}>
|
|
|
|
<FormattedMessage {...buttonMessages.back} />
|
|
|
|
</Button>
|
|
|
|
<ConfirmButton
|
2021-06-07 11:35:53 +00:00
|
|
|
data-test="submit"
|
2019-10-30 14:34:24 +00:00
|
|
|
color="primary"
|
|
|
|
disabled={!hasChanged}
|
|
|
|
variant="contained"
|
|
|
|
type="submit"
|
|
|
|
transitionState={confirmButtonState}
|
|
|
|
>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Send invite"
|
|
|
|
description="button"
|
|
|
|
/>
|
|
|
|
</ConfirmButton>
|
|
|
|
</DialogActions>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Form>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
StaffAddMemberDialog.displayName = "StaffAddMemberDialog";
|
|
|
|
export default StaffAddMemberDialog;
|