handle textarea text state with redux and setText on example click

This commit is contained in:
Danny Avila 2023-02-08 09:59:01 -05:00
parent 581ee0e2ca
commit 5cd50e7826
4 changed files with 41 additions and 8 deletions

View file

@ -6,12 +6,14 @@ import { useSelector, useDispatch } from 'react-redux';
import { setConversation } from '~/store/convoSlice';
import { setMessages } from '~/store/messageSlice';
import { setSubmitState } from '~/store/submitSlice';
import { setText } from '~/store/textSlice';
export default function TextChat({ messages, reloadConvos }) {
const [text, setText] = useState('');
// const [text, setText] = useState('');
const dispatch = useDispatch();
const convo = useSelector((state) => state.convo);
const { isSubmitting } = useSelector((state) => state.submit);
const { text } = useSelector((state) => state.text);
const submitMessage = () => {
if (!!isSubmitting || text.trim() === '') {
@ -22,7 +24,7 @@ export default function TextChat({ messages, reloadConvos }) {
const currentMsg = { sender: 'user', text: payload, current: true };
const initialResponse = { sender: 'GPT', text: '' };
dispatch(setMessages([...messages, currentMsg, initialResponse]));
setText('');
dispatch(setText(''));
const messageHandler = (data) => {
dispatch(setMessages([...messages, currentMsg, { sender: 'GPT', text: data }]));
};
@ -76,7 +78,7 @@ export default function TextChat({ messages, reloadConvos }) {
if (isSubmitting && (value === '' || value === '\n')) {
return;
}
setText(value);
dispatch(setText(value));
};
return (