use the backend properly
This commit is contained in:
parent
be7afb4569
commit
ca371e1006
@ -1,8 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { products } from "../../data/products";
|
import { products } from "../../data/products";
|
||||||
import discountCodes from "../../data/discountCodes.json";
|
|
||||||
import type { DiscountCode } from "../../types/discount";
|
import type { DiscountCode } from "../../types/discount";
|
||||||
|
|
||||||
export default function AdminPage() {
|
export default function AdminPage() {
|
||||||
@ -215,12 +214,28 @@ function ProductsManager() {
|
|||||||
|
|
||||||
function DiscountManager() {
|
function DiscountManager() {
|
||||||
const [discountList, setDiscountList] =
|
const [discountList, setDiscountList] =
|
||||||
useState<Record<string, DiscountCode>>(discountCodes);
|
useState<Record<string, DiscountCode>>({});
|
||||||
const [newCode, setNewCode] = useState("");
|
const [newCode, setNewCode] = useState("");
|
||||||
const [newPercentage, setNewPercentage] = useState(0);
|
const [newPercentage, setNewPercentage] = useState(0);
|
||||||
const [newExpiration, setNewExpiration] = useState("");
|
const [newExpiration, setNewExpiration] = useState("");
|
||||||
const [newDescription, setNewDescription] = useState("");
|
const [newDescription, setNewDescription] = useState("");
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchDiscountCodes = async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/discount-codes');
|
||||||
|
if (response.ok) {
|
||||||
|
const codes = await response.json();
|
||||||
|
setDiscountList(codes);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch discount codes:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchDiscountCodes();
|
||||||
|
}, []);
|
||||||
|
|
||||||
const addDiscountCode = async () => {
|
const addDiscountCode = async () => {
|
||||||
if (!newCode || newPercentage <= 0) return;
|
if (!newCode || newPercentage <= 0) return;
|
||||||
|
|
||||||
|
|||||||
22
src/app/api/discount-codes/route.ts
Normal file
22
src/app/api/discount-codes/route.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
const DATA_SERVER_URL = process.env.DATA_SERVER_URL || "http://localhost:3001";
|
||||||
|
|
||||||
|
export async function GET() {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${DATA_SERVER_URL}/api/discount-codes`);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Failed to fetch discount codes");
|
||||||
|
}
|
||||||
|
|
||||||
|
const discountCodes = await response.json();
|
||||||
|
return NextResponse.json(discountCodes);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching discount codes:", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Failed to fetch discount codes" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,14 +1,30 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { useCart } from "../contexts/CartContext";
|
import { useCart } from "../contexts/CartContext";
|
||||||
import { formatDiamonds } from "../utils/formatDiamonds";
|
import { formatDiamonds } from "../utils/formatDiamonds";
|
||||||
import discountCodes from "../data/discountCodes.json";
|
import type { DiscountCode, DiscountCodes } from "../types/discount";
|
||||||
import type { DiscountCode } from "../types/discount";
|
|
||||||
|
|
||||||
export default function CartView() {
|
export default function CartView() {
|
||||||
const { state, dispatch } = useCart();
|
const { state, dispatch } = useCart();
|
||||||
const [discountCode, setDiscountCode] = useState("");
|
const [discountCode, setDiscountCode] = useState("");
|
||||||
|
const [discountCodes, setDiscountCodes] = useState<DiscountCodes>({});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchDiscountCodes = async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/discount-codes');
|
||||||
|
if (response.ok) {
|
||||||
|
const codes = await response.json();
|
||||||
|
setDiscountCodes(codes);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch discount codes:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchDiscountCodes();
|
||||||
|
}, []);
|
||||||
|
|
||||||
const calculateItemTotal = (item: (typeof state.items)[0]) => {
|
const calculateItemTotal = (item: (typeof state.items)[0]) => {
|
||||||
let basePrice = item.product.pricePerStack * item.quantity;
|
let basePrice = item.product.pricePerStack * item.quantity;
|
||||||
|
|||||||
@ -1 +0,0 @@
|
|||||||
{}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user