Skip to content

Commit

Permalink
Merge pull request #170 from massalabs/169-update-sum-front
Browse files Browse the repository at this point in the history
update front end with new web3 & wallet provider merge
  • Loading branch information
Ben-Rey authored Aug 18, 2023
2 parents 5c1c224 + 8c16da4 commit f0bb4dc
Show file tree
Hide file tree
Showing 11 changed files with 3,128 additions and 731 deletions.
3,236 changes: 2,954 additions & 282 deletions sum/front/package-lock.json

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions sum/front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@massalabs/massa-web3": "^2.0.0",
"@massalabs/massa-web3": "^2.1.1-dev.20230816120244",
"@massalabs/react-ui-kit": "^0.0.4-dev",
"@massalabs/wallet-provider": "^1.0.0",
"@massalabs/wallet-provider": "^1.2.1-dev.20230816122956",
"@types/node": "^16.18.37",
"@types/react": "^18.2.14",
"@types/react-dom": "^18.2.6",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.14.2",
"react-scripts": "5.0.1",
"typescript": "^4.9.5",
"web-vitals": "^2.1.4"
Expand Down Expand Up @@ -41,11 +42,11 @@
]
},
"devDependencies": {
"prettier": "^2.8.8",
"tailwindcss": "^3.3.2",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2"
"@types/jest": "^27.5.2",
"prettier": "^2.8.8",
"tailwindcss": "^3.3.2"
}
}
9 changes: 0 additions & 9 deletions sum/front/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,16 @@ import "@massalabs/react-ui-kit/src/global.css";
import Header from "./components/header";
import Body from "./components/body";
import Footer from "./components/footer";
import UseMassaStation from "./hooks/useMassaStation";
import { createContext } from "react";
import { ProviderService } from "./interfaces/ProviderService";

function App() {
const massaStation = UseMassaStation();

return (
<div className="App theme-light">
<Header />
<MassaContext.Provider value={massaStation}>
<Body />
</MassaContext.Provider>
<Footer />
</div>
);
}

export default App;

const MassaContext = createContext<ProviderService | null>(null);
export { MassaContext };
35 changes: 0 additions & 35 deletions sum/front/src/components/accountCreation.tsx

This file was deleted.

33 changes: 0 additions & 33 deletions sum/front/src/components/accountInformation.tsx

This file was deleted.

136 changes: 2 additions & 134 deletions sum/front/src/components/body.tsx
Original file line number Diff line number Diff line change
@@ -1,143 +1,11 @@
import AccountInformation from "./accountInformation";
import ContractInteraction from "./contractInteraction";
import AccountCreation from "./accountCreation";
import { useContext, useEffect, useState } from "react";
import { MassaContext } from "../App";

export default function Body() {
const {
account,
accounts,
connected,
loadingProvider,
providerList,
selectedProvider,
errorMessage,
} = useContext(MassaContext)!; // Use the context

const isLoadingProvider = providerList?.length === 0;

const noAccount = !!accounts && !isLoadingProvider && accounts.length === 0;

const disconnected = !connected && accounts && accounts.length > 0;

return (
<div className="body flex-col justify-center align-middle">
{isLoadingProvider && (
<div className="bodyInformationRed w-1/3 m-auto">
<p>{loadingProvider}</p>
{!errorMessage && <div className="loader"></div>}
</div>
)}

{!isLoadingProvider && !connected && <SelectProvider />}

{noAccount && disconnected && (
<>
<AccountCreation />
</>
)}

{disconnected && <ConnectionForm />}

{connected && (
<div className="flex-col">
<AccountInformation />
<ContractInteraction />
</div>
)}

<div className="mas-h2 text-red-500">{errorMessage}</div>
</div>
);
}

function SelectProvider() {
const [providersName, setProvidersNames] = useState<string[]>([]);
const { providerList, selectedProvider, setSelectedProvider } =
useContext(MassaContext)!; // Use the context

useEffect(() => {
if (providerList) {
setSelectedProvider(providerList[0]);
setProvidersNames(providerList.map((provider) => provider.name()));
}
}, [providerList]);

const handleChangeProvider = (
event: React.ChangeEvent<HTMLSelectElement>
) => {
const selectedProviderName = event.target.value;
const selectedProvider = providerList?.find(
(provider) => provider.name() === selectedProviderName
);
if (selectedProvider) {
setSelectedProvider(selectedProvider);
}
};

return (
<div className="">
<p>Wallet:</p>
<select
key="providerSelect"
className="bodyButton"
onChange={handleChangeProvider}
value={selectedProvider?.name()}
>
{providerList?.map((provider, index) => (
<option key={`provider_${provider.name()}`}>
{providersName[index]}
</option>
))}
</select>
</div>
);
}

function ConnectionForm() {
const { accounts, connect, selectedAccount, setSelectedAccount } =
useContext(MassaContext)!; // Use the context

const handleChangeAccount = (
event: React.ChangeEvent<HTMLSelectElement>
) => {
const selectedAccountName = event.target.value;
const selectedAccount = accounts?.find(
(account) => account.name() === selectedAccountName
);
if (selectedAccount) {
setSelectedAccount(selectedAccount);
}
};

return (
<div className=" p-5 border rounded-lg flex-col justify-start bg-gray-100 gap-5 h-3/4 w-1/3 m-auto">
<div>
<p>Account:</p>
<select
className="bodyButton"
onChange={handleChangeAccount}
value={selectedAccount?.name()}
>
{accounts?.map((account, index) => (
<option
key={`account_${
account.address + index.toString()
}`}
>
{accounts[index].name()}
</option>
))}
</select>
<div className="flex-col">
<ContractInteraction />
</div>
<button
className="bodyButton"
onClick={() => connect(selectedAccount!)}
disabled={!selectedAccount}
>
Connect Wallet
</button>
</div>
);
}
Loading

0 comments on commit f0bb4dc

Please sign in to comment.