aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/ducks/custom-gas.js
blob: f1f483e9304ea6bc39697ca2eb13b9bf3fa4fc8c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import extend from 'xtend'

// Actions
const SET_CUSTOM_GAS_PRICE = 'metamask/custom-gas/SET_CUSTOM_GAS_PRICE'
const SET_CUSTOM_GAS_LIMIT = 'metamask/custom-gas/SET_CUSTOM_GAS_LIMIT'
const SET_CUSTOM_GAS_ERRORS = 'metamask/custom-gas/SET_CUSTOM_GAS_ERRORS'
const RESET_CUSTOM_GAS_STATE = 'metamask/custom-gas/RESET_CUSTOM_GAS_STATE'

// TODO: determine if this approach to initState is consistent with conventional ducks pattern
const initState = {
  price: 0,
  limit: 21000,
  errors: {},
}

// Reducer
export default function reducer ({ customGas: customGasState = initState }, action = {}) {
  const newState = extend({}, customGasState)

  switch (action.type) {
    case SET_CUSTOM_GAS_PRICE:
      return extend(newState, {
        price: action.value,
      })
    case SET_CUSTOM_GAS_LIMIT:
      return extend(newState, {
        limit: action.value,
      })
    case SET_CUSTOM_GAS_ERRORS:
      return extend(newState, {
        errors: {
          ...newState.errors,
          ...action.value,
        },
      })
    case RESET_CUSTOM_GAS_STATE:
      return extend({}, initState)
    default:
      return newState
  }
}

// Action Creators
export function setCustomGasPrice (newPrice) {
  return {
    type: SET_CUSTOM_GAS_PRICE,
    value: newPrice,
  }
}

export function setCustomGasLimit (newLimit) {
  return {
    type: SET_CUSTOM_GAS_LIMIT,
    value: newLimit,
  }
}

export function setCustomGasErrors (newErrors) {
  return {
    type: SET_CUSTOM_GAS_ERRORS,
    value: newErrors,
  }
}

export function resetCustomGasState () {
  return { type: RESET_CUSTOM_GAS_STATE }
}