import { FC, useState } from 'react'
import SwitchComponent from '../../../components/Switch'
import { useUpdateSetting } from '../hooks/useSettingData'
import { ErrorType } from '../../../helpers/types'
import { toast } from '../../../components/Toast'

type Props = {
    sms: boolean,
    id: string
}

const Sms: FC<Props> = (props: Props) => {
    const updateSetting = useUpdateSetting()
    const [sms, setSms] = useState<boolean>(props.sms)

    const handleChange = (value: boolean) => {
        setSms(value)
        updateSetting.mutate({ sms: value, id: props.id }, {
            onError: (error: ErrorType) => {
                toast(error.response?.data?.error?.message[0], 'error')
            }
        })
    }

    return (
        <SwitchComponent
            active={sms}
            onChange={handleChange}
        />
    )
}

export default Sms