Skip to content

Commit

Permalink
Add error ui for product edit
Browse files Browse the repository at this point in the history
  • Loading branch information
minsoeaung committed Nov 22, 2023
1 parent dcadd43 commit 28d9958
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 29 deletions.
3 changes: 2 additions & 1 deletion API/Middlewares/ExceptionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public async Task InvokeAsync(HttpContext context)
var response = new ProblemDetails()
{
Status = StatusCodes.Status500InternalServerError,
Detail = _env.IsDevelopment() ? ex.StackTrace : null,
Detail = _env.IsDevelopment() ? ex.StackTrace : "Something went wrong.",
Title = ex.Message,
Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.6.1"
};

var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
Expand Down
6 changes: 3 additions & 3 deletions Client/src/api/apiClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ ApiClient.interceptors.response.use(
resolve(response);
})
.catch((error) => {
reject(error);
reject(error.response?.data);
});
})
.catch((error) => {
reject(error);
reject(error.response?.data);
});
});
} else {
return Promise.reject(error);
return Promise.reject(error.response?.data);
}
}
);
21 changes: 21 additions & 0 deletions Client/src/components/ErrorDisplay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Alert, AlertDescription, AlertIcon, AlertTitle, VStack } from '@chakra-ui/react';
import { ApiError } from '../types/apiError.ts';

export const ErrorDisplay = ({ error }: { error: ApiError }) => {
return (
<Alert status="error">
<AlertIcon />
<VStack alignItems="start" spacing={0}>
<AlertTitle>{error?.title || 'Something went wrong'}</AlertTitle>
{error?.detail ? (
<AlertDescription>{error.detail}</AlertDescription>
) : !!error?.errors ? (
Object.keys(error.errors).map((key) => {
const errors = error.errors![key];
return errors.map((error) => <AlertDescription>{error}</AlertDescription>);
})
) : null}
</VStack>
</Alert>
);
};
18 changes: 11 additions & 7 deletions Client/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,17 @@ const Header = () => {
Cart {!!cart?.cartItems.length && `(${cart.cartItems.length})`}
</MenuItem>
{user.roles.some((role) => ['Admin', 'Super'].includes(role)) && (
<MenuItem
as={Link}
to="/inventory?pageSize=10"
icon={<Icon as={MdOutlineInventory2} />}
>
Inventory
</MenuItem>
<>
<MenuDivider />
<MenuItem
as={Link}
to="/inventory?pageSize=10"
icon={<Icon as={MdOutlineInventory2} />}
fontWeight="bold"
>
Inventory
</MenuItem>
</>
)}
<MenuDivider />
<MenuItem onClick={logout} color="red.500">
Expand Down
3 changes: 3 additions & 0 deletions Client/src/hooks/mutations/useProductCUD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ export const useProductCUD = () => {

if (data.type === 'UPDATE') await queryClient.invalidateQueries([PRODUCT_DETAILS, String(data.id)]);
},
// onError: (error) => {
// console.log(error, 'from query');
// },
}
);
};
11 changes: 11 additions & 0 deletions Client/src/pages/Inventory/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AlertDialogFooter,
AlertDialogHeader,
AlertDialogOverlay,
Box,
Button,
Card,
CardBody,
Expand Down Expand Up @@ -42,6 +43,8 @@ import { useBrands } from '../../hooks/queries/useBrands.ts';
import { useProductCUD } from '../../hooks/mutations/useProductCUD.ts';
import { ImageInputWithPreview } from '../../components/ImageInputWithPreview.tsx';
import { BRAND_IMAGES, CATEGORY_IMAGES, PRODUCT_IMAGES } from '../../constants/fileUrls.ts';
import { ErrorDisplay } from '../../components/ErrorDisplay.tsx';
import { ApiError } from '../../types/apiError.ts';

const validType = ['category', 'brand', 'product'] as const;

Expand Down Expand Up @@ -308,6 +311,14 @@ const ProductEdit = ({ id }: { id: number }) => {
)
)}
</Flex>
{!!mutation.error && (
<>
<br />
<Box>
<ErrorDisplay error={mutation.error as ApiError} />
</Box>
</>
)}
<AlertDialog isOpen={isOpen} leastDestructiveRef={cancelRef} onClose={onClose} isCentered>
<AlertDialogOverlay>
<AlertDialogContent>
Expand Down
23 changes: 5 additions & 18 deletions Client/src/pages/User/Cart/index.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
import {
AbsoluteCenter,
Box,
Card,
Center,
Flex,
Heading,
HStack,
Link,
Stack,
useColorModeValue,
} from '@chakra-ui/react';
import { AbsoluteCenter, Box, Card, Flex, Heading, HStack, Link, Stack, useColorModeValue } from '@chakra-ui/react';
import { useCart } from '../../../hooks/queries/useCart.ts';
import { CartItem } from './CartItem.tsx';
import { CartOrderSummary } from './CartOrderSummary.tsx';
import { Link as ReactRouterLink } from 'react-router-dom';
import AntdSpin from '../../../components/AntdSpin';
import { Fallback } from '../../../components/Fallback';
import { ErrorDisplay } from '../../../components/ErrorDisplay.tsx';
import { ApiError } from '../../../types/apiError.ts';

const CartPage = () => {
const { isLoading, isFetching, data, isError } = useCart();
const { isLoading, isFetching, data, isError, error } = useCart();

if (isError) {
return (
<Center mt={10}>
<p>Error loading cart items.</p>
</Center>
);
return <ErrorDisplay error={error as unknown as ApiError} />;
}

if (isLoading) {
Expand Down
7 changes: 7 additions & 0 deletions Client/src/types/apiError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type ApiError = {
status: number;
detail: string | undefined;
title: string;
type: string;
errors: Record<string, string[]> | undefined;
};

0 comments on commit 28d9958

Please sign in to comment.