Skip to content

Commit

Permalink
🗿🪒 -> getOwnerGears function in s.contract & pull into App.js(x) for #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Gizmotronn committed Feb 10, 2022
1 parent bc672d8 commit a9df5b5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
13 changes: 13 additions & 0 deletions contracts/GearToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@ contract GearToken is ERC721, Ownable {
function getGears() public view returns(Gear[] memory){
return gears;
}

function getOwnerGears(address _owner) public view returns (Gear[] memory) { // Get all of the owner's Gears
// Filter & loop through all gears, retrieve only the owner's gears (getter)
Gear[] memory result = new Gear[](balanceOf(_owner)); // balanceOf(_owner) => how many NFTs the owner has (from ERC721 smartcontract script from openzeppelin)
uint256 counter = 0;
for(uint256 i = 0; i < gears.length; i++) {
if(ownerOf(i) == _owner) { // If the owner of the NFT is the same as the address that's calling the function
result[counter] = gears[i];
counter++;
}
}
return result;
}
}
17 changes: 12 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@ function App() {
console.table(blockchain);

useEffect(() => {
dispatch(connect());
}, [dispatch]);

return <s.Screen>
{blockchain.account !== "" || blockchain.gearToken !== null ? (
<s.Container flex={1} ai={"center"} jc={"center"}>
<s.TextTitle>
Our game
</s.TextTitle>
<s.SpacerSmall />
<button>CONNECT</button>
<button onClick={(e) => {
e.preventDefault(); // prevent default form submit
dispatch(connect());
}}>CONNECT</button>
</s.Container>
) : (
<s.Container>
<s.TextTitle>
Connect to our Game
</s.TextTitle>
</s.Container>
)}
</s.Screen>
}

Expand Down
2 changes: 2 additions & 0 deletions src/redux/data/dataReducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const initialState = {
loading: false,
allGears: [],
allOwnerGears: [],
error: false,
errorMsg: "",
};
Expand All @@ -17,6 +18,7 @@ const dataReducer = (state = initialState, action) => {
...state,
loading: false,
allGears: action.payload.allGears,
allOwnerGears: action.payload.allOwnerGears,
};
case "CHECK_DATA_FAILED":
return {
Expand Down

0 comments on commit a9df5b5

Please sign in to comment.