Skip to content

Commit

Permalink
Add Minimal React Template
Browse files Browse the repository at this point in the history
  • Loading branch information
danforbes committed Sep 24, 2024
1 parent a34f9c9 commit a318a99
Show file tree
Hide file tree
Showing 25 changed files with 20,523 additions and 2 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/web3js-react-dapp-min.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: web3js-react-dapp-min Build & Test
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
types: [ opened, reopened, synchronize ]
jobs:
build:
name: Build & Test
runs-on: ubuntu-latest
defaults:
run:
working-directory: templates/min/web3js-react-dapp-min
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm i
- run: npm run build
- run: npm test
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ for their selection.
Usage: npx create-web3js-dapp [options]

Options:
-f, --framework <name> front-end framework (choices: "angular")
-f, --framework <name> front-end framework (choices: "angular", "react")
-t, --template <type> template type (choices: "minimal")
-h, --help display help for command
```
Expand All @@ -31,3 +31,4 @@ can be used to build dApps.
This utility supports the following front-end frameworks:

- [Angular](https://angular.dev/)
- [React](https://react.dev/)
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ program
.addOption(
new Option("-f, --framework <name>", "front-end framework").choices([
"angular",
"react",
]),
)
.addOption(
Expand Down Expand Up @@ -63,6 +64,10 @@ async function inquire(cliOpts: OptionValues): Promise<Options> {
name: "Angular",
value: "angular",
},
{
name: "React",
value: "react",
},
],
});
}
Expand Down
25 changes: 25 additions & 0 deletions src/lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,28 @@ test("getSelectedOption(angular, minimal)", () => {
"unexpected project location",
);
});

test("getSelectedOption(react, minimal)", () => {
const opts: Options = { framework: "react", template: "minimal" };
const result: SelectedOption = getSelectedOption(opts);

const expectedName: string = "web3js-react-dapp-min";
assert.strictEqual(
result.projectName,
expectedName,
"unexpected project name",
);

const expectedLocation: string = join(
__dirname,
"..",
"templates",
"min",
expectedName,
);
assert.strictEqual(
result.projectLocation,
expectedLocation,
"unexpected project location",
);
});
11 changes: 11 additions & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@ export function getSelectedOption(opts: Options): SelectedOption {
);
return { projectName, projectLocation };
}
case "react": {
const projectName: string = "web3js-react-dapp-min";
const projectLocation: string = join(
__dirname,
"..",
"templates",
"min",
projectName,
);
return { projectName, projectLocation };
}
}
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Framework = "angular";
export type Framework = "angular" | "react";
export type TemplateType = "minimal";

export interface Options {
Expand Down
23 changes: 23 additions & 0 deletions templates/min/web3js-react-dapp-min/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
21 changes: 21 additions & 0 deletions templates/min/web3js-react-dapp-min/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 web3js-react-dapp-min

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
28 changes: 28 additions & 0 deletions templates/min/web3js-react-dapp-min/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Web3.js + React Minimal dApp Template

This project is a minimal template for using [Web3.js](https://web3js.org/) with
[React](https://react.dev/).

- [Web3.js Docs](https://docs.web3js.org/)
- [React Docs](https://react.dev/learn)

This project was bootstrapped with
[Create React App](https://github.com/facebook/create-react-app).

## Getting Started

Install the project dependencies with a dependency manager like npm or Yarn. Use
`npm start` to start a local development server and navigate to
http://localhost:3000 to view the dApp.

## Project Design

This project defines a service in
[./src/web3/web3-service.ts](./src/web3/web3-service.ts) that exposes an
instance of the [`Web3` class](https://docs.web3js.org/api/web3/class/Web3) and
an [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) provider, if present. If
the provider is present, it's used by the `Web3` instance to communicate with
the network and the service registers a
[`chainChanged` handler](https://docs.metamask.io/wallet/reference/provider-api/#chainchanged)
that reloads the page. Refer to [./src/App.tsx](./src/App.tsx) for an example of
using the service.
Loading

0 comments on commit a318a99

Please sign in to comment.