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

Demo: Use grid in dialog as multi-select, forward data-grid-props #2428

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions demo/admin/src/common/MasterMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,6 @@ export const masterMenuData: MasterMenuData = [
component: ProductsPage,
},
},
{
type: "route",
primary: <FormattedMessage id="menu.productCategories" defaultMessage="Categories" />,
route: {
path: "/product-categories",
component: ProductCategoriesPage,
},
},
{
type: "route",
primary: <FormattedMessage id="menu.productTags" defaultMessage="Tags" />,
Expand Down Expand Up @@ -303,6 +295,14 @@ export const masterMenuData: MasterMenuData = [
component: ManufacturersHandmadePage,
},
},
{
type: "route",
primary: <FormattedMessage id="menu.productCategories" defaultMessage="Categories" />,
route: {
path: "/product-categories",
component: ProductCategoriesPage,
},
},
],
},
],
Expand Down
82 changes: 82 additions & 0 deletions demo/admin/src/products/categories/AssignProductsGrid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { gql, useApolloClient, useQuery } from "@apollo/client";
import { Loading, Savable } from "@comet/admin";
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, { 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 },
onCompleted: (data) => {
setValues(data.products.nodes.map((product) => product.id));
},
},
);

const [values, setValues] = useState<string[]>([]);

if (error) return <FormattedMessage id="common.error" defaultMessage="An error has occured. Please try again at later" />;
if (loading) return <Loading />;

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((data?.products.nodes.map((product) => product.id) ?? []).sort(), values.sort())}
doReset={() => {
setValues(data?.products.nodes.map((product) => product.id) ?? []);
}}
/>
<ProductsSelectGrid
dataGridProps={{
checkboxSelection: true,
keepNonExistentRowsSelected: true,
selectionModel: values,
onSelectionModelChange: (newSelectionModel) => {
setValues(newSelectionModel.map((rowId) => String(rowId)));
},
}}
/>
</>
);
}
Loading
Loading