Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 3175 Rev 3176
Línea 39... Línea 39...
39
    formState: { errors, isSubmitting },
39
    formState: { errors, isSubmitting },
40
    register,
40
    register,
41
    unregister,
41
    unregister,
42
    handleSubmit,
42
    handleSubmit,
43
    setValue,
43
    setValue,
44
    getValues,
-
 
45
    clearErrors,
44
    clearErrors,
46
    watch
45
    watch
47
  } = useForm({
46
  } = useForm({
48
    defaultValues: {
47
    defaultValues:
-
 
48
      modalType !== shareModalTypes.POST && modalType !== shareModalTypes.SHARE
-
 
49
        ? {
-
 
50
            posted_or_shared: modalType === shareModalTypes.SHARE ? 's' : 'p',
-
 
51
            share_width: 'p',
49
      description: '',
52
            description: '',
-
 
53
            file: ''
-
 
54
          }
-
 
55
        : {
-
 
56
            posted_or_shared: modalType === shareModalTypes.SHARE ? 's' : 'p',
50
      share_width: 'p'
57
            share_width: 'p',
-
 
58
            description: ''
51
    }
59
          }
52
  })
60
  })
-
 
61
  const watchedDescription = watch('description')
-
 
62
  const watchedFile = watch('file')
Línea 53... Línea 63...
53
 
63
 
Línea 54... Línea 64...
54
  const toggleConfirmModal = () => setShowConfirmModal(!showConfirmModal)
64
  const toggleConfirmModal = () => setShowConfirmModal(!showConfirmModal)
55
 
65
 
Línea 76... Línea 86...
76
  const onClose = () => {
86
  const onClose = () => {
77
    clearErrors()
87
    clearErrors()
78
    dispatch(closeShareModal())
88
    dispatch(closeShareModal())
79
  }
89
  }
Línea 80... Línea 90...
80
 
90
 
81
  const onSubmit = (data) => {
-
 
82
    const form = new FormData()
91
  const onSubmit = async (feed) => {
83
    form.append(
92
    try {
84
      'posted_or_shared',
-
 
85
      modalType === shareModalTypes.SHARE ? 's' : 'p'
-
 
86
    )
93
      const form = new FormData()
87
    Object.entries(data).forEach(([key, value]) => form.append(key, value))
94
      Object.entries(feed).forEach(([key, value]) => form.append(key, value))
88
 
-
 
89
    axios
95
 
90
      .post(postUrl, form)
-
 
91
      .then((response) => {
96
      const response = await axios.post(postUrl, form)
92
        const { data, success } = response.data
97
      const { data, success } = response.data
93
 
98
 
94
        if (!success) {
99
      if (!success) {
95
          const errorMessage =
100
        const errorMessage =
96
            typeof data === 'string'
101
          typeof data === 'string'
97
              ? data
102
            ? data
98
              : 'Ha ocurrido un error al publicar, inténtalo de nuevo más tarde.'
103
            : 'Ha ocurrido un error al publicar, inténtalo de nuevo más tarde.'
99
          throw new Error(errorMessage)
104
        throw new Error(errorMessage)
100
        }
105
      }
101
 
106
 
102
        const newFeed = data
107
      const newFeed = data
103
 
108
 
104
        dispatch(
109
      dispatch(
105
          addNotification({
110
        addNotification({
106
            style: 'success',
111
          style: 'success',
107
            msg: 'La publicación ha sido compartida'
112
          msg: 'La publicación ha sido compartida'
108
          })
113
        })
109
        )
114
      )
110
 
115
 
111
        onClose()
116
      onClose()
112
 
117
 
113
        if (feedSharedId) {
118
      if (feedSharedId) {
114
          dispatch(addFeed(newFeed, feedSharedId))
119
        dispatch(addFeed(newFeed, feedSharedId))
115
        } else {
120
      } else {
116
          dispatch(addFeed(newFeed))
121
        dispatch(addFeed(newFeed))
117
        }
122
      }
118
 
123
 
119
        setValue('description', '')
124
      setValue('description', '')
120
        setValue('file', '')
-
 
121
      })
125
      setValue('file', '')
122
      .catch((error) => {
126
    } catch (error) {
123
        console.error(error)
127
      console.error(error)
124
        dispatch(addNotification({ style: 'danger', msg: error.message }))
128
      dispatch(addNotification({ style: 'danger', msg: error.message }))
125
      })
129
    }
Línea 126... Línea 130...
126
  }
130
  }
127
 
-
 
128
  useEffect(() => {
-
 
129
    if (
-
 
130
      modalType !== shareModalTypes.POST &&
-
 
131
      modalType !== shareModalTypes.SHARE
131
 
132
    ) {
132
  useEffect(() => {
133
      register('file', {
-
 
134
        required: { value: 'true', message: 'El campo es requerido' }
133
    if (watchedFile !== undefined) {
135
      })
134
      register('file', { required: 'El campo es requerido' })
136
    } else {
135
    } else {
Línea 137... Línea -...
137
      if (!getValues('file')) unregister('file')
-
 
138
    }
136
      unregister('file')
139
 
137
    }
140
    if (getValues('file') || getValues('description')) {
138
 
141
      if (modalType !== lastModalType) {
-
 
142
        onClose()
139
    if ((watchedFile || watchedDescription) && modalType !== lastModalType) {
143
        toggleConfirmModal()
140
      onClose()
Línea 144... Línea 141...
144
      }
141
      toggleConfirmModal()
145
    }
142
    }