Skip to content

Commit

Permalink
Merge pull request #113 from KrlosPK/carlosDev
Browse files Browse the repository at this point in the history
v1.5.0
  • Loading branch information
StivenKN authored Mar 28, 2023
2 parents 391cc29 + b9e658c commit f1a8f44
Show file tree
Hide file tree
Showing 15 changed files with 723 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "webfam",
"private": true,
"version": "1.3.0",
"version": "1.5.0",
"type": "module",
"scripts": {
"dev": "vite --open",
Expand Down
28 changes: 17 additions & 11 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -52,11 +54,15 @@ export const App = () => {
<Route path='/services' element={<Services />} />
<Route path='/add-service' element={<AddService />} />
<Route path='/services/:serviceId' element={<Service />} />
<Route path='/edit-service/:serviceId' element={<EditService />} />
{session && <Route path='/edit-service/:serviceId' element={<EditService />} />}

<Route path='/all-users' element={<AllUsers />} />
<Route path='/info-user-edit/:id' element={<InfoUserEdit />} />

<Route path='/frequent-questions' element={<FAQ />} />
<Route path='/add-frequent-question' element={<AddFAQ />} />
{session && <Route path='/edit-frequent-question/:faqId' element={<EditFAQ />} />}

<Route path='/recover-password' element={<RecoverPassword />} />
<Route path='*' element={<NotFound />} />
</Routes>
Expand Down
161 changes: 161 additions & 0 deletions src/components/FAQ/AddFAQ/AddFAQ.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<ToastContainer transition={Zoom} limit={3} pauseOnFocusLoss={false} />
<ResponsiveNav
linkText={
!session
? ['Inicio', 'Servicios']
: idRol && idRol === 2
? ['Inicio', 'Servicios', 'Mis Agendas']
: ['Inicio', 'Agendas', 'Servicios']
}
linkUrl={
!session
? ['/', '/services']
: idRol && idRol === 2
? ['/', '/services', '/mis-citas']
: ['/', '/citas', '/services']
}
renderButtons={button}
/>
<Navbar
linkText={
!session
? ['Inicio', 'Servicios']
: idRol && idRol === 2
? ['Inicio', 'Servicios', 'Mis Agendas']
: ['Inicio', 'Agendas', 'Servicios']
}
linkUrl={
!session
? ['/', '/services']
: idRol && idRol === 2
? ['/', '/services', '/mis-citas']
: ['/', '/citas', '/services']
}
renderButtons={button}
/>
<section className='add-service-form'>
<div className='info-create'>
<p>Llena este formulario para crear una nueva pregunta frecuente</p>
<div className='buttons'></div>
</div>
<form className='service-form' onSubmit={postFAQ}>
<div className='main-form'>
<Input
text='Título de la pregunta'
innerId='titulo-pregunta'
nameID='titulo-pregunta'
max={30}
innerRef={tituloInputEl}
/>
<TextArea
innerRef={respuestaInputEl}
placeholder='Respuesta de la pregunta'
rows={10}
cols={10}
max={100}
/>
</div>
<div className='send'>
<Button2 text={'Crear'} width={150} disable={disabled} textDisabled={'Cargando'} />
</div>
</form>
</section>
<Footer />
</>
)
}

export { AddFAQ }
Loading

0 comments on commit f1a8f44

Please sign in to comment.