fix: make accounts a object

This commit is contained in:
Taranto 2020-04-22 17:28:11 +01:00
parent ec73f0b022
commit 6b3db134e7
7 changed files with 26 additions and 38 deletions

View file

@ -196,7 +196,7 @@ const typeDefs = gql`
uptime: [ProcessStatus]
serverLogs: [ServerLog]
transactions: [Transaction]
accounts: [JSONObject]
accounts: JSONObject
config: JSONObject
}
@ -221,8 +221,7 @@ const typeDefs = gql`
setCustomer(customerId: ID!, customerInput: CustomerInput): Customer
saveConfig(config: JSONObject): JSONObject
createPairingTotem(name: String!): String
saveAccount(account: JSONObject): [JSONObject]
saveAccounts(accounts: [JSONObject]): [JSONObject]
saveAccounts(accounts: JSONObject): JSONObject
}
`
@ -259,7 +258,6 @@ const resolvers = {
machineSupportLogs: (...[, { deviceId }]) => supportLogs.insert(deviceId),
createPairingTotem: (...[, { name }]) => pairing.totem(name),
serverSupportLogs: () => serverLogs.insert(),
saveAccount: (...[, { account }]) => settingsLoader.saveAccounts([account]),
saveAccounts: (...[, { accounts }]) => settingsLoader.saveAccounts(accounts),
setCustomer: (...[, { customerId, customerInput } ]) => customers.updateCustomer(customerId, customerInput),
saveConfig: (...[, { config }]) => settingsLoader.saveConfig(config)

View file

@ -9,20 +9,11 @@ low(adapter).then(it => {
db = it
})
function replace (array, index, value) {
return array.slice(0, index).concat([value]).concat(array.slice(index + 1))
}
function replaceOrAdd (accounts, account) {
const index = _.findIndex(['code', account.code], accounts)
return index !== -1 ? replace(accounts, index, account) : _.concat(accounts)(account)
}
function saveAccounts (accountsToSave) {
const currentState = db.getState() || {}
const accounts = currentState.accounts || []
const accounts = currentState.accounts || {}
const newAccounts = _.reduce(replaceOrAdd)(accounts)(accountsToSave)
const newAccounts = _.assign(accounts)(accountsToSave)
const newState = _.set('accounts', newAccounts, currentState)
db.setState(newState)

View file

@ -19,8 +19,8 @@ const GET_INFO = gql`
`
const SAVE_ACCOUNT = gql`
mutation Save($account: JSONObject) {
saveAccount(account: $account)
mutation Save($accounts: JSONObject) {
saveAccounts(accounts: $accounts)
}
`
@ -45,13 +45,11 @@ const Services = ({ key: SCREEN_KEY }) => {
const classes = useStyles()
const accounts = data?.accounts ?? []
const getValue = code => R.find(R.propEq('code', code))(accounts)
const accounts = data?.accounts ?? {}
const getItems = (code, elements) => {
const faceElements = R.filter(R.prop('face'))(elements)
const values = getValue(code) || {}
const values = accounts[code] || {}
return R.map(({ display, code, long }) => ({
label: display,
value: long ? formatLong(values[code]) : values[code]
@ -81,12 +79,12 @@ const Services = ({ key: SCREEN_KEY }) => {
<FormRenderer
save={it =>
saveAccount({
variables: { account: { code: editingSchema.code, ...it } }
variables: { accounts: { [editingSchema.code]: it } }
})
}
elements={editingSchema.elements}
validationSchema={editingSchema.validationSchema}
value={getValue(editingSchema.code)}
value={accounts[editingSchema.code]}
/>
</Modal>
)}

View file

@ -11,7 +11,7 @@ import Wizard from './Wizard'
import { WalletSchema, getElements } from './helper'
const SAVE_CONFIG = gql`
mutation Save($config: JSONObject, $accounts: [JSONObject]) {
mutation Save($config: JSONObject, $accounts: JSONObject) {
saveConfig(config: $config)
saveAccounts(accounts: $accounts)
}

View file

@ -18,7 +18,7 @@ const filterConfig = (crypto, type) =>
const getItems = (accountsConfig, accounts, type, crypto) => {
const fConfig = filterConfig(crypto, type)(accountsConfig)
const find = code => R.find(R.propEq('code', code))(accounts)
const find = code => accounts && accounts[code]
const [filled, unfilled] = R.partition(({ code }) => {
const account = find(code)
@ -35,7 +35,7 @@ const Wizard = ({ coin, onClose, accountsConfig, accounts, save, error }) => {
const [{ step, config, accountsToSave }, setState] = useState({
step: 0,
config: { active: true },
accountsToSave: []
accountsToSave: {}
})
const title = `Enable ${coin.display}`
@ -48,9 +48,11 @@ const Wizard = ({ coin, onClose, accountsConfig, accounts, save, error }) => {
const getValue = code => R.find(R.propEq('code', code))(accounts)
const onContinue = async (it, it2) => {
const newConfig = R.merge(config, it)
const newAccounts = it2 ? R.concat(accountsToSave, [it2]) : accountsToSave
const onContinue = async (stepConfig, stepAccount) => {
const newConfig = R.merge(config, stepConfig)
const newAccounts = stepAccount
? R.merge(accountsToSave, stepAccount)
: accountsToSave
if (isLastStep) {
return save(toNamespace(coin.code, newConfig), newAccounts)

View file

@ -13,6 +13,7 @@ import schema from 'src/pages/Services/schemas'
import { startCase } from 'src/utils/string'
import styles from './WizardStep.styles'
const useStyles = makeStyles(styles)
const initialState = {
@ -41,7 +42,7 @@ const reducer = (state, action) => {
iError: false
}
case 'error':
return R.merge(state, { iError: true })
return R.merge(state, { innerError: true })
case 'reset':
return initialState
default:
@ -61,7 +62,7 @@ const WizardStep = ({
getValue
}) => {
const classes = useStyles()
const [{ iError, selected, form, isNew }, dispatch] = useReducer(
const [{ innerError, selected, form, isNew }, dispatch] = useReducer(
reducer,
initialState
)
@ -70,7 +71,7 @@ const WizardStep = ({
dispatch({ type: 'reset' })
}, [step])
const iContinue = (config, account) => {
const innerContinue = (config, account) => {
if (!config || !config[type]) {
return dispatch({ type: 'error' })
}
@ -81,7 +82,7 @@ const WizardStep = ({
const displayName = name ?? type
const subtitleClass = {
[classes.subtitle]: true,
[classes.error]: iError
[classes.error]: innerError
}
return (
@ -129,9 +130,7 @@ const WizardStep = ({
</div>
{form && (
<FormRenderer
save={it =>
iContinue({ [type]: form.code }, R.merge(it, { code: form.code }))
}
save={it => innerContinue({ [type]: form.code }, { [form.code]: it })}
elements={schema[form.code].elements}
validationSchema={schema[form.code].validationSchema}
value={getValue(form.code)}
@ -143,7 +142,7 @@ const WizardStep = ({
{error && <ErrorMessage>Failed to save</ErrorMessage>}
<Button
className={classes.button}
onClick={() => iContinue({ [type]: selected })}>
onClick={() => innerContinue({ [type]: selected })}>
{label}
</Button>
</div>

View file

@ -8,7 +8,7 @@ const namespaces = {
SERVICES: 'services',
LOCALE: 'locale',
COMMISSIONS: 'commissions',
CONTACT_INFO: 'contactInfo',
CONTACT_INFO: 'operatorInfo',
RECEIPT: 'receipt',
COIN_ATM_RADAR: 'coinAtmRadar',
TERMS_CONDITIONS: 'termsConditions'