QRCode scanner logic

This commit is contained in:
mmv 2019-07-19 17:09:27 +04:00
parent d882f17d7d
commit e827837c58
5 changed files with 51 additions and 12 deletions

View File

@ -21,6 +21,7 @@ type Props = {
classes: Object, classes: Object,
initialValues?: Object, initialValues?: Object,
disabledWhenValidating?: boolean, disabledWhenValidating?: boolean,
mutators?: Object,
testId?: string, testId?: string,
} }
@ -109,7 +110,7 @@ const GnoStepper = (props: Props) => {
} }
const { const {
steps, children, classes, disabledWhenValidating = false, testId, steps, children, classes, disabledWhenValidating = false, testId, mutators,
} = props } = props
const activePage = getActivePageFrom(children) const activePage = getActivePageFrom(children)
@ -118,7 +119,13 @@ const GnoStepper = (props: Props) => {
return ( return (
<React.Fragment> <React.Fragment>
<GnoForm onSubmit={handleSubmit} initialValues={values} validation={validate} testId={testId}> <GnoForm
onSubmit={handleSubmit}
initialValues={values}
validation={validate}
testId={testId}
formMutators={mutators}
>
{(submitting: boolean, validating: boolean, ...rest: any) => { {(submitting: boolean, validating: boolean, ...rest: any) => {
const disabled = disabledWhenValidating ? submitting || validating : submitting const disabled = disabledWhenValidating ? submitting || validating : submitting
const controls = ( const controls = (

View File

@ -38,6 +38,12 @@ const back = () => {
history.goBack() history.goBack()
} }
const formMutators = {
setValue: ([field, value], state, { changeValue }) => {
changeValue(state, field, () => value)
},
}
const Layout = ({ const Layout = ({
provider, userAccount, onCallSafeContractSubmit, network, provider, userAccount, onCallSafeContractSubmit, network,
}: Props) => { }: Props) => {
@ -58,6 +64,7 @@ const Layout = ({
onSubmit={onCallSafeContractSubmit} onSubmit={onCallSafeContractSubmit}
steps={steps} steps={steps}
initialValues={initialValues} initialValues={initialValues}
mutators={formMutators}
testId="create-safe-form" testId="create-safe-form"
> >
<StepperPage>{SafeNameField}</StepperPage> <StepperPage>{SafeNameField}</StepperPage>

View File

@ -46,19 +46,22 @@ const styles = () => ({
button: { button: {
'&:last-child': { '&:last-child': {
marginLeft: sm, marginLeft: sm,
} },
}, },
}) })
type Props = { type Props = {
onClose: () => void, onClose: () => void,
classes: Object, classes: Object,
onScan: Function,
isOpen: boolean, isOpen: boolean,
} }
const ScanQRModal = ({ classes, onClose, isOpen }: Props) => { const ScanQRModal = ({
classes, onClose, isOpen, onScan,
}: Props) => {
const [hasWebcam, setHasWebcam] = useState(null) const [hasWebcam, setHasWebcam] = useState(null)
const scannerRef = React.createRef() const scannerRef: Object = React.createRef()
const openImageDialog = () => { const openImageDialog = () => {
scannerRef.current.openImageDialog() scannerRef.current.openImageDialog()
} }
@ -101,7 +104,7 @@ const ScanQRModal = ({ classes, onClose, isOpen }: Props) => {
ref={scannerRef} ref={scannerRef}
legacyMode={!hasWebcam} legacyMode={!hasWebcam}
onScan={(data) => { onScan={(data) => {
if (data) console.log(data) if (data) onScan(data)
}} }}
onError={(err) => { onError={(err) => {
console.error(err) console.error(err)
@ -112,7 +115,14 @@ const ScanQRModal = ({ classes, onClose, isOpen }: Props) => {
</Col> </Col>
<Hairline /> <Hairline />
<Row align="center" className={classes.buttonRow}> <Row align="center" className={classes.buttonRow}>
<Button color="secondary" className={classes.button} minHeight={42} minWidth={140} onClick={onClose} variant="contained"> <Button
color="secondary"
className={classes.button}
minHeight={42}
minWidth={140}
onClick={onClose}
variant="contained"
>
Close Close
</Button> </Button>
<Button <Button

View File

@ -66,22 +66,24 @@ export const calculateValuesAfterRemoving = (index: number, notRemovedOwners: nu
const SafeOwners = (props: Props) => { const SafeOwners = (props: Props) => {
const { const {
classes, errors, otherAccounts, values, updateInitialProps, classes, errors, otherAccounts, values, updateInitialProps, form,
} = props } = props
const [numOwners, setNumOwners] = useState<number>(1) const [numOwners, setNumOwners] = useState<number>(1)
const [qrModalOpen, setQrModalOpen] = useState<boolean>(false) const [qrModalOpen, setQrModalOpen] = useState<boolean>(false)
const [scanQrForOwnerName, setScanQrForOwnerName] = useState<string | null>(null)
const validOwners = getNumOwnersFrom(values) const validOwners = getNumOwnersFrom(values)
const openQrModal = () => { const openQrModal = (ownerName) => {
setScanQrForOwnerName(ownerName)
setQrModalOpen(true) setQrModalOpen(true)
} }
const closeQrModal = () => { const closeQrModal = () => {
setQrModalOpen(false) setQrModalOpen(false)
} }
const onRemoveRow = (index: number) => () => { const onRemoveRow = (index: number) => () => {
if (numOwners === 2) { if (numOwners === 2) {
const { form } = props
form.reset() form.reset()
} else { } else {
const initialValues = calculateValuesAfterRemoving(index, numOwners, values) const initialValues = calculateValuesAfterRemoving(index, numOwners, values)
@ -95,6 +97,11 @@ const SafeOwners = (props: Props) => {
setNumOwners(numOwners + 1) setNumOwners(numOwners + 1)
} }
const handleScan = (value) => {
form.mutators.setValue(scanQrForOwnerName, value)
closeQrModal()
}
return ( return (
<React.Fragment> <React.Fragment>
<Block className={classes.title}> <Block className={classes.title}>
@ -145,7 +152,14 @@ const SafeOwners = (props: Props) => {
/> />
</Col> </Col>
<Col xs={1} center="xs" middle="xs" className={classes.remove}> <Col xs={1} center="xs" middle="xs" className={classes.remove}>
<Img src={QRIcon} height={20} alt="Scan QR" onClick={openQrModal} /> <Img
src={QRIcon}
height={20}
alt="Scan QR"
onClick={() => {
openQrModal(addressName)
}}
/>
</Col> </Col>
<Col xs={1} center="xs" middle="xs" className={classes.remove}> <Col xs={1} center="xs" middle="xs" className={classes.remove}>
{index > 0 && <Img src={trash} height={20} alt="Delete" onClick={onRemoveRow(index)} />} {index > 0 && <Img src={trash} height={20} alt="Delete" onClick={onRemoveRow(index)} />}
@ -191,7 +205,7 @@ owner(s)
</Col> </Col>
</Row> </Row>
</Block> </Block>
{qrModalOpen && <ScanQRModal isOpen={qrModalOpen} onClose={closeQrModal} />} {qrModalOpen && <ScanQRModal isOpen={qrModalOpen} onScan={handleScan} onClose={closeQrModal} />}
</React.Fragment> </React.Fragment>
) )
} }