saleor-dashboard/src/components/AccountPermissions/AccountPermissions.tsx

121 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-06-19 14:40:52 +00:00
import Typography from "@material-ui/core/Typography";
2019-08-09 10:26:22 +00:00
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import CardTitle from "@saleor/components/CardTitle";
import { ControlledCheckbox } from "@saleor/components/ControlledCheckbox";
import { ShopInfo_shop_permissions } from "@saleor/components/Shop/types/ShopInfo";
2019-06-19 14:40:52 +00:00
import Skeleton from "@saleor/components/Skeleton";
2019-10-30 14:34:24 +00:00
const useStyles = makeStyles(
theme => ({
2019-06-19 14:40:52 +00:00
checkboxContainer: {
2019-10-28 16:16:49 +00:00
marginTop: theme.spacing()
2019-06-19 14:40:52 +00:00
},
hr: {
2019-10-30 14:34:24 +00:00
backgroundColor: theme.palette.divider,
2019-06-19 14:40:52 +00:00
border: "none",
height: 1,
marginBottom: 0,
marginTop: 0
}
2019-10-30 14:34:24 +00:00
}),
{ name: "AccountPermissions" }
);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
interface AccountPermissionsProps {
permissions: ShopInfo_shop_permissions[];
2019-06-19 14:40:52 +00:00
data: {
hasFullAccess: boolean;
permissions: string[];
};
disabled: boolean;
onChange: (event: React.ChangeEvent<any>, cb?: () => void) => void;
}
2019-10-30 14:34:24 +00:00
const AccountPermissions: React.FC<AccountPermissionsProps> = props => {
const { data, disabled, permissions, onChange } = props;
const classes = useStyles(props);
const intl = useIntl();
2019-10-30 14:34:24 +00:00
const handleFullAccessChange = (event: React.ChangeEvent<any>) =>
onChange(event, () =>
2019-06-19 14:40:52 +00:00
onChange({
target: {
name: "permissions",
2019-10-30 14:34:24 +00:00
value: event.target.value ? permissions.map(perm => perm.code) : []
2019-06-19 14:40:52 +00:00
}
2019-10-30 14:34:24 +00:00
} as any)
2019-06-19 14:40:52 +00:00
);
2019-10-30 14:34:24 +00:00
const handlePermissionChange = (event: React.ChangeEvent<any>) => {
onChange({
target: {
name: "permissions",
value: event.target.value
? data.permissions.concat([event.target.name])
: data.permissions.filter(perm => perm !== event.target.name)
}
} as any);
};
return (
<Card>
<CardTitle
title={intl.formatMessage({
defaultMessage: "Permissions",
description: "dialog header"
})}
/>
<CardContent>
<Typography>
<FormattedMessage defaultMessage="Expand or restrict user's permissions to access certain part of saleor system." />
</Typography>
<div className={classes.checkboxContainer}>
<ControlledCheckbox
checked={data.hasFullAccess}
disabled={disabled}
label={intl.formatMessage({
defaultMessage: "User has full access to the store",
description: "checkbox label"
})}
name="hasFullAccess"
onChange={handleFullAccessChange}
/>
</div>
</CardContent>
{!data.hasFullAccess && (
<>
<hr className={classes.hr} />
<CardContent>
{permissions === undefined ? (
<Skeleton />
) : (
permissions.map(perm => (
<div key={perm.code}>
<ControlledCheckbox
checked={
data.permissions.filter(
userPerm => userPerm === perm.code
).length === 1
}
disabled={disabled}
label={perm.name.replace(/\./, "")}
name={perm.code}
onChange={handlePermissionChange}
/>
</div>
))
)}
</CardContent>
</>
)}
</Card>
);
};
AccountPermissions.displayName = "AccountPermissions";
export default AccountPermissions;