Introduce caching in CustomerRepository and refactor API integration
- Add in-memory caching for customer data in `CustomerRepository` to reduce API calls. - Replace direct API calls with methods from `CustomerRepository`. - Update customer-related pages (`[id]/page.tsx`, `customers/page.tsx`) to use `CustomerRepository` for data fetching. - Adjust breadcrumb resolver to leverage `CustomerRepository`. - Remove `axios` dependency from customer-related components.
This commit is contained in:
@@ -1,19 +1,17 @@
|
||||
"use client";
|
||||
|
||||
import React, {useEffect, useState, useMemo} from "react";
|
||||
import React, {useEffect, useState} from "react";
|
||||
import {useParams, useRouter} from "next/navigation";
|
||||
import {ChevronLeft} from "lucide-react";
|
||||
import axios, {AxiosError} from "axios";
|
||||
import {Button} from "@/components/ui/button";
|
||||
import {Dialog} from "@/components/ui/dialog";
|
||||
import {Skeleton} from "@/components/ui/skeleton";
|
||||
import CustomerDetailContent from "@/components/customers/details/CustomerDetailContent";
|
||||
import {Customer} from "@/services/customers/entities/customer";
|
||||
import CustomerInformationContent from "@/components/customers/details/sub/ContactInformationContent";
|
||||
import CustomerPhoneNumberContent from "@/components/customers/details/sub/CustomerPhoneNumberContent";
|
||||
import CustomerNotesContent from "@/components/customers/details/sub/CustomerNotesContent";
|
||||
|
||||
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || '/api';
|
||||
import {Customer} from "@/services/customers/entities/customer";
|
||||
import {CustomerRepository} from "@/services/customers/repositories/customerRepository";
|
||||
|
||||
export default function CustomerDetailPage() {
|
||||
const router = useRouter();
|
||||
@@ -35,23 +33,22 @@ export default function CustomerDetailPage() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
axios
|
||||
.get<Customer>(`${API_BASE_URL}/customers/${id}`)
|
||||
.then((res) => {
|
||||
if (isMounted) {
|
||||
setCustomer(res.data);
|
||||
CustomerRepository.getById(id)
|
||||
.then((result) => {
|
||||
if (!isMounted) return;
|
||||
|
||||
if (!result) {
|
||||
setError("Kunde nicht gefunden");
|
||||
setCustomer(null);
|
||||
} else {
|
||||
setCustomer(result);
|
||||
}
|
||||
})
|
||||
.catch((error: AxiosError) => {
|
||||
if (isMounted) {
|
||||
console.error('Error fetching customer:', error);
|
||||
setCustomer(null);
|
||||
setError(
|
||||
error.response?.status === 404
|
||||
? "Kunde nicht gefunden"
|
||||
: "Fehler beim Laden der Kundendaten"
|
||||
);
|
||||
}
|
||||
.catch((error) => {
|
||||
if (!isMounted) return;
|
||||
console.error('Error fetching customer:', error);
|
||||
setCustomer(null);
|
||||
setError("Fehler beim Laden der Kundendaten");
|
||||
})
|
||||
.finally(() => {
|
||||
if (isMounted) {
|
||||
@@ -85,14 +82,14 @@ export default function CustomerDetailPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const customerMetadata = useMemo(() => ({
|
||||
const customerMetadata = {
|
||||
createdInfo: customer
|
||||
? `Erstellt von ${customer.createdBy || "-"} am ${formatDate(customer.createdAt)}`
|
||||
: "Erstellt von - am -",
|
||||
lastActivityInfo: customer
|
||||
? `Letzte Aktivität: ${customer.updatedBy || "-"} am ${formatDate(customer.updatedAt)}`
|
||||
: "Letzte Aktivität: - am -"
|
||||
}), [customer]);
|
||||
};
|
||||
|
||||
const renderMetadata = () => {
|
||||
if (loading) {
|
||||
|
||||
@@ -22,9 +22,9 @@ import {
|
||||
import {motion} from "framer-motion";
|
||||
import {ArrowRight} from "lucide-react";
|
||||
import {NewCustomerModal} from "@/components/customers/modal/NewCustomerModal";
|
||||
import axios from "axios";
|
||||
import {Customer} from "@/services/customers/entities/customer";
|
||||
import Link from "next/link";
|
||||
import {CustomerRepository} from "@/services/customers/repositories/customerRepository";
|
||||
|
||||
export default function CustomersPage() {
|
||||
const [customers, setCustomers] = useState<Customer[]>([]);
|
||||
@@ -35,9 +35,9 @@ export default function CustomersPage() {
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true);
|
||||
axios.get("/api/customers")
|
||||
.then((res) => {
|
||||
setCustomers(res.data);
|
||||
CustomerRepository.getAll()
|
||||
.then((data) => {
|
||||
setCustomers(data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error fetching customers:', error);
|
||||
@@ -176,4 +176,4 @@ export default function CustomersPage() {
|
||||
</motion.div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user