feat: twilio setup in notifications

This commit is contained in:
Nikola Ubavic 2022-03-07 23:42:29 +01:00
parent 348260931e
commit cba18edc95
2 changed files with 107 additions and 13 deletions

View file

@ -3,7 +3,11 @@ import gql from 'graphql-tag'
import * as R from 'ramda' import * as R from 'ramda'
import React, { useState } from 'react' import React, { useState } from 'react'
import Modal from 'src/components/Modal'
import TitleSection from 'src/components/layout/TitleSection' import TitleSection from 'src/components/layout/TitleSection'
import { P } from 'src/components/typography'
import FormRenderer from 'src/pages/Services/FormRenderer'
import twilioSchema from 'src/pages/Services/schemas/twilio'
import { fromNamespace, toNamespace, namespaces } from 'src/utils/config' import { fromNamespace, toNamespace, namespaces } from 'src/utils/config'
import Section from '../../components/layout/Section' import Section from '../../components/layout/Section'
@ -28,6 +32,7 @@ const GET_INFO = gql`
code code
display display
} }
accounts
} }
` `
@ -37,6 +42,12 @@ const SAVE_CONFIG = gql`
} }
` `
const SAVE_ACCOUNT = gql`
mutation Save($accounts: JSONObject) {
saveAccounts(accounts: $accounts)
}
`
const FIELDS_WIDTH = 130 const FIELDS_WIDTH = 130
const Notifications = ({ const Notifications = ({
@ -52,6 +63,7 @@ const Notifications = ({
const [section, setSection] = useState(null) const [section, setSection] = useState(null)
const [error, setError] = useState(null) const [error, setError] = useState(null)
const [editingKey, setEditingKey] = useState(null) const [editingKey, setEditingKey] = useState(null)
const [smsSetupPopup, setSmsSetupPopup] = useState(false)
const { data, loading } = useQuery(GET_INFO) const { data, loading } = useQuery(GET_INFO)
@ -61,9 +73,16 @@ const Notifications = ({
onError: error => setError(error) onError: error => setError(error)
}) })
const [saveAccount] = useMutation(SAVE_ACCOUNT, {
onCompleted: () => setSmsSetupPopup(false),
refetchQueries: ['getData'],
onError: error => setError(error)
})
const config = fromNamespace(SCREEN_KEY)(data?.config) const config = fromNamespace(SCREEN_KEY)(data?.config)
const machines = data?.machines const machines = data?.machines
const cryptoCurrencies = data?.cryptoCurrencies const cryptoCurrencies = data?.cryptoCurrencies
const twilioAvailable = R.has('twilio', data?.accounts || {})
const currency = R.path(['fiatCurrency'])( const currency = R.path(['fiatCurrency'])(
fromNamespace(namespaces.LOCALE)(data?.config) fromNamespace(namespaces.LOCALE)(data?.config)
@ -83,6 +102,14 @@ const Notifications = ({
setEditingKey(state ? key : null) setEditingKey(state ? key : null)
} }
const twilioSave = it => {
setError(null)
R.compose(save(null), toNamespace('sms'))({ active: true })
return saveAccount({
variables: { accounts: { twilio: it } }
})
}
const isEditing = key => editingKey === key const isEditing = key => editingKey === key
const isDisabled = key => editingKey && editingKey !== key const isDisabled = key => editingKey && editingKey !== key
@ -97,7 +124,9 @@ const Notifications = ({
setEditing, setEditing,
setSection, setSection,
machines, machines,
cryptoCurrencies cryptoCurrencies,
twilioAvailable,
setSmsSetupPopup
} }
return ( return (
@ -140,6 +169,23 @@ const Notifications = ({
)} )}
</Section> </Section>
)} )}
{smsSetupPopup && (
<Modal
title={`Configure Twilio`}
width={478}
handleClose={() => setSmsSetupPopup(false)}
open={true}>
<P>
In order for the SMS notifications to work, you'll first need to
configure Twilio.
</P>
<FormRenderer
save={twilioSave}
elements={twilioSchema.elements}
validationSchema={twilioSchema.getValidationSchema}
/>
</Modal>
)}
</NotificationsCtx.Provider> </NotificationsCtx.Provider>
) )
) )

View file

@ -26,25 +26,33 @@ const sizes = {
active: 263 active: 263
} }
const Row = ({ namespace, forceDisable, shouldUpperCase }) => { const Row = ({
const { data: rawData, save: rawSave } = useContext(NotificationsCtx) namespace,
data,
const save = R.compose(rawSave(null), toNamespace(namespace)) forceDisable,
const data = fromNamespace(namespace)(rawData) save,
shouldUpperCase,
onActivation
}) => {
const disabled = forceDisable || !data || !data.active const disabled = forceDisable || !data || !data.active
const Cell = ({ name, disabled }) => { const Cell = ({ name, disabled }) => {
const value = !!(data && data[name]) const value = !!(data && data[name])
const onChange = event => {
if (name === 'active' && value === false) {
if (!onActivation()) return
}
save({ [name]: event.target.checked })
}
return ( return (
<Td width={sizes[name]} textAlign="center"> <Td width={sizes[name]} textAlign="center">
<Switch <Switch
disabled={disabled} disabled={disabled}
checked={value} checked={value}
onChange={event => { onChange={onChange}
save({ [name]: event.target.checked })
}}
value={value} value={value}
/> />
</Td> </Td>
@ -71,7 +79,40 @@ const useStyles = makeStyles({
width: 930 width: 930
} }
}) })
const Setup = ({ wizard, forceDisable }) => { const Setup = ({ wizard, forceDisable }) => {
const {
data: rawData,
save: rawSave,
twilioAvailable,
setSmsSetupPopup
} = useContext(NotificationsCtx)
const namespaces = [
{
name: 'email',
forceDisable: forceDisable,
shouldUpperCase: false,
onActivation: () => true
},
{
name: 'sms',
forceDisable: forceDisable,
shouldUpperCase: true,
onActivation: () => {
if (twilioAvailable) return true
setSmsSetupPopup(true)
return false
}
},
{
name: 'notificationCenter',
forceDisable: forceDisable,
shouldUpperCase: false,
onActivation: () => true
}
]
const widthAdjust = wizard ? 20 : 0 const widthAdjust = wizard ? 20 : 0
const classes = useStyles() const classes = useStyles()
return ( return (
@ -85,9 +126,16 @@ const Setup = ({ wizard, forceDisable }) => {
))} ))}
</THead> </THead>
<TBody> <TBody>
<Row namespace="email" forceDisable={forceDisable} /> {namespaces.map(namespace => (
<Row namespace="sms" shouldUpperCase forceDisable={forceDisable} /> <Row
<Row namespace="notificationCenter" forceDisable={forceDisable} /> namespace={namespace.name}
forceDisable={namespace.forceDisable}
save={R.compose(rawSave(null), toNamespace(namespace.name))}
data={fromNamespace(namespace.name)(rawData)}
shouldUpperCase={namespace.shouldUpperCase}
onActivation={namespace.onActivation}
/>
))}
</TBody> </TBody>
</Table> </Table>
) )