-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add select-grid, assign-grid and assigned-grid
- Loading branch information
Showing
4 changed files
with
393 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { gql, useApolloClient, useQuery } from "@apollo/client"; | ||
import { Savable } from "@comet/admin"; | ||
import { CircularProgress } from "@mui/material"; | ||
import { | ||
GQLGetProductIdsForProductCategoryQuery, | ||
GQLGetProductIdsForProductCategoryQueryVariables, | ||
GQLSetProductCategoryMutation, | ||
GQLSetProductCategoryMutationVariables, | ||
} from "@src/products/categories/AssignProductsGrid.generated"; | ||
import { ProductsSelectGrid } from "@src/products/categories/ProductsSelectGrid"; | ||
import isEqual from "lodash.isequal"; | ||
import React, { useEffect, useMemo, useState } from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
const setProductCategoryMutation = gql` | ||
mutation SetProductCategory($id: ID!, $input: [ID!]!) { | ||
updateProductCategory(id: $id, input: { products: $input }) { | ||
id | ||
} | ||
} | ||
`; | ||
|
||
const getProductIdsForProductCategory = gql` | ||
query GetProductIdsForProductCategory($id: ID!) { | ||
products(filter: { category: { equal: $id } }) { | ||
nodes { | ||
id | ||
} | ||
} | ||
} | ||
`; | ||
|
||
interface FormProps { | ||
productCategoryId: string; | ||
} | ||
|
||
export function AssignProductsGrid({ productCategoryId }: FormProps): React.ReactElement { | ||
const client = useApolloClient(); | ||
|
||
const { data, error, loading } = useQuery<GQLGetProductIdsForProductCategoryQuery, GQLGetProductIdsForProductCategoryQueryVariables>( | ||
getProductIdsForProductCategory, | ||
{ | ||
variables: { id: productCategoryId }, | ||
}, | ||
); | ||
|
||
const initialValues = useMemo(() => { | ||
return data?.products.nodes ? data.products.nodes.map((product) => product.id) : []; | ||
}, [data]); | ||
const [values, setValues] = useState<string[]>(initialValues); | ||
useEffect(() => { | ||
setValues(initialValues); | ||
}, [initialValues]); | ||
|
||
if (error) return <FormattedMessage id="common.error" defaultMessage="An error has occured. Please try again at later" />; | ||
if (loading) return <CircularProgress />; | ||
|
||
return ( | ||
<> | ||
<Savable | ||
doSave={async () => { | ||
await client.mutate<GQLSetProductCategoryMutation, GQLSetProductCategoryMutationVariables>({ | ||
mutation: setProductCategoryMutation, | ||
variables: { id: productCategoryId, input: values }, | ||
update: (cache, result) => cache.evict({ fieldName: "products" }), | ||
}); | ||
return true; | ||
}} | ||
hasChanges={!isEqual(initialValues.sort(), values.sort())} | ||
doReset={() => { | ||
setValues(initialValues); | ||
}} | ||
/> | ||
<ProductsSelectGrid | ||
dataGridProps={{ | ||
checkboxSelection: true, | ||
keepNonExistentRowsSelected: true, | ||
selectionModel: values, | ||
onSelectionModelChange: (newSelectionModel) => { | ||
setValues(newSelectionModel.map((rowId) => String(rowId))); | ||
}, | ||
}} | ||
/> | ||
</> | ||
); | ||
} |
57 changes: 57 additions & 0 deletions
57
demo/admin/src/products/categories/AssignedProductsGrid.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { CancelButton, SaveBoundary, SaveBoundarySaveButton } from "@comet/admin"; | ||
import { Add as AddIcon } from "@comet/admin-icons"; | ||
import { Button, Dialog, DialogActions, DialogContent, DialogTitle } from "@mui/material"; | ||
import { AssignProductsGrid } from "@src/products/categories/AssignProductsGrid"; | ||
import { ProductsGrid } from "@src/products/ProductsGrid"; | ||
import React from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
type Props = { | ||
productCategoryId: string; | ||
}; | ||
|
||
export function AssignedProductsGrid({ productCategoryId }: Props): React.ReactElement { | ||
const [isOpen, setIsOpen] = React.useState(false); | ||
|
||
const handleCloseDialog = () => { | ||
setIsOpen(false); | ||
}; | ||
return ( | ||
<> | ||
<ProductsGrid | ||
hideCrudContextMenu={true} | ||
toolbarAction={ | ||
<Button startIcon={<AddIcon />} onClick={() => setIsOpen(true)} variant="contained" color="primary"> | ||
<FormattedMessage id="products.editProductCategory.assignProducts" defaultMessage="Assign Products" /> | ||
</Button> | ||
} | ||
filter={{ category: { equal: productCategoryId } }} | ||
/> | ||
<SaveBoundary | ||
onAfterSave={() => { | ||
setIsOpen(false); | ||
}} | ||
> | ||
<Dialog open={isOpen} onClose={handleCloseDialog} fullWidth maxWidth="xl"> | ||
<DialogTitle> | ||
<FormattedMessage id="products.editProductCategory.assignProducts" defaultMessage="Assign Products" /> | ||
</DialogTitle> | ||
<DialogContent | ||
sx={{ | ||
height: "70vh", | ||
padding: 0, | ||
paddingTop: "0 !important" /* is connected to title-style */, | ||
}} | ||
> | ||
<AssignProductsGrid productCategoryId={productCategoryId} /> | ||
</DialogContent> | ||
<DialogActions> | ||
{/* TODO Missing close-dialog-unsaved-changes-check */} | ||
<CancelButton onClick={handleCloseDialog} /> | ||
<SaveBoundarySaveButton disabled={false} /> | ||
</DialogActions> | ||
</Dialog> | ||
</SaveBoundary> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.