<commit_before>import { useQuery, useMutation } from '@apollo/react-hooks'
import { makeStyles } from '@material-ui/core/styles'
import gql from 'graphql-tag'
import * as R from 'ramda'
import React, { useState } from 'react'

import Tooltip from 'src/components/Tooltip'
import { NamespacedTable as EditableTable } from 'src/components/editableTable'
import { Switch } from 'src/components/inputs'
import TitleSection from 'src/components/layout/TitleSection'
import { P, Label2 } from 'src/components/typography'
import { fromNamespace, toNamespace } from 'src/utils/config'

import Wizard from './Wizard'
import { DenominationsSchema, getElements } from './helper'

const useStyles = makeStyles({
  fudgeFactor: {
    display: 'flex',
    alignItems: 'center',
    marginRight: 156
  },
  switchLabel: {
    margin: 6,
    width: 24
  }
})

const SAVE_CONFIG = gql`
  mutation Save($config: JSONObject) {
    saveConfig(config: $config)
  }
`

const GET_INFO = gql`
  query getData {
    machines {
      name
      deviceId
      cashbox
      cassette1
      cassette2
    }
    config
  }
`

const CashOut = ({ name: SCREEN_KEY }) => {
  const classes = useStyles()
  const [wizard, setWizard] = useState(false)
  const { data } = useQuery(GET_INFO)

  const [saveConfig, { error }] = useMutation(SAVE_CONFIG, {
    onCompleted: () => setWizard(false),
    refetchQueries: () => ['getData']
  })

  const save = (rawConfig, accounts) => {
    const config = toNamespace(SCREEN_KEY)(rawConfig)
    return saveConfig({ variables: { config, accounts } })
  }

  const config = data?.config && fromNamespace(SCREEN_KEY)(data.config)
  const fudgeFactorActive = config?.fudgeFactorActive ?? false
  const locale = data?.config && fromNamespace('locale')(data.config)
  const machines = data?.machines ?? []

  const onToggle = id => {
    const namespaced = fromNamespace(id)(config)
    if (!DenominationsSchema.isValidSync(namespaced)) return setWizard(id)
    save(toNamespace(id, { active: !namespaced?.active }))
  }

  return (
    <>
      <TitleSection title="Cash-out">
        <div className={classes.fudgeFactor}>
          <P>Transaction fudge factor</P>
          <Switch
            checked={fudgeFactorActive}
            onChange={event => {
              save({ fudgeFactorActive: event.target.checked })
            }}
            value={fudgeFactorActive}
          />
          <Label2 className={classes.switchLabel}>
            {fudgeFactorActive ? 'On' : 'Off'}
          </Label2>
          <Tooltip width={304}>
            <P>
              Automatically accept customer deposits as complete if their
              received amount is 10 crypto atoms or less.
            </P>
            <P>
              (Crypto atoms are the smallest unit in each cryptocurrency. E.g.,
              satoshis in Bitcoin, or wei in Ethereum.)<commit_msg>fix: fudge factor text(crypto atoms)<commit_after>import { useQuery, useMutation } from '@apollo/react-hooks'
import { makeStyles } from '@material-ui/core/styles'
import gql from 'graphql-tag'
import * as R from 'ramda'
import React, { useState } from 'react'

import Tooltip from 'src/components/Tooltip'
import { NamespacedTable as EditableTable } from 'src/components/editableTable'
import { Switch } from 'src/components/inputs'
import TitleSection from 'src/components/layout/TitleSection'
import { P, Label2 } from 'src/components/typography'
import { fromNamespace, toNamespace } from 'src/utils/config'

import Wizard from './Wizard'
import { DenominationsSchema, getElements } from './helper'

const useStyles = makeStyles({
  fudgeFactor: {
    display: 'flex',
    alignItems: 'center',
    marginRight: 156
  },
  switchLabel: {
    margin: 6,
    width: 24
  }
})

const SAVE_CONFIG = gql`
  mutation Save($config: JSONObject) {
    saveConfig(config: $config)
  }
`

const GET_INFO = gql`
  query getData {
    machines {
      name
      deviceId
      cashbox
      cassette1
      cassette2
    }
    config
  }
`

const CashOut = ({ name: SCREEN_KEY }) => {
  const classes = useStyles()
  const [wizard, setWizard] = useState(false)
  const { data } = useQuery(GET_INFO)

  const [saveConfig, { error }] = useMutation(SAVE_CONFIG, {
    onCompleted: () => setWizard(false),
    refetchQueries: () => ['getData']
  })

  const save = (rawConfig, accounts) => {
    const config = toNamespace(SCREEN_KEY)(rawConfig)
    return saveConfig({ variables: { config, accounts } })
  }

  const config = data?.config && fromNamespace(SCREEN_KEY)(data.config)
  const fudgeFactorActive = config?.fudgeFactorActive ?? false
  const locale = data?.config && fromNamespace('locale')(data.config)
  const machines = data?.machines ?? []

  const onToggle = id => {
    const namespaced = fromNamespace(id)(config)
    if (!DenominationsSchema.isValidSync(namespaced)) return setWizard(id)
    save(toNamespace(id, { active: !namespaced?.active }))
  }

  return (
    <>
      <TitleSection title="Cash-out">
        <div className={classes.fudgeFactor}>
          <P>Transaction fudge factor</P>
          <Switch
            checked={fudgeFactorActive}
            onChange={event => {
              save({ fudgeFactorActive: event.target.checked })
            }}
            value={fudgeFactorActive}
          />
          <Label2 className={classes.switchLabel}>
            {fudgeFactorActive ? 'On' : 'Off'}
          </Label2>
          <Tooltip width={304}>
            <P>
              Automatically accept customer deposits as complete if their
              received amount is 100 crypto atoms or less.
            </P>
            <P>
              (Crypto atoms are the smallest unit in each cryptocurrency. E.g.,
              satoshis in Bitcoin, or wei in Ethereum.)
{'fraction_non_alphanumeric': 0.1172282042375825, 'fraction_numerical': 0.005731156651615144, 'mean_word_length': 2.8885887913571913, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6951983', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1708, 'n_words': 435}