Skip to content

Instantly share code, notes, and snippets.

@JoshOrndorff
Created September 9, 2019 16:51
Show Gist options
  • Save JoshOrndorff/ad44c1c45a523b776246841f6276a562 to your computer and use it in GitHub Desktop.
Save JoshOrndorff/ad44c1c45a523b776246841f6276a562 to your computer and use it in GitHub Desktop.
Template Module frontend
import React, { useState, useEffect } from "react";
import { Form, Input, Grid, Card, Statistic } from "semantic-ui-react";
import TxButton from "./TxButton";
export default function TemplateModule(props) {
const { api, accountPair } = props;
const [status, setStatus] = useState("");
const [currentValue, setCurrentValue] = useState(0);
const [newValue, setNewValue] = useState(0);
useEffect(() => {
let unsubscribe;
api.query.templateModule.something()
.then(onChainValue => {
setCurrentValue(onChainValue)
})
.catch(console.error);
// Call this when the component unmounts (eg page crashes)
//return () => unsubscribeAll && unsubscribeAll();
}, [api.query.templateModule.something]);
return (
<Grid.Column>
<h1>Template Module</h1>
<Card>
<Card.Content textAlign="center">
<Statistic
label={"Current Value"}
value={currentValue}
/>
</Card.Content>
</Card>
<Form>
<Form.Field>
<Input
label="New Value"
fluid
// data.value will contain the new value in the field
onChange={(_, data) => {
setNewValue(data.value)
}}
state="newValue"
type="number"
/>
</Form.Field>
<Form.Field>
<TxButton
api={api}
accountPair={accountPair}
label={"Store New Value"}
params={["TODO"]}
setStatus={setStatus}
tx={api.tx.templateModule.doSomething}
/>
{status}
</Form.Field>
</Form>
</Grid.Column>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment