diff --git a/backend/DTOs/WarrantyCard/WarrantyCardDTO.cs b/backend/DTOs/WarrantyCard/WarrantyCardDTO.cs index 948eead..8d3789e 100644 --- a/backend/DTOs/WarrantyCard/WarrantyCardDTO.cs +++ b/backend/DTOs/WarrantyCard/WarrantyCardDTO.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using backend.DTOs.Accessory; namespace backend.DTOs.WarrantyCard { @@ -13,7 +14,9 @@ public class WarrantyCardDTO public string? CustomerName { get; set; } public string? AccessoryName { get; set; } public string? DiamondName { get; set; } + public AccessoryDTO? Accessory { get; set; } + public DiamondDTO? Diamond { get; set; } public DateTime StartTime { get; set; } = DateTime.Now; public DateTime EndTime { get; set; } = DateTime.Now.AddMonths(12); } -} \ No newline at end of file +} diff --git a/backend/Enums/WarrantyStatus.cs b/backend/Enums/WarrantyStatus.cs index 4920ecf..d3e98f7 100644 --- a/backend/Enums/WarrantyStatus.cs +++ b/backend/Enums/WarrantyStatus.cs @@ -9,4 +9,4 @@ public enum WarrantyStatus Failed, Completed, } -} \ No newline at end of file +} diff --git a/backend/Repository/WarrantyCardRepository.cs b/backend/Repository/WarrantyCardRepository.cs index 5366df8..6d80298 100644 --- a/backend/Repository/WarrantyCardRepository.cs +++ b/backend/Repository/WarrantyCardRepository.cs @@ -5,8 +5,10 @@ using System.Threading.Tasks; using backend.Data; using backend.DTOs.WarrantyCard; +using backend.Enums; using backend.Helper; using backend.Interfaces; +using backend.Mappers; using backend.Models; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; @@ -33,23 +35,35 @@ public WarrantyCardRepository(ApplicationDbContext context) .WarrantyCards.Include(x => x.Diamond) .ThenInclude(x => x.Shape) .Include(x => x.Accessory) - .Where(x => x.OrderDetail!.Order.CustomerId == userId) + .Where(x => + x.OrderDetail!.Order.CustomerId == userId + && ( + // Only get warranty cards that have no request or have warranty requests that are completed + !x.WarrantyRequests.Any() + || x.WarrantyRequests.Any(wr => + wr.WarrantyStatus == WarrantyStatus.Completed + ) + ) + ) .ToListAsync(); return warrantyCardQueries; } public async Task getWarrantyCards(WarrantyCardQuery query) { - var warrantyCardQueries = _context.WarrantyCards.Include(x => x.OrderDetail) + var warrantyCardQueries = _context + .WarrantyCards.Include(x => x.OrderDetail) .ThenInclude(x => x.Order) + .Include(x => x.Accessory) + .ThenInclude(x => x.AccessoryImages) .AsQueryable(); - // if (query.WarrantyCardId.HasValue) - // { - // warrantyCardQueries = warrantyCardQueries.Where(x => - // x.WarrantyCardId == query.WarrantyCardId - // ); - // } + if (query.WarrantyCardId.HasValue) + { + warrantyCardQueries = warrantyCardQueries.Where(x => + x.WarrantyCardId == query.WarrantyCardId + ); + } // if (query.DiamondId.HasValue) // { // warrantyCardQueries = warrantyCardQueries.Where(x => @@ -80,19 +94,18 @@ public WarrantyCardRepository(ApplicationDbContext context) if (!query.ProductName.IsNullOrEmpty()) { - - warrantyCardQueries = warrantyCardQueries.Where((x) => - x.Accessory != null ? - ( - x.Accessory.Name.Contains(query.ProductName!) - ):( - (x.Diamond!.Carat + " Carat, " + x.Diamond.Shape.Name).Contains(query.ProductName!) - ) + warrantyCardQueries = warrantyCardQueries.Where( + (x) => + x.Accessory != null + ? (x.Accessory.Name.Contains(query.ProductName!)) + : ( + (x.Diamond!.Carat + " Carat, " + x.Diamond.Shape.Name).Contains( + query.ProductName! + ) + ) ); } - - var totalCount = await warrantyCardQueries.CountAsync(); var totalPages = (int)Math.Ceiling(totalCount / (double)query.PageSize); @@ -106,11 +119,19 @@ public WarrantyCardRepository(ApplicationDbContext context) WarrantyCardId = x.WarrantyCardId, DiamondId = x.DiamondId, AccessoryId = x.AccessoryId, + Accessory = x.Accessory != null ? x.Accessory.ToAccessoryDTO() : null, + Diamond = x.Diamond != null ? x.Diamond.ToDiamondDTO() : null, StartTime = x.StartTime, EndTime = x.EndTime, - CustomerName = x.OrderDetail!.Order.Customer != null ? x.OrderDetail.Order.Customer.Name : null, + CustomerName = + x.OrderDetail!.Order.Customer != null + ? x.OrderDetail.Order.Customer.Name + : null, AccessoryName = x.Accessory != null ? x.Accessory.Name : null, - DiamondName = x.Diamond != null ? x.Diamond.Carat + " Carat, " + x.Diamond.Shape.Name : null + DiamondName = + x.Diamond != null + ? x.Diamond.Carat + " Carat, " + x.Diamond.Shape.Name + : null }) .ToListAsync(); @@ -123,6 +144,5 @@ public WarrantyCardRepository(ApplicationDbContext context) TotalCount = totalCount }; } - } } diff --git a/frontend/src/components/nav/TopNav.tsx b/frontend/src/components/nav/TopNav.tsx index 1aa8524..b3297d0 100644 --- a/frontend/src/components/nav/TopNav.tsx +++ b/frontend/src/components/nav/TopNav.tsx @@ -16,6 +16,7 @@ import { useCartStore } from "../../store/cartStore"; import { GET } from "../../utils/request"; import { jwtDecode } from "jwt-decode"; import { getCookie } from "../../utils/cookie"; +import { useQueries } from "@tanstack/react-query"; const TopNavItem = ({ children, @@ -60,16 +61,24 @@ export default function TopNav() { const [diamondDrop, setDiamondDrop] = useState(false); const [menuDrop, setMenuDrop] = useState(false); const { message } = App.useApp(); - const [account, setAccount] = useState(); const location = useLocation(); const navigate = useNavigate(); - useEffect(() => { - const fetchData = async () => { - const response = await GET("/api/Accounts/me"); - setAccount(response); - }; - fetchData(); - }, [location, account]); + // useEffect(() => { + // const fetchData = async () => { + // const response = await GET("/api/Accounts/me"); + // setAccount(response); + // }; + // fetchData(); + // }, [location, account]); + const [info] = useQueries({ + queries: [ + { + queryKey: ["info"], + queryFn: () => GET("/api/Accounts/me"), + staleTime: Infinity, + }, + ], + }); return (
@@ -102,27 +111,27 @@ export default function TopNav() { className="cursor-pointer" onClick={async () => { const response = await GET("/api/Accounts/me"); - console.log(response); - switch(response.role) { - case "Manager": - navigate("/admin"); - break; - case "SaleStaff": - navigate("/admin/sale-staffs"); - break; - case "DeliveryStaff": - navigate("/admin/delivery-staffs"); - break; - case "Administrator": - navigate("/admin"); - break; - case "WarrantyStaff": - navigate("/admin/warranty-request"); - break; - default: - navigate("/"); - break; - } + console.log(response); + switch (response.role) { + case "Manager": + navigate("/admin"); + break; + case "SaleStaff": + navigate("/admin/sale-staffs"); + break; + case "DeliveryStaff": + navigate("/admin/delivery-staffs"); + break; + case "Administrator": + navigate("/admin"); + break; + case "WarrantyStaff": + navigate("/admin/warranty-request"); + break; + default: + navigate("/"); + break; + } }} > @@ -139,9 +148,9 @@ export default function TopNav() { )}
- {account && ( + {info?.data && (
- Welcome, {account?.name} + Welcome, {info?.data?.name}
)}
- Blogs + Blog About us diff --git a/frontend/src/pages/account/CreateWarrantyRequest.tsx b/frontend/src/pages/account/CreateWarrantyRequest.tsx index 7ec3291..101927f 100644 --- a/frontend/src/pages/account/CreateWarrantyRequest.tsx +++ b/frontend/src/pages/account/CreateWarrantyRequest.tsx @@ -11,13 +11,36 @@ import { useQueries } from "@tanstack/react-query"; import Loading from "../../components/Loading"; import moment from "moment"; import dayjs from "dayjs"; +import WarrantyInfo from "./components/WarrantyInfo"; const phoneRegex = new RegExp( /^([+]?[\s0-9]+)?(\d{3}|[(]?[0-9]+[)])?([-]?[\s]?[0-9])+$/ ); +const reasonOptions = [ + { + value: "The ring may need to be resized to fit the wearer's finger", + label: "The ring may need to be resized to fit the wearer's finger", + }, + { + value: "The prongs holding the diamond in place are loose or broken", + label: "The prongs holding the diamond in place are loose or broken", + }, + { + value: + "Requiring cleaning and polishing to restore diamond ring appearance", + label: + "Requiring cleaning and polishing to restore diamond ring appearance", + }, + { + value: "Upgrade the diamond or redesign the setting for a new look.", + label: "Upgrade the diamond or redesign the setting for a new look.", + }, + { value: "Other", label: "Other" }, +]; + const schema = z.object({ - warrantyCardId: z.coerce.number().min(0, "Please choose your product!"), + warrantyCardId: z.coerce.number().min(1, "Please choose your product!"), phoneNumber: z .string() .regex(phoneRegex, "Invalid phone number!") @@ -40,10 +63,13 @@ type Option = { value: string; label: string; key: number; + info: any; }; export default function CreateWarrantyRequest() { const [isLoading, setIsLoading] = useState(false); + const [warrantyCardId, setWarrantyCardId] = useState(0); + const [reason, setReason] = useState(""); const navigate = useNavigate(); const [messageApi, contextHolder] = message.useMessage(); const [warrantyCards] = useQueries({ @@ -62,25 +88,10 @@ export default function CreateWarrantyRequest() { ? `${warrantyCard?.diamond?.carat} ct ${warrantyCard?.diamond?.shape?.name} Shape Diamond #${warrantyCard?.diamond.certificateNumber}` : warrantyCard?.accessory?.name, })); - // const options: Option[] = warrantyCards?.data?.orders - // ?.map((order: any) => - // order.orderDetails.map((orderDetail: any) => ({ - // key: orderDetail.warrantyCardId, - // value: orderDetail.warrantyCardId, - // label: `${orderDetail?.diamond.carat} ct ${ - // orderDetail?.diamond.shape - // } Shape Diamond #${orderDetail?.diamond.certificateNumber} ${ - // orderDetail?.accessory?.name - // ? "+ " + orderDetail?.accessory?.name - // : "" - // }`, - // })) - // ) - // .flat(); console.log(warrantyCards?.data); - const { control, handleSubmit, reset } = useForm({ + const { control, handleSubmit, reset, setValue, getValues } = useForm({ defaultValues: { warrantyCardId: "", warrantyReason: "", @@ -109,7 +120,7 @@ export default function CreateWarrantyRequest() { } })(); }, [reset]); - + console.log(getValues().warrantyCardId); return ( <> {contextHolder} @@ -119,7 +130,6 @@ export default function CreateWarrantyRequest() {
Request a warranty service
-
{ + setWarrantyCardId(value); + }} /> + - - +
+ * Warranty + reason +
+
, @@ -141,7 +155,7 @@ export default function Filter() { /> */}
{ + return ( +
+ {description} +
+ + More detail + +
+ ); +}; + export default function SliderItem({ mark, min, max, defaultValue, - title, + info, step, debounceCallback, }: { step?: number; - title: string; + info: any; mark: { [key: number]: string }; min: number; max: number; @@ -19,10 +39,19 @@ export default function SliderItem({ debounceCallback: any; }) { const debounced = useDebouncedCallback(debounceCallback, 800); - return (
-
{title}
+
+ BY {info.title.toUpperCase()} + + + +
{