diff --git a/database.db b/database.db index 5d6e5f5..189797d 100644 Binary files a/database.db and b/database.db differ diff --git a/src/app/Components/SelectDropdown.tsx b/src/app/Components/SelectDropdown.tsx index 6a4b419..1b25992 100644 --- a/src/app/Components/SelectDropdown.tsx +++ b/src/app/Components/SelectDropdown.tsx @@ -1,9 +1,15 @@ -import React, { useState, ChangeEvent } from "react"; +import React, { useState, useEffect, ChangeEvent } from "react"; import { ISelectDropdownProps } from "typings"; const SelectDropdown: React.FC = (props) => { const [selectedValue, setSelectedValue] = useState(""); + useEffect(() => { + if (!props.isActive) { + setSelectedValue(""); + } + }, [props.isActive]); + const handleChange = (event: ChangeEvent) => { setSelectedValue(event.target.value); if (props.onChange) { @@ -12,20 +18,18 @@ const SelectDropdown: React.FC = (props) => { }; return ( -
- -
+ ); }; diff --git a/src/app/Components/StakingSource.tsx b/src/app/Components/StakingSource.tsx index d94eec8..4350fd1 100644 --- a/src/app/Components/StakingSource.tsx +++ b/src/app/Components/StakingSource.tsx @@ -12,8 +12,11 @@ const StakingSource = (props: { stakingSource: IStakingSource; inventoryItems: IInventoryItem[] | null | undefined; claimStake: (stakingEventId: number) => void; + startStake: (inventoryItemId: number, wellId: number) => void; }) => { const [activeStakes, setActiveStakes] = useState([]); + const [selectedItemId, setSelectedItemId] = useState(null); + const [selectedWellId, setSelectedWellId] = useState(null); // Check if claimable every second useEffect(() => { @@ -42,16 +45,18 @@ const StakingSource = (props: { }); const handleStartMining = () => { - console.log("Start mining.."); + if (selectedItemId && selectedWellId) { + props.startStake(selectedItemId, selectedWellId); + } }; const handleClaim = (e: React.MouseEvent) => { console.log("Start claiming.."); }; - const handleSelectChange = (selectedValue: string) => { - console.log(selectedValue); - // Render a staking button + const handleSelectChange = (wellId: string, itemId: number) => { + setSelectedWellId(Number(wellId)); + setSelectedItemId(itemId); }; const RenderButtonOrCountdown = (props: { stake: IStake }) => { @@ -80,7 +85,7 @@ const StakingSource = (props: { } }; - const isChecked = (item: IInventoryItem): boolean => { + const isActive = (item: IInventoryItem): boolean => { return props.stakingSource.activeStakes.some((obj) => obj.id === item.id); }; @@ -121,18 +126,33 @@ const StakingSource = (props: { {props.inventoryItems && props.inventoryItems.map( (item, id) => - !isChecked(item) && ( -
-

{item.storeItem.name}

- ({ - value: well.resourceType, - label: well.resourceType, - }) + !isActive(item) && ( +
+

+ {item.storeItem.name} ID{item.storeItem.id} +

+
+ ({ + value: well.id, + label: `${well.resourceType} - ID# ${well.id}`, + }) + )} + onChange={(value) => handleSelectChange(value, item.id)} + isActive={selectedItemId === item.id} + /> + {selectedItemId === item.id ? ( + + ) : ( + <> )} - onChange={handleSelectChange} - /> +
) )} diff --git a/src/app/Components/StakingSourcesView.tsx b/src/app/Components/StakingSourcesView.tsx index 9da2240..e00f425 100644 --- a/src/app/Components/StakingSourcesView.tsx +++ b/src/app/Components/StakingSourcesView.tsx @@ -7,6 +7,7 @@ const StakingSourcesView = (props: { stakingSources: IStakingSource[] | null; inventoryItems: IInventoryItem[] | null | undefined; claimStake: (stakingEventId: number) => void; + startStake: (inventoryItemId: number, wellId: number) => void; }) => { return (
@@ -18,6 +19,7 @@ const StakingSourcesView = (props: { stakingSource={stakingSource} inventoryItems={props.inventoryItems} claimStake={props.claimStake} + startStake={props.startStake} /> ))}
diff --git a/src/app/page.tsx b/src/app/page.tsx index fe06971..d5af349 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -28,7 +28,7 @@ export default function Home() { const [lightBoxIsActive, setLightBoxIsActive] = useState(false); const [userId, setUserId] = useState(null); const [notification, setNotification] = useState(null); - const [notificationTime, setNotificationTime] = useState(3); + const [notificationTime, setNotificationTime] = useState(30); const [storeItems, setStoreItems] = useState(gameConfig.store); const isOwned = (storeItemId: string) => { @@ -52,6 +52,23 @@ export default function Home() { setInventoryItems(DBInventoryItems); }; + const fetchStakingSources = async () => { + const response = await fetch(`/api/user/${userId}/staking-sources`); + const sources = await response.json(); + setStakingSources(sources); + }; + + const fetchStakes = async () => { + const response = await fetch(`/api/user/${userId}/stakes`); + const stakes = await response.json(); + setUserStakes( + stakes.map((stake: IStake) => ({ + ...stake, + unclaimed: stake.unclaimed == 1, + })) + ); + }; + useEffect(() => { const fetchUser = async (wallet: string) => { const response = await fetch(`/api/user/login`, { @@ -74,23 +91,6 @@ export default function Home() { setBankAccount(bankAccount); }; - const fetchStakingSources = async () => { - const response = await fetch(`/api/user/${userId}/staking-sources`); - const sources = await response.json(); - setStakingSources(sources); - }; - - const fetchStakes = async () => { - const response = await fetch(`/api/user/${userId}/stakes`); - const stakes = await response.json(); - setUserStakes( - stakes.map((stake: IStake) => ({ - ...stake, - unclaimed: stake.unclaimed == 1, - })) - ); - }; - fetchUser("abcdefg"); // Public key goes here // Nico is there a better way of checking if a user is logged in? if (userId) { @@ -121,7 +121,7 @@ export default function Home() { if (notificationTime === 0) { setNotification(null); clearInterval(intervalId); - setNotificationTime(3); + setNotificationTime(30); } return () => { @@ -148,7 +148,19 @@ export default function Home() { wellId: wellId, }), }); - return await response.json(); + if (!response.ok) { + const error = await response.text(); + setNotification({ message: error, type: "Error" }); + } + if (response.status == 200) { + const data = await response.json(); + setNotification({ + message: `You've successfully staked`, + type: "Success", + }); + fetchStakes(); + fetchStakingSources(); + } }; // Which object is it? @@ -279,6 +291,7 @@ export default function Home() { stakingSources={stakingSources} inventoryItems={inventoryItems} claimStake={claimStake} + startStake={startStake} /> void; + isActive: boolean }