Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15507 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5647 stevensc 1
import React, { useEffect, useState } from 'react'
15507 stevensc 2
import { useForm } from 'react-hook-form'
3
import { CKEditor } from 'ckeditor4-react'
4
import { Modal, Button } from 'react-bootstrap'
5
import { axios, CKEDITOR_OPTIONS } from '../../../utils'
6
import ToggleComponent from '../../../shared/ToggleComponent'
5647 stevensc 7
 
8
const EditModal = ({
15507 stevensc 9
	title = 'Indusrias',
10
	url = '',
11
	isOpen = false,
12
	currentItem = null,
13
	onClose = function () { },
14
	onComplete= function () { }
5647 stevensc 15
}) => {
15511 stevensc 16
	const [isActive, setIsActive] = useState(currentItem?.status === 'a' ? true : false)
15507 stevensc 17
	const {register,handleSubmit,getValues,setValue,clearErrors,formState: { errors }} = useForm({
18
		defaultValues: {
19
			description: '',
20
		}
21
	})
5647 stevensc 22
 
15507 stevensc 23
	const onSubmit = (data) => {
24
		const formData = new FormData()
5647 stevensc 25
 
15507 stevensc 26
		Object.entries(data).forEach(([key, value])=> formData.append(key, value))
27
		formData.append('status', isActive === true ? 'a' : 'i')
5647 stevensc 28
 
15507 stevensc 29
		axios.post(url, formData)
30
			.then(({ data: response }) => {
31
				if (response.success) {
32
					onComplete()
33
					onClose()
34
				}
35
			})
36
			.catch((err) => console.log(err))
37
	}
5647 stevensc 38
 
15507 stevensc 39
	useEffect(() => {
40
		register('description', {
41
			required: { value: 'true' },
42
		})
43
	}, [currentItem])
5647 stevensc 44
 
15507 stevensc 45
	useEffect(() => {
46
		if (currentItem?.status === 'a') {
47
			setIsActive(true)
48
		}
49
	}, [currentItem])
50
 
51
	return (
52
		<Modal show={isOpen} onHide={onClose}>
53
			<Modal.Header closeButton>
54
				<Modal.Title>{title} - {currentItem ? 'Editar' : 'Agregar'}</Modal.Title>
55
			</Modal.Header>
56
			<form onSubmit={handleSubmit(onSubmit)}>
57
				<Modal.Body>
58
					<div className="form-group">
59
						<label>Nombre</label>
60
						<input
61
							className='form-control'
62
							name='name'
63
							ref={register}
64
							defaultValue={currentItem ? currentItem.name : ''}
65
						/>
66
						{errors.name && <p>Este campo es requerido</p>}
67
					</div>
68
					<CKEditor
69
						onChange={(e) => {
70
							const text = e.editor.getData()
71
							setValue('description', text)
72
							if (errors.description && getValues('description')) {
73
								clearErrors('description')
74
							}
75
						}}
76
						onInstanceReady={(e) => currentItem && e.editor.setData(currentItem.name)}
77
						config={CKEDITOR_OPTIONS}
78
						name="description"
79
					/>
80
					{errors.description && <p>Este campo es requerido</p>}
15511 stevensc 81
					<ToggleComponent currentValue={isActive} setValue={(value)=> setIsActive(value)}/>
15507 stevensc 82
				</Modal.Body>
83
				<Modal.Footer>
84
					<Button variant="primary" type="submit">
5647 stevensc 85
                        Enviar
15507 stevensc 86
					</Button>
87
					<Button variant="danger" onClick={onClose}>
5647 stevensc 88
                        Cancelar
15507 stevensc 89
					</Button>
90
				</Modal.Footer>
91
			</form>
92
		</Modal >
93
	)
5647 stevensc 94
}
95
 
96
export default EditModal