This module provides a complete inventory management system for products using SWR + GraphQL-request for data fetching and state management.
- Product Grid View: Display products in a responsive card layout
- CRUD Operations: Add, Edit, Delete products with form validation
- Search & Pagination: Search products and paginate through results
- Optimistic Updates: Immediate UI updates for better UX
- Loading States: Skeleton screens and loading indicators
- Error Handling: Comprehensive error handling with toast notifications
/libs/inventory/
├── types.ts # TypeScript interfaces and types
├── gql.ts # GraphQL client configuration
├── queries.ts # GraphQL queries and mutations
├── hooks.ts # SWR hooks with optimistic updates
└── index.ts # Library exports
/page-components/products/inventory/
├── InventoryCard.tsx # Product card component
├── InventoryFormModal.tsx # Add/Edit product modal
├── ConfirmDialog.tsx # Delete confirmation dialog
├── GridEmpty.tsx # Empty state component
├── GridSkeleton.tsx # Loading skeleton component
├── InventoryManagePage.tsx # Main page component
└── index.ts # Component exports
/pages/products/inventory/manage/
└── index.tsx # Next.js page route
- swr: Data fetching and caching
- graphql: GraphQL query language
- graphql-request: Lightweight GraphQL client
- react-hook-form: Form state management and validation
- @chakra-ui/react: UI components
- Environment Variable: Ensure
NEXT_PUBLIC_INVENTORY_GRAPHQL_API_URLis set in your environment - GraphQL API: The API should implement the following schema:
type Product {
_id: String!
name: String!
description: String
price: Float
salePrice: Float
quantity: Int
image: String
published: Boolean
deleted: Boolean
createdAt: String
updatedAt: String
totalCount: Int
}
input paginationInput {
page: Int!
limit: Int!
search: String
}
input productInput {
name: String!
description: String
price: Float
salePrice: Float
quantity: Int
image: String
published: Boolean
}
input productUpdateInput {
_id: String!
name: String
description: String
price: Float
salePrice: Float
quantity: Int
image: String
published: Boolean
}
type Query {
products(paginationInput: paginationInput!): [Product!]!
product(productId: String!): Product
}
type Mutation {
addProduct(productInput: productInput!): Product!
updateProduct(productUpdateInput: productUpdateInput!): Product!
deleteProduct(productId: String!): Product!
}Navigate to /products/inventory/manage to access the inventory management interface.
- Product Cards: Each product is displayed in a card with image, name, description, price, and action buttons
- Add Product: Click the "Add Product" button to open the form modal
- Edit Product: Click "Edit" on any product card to modify it
- Delete Product: Click "Delete" to remove a product (with confirmation)
- Search: Use the search bar to filter products by name or description
- Pagination: Navigate through multiple pages of products
- SWR Hooks: Handle data fetching with automatic caching and revalidation
- Optimistic Updates: UI updates immediately while API calls run in background
- Error Handling: Failed operations show error messages and revert optimistic updates
- Cache Management: Manual cache invalidation after successful mutations
- Responsive Design: Works on desktop, tablet, and mobile
- Loading States: Skeleton screens during data loading
- Empty States: Friendly UI when no products exist
- Form Validation: Real-time validation with error messages
- Toast Notifications: Success and error feedback
The module uses GraphQL with the following operations:
products()- Fetch paginated product listproduct()- Fetch single product detailsaddProduct()- Create new productupdateProduct()- Update existing productdeleteProduct()- Delete product
All mutations include optimistic updates for immediate UI feedback.