import React, { useState } from 'react';
import { Dialog, DialogTitle, DialogContent, TextField, DialogActions, Button, Chip } from '@mui/material';
import { useShallow } from 'zustand/react/shallow';
import { getNodeData, useWallStore } from '../store/wall';
import { useReactFlow, useStore } from '@xyflow/react';
import { useUserWallStore, Wall } from '../store/user-wall';
import { message } from '@/modules/message';
import { useNavigate } from 'react-router-dom';
import { WallData } from './CustomNode';
function FormDialog({ open, handleClose, handleSubmit, initialData }) {
const [data, setData] = useState(initialData || { title: '', description: '', summary: '', tags: [] });
const handleChange = (event) => {
setData({ ...data, [event.target.name]: event.target.value });
};
const handleTagDelete = (tagToDelete) => {
setData({ ...data, tags: data.tags.filter((tag) => tag !== tagToDelete) });
};
const handleAddTag = (event) => {
if (event.key === 'Enter' && event.target.value !== '') {
setData({ ...data, tags: [...data.tags, event.target.value] });
event.target.value = ''; // Clear input after adding tag
}
};
return (
);
}
export default FormDialog;
export const SaveModal = () => {
const wallStore = useWallStore(useShallow((state) => state));
const userWallStore = useUserWallStore(useShallow((state) => state));
const { showFormDialog, setShowFormDialog, formDialogData, setFormDialogData } = wallStore;
const reactFlowInstance = useReactFlow();
const navigate = useNavigate();
const { id } = wallStore;
const onSubmit = async (values) => {
const nodes = reactFlowInstance.getNodes();
const data = {
nodes: getNodeData(nodes),
};
const fromData = {
title: values.title,
description: values.description,
summary: values.summary,
tags: values.tags,
markType: 'wallnote' as 'wallnote',
data,
} as Wall;
if (id) {
fromData.id = id;
}
const loading = message.loading('保存中...');
const res = await userWallStore.saveWall(fromData, { refresh: false });
message.close(loading);
if (res.code === 200) {
setShowFormDialog(false);
if (!id) {
// 新创建
const data = res.data;
message.info('redirect to edit page');
wallStore.clear();
setTimeout(() => {
navigate(`/edit/${data.id}`);
}, 2000);
} else {
// 编辑
wallStore.setData(res.data);
}
} else {
message.error('保存失败');
}
};
if (!showFormDialog) {
return null;
}
return setShowFormDialog(false)} handleSubmit={onSubmit} initialData={formDialogData} />;
};