Skip to content

Commit

Permalink
try to fix market
Browse files Browse the repository at this point in the history
  • Loading branch information
AkekoChan committed Jan 11, 2024
1 parent 51bd8f7 commit 104e810
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/components/navigate/Navigate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ const Dialog = ({ isOpen, onClose, ship }) => {

useEffect(() => {
if (shipData) {
setStatus(shipData.nav?.status || ship.nav.status);
setStatus(shipData?.nav?.status || ship.nav.status);
setFlightMode(shipData?.flightMode || ship.nav.flightMode);
setChangeWaypoint(
shipData.nav?.waypointSymbol || ship.nav.waypointSymbol
shipData?.nav?.waypointSymbol || ship.nav.waypointSymbol
);
setIsOrbited(
shipData?.nav?.status === "IN_ORBIT" || ship.nav.status === "IN_ORBIT"
Expand Down
1 change: 1 addition & 0 deletions src/components/navigate/navigaterow/NavigateRow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const NavigateRow = ({

const handleClickNavigate = async () => {
const res = await shipNavigate(waypoint.symbol, symbolShip);
console.log(res);
updateFuel(res.fuel);
setIsLocating(res?.nav?.route?.destination?.symbol);
};
Expand Down
4 changes: 3 additions & 1 deletion src/components/ship/about/About.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ const About = ({ data }) => {
useEffect(() => {
if (shipData) {
setStatus(shipData?.nav?.status || data.nav.status || shipData?.status);
setFlightMode(shipData?.flightMode || data.nav.flightMode);
setFlightMode(
shipData?.flightMode || data.nav.flightMode || shipData?.nav?.flightMode
);
setArrivalDate({
time: shipData?.nav?.route?.arrival || data.nav.route.arrival,
waypoint:
Expand Down
3 changes: 0 additions & 3 deletions src/components/ship/extract/Extract.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const Extract = ({ shipSymbol, userToken }) => {
const [endTime, setEndTime] = useState(shipData?.cooldown?.expiration);
const [isExtracting, setIsExtracting] = useState(false);

// Utilisez useRef pour stocker la valeur initiale du cooldown
const initialCooldownRef = useRef(shipData?.cooldown?.remainingSeconds);

useEffect(() => {
Expand All @@ -36,7 +35,6 @@ const Extract = ({ shipSymbol, userToken }) => {
const handleCooldownEnd = async () => {
setIsExtracting(false);

// Réinitialisez le cooldown à sa valeur initiale
setCooldown(initialCooldownRef.current);
};

Expand All @@ -56,7 +54,6 @@ const Extract = ({ shipSymbol, userToken }) => {
setCooldown(data?.remainingSeconds);
setEndTime(data?.expiration);

// Mettez à jour la valeur initiale du cooldown lorsque le cooldown commence
if (data?.remainingSeconds !== undefined && !isExtracting) {
initialCooldownRef.current = data.remainingSeconds;
}
Expand Down
100 changes: 48 additions & 52 deletions src/components/ship/storage/Storage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,25 @@ import {
import { useShipContext } from "../../../context/shipContext";

const Storage = ({ data }) => {
const { shipData, getMarket, sellCargo, getCargo, cargo, updateStorage } =
useShipContext();
const {
shipData,
getMarket,
sellCargo,
getCargo,
updateStorage,
market,
cargo,
getWaypoint,
} = useShipContext();

const [symbol, setSymbol] = useState({
systemSymbol: shipData?.nav?.systemSymbol || data.nav.systemSymbol,
waypointSymbol:
shipData?.nav?.route.destination.symbol || data.nav.waypointSymbol,
});
const [market, setMarket] = useState([]);
const [isClicked, setIsClicked] = useState({});
const [quantity, setQuantity] = useState({});

const handleGetMarket = async () => {
try {
const marketData = await getMarket(
symbol.systemSymbol,
symbol.waypointSymbol
);
return (
marketData?.tradeGoods?.map((item) => ({
symbolMarket: formatFirstLetterToUpperCase(
replaceUnderscoreWithSpace(item.symbol)
),
sellPrice: item.sellPrice,
})) || []
);
} catch (error) {
console.error("Error fetching market data:", error);
return [];
}
};
const [matchingItems, setMatchingItems] = useState([]);

const handleSellItem = async (item) => {
setIsClicked({ ...isClicked, [item.symbol]: !isClicked[item.symbol] });
Expand All @@ -61,6 +49,18 @@ const Storage = ({ data }) => {
setQuantity({ ...quantity, [item.symbol]: event.target.value });
};

const handleMatchMarket = () => {
if (cargo?.inventory && market?.tradeGoods) {
console.log("if cargo and market exist");
const matchingItems = market.tradeGoods.filter((marketItem) =>
cargo.inventory.some(
(cargoItem) => cargoItem.symbol === marketItem.symbol
)
);
setMatchingItems(matchingItems);
}
};

useEffect(() => {
setSymbol({
systemSymbol: shipData?.nav?.systemSymbol || data.nav.systemSymbol,
Expand All @@ -74,25 +74,29 @@ const Storage = ({ data }) => {
}, [data.symbol]);

useEffect(() => {
const fetchMarket = async () => {
if (!cargo) return;

const marketData = await handleGetMarket();
const matchingItems = marketData.filter((item) =>
cargo.inventory.some(
(cargoItem) =>
item.symbolMarket ===
replaceUnderscoreWithSpace(
formatFirstLetterToUpperCase(cargoItem.symbol)
)
)
const getCurrentWaypoint = async () => {
const currentWaypoint = await getWaypoint(
symbol.systemSymbol,
symbol.waypointSymbol
);

setMarket(matchingItems);
console.log(currentWaypoint);
currentWaypoint.traits.map((trait) => {
console.log(trait.symbol === "MARKETPLACE");
if (trait.symbol === "MARKETPLACE") {
console.log("Marketplace found");
getMarket(symbol.systemSymbol, symbol.waypointSymbol);
} else {
console.log("Marketplace not found");
setMatchingItems([]);
}
});
};
getCurrentWaypoint();
}, [symbol.waypointSymbol]);

fetchMarket();
}, [cargo, symbol.waypointSymbol]);
useEffect(() => {
handleMatchMarket();
}, [cargo, market]);

return (
<div className="ship-storage">
Expand All @@ -118,23 +122,15 @@ const Storage = ({ data }) => {
</td>
<td>{item.units}</td>
<td>
{Array.isArray(market) && (
{Array.isArray(matchingItems) && (
<>
<span>
{market.find(
(marketItem) =>
marketItem.symbolMarket ===
replaceUnderscoreWithSpace(
formatFirstLetterToUpperCase(item.symbol)
)
{matchingItems.find(
(matchingItem) => matchingItem.symbol === item.symbol
)?.sellPrice || "-"}
</span>
{market.find(
(marketItem) =>
marketItem.symbolMarket ===
replaceUnderscoreWithSpace(
formatFirstLetterToUpperCase(item.symbol)
)
{matchingItems.find(
(matchingItem) => matchingItem.symbol === item.symbol
) && (
<>
<button onClick={() => handleSellItem(item)}>
Expand Down
27 changes: 26 additions & 1 deletion src/context/shipContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const ShipContextProvider = ({ children }) => {
const [shipData, setShipData] = useState({});
const [cargo, setCargo] = useState();
const [fuel, setFuel] = useState();
const [market, setMarket] = useState([]);

const updateShipData = async (options) => {
try {
Expand Down Expand Up @@ -147,6 +148,22 @@ export const ShipContextProvider = ({ children }) => {
localStorage.setItem("waypoints", JSON.stringify(waypoints));
};

const getWaypoint = async (systemSymbol, waypointSymbol) => {
const options = {
endpoint: `systems/${systemSymbol}/waypoints/${waypointSymbol}`,
method: "GET",
headers: {
Accept: "application/json",
Authorization: `Bearer ${userToken}`,
},
};
try {
const data = await fetchData(options);
return data;
} catch (error) {
console.error(error);
}
};
const shipNavigate = async (waypoint, shipSymbol) => {
const options = {
endpoint: `my/ships/${shipSymbol}/navigate`,
Expand Down Expand Up @@ -206,7 +223,13 @@ export const ShipContextProvider = ({ children }) => {
},
};

return updateShipData(options);
try {
const data = await fetchData(options);
setMarket(data);
} catch (error) {
setMarket([]);
console.error(error);
}
};

const sellCargo = async (item, shipSymbol, units) => {
Expand Down Expand Up @@ -241,6 +264,8 @@ export const ShipContextProvider = ({ children }) => {
updateFuel,
fuel,
getFuel,
market,
getWaypoint,
};

return (
Expand Down

0 comments on commit 104e810

Please sign in to comment.