-
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.
Demo: Add custom more actions to handmade products grid (#3259)
Co-authored-by: Ricky James Smith <[email protected]>
- Loading branch information
1 parent
332e3f3
commit 711958c
Showing
5 changed files
with
129 additions
and
4 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
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,56 @@ | ||
import { gql, useApolloClient } from "@apollo/client"; | ||
import { Button, CancelButton, CrudMoreActionsMenuContext, Dialog } from "@comet/admin"; | ||
import { Online } from "@comet/admin-icons"; | ||
import { DialogActions, DialogContent, ListItemIcon, MenuItem } from "@mui/material"; | ||
import { useContext, useState } from "react"; | ||
|
||
import { GQLPublishAllProductsMutation, GQLPublishAllProductsMutationVariables } from "./PublishAllProducts.generated"; | ||
|
||
export function PublishAllProducts() { | ||
const [open, setOpen] = useState(false); | ||
const client = useApolloClient(); | ||
const { closeMenu } = useContext(CrudMoreActionsMenuContext); | ||
|
||
const handlePublishAll = async () => { | ||
await client.mutate<GQLPublishAllProductsMutation, GQLPublishAllProductsMutationVariables>({ | ||
mutation: gql` | ||
mutation PublishAllProducts { | ||
publishAllProducts | ||
} | ||
`, | ||
}); | ||
|
||
client.cache.evict({ fieldName: "products" }); | ||
|
||
handleClose(); | ||
}; | ||
|
||
const handleClose = () => { | ||
setOpen(false); | ||
closeMenu(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<MenuItem | ||
onClick={() => { | ||
setOpen(true); | ||
}} | ||
> | ||
<ListItemIcon> | ||
<Online color="success" /> | ||
</ListItemIcon> | ||
Publish all... | ||
</MenuItem> | ||
<Dialog open={open} onClose={handleClose} title="Publish all products?"> | ||
<DialogContent>You are about to publish all products.</DialogContent> | ||
<DialogActions> | ||
<CancelButton onClick={handleClose} /> | ||
<Button onClick={handlePublishAll} startIcon={<Online />} variant="success"> | ||
Publish | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
</> | ||
); | ||
} |
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
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,18 @@ | ||
import { RequiredPermission } from "@comet/cms-api"; | ||
import { InjectRepository } from "@mikro-orm/nestjs"; | ||
import { EntityRepository } from "@mikro-orm/postgresql"; | ||
import { Mutation, Resolver } from "@nestjs/graphql"; | ||
|
||
import { Product, ProductStatus } from "./entities/product.entity"; | ||
|
||
@Resolver(() => Product) | ||
@RequiredPermission(["products"], { skipScopeCheck: true }) | ||
export class CustomProductResolver { | ||
constructor(@InjectRepository(Product) private readonly repository: EntityRepository<Product>) {} | ||
|
||
@Mutation(() => Boolean) | ||
async publishAllProducts(): Promise<boolean> { | ||
await this.repository.nativeUpdate({ status: { $ne: ProductStatus.Published } }, { status: ProductStatus.Published }); | ||
return true; | ||
} | ||
} |
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