diff --git a/package.json b/package.json index 8af2be3..20c186a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "webfam", "private": true, - "version": "1.3.0", + "version": "1.5.0", "type": "module", "scripts": { "dev": "vite --open", diff --git a/src/App.jsx b/src/App.jsx index e58b955..b4af1a5 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -3,23 +3,25 @@ import { Route, Routes } from 'react-router-dom' import { useContext } from 'react' //* Components +import { AllUsers } from './components/AllUsers/AllUsers' +import { Citas } from './components/Citas/Citas' +import { CitaDetalle } from './components/Citas/CitaDetalle/CitaDetalle' +import { CitasUser } from './components/Citas/CitasUser/CitasUser' +import { EditUser } from './components/EditUser/EditUser' +import { InfoUserEdit } from './components/AllUsers/EditUser/InfoUserEdit' +import { FAQ } from './components/FAQ/FAQ' +import { AddFAQ } from './components/FAQ/AddFAQ/AddFAQ' +import { EditFAQ } from './components/FAQ/EditFAQ/EditFAQ' import { Home } from './components/Home/Home' import { Login } from './components/Login/Login' +import { RecoverPassword } from './components/Login/RecoverPassword/RecoverPassword' +import { ResetPassword } from './components/Login/ResetPassword/ResetPassword' +import { NotFound } from './components/Not-found/NotFound' import { Register } from './components/Register/Register' import { Services } from './components/Services/Services' import { Service } from './components/Services/Service/Service' -import { Citas } from './components/Citas/Citas' -import { CitaDetalle } from './components/Citas/CitaDetalle/CitaDetalle' -import { EditUser } from './components/EditUser/EditUser' -import { RecoverPassword } from './components/Login/RecoverPassword/RecoverPassword' import { AddService } from './components/Services/AddService/AddService' -import { AllUsers } from './components/AllUsers/AllUsers' -import { InfoUserEdit } from './components/AllUsers/EditUser/InfoUserEdit' import { EditService } from './components/Services/EditService/EditService' -import { CitasUser } from './components/Citas/CitasUser/CitasUser' - -import { NotFound } from './components/Not-found/NotFound' -import { ResetPassword } from './components/Login/ResetPassword/ResetPassword' import { ProtectedRoute } from './components/Utils' @@ -52,11 +54,15 @@ export const App = () => { } /> } /> } /> - } /> + {session && } />} } /> } /> + } /> + } /> + {session && } />} + } /> } /> diff --git a/src/components/FAQ/AddFAQ/AddFAQ.jsx b/src/components/FAQ/AddFAQ/AddFAQ.jsx new file mode 100644 index 0000000..2c6d381 --- /dev/null +++ b/src/components/FAQ/AddFAQ/AddFAQ.jsx @@ -0,0 +1,161 @@ +import axios from 'axios' +import Cookies from 'js-cookie' +import jwtDecode from 'jwt-decode' +import { useContext, useEffect, useRef, useState } from 'react' +import { useNavigate } from 'react-router-dom' +import { toast, ToastContainer, Zoom } from 'react-toastify' +import { SessionContext } from '../../../context/SessionContext' +import { ToastifyContext } from '../../../context/ToastifyContext' +import { Footer } from '../../Home/Footer/Footer' +import { API_URL, Button2, Input, Navbar, ResponsiveNav, TextArea } from '../../Utils' + +const AddFAQ = () => { + const { session } = useContext(SessionContext) + const { setToastify } = useContext(ToastifyContext) + + const tituloInputEl = useRef(null) + const respuestaInputEl = useRef(null) + + const [button, setButton] = useState(null) + const [idRol, setIdRol] = useState(null) + const [disabled, setDisabled] = useState(false) + + const navigate = useNavigate() + + useEffect(() => { + !session ? setButton(1) : setButton(2) + + window.scrollTo(0, 0) + + document.title = 'FADEMET Montajes | Crear Pregunta' + }, []) + + useEffect(() => { + const token = Cookies.get('token') + + if (!token) return + + new Promise((resolve, reject) => { + const decoded = jwtDecode(token) + resolve(decoded.data) + reject(new Error('Error al decodificar el token')) + }).then((decoded) => { + setIdRol(decoded[0].id_rol) + }) + }, []) + + const focusInput = (input) => input.current.focus() + + const postFAQ = (e) => { + e.preventDefault() + + const titulo = e.target[0].value + const respuesta = e.target[1].value + + if (titulo.length === 0 || /^\s+$/.test(titulo)) { + toast.error('¡El título no puede estar vacío!', { + theme: 'colored' + }) + focusInput(tituloInputEl) + setDisabled(false) + return + } else if (respuesta.length === 0 || /^\s+$/.test(respuesta)) { + toast.error('¡La respuesta no puede estar vacía!', { + theme: 'colored' + }) + focusInput(respuestaInputEl) + setDisabled(false) + return + } + setDisabled(true) + axios + .post(API_URL('createFaq'), { + titulo, + respuesta + }) + .then(() => { + toast.success('¡Pregunta frecuente creada con éxito!', { + theme: 'colored' + }) + setDisabled(false) + setToastify('faqCreado') + navigate('/frequent-questions') + }) + .catch(() => { + setDisabled(false) + toast.error('¡Hubo un error al crear la pregunta frecuente!', { + theme: 'colored' + }) + }) + } + + return ( + <> + + + +
+
+

Llena este formulario para crear una nueva pregunta frecuente

+
+
+
+
+ +