import { NotificationService } from '@vendure/admin-ui/core'; import { Link, useInjector, useMutation, useQuery } from '@vendure/admin-ui/react'; import gql from 'graphql-tag'; import React from 'react'; const GET_PRODUCTS = gql` query GetProducts($skip: Int, $take: Int) { products(options: { skip: $skip, take: $take }) { items { id name enabled } totalItems } } `; const TOGGLE_ENABLED = gql` mutation ToggleEnabled($id: ID!, $enabled: Boolean!) { updateProduct(input: { id: $id, enabled: $enabled }) { id enabled } } `; export function ProductList() { const { data, loading, error } = useQuery(GET_PRODUCTS, { skip: 0, take: 10 }); const [toggleEnabled] = useMutation(TOGGLE_ENABLED); const notificationService = useInjector(NotificationService); function onToggle(id: string, enabled: boolean) { toggleEnabled({ id, enabled }).then( () => notificationService.success('Updated Product'), reason => notificationService.error(`Couldnt update product: ${reason as string}`), ); } if (loading || !data) return (
| Toggle | State | Product |
|---|---|---|
| {p.enabled ? ( Enabled ) : ( Disabled )} | {p.name} |