Commit 3a89a1c6 authored by Yannis Barlas's avatar Yannis Barlas

fix(editor panel): fix chat messages not being registered

parent 99380466
/* eslint-disable react/prop-types */
import React from 'react'
import * as yup from 'yup'
import { Form } from './index'
const stripHTML = html => {
if (html.length === 0) return ''
const tmp = document.createElement('DIV')
tmp.innerHTML = html
return tmp.textContent || tmp.innerText || ''
}
const initialValues = {
content: '',
}
const validations = yup.object().shape({
content: yup
.string()
.test(
'chat-message-not-empty',
'Chat message is required',
val => stripHTML(val).length > 0,
),
})
const DiscussForm = props => {
const { article, sendChat, ...otherProps } = props
const handleSubmit = (values, formikBag) => {
const { content: message } = values
const { id: manuscriptId } = article
const { resetForm, setStatus } = formikBag
sendChat({
variables: {
......@@ -22,6 +41,9 @@ const DiscussForm = props => {
message,
},
},
}).then(() => {
resetForm()
setStatus({ hasReset: true })
})
}
......@@ -29,6 +51,7 @@ const DiscussForm = props => {
<Form
initialValues={initialValues}
onSubmit={handleSubmit}
validationSchema={validations}
{...otherProps}
/>
)
......
......@@ -34,12 +34,12 @@ let key = 0
const NewEntry = props => (
<DiscussForm {...props}>
{formProps => {
const { handleSubmit: submit, setFieldValue, values } = formProps
const { handleSubmit, isValid, setStatus, status, values } = formProps
const handleSubmit = e => {
// See note on key above
if (status && status.hasReset) {
key += 1
submit(e)
setFieldValue('content', '')
setStatus({ hasReset: false })
}
return (
......@@ -52,7 +52,12 @@ const NewEntry = props => (
{...formProps}
/>
<Button onClick={handleSubmit} primary type="submit">
<Button
disabled={!isValid}
onClick={handleSubmit}
primary
type="submit"
>
Send
</Button>
</React.Fragment>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment