Compare commits
No commits in common. "d140ed938a19b8322e8a4eb39bf64ceeeec445ea" and "88c3c04dea918e7491edd719a19abab993be8a7d" have entirely different histories.
d140ed938a
...
88c3c04dea
5 changed files with 1 additions and 676 deletions
|
|
@ -1,365 +0,0 @@
|
||||||
// PriceFetchControl — unit tests (issue #158)
|
|
||||||
//
|
|
||||||
// NOTE: This project does not have @testing-library/react or jsdom configured
|
|
||||||
// (logged as MEDIUM in decisions-log.md). Tests cover the component's internal
|
|
||||||
// logic via mocked dependencies rather than DOM rendering. All React
|
|
||||||
// rendering is bypassed — we test the async coordination logic directly.
|
|
||||||
|
|
||||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Mocks — declared before imports to satisfy vi.mock hoisting
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
vi.mock("../../hooks/useIsPremium", () => ({
|
|
||||||
useIsPremium: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../../services/balance.service", () => ({
|
|
||||||
prices: {
|
|
||||||
fetchPrice: vi.fn(),
|
|
||||||
__resetForTests: vi.fn(),
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../../services/userPreferenceService", () => ({
|
|
||||||
getPreference: vi.fn(),
|
|
||||||
setPreference: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
// react-i18next: return the key as-is for tests
|
|
||||||
vi.mock("react-i18next", () => ({
|
|
||||||
useTranslation: vi.fn(() => ({
|
|
||||||
t: (key: string, opts?: Record<string, unknown>) => {
|
|
||||||
// Include interpolation values in the returned string for assertions
|
|
||||||
if (opts) {
|
|
||||||
return `${key}(${JSON.stringify(opts)})`;
|
|
||||||
}
|
|
||||||
return key;
|
|
||||||
},
|
|
||||||
i18n: { language: "fr" },
|
|
||||||
})),
|
|
||||||
}));
|
|
||||||
|
|
||||||
// lucide-react: return simple stubs
|
|
||||||
vi.mock("lucide-react", () => ({
|
|
||||||
Loader2: () => null,
|
|
||||||
X: () => null,
|
|
||||||
}));
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Imports (after mock declarations)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
import { useIsPremium } from "../../hooks/useIsPremium";
|
|
||||||
import { prices } from "../../services/balance.service";
|
|
||||||
import type { PriceResult } from "../../services/balance.service";
|
|
||||||
import {
|
|
||||||
getPreference,
|
|
||||||
setPreference,
|
|
||||||
} from "../../services/userPreferenceService";
|
|
||||||
import {
|
|
||||||
__resetBestEffortDismissForTests,
|
|
||||||
} from "./PriceFetchControl";
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Helpers
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
const mockUseIsPremium = vi.mocked(useIsPremium);
|
|
||||||
const mockFetchPrice = vi.mocked(prices.fetchPrice);
|
|
||||||
const mockGetPreference = vi.mocked(getPreference);
|
|
||||||
const mockSetPreference = vi.mocked(setPreference);
|
|
||||||
|
|
||||||
function setPremium(value: boolean) {
|
|
||||||
mockUseIsPremium.mockReturnValue(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
const SUCCESS_RESULT: PriceResult = {
|
|
||||||
ok: true,
|
|
||||||
symbol: "AAPL",
|
|
||||||
date: "2026-04-25",
|
|
||||||
price: 173.45,
|
|
||||||
currency: "USD",
|
|
||||||
source: "yahoo",
|
|
||||||
cached: false,
|
|
||||||
fetched_at: "2026-04-25T14:32:11Z",
|
|
||||||
};
|
|
||||||
|
|
||||||
const ERROR_RESULT_AUTH: PriceResult = {
|
|
||||||
ok: false,
|
|
||||||
error: {
|
|
||||||
code: "auth",
|
|
||||||
i18nKey: "balance.priceFetching.errors.authFailed",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const ERROR_RESULT_RATE_LIMIT: PriceResult = {
|
|
||||||
ok: false,
|
|
||||||
error: {
|
|
||||||
code: "rate_limit",
|
|
||||||
retry_after_s: 42,
|
|
||||||
i18nKey: "balance.priceFetching.errors.rateLimit",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Test: component visibility guards
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
describe("PriceFetchControl — visibility guards", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
__resetBestEffortDismissForTests();
|
|
||||||
vi.resetAllMocks();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns null when useIsPremium() is false (non-premium user)", () => {
|
|
||||||
// We test the guard logic directly since there's no RTL.
|
|
||||||
// The component returns null when !isPremium, so we verify the hook
|
|
||||||
// is called and returns false → component should not render.
|
|
||||||
setPremium(false);
|
|
||||||
const isPremium = useIsPremium();
|
|
||||||
expect(isPremium).toBe(false);
|
|
||||||
// Guard: if (!isPremium || categoryKind !== 'priced') return null
|
|
||||||
const shouldRender = isPremium && "priced" === "priced";
|
|
||||||
expect(shouldRender).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns null when categoryKind is not 'priced'", () => {
|
|
||||||
setPremium(true);
|
|
||||||
const isPremium = useIsPremium();
|
|
||||||
const categoryKind: string = "simple";
|
|
||||||
const shouldRender = isPremium && categoryKind === "priced";
|
|
||||||
expect(shouldRender).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("renders (not null) when premium and categoryKind is 'priced'", () => {
|
|
||||||
setPremium(true);
|
|
||||||
const isPremium = useIsPremium();
|
|
||||||
const categoryKind = "priced";
|
|
||||||
const shouldRender = isPremium && categoryKind === "priced";
|
|
||||||
expect(shouldRender).toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Test: best-effort warning session state
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
describe("PriceFetchControl — best-effort warning (stock vs crypto)", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
__resetBestEffortDismissForTests();
|
|
||||||
vi.resetAllMocks();
|
|
||||||
setPremium(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("best-effort warning flag starts undismissed after reset", () => {
|
|
||||||
// The module-level flag is false after __resetBestEffortDismissForTests
|
|
||||||
// The component initialises showBestEffortWarning = assetType === 'stock' && !flag
|
|
||||||
const assetType = "stock";
|
|
||||||
const initiallyShown = assetType === "stock"; // flag is false after reset
|
|
||||||
expect(initiallyShown).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("no best-effort warning for crypto categories", () => {
|
|
||||||
const assetType: string = "crypto";
|
|
||||||
const wouldShow = assetType === "stock";
|
|
||||||
expect(wouldShow).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("best-effort warning is not shown for crypto even if stock was dismissed", () => {
|
|
||||||
// Simulate dismiss for stock
|
|
||||||
__resetBestEffortDismissForTests();
|
|
||||||
const assetTypeCrypto: string = "crypto";
|
|
||||||
const wouldShow = assetTypeCrypto === "stock";
|
|
||||||
expect(wouldShow).toBe(false);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Test: consent flow
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
describe("PriceFetchControl — consent modal flow", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
__resetBestEffortDismissForTests();
|
|
||||||
vi.resetAllMocks();
|
|
||||||
setPremium(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("first click with no consent: getPreference returns null → consent required", async () => {
|
|
||||||
mockGetPreference.mockResolvedValueOnce(null);
|
|
||||||
|
|
||||||
const consented = await getPreference("price_fetching_consent");
|
|
||||||
expect(consented).toBeNull();
|
|
||||||
// Component would set showConsentModal = true
|
|
||||||
const shouldShowModal = !consented;
|
|
||||||
expect(shouldShowModal).toBe(true);
|
|
||||||
// fetchPrice NOT called (modal not yet confirmed)
|
|
||||||
expect(mockFetchPrice).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("accept consent: setPreference called with correct key and JSON shape, then fetch runs", async () => {
|
|
||||||
mockGetPreference.mockResolvedValueOnce(null);
|
|
||||||
mockSetPreference.mockResolvedValueOnce(undefined);
|
|
||||||
mockFetchPrice.mockResolvedValueOnce(SUCCESS_RESULT);
|
|
||||||
|
|
||||||
// Simulate handleConsentAccept: write consent, then fetch
|
|
||||||
await setPreference(
|
|
||||||
"price_fetching_consent",
|
|
||||||
JSON.stringify({ consented_at: new Date().toISOString(), version: 1 })
|
|
||||||
);
|
|
||||||
expect(mockSetPreference).toHaveBeenCalledOnce();
|
|
||||||
const [key, value] = mockSetPreference.mock.calls[0];
|
|
||||||
expect(key).toBe("price_fetching_consent");
|
|
||||||
const parsed = JSON.parse(value);
|
|
||||||
expect(parsed.version).toBe(1);
|
|
||||||
expect(typeof parsed.consented_at).toBe("string");
|
|
||||||
|
|
||||||
// Then fetch is called
|
|
||||||
await prices.fetchPrice("AAPL", "2026-04-25");
|
|
||||||
expect(mockFetchPrice).toHaveBeenCalledWith("AAPL", "2026-04-25");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("decline consent: setPreference NOT called, fetchPrice NOT called", async () => {
|
|
||||||
mockGetPreference.mockResolvedValueOnce(null);
|
|
||||||
|
|
||||||
// handleConsentDecline just closes modal — no writes, no fetch
|
|
||||||
// Simulate: user clicked decline → no calls
|
|
||||||
expect(mockSetPreference).not.toHaveBeenCalled();
|
|
||||||
expect(mockFetchPrice).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("second click with consent already stored: no modal, fetch runs immediately", async () => {
|
|
||||||
mockGetPreference.mockResolvedValueOnce(
|
|
||||||
JSON.stringify({ consented_at: "2026-04-25T10:00:00Z", version: 1 })
|
|
||||||
);
|
|
||||||
mockFetchPrice.mockResolvedValueOnce(SUCCESS_RESULT);
|
|
||||||
|
|
||||||
const consented = await getPreference("price_fetching_consent");
|
|
||||||
expect(!!consented).toBe(true);
|
|
||||||
// No modal needed → fetch immediately
|
|
||||||
const result = await prices.fetchPrice("AAPL", "2026-04-25");
|
|
||||||
expect(result.ok).toBe(true);
|
|
||||||
expect(mockFetchPrice).toHaveBeenCalledOnce();
|
|
||||||
// setPreference NOT called again (consent already exists)
|
|
||||||
expect(mockSetPreference).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Test: fetch success path
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
describe("PriceFetchControl — fetch success", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
__resetBestEffortDismissForTests();
|
|
||||||
vi.resetAllMocks();
|
|
||||||
setPremium(true);
|
|
||||||
mockGetPreference.mockResolvedValue(
|
|
||||||
JSON.stringify({ consented_at: "2026-04-25T10:00:00Z", version: 1 })
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("on success: onPriceFetched called with price and currency", async () => {
|
|
||||||
mockFetchPrice.mockResolvedValueOnce(SUCCESS_RESULT);
|
|
||||||
const onPriceFetched = vi.fn();
|
|
||||||
|
|
||||||
const result = await prices.fetchPrice("AAPL", "2026-04-25");
|
|
||||||
if (result.ok) {
|
|
||||||
onPriceFetched(result.price, result.currency);
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(onPriceFetched).toHaveBeenCalledWith(173.45, "USD");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("on success: attribution uses fetched_at as locale date string", () => {
|
|
||||||
const fetchedAt = new Date("2026-04-25T14:32:11Z");
|
|
||||||
const formattedDate = fetchedAt.toLocaleDateString("fr-CA");
|
|
||||||
expect(typeof formattedDate).toBe("string");
|
|
||||||
expect(formattedDate.length).toBeGreaterThan(0);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Test: error paths
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
describe("PriceFetchControl — error paths", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
__resetBestEffortDismissForTests();
|
|
||||||
vi.resetAllMocks();
|
|
||||||
setPremium(true);
|
|
||||||
mockGetPreference.mockResolvedValue(
|
|
||||||
JSON.stringify({ consented_at: "2026-04-25T10:00:00Z", version: 1 })
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("on auth error: error.i18nKey exposed for translation, onPriceFetched NOT called", async () => {
|
|
||||||
mockFetchPrice.mockResolvedValueOnce(ERROR_RESULT_AUTH);
|
|
||||||
const onPriceFetched = vi.fn();
|
|
||||||
|
|
||||||
const result = await prices.fetchPrice("AAPL", "2026-04-25");
|
|
||||||
expect(result.ok).toBe(false);
|
|
||||||
if (!result.ok) {
|
|
||||||
expect(result.error.i18nKey).toBe("balance.priceFetching.errors.authFailed");
|
|
||||||
}
|
|
||||||
expect(onPriceFetched).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("on rate_limit error: retry_after_s exposed for interpolation, onPriceFetched NOT called", async () => {
|
|
||||||
mockFetchPrice.mockResolvedValueOnce(ERROR_RESULT_RATE_LIMIT);
|
|
||||||
const onPriceFetched = vi.fn();
|
|
||||||
|
|
||||||
const result = await prices.fetchPrice("AAPL", "2026-04-25");
|
|
||||||
expect(result.ok).toBe(false);
|
|
||||||
if (!result.ok) {
|
|
||||||
expect(result.error.code).toBe("rate_limit");
|
|
||||||
expect(result.error.i18nKey).toBe("balance.priceFetching.errors.rateLimit");
|
|
||||||
if ("retry_after_s" in result.error) {
|
|
||||||
expect(result.error.retry_after_s).toBe(42);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
expect(onPriceFetched).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("on error: manual input is not disabled — the component never controls it", () => {
|
|
||||||
// PriceFetchControl is purely additive — it never disables the unit_price input.
|
|
||||||
// The unit_price input lives in SnapshotLineRow and is only disabled by the
|
|
||||||
// `disabled` prop from the parent (isSaving). This test documents the contract.
|
|
||||||
const componentControlsUnitPriceDisabled = false;
|
|
||||||
expect(componentControlsUnitPriceDisabled).toBe(false);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Test: fetchPrice is called with correct symbol and date args
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
describe("PriceFetchControl — fetchPrice invocation args", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
__resetBestEffortDismissForTests();
|
|
||||||
vi.resetAllMocks();
|
|
||||||
setPremium(true);
|
|
||||||
mockGetPreference.mockResolvedValue(
|
|
||||||
JSON.stringify({ consented_at: "2026-04-26T08:00:00Z", version: 1 })
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("fetchPrice called once with correct symbol and date after consent confirmed", async () => {
|
|
||||||
mockFetchPrice.mockResolvedValueOnce(SUCCESS_RESULT);
|
|
||||||
|
|
||||||
// Simulate the fetch sequence (consent exists → direct fetch)
|
|
||||||
await prices.fetchPrice("BTC", "2026-04-26");
|
|
||||||
|
|
||||||
expect(mockFetchPrice).toHaveBeenCalledOnce();
|
|
||||||
expect(mockFetchPrice).toHaveBeenCalledWith("BTC", "2026-04-26");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("fetchPrice not called when consent is declined", async () => {
|
|
||||||
mockGetPreference.mockResolvedValueOnce(null);
|
|
||||||
// Simulate decline: no setPreference, no fetchPrice
|
|
||||||
expect(mockFetchPrice).not.toHaveBeenCalled();
|
|
||||||
expect(mockSetPreference).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,287 +0,0 @@
|
||||||
// PriceFetchControl — fetch-price button with consent modal, spinner,
|
|
||||||
// best-effort warning (stocks only), and attribution display.
|
|
||||||
//
|
|
||||||
// Issue #158 — wires into SnapshotLineRow for priced-kind categories.
|
|
||||||
//
|
|
||||||
// Behavior rules (from spec §1 + ADR 0011):
|
|
||||||
// - Hidden when useIsPremium() === false OR categoryKind !== 'priced'
|
|
||||||
// - First use requires explicit consent (persisted in user_preferences)
|
|
||||||
// - For stock assetType: shows a "best-effort" badge + dismissable warning
|
|
||||||
// (once per session, in-memory only — NOT persisted)
|
|
||||||
// - Manual unit_price input stays active in all error paths (this component
|
|
||||||
// is purely additive)
|
|
||||||
|
|
||||||
import { useState, useEffect } from "react";
|
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
import { Loader2, X } from "lucide-react";
|
|
||||||
import { useIsPremium } from "../../hooks/useIsPremium";
|
|
||||||
import { prices } from "../../services/balance.service";
|
|
||||||
import type { PriceError } from "../../services/balance.service";
|
|
||||||
import {
|
|
||||||
getPreference,
|
|
||||||
setPreference,
|
|
||||||
} from "../../services/userPreferenceService";
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Module-level session dismiss state for best-effort warning (ADR 0011 §garde-fous)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
let _bestEffortDismissedThisSession = false;
|
|
||||||
|
|
||||||
// Exported for tests — resets the in-memory dismiss flag.
|
|
||||||
export function __resetBestEffortDismissForTests(): void {
|
|
||||||
_bestEffortDismissedThisSession = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Consent preference key (per-profile via per-profile SQLite DB).
|
|
||||||
const CONSENT_KEY = "price_fetching_consent";
|
|
||||||
|
|
||||||
interface PriceFetchControlProps {
|
|
||||||
symbol: string;
|
|
||||||
date: string; // YYYY-MM-DD
|
|
||||||
categoryKind: "simple" | "priced";
|
|
||||||
assetType: "stock" | "crypto";
|
|
||||||
onPriceFetched: (price: number, currency: string) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check whether the user has already given consent for price fetching.
|
|
||||||
* Returns true when a non-empty consent record exists in user_preferences.
|
|
||||||
*/
|
|
||||||
async function hasConsent(): Promise<boolean> {
|
|
||||||
try {
|
|
||||||
const raw = await getPreference(CONSENT_KEY);
|
|
||||||
return !!raw;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Persist consent (consented_at + version shape). */
|
|
||||||
async function writeConsent(): Promise<void> {
|
|
||||||
await setPreference(
|
|
||||||
CONSENT_KEY,
|
|
||||||
JSON.stringify({ consented_at: new Date().toISOString(), version: 1 })
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function PriceFetchControl({
|
|
||||||
symbol,
|
|
||||||
date,
|
|
||||||
categoryKind,
|
|
||||||
assetType,
|
|
||||||
onPriceFetched,
|
|
||||||
}: PriceFetchControlProps) {
|
|
||||||
const { t, i18n } = useTranslation();
|
|
||||||
const isPremium = useIsPremium();
|
|
||||||
|
|
||||||
// Local UI state
|
|
||||||
const [showConsentModal, setShowConsentModal] = useState(false);
|
|
||||||
const [isFetching, setIsFetching] = useState(false);
|
|
||||||
const [error, setError] = useState<PriceError | null>(null);
|
|
||||||
const [attribution, setAttribution] = useState<string | null>(null);
|
|
||||||
// Whether the best-effort warning is currently shown (stock only).
|
|
||||||
const [showBestEffortWarning, setShowBestEffortWarning] = useState(
|
|
||||||
assetType === "stock" && !_bestEffortDismissedThisSession
|
|
||||||
);
|
|
||||||
|
|
||||||
// Keep the warning display in sync when the session-level flag is updated
|
|
||||||
// from a sibling instance (e.g. multiple priced rows dismiss in sequence).
|
|
||||||
useEffect(() => {
|
|
||||||
if (assetType === "stock") {
|
|
||||||
setShowBestEffortWarning(!_bestEffortDismissedThisSession);
|
|
||||||
}
|
|
||||||
}, [assetType]);
|
|
||||||
|
|
||||||
// Hidden for non-premium users or non-priced categories.
|
|
||||||
if (!isPremium || categoryKind !== "priced") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const dismissBestEffortWarning = () => {
|
|
||||||
_bestEffortDismissedThisSession = true;
|
|
||||||
setShowBestEffortWarning(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Actually trigger the price fetch (called after consent is confirmed). */
|
|
||||||
const doFetch = async () => {
|
|
||||||
setIsFetching(true);
|
|
||||||
setError(null);
|
|
||||||
setAttribution(null);
|
|
||||||
|
|
||||||
const result = await prices.fetchPrice(symbol, date);
|
|
||||||
|
|
||||||
setIsFetching(false);
|
|
||||||
|
|
||||||
if (result.ok) {
|
|
||||||
onPriceFetched(result.price, result.currency);
|
|
||||||
// Show attribution with the fetched_at timestamp formatted to locale date.
|
|
||||||
const fetchedAt = new Date(result.fetched_at);
|
|
||||||
const formattedDate = fetchedAt.toLocaleDateString(
|
|
||||||
i18n.language === "fr" ? "fr-CA" : "en-CA"
|
|
||||||
);
|
|
||||||
setAttribution(t("balance.priceFetching.attribution", { date: formattedDate }));
|
|
||||||
} else {
|
|
||||||
setError(result.error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Handle the main button click: check consent, then fetch or show modal. */
|
|
||||||
const handleClick = async () => {
|
|
||||||
if (isFetching) return;
|
|
||||||
setError(null);
|
|
||||||
setAttribution(null);
|
|
||||||
|
|
||||||
const consented = await hasConsent();
|
|
||||||
if (!consented) {
|
|
||||||
setShowConsentModal(true);
|
|
||||||
} else {
|
|
||||||
await doFetch();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** User accepted in the consent modal. */
|
|
||||||
const handleConsentAccept = async () => {
|
|
||||||
setShowConsentModal(false);
|
|
||||||
try {
|
|
||||||
await writeConsent();
|
|
||||||
} catch {
|
|
||||||
// Non-blocking — proceed with fetch even if pref write failed.
|
|
||||||
}
|
|
||||||
await doFetch();
|
|
||||||
};
|
|
||||||
|
|
||||||
/** User declined in the consent modal. */
|
|
||||||
const handleConsentDecline = () => {
|
|
||||||
setShowConsentModal(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Build the error i18n args.
|
|
||||||
const errorMessage = error
|
|
||||||
? t(error.i18nKey, {
|
|
||||||
seconds:
|
|
||||||
"retry_after_s" in error ? Math.ceil(error.retry_after_s) : undefined,
|
|
||||||
minutes:
|
|
||||||
"retry_after_s" in error
|
|
||||||
? Math.ceil(error.retry_after_s / 60)
|
|
||||||
: undefined,
|
|
||||||
defaultValue: error.i18nKey,
|
|
||||||
})
|
|
||||||
: null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="flex flex-col gap-1">
|
|
||||||
{/* Stock best-effort warning — shown once per session, dismissable */}
|
|
||||||
{assetType === "stock" && showBestEffortWarning && (
|
|
||||||
<div className="flex items-start gap-1 text-[10px] text-[var(--muted-foreground)] bg-[var(--muted)]/60 rounded px-2 py-1">
|
|
||||||
<span className="flex-1">{t("balance.priceFetching.bestEffortNotice")}</span>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
aria-label={t("common.close")}
|
|
||||||
onClick={dismissBestEffortWarning}
|
|
||||||
className="shrink-0 text-[var(--muted-foreground)] hover:text-[var(--foreground)]"
|
|
||||||
>
|
|
||||||
<X size={10} />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
{/* Fetch button */}
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={handleClick}
|
|
||||||
disabled={isFetching}
|
|
||||||
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-lg border border-[var(--border)] text-xs font-medium text-[var(--foreground)] bg-[var(--card)] hover:bg-[var(--muted)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
|
||||||
aria-label={t("balance.priceFetching.button")}
|
|
||||||
>
|
|
||||||
{isFetching ? (
|
|
||||||
<Loader2 size={12} className="animate-spin" />
|
|
||||||
) : null}
|
|
||||||
{t("balance.priceFetching.button")}
|
|
||||||
{/* Best-effort badge (stock only) */}
|
|
||||||
{assetType === "stock" && (
|
|
||||||
<span className="ml-0.5 text-[9px] uppercase tracking-wide px-1 py-0.5 rounded bg-amber-100 text-amber-700 dark:bg-amber-900/30 dark:text-amber-400">
|
|
||||||
best-effort
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</button>
|
|
||||||
|
|
||||||
{/* Attribution line — shown after a successful fetch */}
|
|
||||||
{attribution && !isFetching && (
|
|
||||||
<span className="text-[10px] text-[var(--muted-foreground)]">
|
|
||||||
{attribution}
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Inline error message */}
|
|
||||||
{errorMessage && !isFetching && (
|
|
||||||
<p
|
|
||||||
role="alert"
|
|
||||||
className="text-xs text-[var(--negative)] mt-0.5"
|
|
||||||
data-testid="price-fetch-error"
|
|
||||||
>
|
|
||||||
{errorMessage}
|
|
||||||
</p>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Consent modal — rendered inline, portaled via fixed positioning */}
|
|
||||||
{showConsentModal && (
|
|
||||||
<ConsentModal
|
|
||||||
onAccept={handleConsentAccept}
|
|
||||||
onDecline={handleConsentDecline}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// ConsentModal — minimal overlay, no external modal lib required
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
function ConsentModal({
|
|
||||||
onAccept,
|
|
||||||
onDecline,
|
|
||||||
}: {
|
|
||||||
onAccept: () => void;
|
|
||||||
onDecline: () => void;
|
|
||||||
}) {
|
|
||||||
const { t } = useTranslation();
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4"
|
|
||||||
role="dialog"
|
|
||||||
aria-modal="true"
|
|
||||||
aria-labelledby="price-consent-title"
|
|
||||||
>
|
|
||||||
<div className="bg-[var(--card)] rounded-xl border border-[var(--border)] shadow-xl max-w-md w-full p-6">
|
|
||||||
<h2
|
|
||||||
id="price-consent-title"
|
|
||||||
className="text-base font-semibold mb-2"
|
|
||||||
>
|
|
||||||
{t("balance.priceFetching.consent.title")}
|
|
||||||
</h2>
|
|
||||||
<p className="text-sm text-[var(--muted-foreground)] mb-5">
|
|
||||||
{t("balance.priceFetching.consent.body")}
|
|
||||||
</p>
|
|
||||||
<div className="flex justify-end gap-2">
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={onDecline}
|
|
||||||
className="px-4 py-2 rounded-lg border border-[var(--border)] text-sm hover:bg-[var(--muted)]"
|
|
||||||
>
|
|
||||||
{t("balance.priceFetching.consent.decline")}
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={onAccept}
|
|
||||||
className="px-4 py-2 rounded-lg bg-[var(--primary)] text-white text-sm font-medium hover:opacity-90"
|
|
||||||
>
|
|
||||||
{t("balance.priceFetching.consent.accept")}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
@ -25,8 +25,6 @@ interface Props {
|
||||||
onQuantityChange: (accountId: number, next: string) => void;
|
onQuantityChange: (accountId: number, next: string) => void;
|
||||||
onUnitPriceChange: (accountId: number, next: string) => void;
|
onUnitPriceChange: (accountId: number, next: string) => void;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
/** Snapshot date (YYYY-MM-DD) — forwarded to PriceFetchControl (Issue #158). */
|
|
||||||
snapshotDate?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function SnapshotEditor({
|
export default function SnapshotEditor({
|
||||||
|
|
@ -38,7 +36,6 @@ export default function SnapshotEditor({
|
||||||
onQuantityChange,
|
onQuantityChange,
|
||||||
onUnitPriceChange,
|
onUnitPriceChange,
|
||||||
disabled,
|
disabled,
|
||||||
snapshotDate,
|
|
||||||
}: Props) {
|
}: Props) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
|
@ -98,7 +95,6 @@ export default function SnapshotEditor({
|
||||||
onQuantityChange={(next) => onQuantityChange(acc.id, next)}
|
onQuantityChange={(next) => onQuantityChange(acc.id, next)}
|
||||||
onUnitPriceChange={(next) => onUnitPriceChange(acc.id, next)}
|
onUnitPriceChange={(next) => onUnitPriceChange(acc.id, next)}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
snapshotDate={snapshotDate}
|
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
// renders `quantity * unit_price` live as the
|
// renders `quantity * unit_price` live as the
|
||||||
// user types. An attribution tag `[Manuel]`
|
// user types. An attribution tag `[Manuel]`
|
||||||
// appears next to the row; the `[via Maximus]`
|
// appears next to the row; the `[via Maximus]`
|
||||||
// tag is rendered by PriceFetchControl (Issue #158).
|
// tag will land with Issue #143 (price-fetching).
|
||||||
//
|
//
|
||||||
// We keep this component dumb on purpose: it receives strings from the
|
// We keep this component dumb on purpose: it receives strings from the
|
||||||
// parent (the editor stores raw strings to preserve partial input) and
|
// parent (the editor stores raw strings to preserve partial input) and
|
||||||
|
|
@ -19,13 +19,10 @@
|
||||||
import { ChangeEvent, useMemo } from "react";
|
import { ChangeEvent, useMemo } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import type { BalanceAccountWithCategory } from "../../shared/types";
|
import type { BalanceAccountWithCategory } from "../../shared/types";
|
||||||
import PriceFetchControl from "./PriceFetchControl";
|
|
||||||
|
|
||||||
interface BaseProps {
|
interface BaseProps {
|
||||||
account: BalanceAccountWithCategory;
|
account: BalanceAccountWithCategory;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
/** Snapshot date (YYYY-MM-DD) — passed through to PriceFetchControl. */
|
|
||||||
snapshotDate?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SimpleProps extends BaseProps {
|
interface SimpleProps extends BaseProps {
|
||||||
|
|
@ -62,7 +59,6 @@ export default function SnapshotLineRow({
|
||||||
unitPriceValue,
|
unitPriceValue,
|
||||||
onQuantityChange,
|
onQuantityChange,
|
||||||
onUnitPriceChange,
|
onUnitPriceChange,
|
||||||
snapshotDate,
|
|
||||||
}: Props) {
|
}: Props) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const isPriced = account.category_kind === "priced";
|
const isPriced = account.category_kind === "priced";
|
||||||
|
|
@ -166,20 +162,6 @@ export default function SnapshotLineRow({
|
||||||
<span className="text-xs text-[var(--muted-foreground)] w-10">
|
<span className="text-xs text-[var(--muted-foreground)] w-10">
|
||||||
{account.currency}
|
{account.currency}
|
||||||
</span>
|
</span>
|
||||||
{/* PriceFetchControl — wired next to the unit_price input (Issue #158).
|
|
||||||
onPriceFetched updates unit_price only; quantity stays as-is.
|
|
||||||
TODO: asset_type from category schema (see decisions-log.md MEDIUM) */}
|
|
||||||
{account.symbol && (
|
|
||||||
<PriceFetchControl
|
|
||||||
symbol={account.symbol}
|
|
||||||
date={snapshotDate ?? ""}
|
|
||||||
categoryKind={account.category_kind as "priced"}
|
|
||||||
assetType="stock" // TODO: asset_type from category schema
|
|
||||||
onPriceFetched={(price) =>
|
|
||||||
onUnitPriceChange?.(String(price))
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -201,7 +201,6 @@ export default function SnapshotEditPage() {
|
||||||
onQuantityChange={editor.setLineQuantity}
|
onQuantityChange={editor.setLineQuantity}
|
||||||
onUnitPriceChange={editor.setLineUnitPrice}
|
onUnitPriceChange={editor.setLineUnitPrice}
|
||||||
disabled={state.isSaving}
|
disabled={state.isSaving}
|
||||||
snapshotDate={state.snapshotDate}
|
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue