Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 14864 | Rev 14866 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 14864 Rev 14865
Línea 1... Línea 1...
1
import React, { useState, useEffect } from 'react'
1
import React, { useState, useEffect } from 'react'
2
import axios from 'axios'
2
import axios from 'axios'
3
import { Button, Modal } from 'react-bootstrap'
3
import { Button, Modal } from 'react-bootstrap'
4
import { useForm } from 'react-hook-form'
4
import { useForm } from 'react-hook-form'
5
import DescriptionInput from './DescriptionInput'
-
 
6
import { shareModalTypes } from '../redux/share-modal/shareModal.types'
5
import { shareModalTypes } from '../redux/share-modal/shareModal.types'
7
import { closeShareModal } from '../redux/share-modal/shareModal.actions'
6
import { closeShareModal } from '../redux/share-modal/shareModal.actions'
8
import { useDispatch, useSelector } from 'react-redux'
7
import { useDispatch, useSelector } from 'react-redux'
9
import DropzoneComponent from './Dropzone/DropzoneComponent'
8
import DropzoneComponent from './Dropzone/DropzoneComponent'
10
import { addFeed, fetchFeeds } from '../redux/feed/feed.actions'
9
import { addFeed, fetchFeeds } from '../redux/feed/feed.actions'
Línea 27... Línea 26...
27
		errors,
26
		errors,
28
		handleSubmit,
27
		handleSubmit,
29
		setValue,
28
		setValue,
30
		getValues,
29
		getValues,
31
		clearErrors,
30
		clearErrors,
32
		setError,
-
 
33
	} = useForm({
31
	} = useForm({
34
		defaultValues: {
32
		defaultValues: {
35
			description: '',
33
			description: '',
36
			share_width: '',
34
			share_width: '',
37
		}
35
		}
Línea 40... Línea 38...
40
	useEffect(() => {
38
	useEffect(() => {
41
		register('description', {
39
		register('description', {
42
			required: { value: 'true', message: 'El campo es requerido' },
40
			required: { value: 'true', message: 'El campo es requerido' },
43
		})
41
		})
44
		register('posted_or_shared')
42
		register('posted_or_shared')
45
		if (
-
 
46
			modalType !== shareModalTypes.POST &&
43
		if (modalType !== shareModalTypes.POST && modalType !== shareModalTypes.SHARE) {
47
			modalType !== shareModalTypes.SHARE
-
 
48
		) {
-
 
49
			register('file', {
44
			register('file', {
50
				required: { value: 'true', message: 'El campo es requerido' },
45
				required: { value: 'true', message: 'El campo es requerido' },
51
			})
46
			})
52
		} else {
47
		} else {
53
			if (!getValues('file')) unregister('file')
48
			if (!getValues('file')) unregister('file')
Línea 67... Línea 62...
67
	useEffect(() => {
62
	useEffect(() => {
68
		clearErrors()
63
		clearErrors()
69
	}, [isOpen])
64
	}, [isOpen])
Línea 70... Línea 65...
70
 
65
 
71
	const onSubmit = (data, e) => {
-
 
72
 
66
	const onSubmit = (data, e) => {
73
		setLoading(true)
-
 
74
 
67
		setLoading(true)
75
		const currentFormData = new FormData()
68
		const currentFormData = new FormData()
76
		for (let input in data) {
69
		for (let input in data) {
77
			currentFormData.append(input, data[input]);
-
 
78
			(`${input}:${data[input]}`)
70
			currentFormData.append(input, data[input])
-
 
71
		}
Línea 79... Línea 72...
79
		}
72
		currentFormData.append('posted_or_shared', modalType === shareModalTypes.SHARE ? shareModalTypes.SHARE : shareModalTypes.POST)
80
 
73
 
81
		axios.post(postUrl, currentFormData)
-
 
82
			.then(({ data }) => {
-
 
83
				const newFeed = data.data
74
		axios.post(postUrl, currentFormData)
84
 
75
			.then(({ data }) => {
85
				/* if (data.data.description || data.data.file || data.data.share_width) {
76
				/* if (data.data.description || data.data.file || data.data.share_width) {
86
					return Object.entries(data.data).map(([key, value]) => {
77
					return Object.entries(data.data).map(([key, value]) => {
87
						setError(key, { type: "required", message: value })
78
						setError(key, { type: "required", message: value })
88
					})
-
 
-
 
79
					})
89
				} */
80
				} */
90
 
81
				const newFeed = data.data
91
				if (data.success) {
82
				if (data.success) {
92
					setLoading(false)
83
					setLoading(false)
93
					e.target.reset()
84
					e.target.reset()
94
					setValue('description', '')
85
					setValue('description', '')
95
					setValue('file', '')
-
 
96
					clearErrors()
86
					setValue('file', '')
97
 
87
					clearErrors()
98
					closeModal()
88
					closeModal()
99
					dispatch(addNotification({
89
					dispatch(addNotification({
100
						style: 'success',
90
						style: 'success',
Línea 109... Línea 99...
109
						return dispatch(addFeed(newFeed, feedSharedId))
99
						return dispatch(addFeed(newFeed, feedSharedId))
110
					}
100
					}
Línea 111... Línea 101...
111
 
101
 
112
					return dispatch(addFeed(newFeed))
102
					return dispatch(addFeed(newFeed))
-
 
103
				}
113
				}
104
				typeof data.data === 'string'
-
 
105
					?
-
 
106
					dispatch(addNotification({
-
 
107
						style: 'danger',
114
 
108
						msg: data.data
-
 
109
					}))
-
 
110
					: Object.entries(data.data).map(([key, value]) =>
115
 
111
						value.map(err =>
116
				dispatch(addNotification({
112
							dispatch(addNotification({
117
					style: 'danger',
113
								style: 'danger',
-
 
114
								msg: `${key}: ${err}`
118
					msg: 'Ha ocurrido un error',
115
							}))
119
				}))
116
						)
120
 
117
					)
121
			})
-
 
122
 
118
			})
Línea 123... Línea 119...
123
	}
119
	}
124
 
120
 
125
	const onUploadedHandler = (files) => {
121
	const onUploadedHandler = (files) => {
126
		setValue('file', files)
122
		setValue('file', files)
Línea 127... Línea -...
127
		clearErrors('file')
-
 
128
	}
123
		clearErrors('file')
129
 
124
	}
130
 
125
 
131
	return (
126
	return (
132
		<Modal
127
		<Modal