import React, { useEffect, useState } from 'react' import { useSelector } from 'react-redux' import { useNavigate } from 'react-router-dom' import useValidation from '../custom/useValidation' import Spinner from '../layouts/Spinner' import axios from 'axios' import { BASE_URL, getConfig } from '../../helpers/config' import { toast } from 'react-toastify' import useTitle from '../custom/useTitle' export default function UpdatePassword() { const { token, isLoggedIn } = useSelector(state => state.user) const navigate = useNavigate() const [newPassword, setNewPassword] = useState('') const [currentPassword, setCurrentPassword] = useState('') const [errors, setErrors] = useState([]) const [loading, setLoading] = useState(false) //change page title useTitle('Update Password') useEffect(() => { if (!isLoggedIn) navigate('/login') }, [isLoggedIn]) const updatePassword = async (e) => { e.preventDefault() setErrors([]) setLoading(true) const data = { currentPassword, newPassword} try { const response = await axios.put(`${BASE_URL}/update/password`, data, getConfig(token)) if (response.data.error) { setLoading(false) toast.error(response.data.error) }else { setLoading(false) setCurrentPassword('') setNewPassword('') toast.success(response.data.message) navigate('/profile') } } catch (error) { setLoading(false) if (error?.response?.status === 422) { setErrors(error.response.data.errors) } console.log(error) } } return (