we went to mocks - now going to unit-setup.ts - centralized
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
// src/components/PriceHistoryChart.tsx
|
||||
import React, { useState, useEffect, useMemo } from 'react';
|
||||
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts';
|
||||
import { fetchHistoricalPriceData } from '../../services/apiClient';
|
||||
import { LoadingSpinner } from '../../components/LoadingSpinner';
|
||||
import * as apiClient from '../../services/apiClient';
|
||||
import { LoadingSpinner } from '../../components/LoadingSpinner'; // This path is correct
|
||||
import type { MasterGroceryItem } from '../../types';
|
||||
|
||||
type HistoricalData = Record<string, { date: string; price: number }[]>; // price is in cents
|
||||
interface HistoricalPriceDataPoint {
|
||||
master_item_id: number;
|
||||
avg_price_in_cents: number | null;
|
||||
summary_date: string;
|
||||
}
|
||||
type HistoricalData = Record<string, { date: string; price: number }[]>;
|
||||
type ChartData = { date: string; [itemName: string]: number | string };
|
||||
|
||||
const COLORS = ['#10B981', '#3B82F6', '#F59E0B', '#EF4444', '#8B5CF6', '#EC4899'];
|
||||
@@ -32,14 +37,15 @@ export const PriceHistoryChart: React.FC<PriceHistoryChartProps> = ({ watchedIte
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const watchedItemIds = watchedItems.map(item => item.master_grocery_item_id);
|
||||
const rawData = await fetchHistoricalPriceData(watchedItemIds);
|
||||
const watchedItemIds = watchedItems.map(item => item.master_grocery_item_id).filter((id): id is number => id !== undefined); // Ensure only numbers are passed
|
||||
const response = await apiClient.fetchHistoricalPriceData(watchedItemIds);
|
||||
const rawData: HistoricalPriceDataPoint[] = await response.json();
|
||||
if (rawData.length === 0) {
|
||||
setHistoricalData({});
|
||||
return;
|
||||
}
|
||||
|
||||
const processedData = rawData.reduce<HistoricalData>((acc, record) => {
|
||||
const processedData = rawData.reduce<HistoricalData>((acc, record: HistoricalPriceDataPoint) => {
|
||||
if (!record.master_item_id || record.avg_price_in_cents === null || !record.summary_date) return acc;
|
||||
|
||||
const itemName = watchedItemsMap.get(record.master_item_id);
|
||||
@@ -151,8 +157,8 @@ export const PriceHistoryChart: React.FC<PriceHistoryChartProps> = ({ watchedIte
|
||||
<XAxis dataKey="date" tick={{ fill: '#9CA3AF', fontSize: 12 }} />
|
||||
<YAxis
|
||||
tick={{ fill: '#9CA3AF', fontSize: 12 }}
|
||||
tickFormatter={(value) => `$${(Number(value) / 100).toFixed(2)}`}
|
||||
domain={['dataMin', 'auto']}
|
||||
tickFormatter={(value: number) => `$${(value / 100).toFixed(2)}`}
|
||||
domain={[(a: number) => Math.floor(a * 0.95), (b: number) => Math.ceil(b * 1.05)]}
|
||||
/>
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
|
||||
Reference in New Issue
Block a user