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

228 lines
6.5 KiB
TypeScript
Raw Normal View History

import {
Button,
CircularProgress,
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
TableBody,
TableCell,
TableRow,
TextField,
Typography
} from "@material-ui/core";
2019-08-09 10:17:04 +00:00
import Checkbox from "@saleor/components/Checkbox";
import ConfirmButton, {
ConfirmButtonTransitionState
} from "@saleor/components/ConfirmButton";
2019-11-13 13:00:53 +00:00
import ResponsiveTable from "@saleor/components/ResponsiveTable";
Page types (#807) * Create attribute class selector * Use ProductAttributeType to check if product is simple or with variants * Allow attribute class selection only during its creation * Update attribute type selection translations * Show only product attributes in columns picker on product list view * Cleanups in Attribute Organization component * Create Page Types list page * Create content management section in settings * Implement page types list view * Remove unused imports from page type list * Updatte page type list style * Remove legacy code from page type list component * Update PageTypeListPage component * Create Page Types details page * Fix page type attribute reordering * Implement PageType create view * Implement PageType update view * gUpdate page type details components * Fix page type update component * Update page type components stories * Update page type errors handling * Update page type details view * Create Page Types details page * Implement PageType create view * Update product attribute assignment mutations * Add page types attribute assignment mutations * Add page types attribute assignment handling * Temporarily fix page create mutation * Update page type error messages * Remove legacy storybook page type stories * Update attribute assignment dialogs stories * Update page type details error handling * Update props for page type components * Create attribute class selector * Implement page types list view * Add page type selector on page create and details views * Add attributes list to page details views * Update page types list * Use attribute errors for attributes muatations * Save attribute values on page create and update * Update messages for page view * Update page attributes fragment * Use AttributeError in AttributeBulkDelete * Update page type and its attribute selection * Handle page types deleting * Update page types deleting messages * Handle page types attribute reorder * Fix PageOrganizeContent component types * Update graphqql types * Fix page fixture * Update messages * Update test snapshots * Pass pageTypes to PageForm * Update changelog with page type addition note * Update package-lock * Update test snapshots * Fix malformed generated type * Update messages after rebase
2020-11-19 14:42:14 +00:00
import { AvailableAttributeFragment } from "@saleor/fragments/types/AvailableAttributeFragment";
2019-11-04 13:36:25 +00:00
import useElementScroll, {
isScrolledToBottom
} from "@saleor/hooks/useElementScroll";
2019-08-09 10:17:04 +00:00
import useModalDialogErrors from "@saleor/hooks/useModalDialogErrors";
import useModalDialogOpen from "@saleor/hooks/useModalDialogOpen";
import useSearchQuery from "@saleor/hooks/useSearchQuery";
import { buttonMessages } from "@saleor/intl";
Use MacawUI (#1229) * Replace withStyleswith useStyles (#1100) * Replace withStyleswith useStyles * Update messages * Use rem as a spacing unit (#1101) * Use rems as spacing units * Fix visual bugs * Update stories * Use macaw-ui as theme provider (#1108) * Use macaw ui as a theme provider * Add react-dom to aliases * Fix jest module resolution * Update useTheme hook usage * Fix test wrapper * Use macaw from git repo * Fix CI * Update stories * Fix aliasing * Extract savebar to macaw ui (#1146) * wip * Use savebar from macaw * Use confirm button from macaw * Improve file structure * Use sidebar context from macaw * Update macaw * Update macaw version * Remove savebar from storybook * Update stories * Use alerts and notifications from macaw (#1166) * Use alerts from macaw * Add notifications from macaw * Update stories * Pin macaw version * Encapsulate limit reached in one component * Remove unused imports * Use backlinks from macaw (#1183) * Use backlink from macaw * Update macaw version * Use macaw sidebar (#1148) * Use sidebar from macaw * Use shipped logo * Use lowercase * Update stories * Use user chip from macaw (#1191) * Use user chip from macaw * Use dedicated components for menu items * Simplify code * Bump version and fix types (#1210) * Rename onBack to onClick * Rename UserChip to UserChipMenu * Rename IMenuItem to SidebarMenuItem * Update macaw version * Fix tables after changes in macaw (#1220) * Update macaw version * Update changelog * Update stories * Fix after rebase * Update to macaw 0.2.0 * Lint files * Update macaw to 0.2.2
2021-07-21 08:59:52 +00:00
import { makeStyles } from "@saleor/macaw-ui";
2019-08-09 10:17:04 +00:00
import { maybe, renderCollection } from "@saleor/misc";
import { FetchMoreProps } from "@saleor/types";
import classNames from "classnames";
import React from "react";
import InfiniteScroll from "react-infinite-scroll-component";
import { FormattedMessage, useIntl } from "react-intl";
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
actions: {
boxShadow: "0px 4px 4px rgba(0, 0, 0, 0.25)"
},
checkboxCell: {
paddingLeft: 0
},
dropShadow: {
boxShadow: `0px -5px 10px 0px ${theme.palette.divider}`
},
loadMoreLoaderContainer: {
alignItems: "center",
display: "flex",
height: theme.spacing(3),
justifyContent: "center"
},
scrollArea: {
overflowY: "scroll"
},
wideCell: {
width: "100%"
}
}),
{ name: "AssignAttributeDialog" }
);
2019-08-09 10:17:04 +00:00
export interface AssignAttributeDialogProps extends FetchMoreProps {
confirmButtonState: ConfirmButtonTransitionState;
errors: string[];
2019-08-09 10:17:04 +00:00
open: boolean;
Page types (#807) * Create attribute class selector * Use ProductAttributeType to check if product is simple or with variants * Allow attribute class selection only during its creation * Update attribute type selection translations * Show only product attributes in columns picker on product list view * Cleanups in Attribute Organization component * Create Page Types list page * Create content management section in settings * Implement page types list view * Remove unused imports from page type list * Updatte page type list style * Remove legacy code from page type list component * Update PageTypeListPage component * Create Page Types details page * Fix page type attribute reordering * Implement PageType create view * Implement PageType update view * gUpdate page type details components * Fix page type update component * Update page type components stories * Update page type errors handling * Update page type details view * Create Page Types details page * Implement PageType create view * Update product attribute assignment mutations * Add page types attribute assignment mutations * Add page types attribute assignment handling * Temporarily fix page create mutation * Update page type error messages * Remove legacy storybook page type stories * Update attribute assignment dialogs stories * Update page type details error handling * Update props for page type components * Create attribute class selector * Implement page types list view * Add page type selector on page create and details views * Add attributes list to page details views * Update page types list * Use attribute errors for attributes muatations * Save attribute values on page create and update * Update messages for page view * Update page attributes fragment * Use AttributeError in AttributeBulkDelete * Update page type and its attribute selection * Handle page types deleting * Update page types deleting messages * Handle page types attribute reorder * Fix PageOrganizeContent component types * Update graphqql types * Fix page fixture * Update messages * Update test snapshots * Pass pageTypes to PageForm * Update changelog with page type addition note * Update package-lock * Update test snapshots * Fix malformed generated type * Update messages after rebase
2020-11-19 14:42:14 +00:00
attributes: AvailableAttributeFragment[];
2019-08-09 10:17:04 +00:00
selected: string[];
onClose: () => void;
2019-08-12 14:11:10 +00:00
onFetch: (query: string) => void;
2019-08-09 10:17:04 +00:00
onOpen: () => void;
onSubmit: () => void;
onToggle: (id: string) => void;
}
const scrollableTargetId = "assignAttributeScrollableDialog";
2019-08-09 10:17:04 +00:00
const AssignAttributeDialog: React.FC<AssignAttributeDialogProps> = ({
attributes,
confirmButtonState,
errors: apiErrors,
hasMore,
loading,
open,
selected,
onClose,
onFetch,
onFetchMore,
onOpen,
onSubmit,
onToggle
}: AssignAttributeDialogProps) => {
const intl = useIntl();
2019-08-09 10:17:04 +00:00
const classes = useStyles({});
const [query, onQueryChange, resetQuery] = useSearchQuery(onFetch);
const errors = useModalDialogErrors(apiErrors, open);
2019-11-04 13:36:25 +00:00
const anchor = React.useRef(null);
const position = useElementScroll(anchor);
2019-08-09 10:17:04 +00:00
useModalDialogOpen(open, {
onClose: resetQuery,
onOpen
});
return (
<Dialog onClose={onClose} open={open} fullWidth maxWidth="sm">
<DialogTitle>
<FormattedMessage
defaultMessage="Assign Attribute"
description="dialog header"
/>
</DialogTitle>
2019-08-09 10:17:04 +00:00
<DialogContent>
<TextField
name="query"
value={query}
onChange={onQueryChange}
label={intl.formatMessage({
defaultMessage: "Search Attributes"
2019-08-09 10:17:04 +00:00
})}
placeholder={intl.formatMessage({
defaultMessage: "Search by attribute name"
2019-08-09 10:17:04 +00:00
})}
fullWidth
InputProps={{
autoComplete: "off",
endAdornment: loading && <CircularProgress size={16} />
}}
/>
</DialogContent>
<DialogContent
className={classes.scrollArea}
ref={anchor}
id={scrollableTargetId}
>
2019-08-09 10:17:04 +00:00
<InfiniteScroll
dataLength={attributes?.length}
next={onFetchMore}
2019-08-09 10:17:04 +00:00
hasMore={hasMore}
scrollThreshold="100px"
2019-08-09 10:17:04 +00:00
loader={
<div className={classes.loadMoreLoaderContainer}>
<CircularProgress size={16} />
</div>
}
scrollableTarget={scrollableTargetId}
2019-08-09 10:17:04 +00:00
>
2019-11-04 14:25:23 +00:00
<ResponsiveTable key="table">
2019-08-09 10:17:04 +00:00
<TableBody>
{renderCollection(
attributes,
attribute => {
if (!attribute) {
return null;
}
const isChecked = !!selected.find(
selectedAttribute => selectedAttribute === attribute.id
);
return (
<TableRow key={maybe(() => attribute.id)}>
<TableCell
padding="checkbox"
className={classes.checkboxCell}
>
<Checkbox
checked={isChecked}
onChange={() => onToggle(attribute.id)}
/>
</TableCell>
<TableCell className={classes.wideCell}>
{attribute.name}
<Typography variant="caption">
{attribute.slug}
</Typography>
</TableCell>
</TableRow>
);
},
() =>
!loading && (
<TableRow>
<TableCell colSpan={2}>
<FormattedMessage defaultMessage="No results found" />
2019-08-09 10:17:04 +00:00
</TableCell>
</TableRow>
)
)}
</TableBody>
2019-11-04 14:25:23 +00:00
</ResponsiveTable>
2019-08-09 10:17:04 +00:00
</InfiniteScroll>
</DialogContent>
{errors.length > 0 && (
<DialogContent>
{errors.map((error, errorIndex) => (
<DialogContentText color="error" key={errorIndex}>
{error}
2019-08-09 10:17:04 +00:00
</DialogContentText>
))}
</DialogContent>
)}
2019-11-04 13:36:25 +00:00
<DialogActions
className={classNames(classes.actions, {
[classes.dropShadow]: !isScrolledToBottom(anchor, position)
})}
>
2019-08-09 10:17:04 +00:00
<Button onClick={onClose}>
2019-10-21 14:46:12 +00:00
<FormattedMessage {...buttonMessages.back} />
2019-08-09 10:17:04 +00:00
</Button>
<ConfirmButton
transitionState={confirmButtonState}
color="primary"
variant="contained"
type="submit"
onClick={onSubmit}
>
<FormattedMessage
defaultMessage="Assign attributes"
description="button"
/>
2019-08-09 10:17:04 +00:00
</ConfirmButton>
</DialogActions>
</Dialog>
);
};
AssignAttributeDialog.displayName = "AssignAttributeDialog";
export default AssignAttributeDialog;