import { useDispatch } from 'react-redux' import { useState, useEffect } from 'react' import PropTypes from 'prop-types' // material-ui import { Button, Box } from '@mui/material' import { IconX } from '@tabler/icons-react' // Project import import { StyledButton } from '@/ui-component/button/StyledButton' import { SwitchInput } from '@/ui-component/switch/Switch' // store import { enqueueSnackbar as enqueueSnackbarAction, closeSnackbar as closeSnackbarAction, SET_CHATFLOW } from '@/store/actions' import useNotifier from '@/utils/useNotifier' // API import chatflowsApi from '@/api/chatflows' const ChatFeedback = ({ dialogProps, onConfirm }) => { const dispatch = useDispatch() useNotifier() const enqueueSnackbar = (...args) => dispatch(enqueueSnackbarAction(...args)) const closeSnackbar = (...args) => dispatch(closeSnackbarAction(...args)) const [chatFeedbackStatus, setChatFeedbackStatus] = useState(false) const [chatbotConfig, setChatbotConfig] = useState({}) const handleChange = (value) => { setChatFeedbackStatus(value) } const onSave = async () => { try { let value = { chatFeedback: { status: chatFeedbackStatus } } chatbotConfig.chatFeedback = value.chatFeedback const saveResp = await chatflowsApi.updateChatflow(dialogProps.chatflow.id, { chatbotConfig: JSON.stringify(chatbotConfig) }) if (saveResp.data) { enqueueSnackbar({ message: 'Chat Feedback Settings Saved', options: { key: new Date().getTime() + Math.random(), variant: 'success', action: (key) => ( ) } }) dispatch({ type: SET_CHATFLOW, chatflow: saveResp.data }) onConfirm?.() } } catch (error) { enqueueSnackbar({ message: `Failed to save Chat Feedback Settings: ${ typeof error.response.data === 'object' ? error.response.data.message : error.response.data }`, options: { key: new Date().getTime() + Math.random(), variant: 'error', persist: true, action: (key) => ( ) } }) } } useEffect(() => { if (dialogProps.chatflow && dialogProps.chatflow.chatbotConfig) { let chatbotConfig = JSON.parse(dialogProps.chatflow.chatbotConfig) setChatbotConfig(chatbotConfig || {}) if (chatbotConfig.chatFeedback) { setChatFeedbackStatus(chatbotConfig.chatFeedback.status) } } return () => {} }, [dialogProps]) return ( <> Save ) } ChatFeedback.propTypes = { dialogProps: PropTypes.object, onConfirm: PropTypes.func } export default ChatFeedback