Jack Mallers
7 years ago
10 changed files with 151 additions and 6 deletions
@ -0,0 +1,20 @@ |
|||||
|
import React, { Component } from 'react' |
||||
|
import PropTypes from 'prop-types' |
||||
|
import styles from './PayForm.scss' |
||||
|
|
||||
|
class PayForm extends Component { |
||||
|
render() { |
||||
|
console.log('props: ', this.props) |
||||
|
return ( |
||||
|
<div> |
||||
|
payform |
||||
|
</div> |
||||
|
) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
PayForm.propTypes = { |
||||
|
|
||||
|
} |
||||
|
|
||||
|
export default PayForm |
@ -0,0 +1,3 @@ |
|||||
|
import PayForm from './PayForm' |
||||
|
|
||||
|
export default PayForm |
@ -0,0 +1,83 @@ |
|||||
|
import { createSelector } from 'reselect' |
||||
|
import bitcoin from 'bitcoinjs-lib' |
||||
|
|
||||
|
// Initial State
|
||||
|
const initialState = { |
||||
|
amount: '0', |
||||
|
payInput: '' |
||||
|
} |
||||
|
|
||||
|
// Constants
|
||||
|
// ------------------------------------
|
||||
|
export const SET_PAY_AMOUNT = 'SET_PAY_AMOUNT' |
||||
|
export const SET_PAY_INPUT = 'SET_PAY_INPUT' |
||||
|
|
||||
|
export const RESET_FORM = 'RESET_FORM' |
||||
|
|
||||
|
// ------------------------------------
|
||||
|
// Actions
|
||||
|
// ------------------------------------
|
||||
|
export function setPayAmount(amount) { |
||||
|
return { |
||||
|
type: SET_PAY_AMOUNT, |
||||
|
amount |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export function setPayInput(payInput) { |
||||
|
return { |
||||
|
type: SET_PAY_INPUT, |
||||
|
payInput |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export function resetForm() { |
||||
|
return { |
||||
|
type: RESET_FORM |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// ------------------------------------
|
||||
|
// Action Handlers
|
||||
|
// ------------------------------------
|
||||
|
const ACTION_HANDLERS = { |
||||
|
[SET_PAY_AMOUNT]: (state, { amount }) => ({ ...state, amount }), |
||||
|
[SET_PAY_INPUT]: (state, { payInput }) => ({ ...state, payInput }), |
||||
|
[RESET_FORM]: () => (initialState) |
||||
|
} |
||||
|
|
||||
|
// ------------------------------------
|
||||
|
// Selector
|
||||
|
// ------------------------------------
|
||||
|
const payFormSelectors = {} |
||||
|
const payInputSelector = state => state.payform.payInput |
||||
|
|
||||
|
payFormSelectors.isOnchain = createSelector( |
||||
|
payInputSelector, |
||||
|
(input) => { |
||||
|
// TODO: work with bitcoin-js to fix p2wkh error and make testnet/mainnet dynamic
|
||||
|
try { |
||||
|
bitcoin.address.toOutputScript(input, bitcoin.networks.testnet) |
||||
|
return true |
||||
|
} catch (e) { |
||||
|
return false |
||||
|
} |
||||
|
} |
||||
|
) |
||||
|
|
||||
|
// TODO: Add more robust logic to detect a LN payment request
|
||||
|
payFormSelectors.isLn = createSelector( |
||||
|
payInputSelector, |
||||
|
input => input.length === 124 |
||||
|
) |
||||
|
|
||||
|
export { payFormSelectors } |
||||
|
|
||||
|
// ------------------------------------
|
||||
|
// Reducer
|
||||
|
// ------------------------------------
|
||||
|
export default function payFormReducer(state = initialState, action) { |
||||
|
const handler = ACTION_HANDLERS[action.type] |
||||
|
|
||||
|
return handler ? handler(state, action) : state |
||||
|
} |
Loading…
Reference in new issue