|
|
@@ -3,6 +3,7 @@ |
|
|
import React, { useState, useEffect } from "react"; |
|
|
import React, { useState, useEffect } from "react"; |
|
|
import { useTranslation } from "react-i18next"; |
|
|
import { useTranslation } from "react-i18next"; |
|
|
import { BomWeightingScoreResult } from "@/app/api/settings/bomWeighting"; |
|
|
import { BomWeightingScoreResult } from "@/app/api/settings/bomWeighting"; |
|
|
|
|
|
import { fetchBomWeightingScoresClient } from "@/app/api/settings/bomWeighting/client"; |
|
|
import type { BomScoreResult } from "@/app/api/bom"; |
|
|
import type { BomScoreResult } from "@/app/api/bom"; |
|
|
import { fetchBomScoresClient } from "@/app/api/bom/client"; |
|
|
import { fetchBomScoresClient } from "@/app/api/bom/client"; |
|
|
import BomWeightingScoreTable from "@/components/BomWeightingScoreTable"; |
|
|
import BomWeightingScoreTable from "@/components/BomWeightingScoreTable"; |
|
|
@@ -16,37 +17,61 @@ interface Props { |
|
|
bomWeightingScores: BomWeightingScoreResult[]; |
|
|
bomWeightingScores: BomWeightingScoreResult[]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const BomWeightingTabs: React.FC<Props> = ({ bomWeightingScores }) => { |
|
|
|
|
|
|
|
|
const BomWeightingTabs: React.FC<Props> = ({ bomWeightingScores: initialBomWeightingScores }) => { |
|
|
const { t } = useTranslation("common"); |
|
|
const { t } = useTranslation("common"); |
|
|
const [tab, setTab] = useState(0); |
|
|
const [tab, setTab] = useState(0); |
|
|
|
|
|
const [bomWeightingScores, setBomWeightingScores] = useState<BomWeightingScoreResult[]>(initialBomWeightingScores); |
|
|
const [bomScores, setBomScores] = useState<BomScoreResult[] | null>(null); |
|
|
const [bomScores, setBomScores] = useState<BomScoreResult[] | null>(null); |
|
|
const [loadingScores, setLoadingScores] = useState(false); |
|
|
const [loadingScores, setLoadingScores] = useState(false); |
|
|
const [loadError, setLoadError] = useState<string | null>(null); |
|
|
const [loadError, setLoadError] = useState<string | null>(null); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (tab !== 1) return; |
|
|
|
|
|
|
|
|
const loadBomScores = React.useCallback(async () => { |
|
|
|
|
|
try { |
|
|
|
|
|
setLoadingScores(true); |
|
|
|
|
|
setLoadError(null); |
|
|
|
|
|
console.log("Fetching BOM scores from /bom/scores..."); |
|
|
|
|
|
const data = await fetchBomScoresClient(); |
|
|
|
|
|
console.log("BOM scores received:", data); |
|
|
|
|
|
setBomScores(data || []); |
|
|
|
|
|
} catch (err: any) { |
|
|
|
|
|
console.error("Failed to load BOM scores:", err); |
|
|
|
|
|
const errorMsg = |
|
|
|
|
|
err?.response?.data?.message || err?.message || t("Update Failed") || "Load failed"; |
|
|
|
|
|
setLoadError(errorMsg); |
|
|
|
|
|
setBomScores([]); |
|
|
|
|
|
} finally { |
|
|
|
|
|
setLoadingScores(false); |
|
|
|
|
|
} |
|
|
|
|
|
}, [t]); |
|
|
|
|
|
|
|
|
|
|
|
const loadBomWeightingScores = React.useCallback(async () => { |
|
|
|
|
|
try { |
|
|
|
|
|
console.log("Fetching BOM weighting scores..."); |
|
|
|
|
|
const data = await fetchBomWeightingScoresClient(); |
|
|
|
|
|
console.log("BOM weighting scores received:", data); |
|
|
|
|
|
setBomWeightingScores(data || []); |
|
|
|
|
|
} catch (err: any) { |
|
|
|
|
|
console.error("Failed to load BOM weighting scores:", err); |
|
|
|
|
|
} |
|
|
|
|
|
}, []); |
|
|
|
|
|
|
|
|
|
|
|
const handleWeightingUpdated = React.useCallback(async () => { |
|
|
|
|
|
// Refresh both weighting scores and BOM scores |
|
|
|
|
|
await Promise.all([ |
|
|
|
|
|
loadBomWeightingScores(), |
|
|
|
|
|
loadBomScores() |
|
|
|
|
|
]); |
|
|
|
|
|
}, [loadBomWeightingScores, loadBomScores]); |
|
|
|
|
|
|
|
|
const load = async () => { |
|
|
|
|
|
try { |
|
|
|
|
|
setLoadingScores(true); |
|
|
|
|
|
setLoadError(null); |
|
|
|
|
|
console.log("Fetching BOM scores from /bom/scores..."); |
|
|
|
|
|
const data = await fetchBomScoresClient(); |
|
|
|
|
|
console.log("BOM scores received:", data); |
|
|
|
|
|
setBomScores(data || []); |
|
|
|
|
|
} catch (err: any) { |
|
|
|
|
|
console.error("Failed to load BOM scores:", err); |
|
|
|
|
|
const errorMsg = |
|
|
|
|
|
err?.response?.data?.message || err?.message || t("Update Failed") || "Load failed"; |
|
|
|
|
|
setLoadError(errorMsg); |
|
|
|
|
|
setBomScores([]); |
|
|
|
|
|
} finally { |
|
|
|
|
|
setLoadingScores(false); |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
// Sync initial prop values |
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
setBomWeightingScores(initialBomWeightingScores); |
|
|
|
|
|
}, [initialBomWeightingScores]); |
|
|
|
|
|
|
|
|
void load(); |
|
|
|
|
|
}, [tab, t]); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (tab !== 1) return; |
|
|
|
|
|
void loadBomScores(); |
|
|
|
|
|
}, [tab, loadBomScores]); |
|
|
|
|
|
|
|
|
return ( |
|
|
return ( |
|
|
<> |
|
|
<> |
|
|
@@ -83,7 +108,10 @@ const BomWeightingTabs: React.FC<Props> = ({ bomWeightingScores }) => { |
|
|
|
|
|
|
|
|
<Box> |
|
|
<Box> |
|
|
{tab === 0 && ( |
|
|
{tab === 0 && ( |
|
|
<BomWeightingScoreTable bomWeightingScores={bomWeightingScores} /> |
|
|
|
|
|
|
|
|
<BomWeightingScoreTable |
|
|
|
|
|
bomWeightingScores={bomWeightingScores} |
|
|
|
|
|
onWeightingUpdated={handleWeightingUpdated} |
|
|
|
|
|
/> |
|
|
)} |
|
|
)} |
|
|
{tab === 1 && ( |
|
|
{tab === 1 && ( |
|
|
loadingScores ? ( |
|
|
loadingScores ? ( |
|
|
|