gpt4 book ai didi

javascript - 通过拖放响应大日历

转载 作者:行者123 更新时间:2023-12-04 13:55:46 25 4
gpt4 key购买 nike

我正在基于 Big Calendar 为我的非营利组织开发一个应用程序。我需要使用拖放功能。我可以将事件从插槽拖到另一个位置,但事件不会停留在原位并返回到其先前位置。
所有其他功能都运行良好:创建和编辑事件。
这是代码

import React, { useState, useEffect } from 'react';
import { Calendar, momentLocalizer } from 'react-big-calendar';
import withDragAndDrop from 'react-big-calendar/lib/addons/dragAndDrop';
import moment from 'moment';
import 'moment/locale/fr';
import Modal from 'react-bootstrap/Modal';
import CalendarForm from './CalendarForm';
import { observer } from 'mobx-react';
import { getCalendar } from './requests';

const localizer = momentLocalizer(moment);
const DnDCalendar = withDragAndDrop(Calendar);
const HomePage = ({ calendarStore }) => {
const [showAddModal, setShowAddModal] = useState(false);
const [showEditModal, setShowEditModal] = useState(false);
const [calendarEvent, setCalendarEvent] = useState({});
const [initialized, setInitialized] = useState(false);
let today = new Date();
const hideModals = () => {
setShowAddModal(false);
setShowEditModal(false);
};

const getCalendarEvents = async () => {
const response = await getCalendar();
const evs = response.data.map((d) => {
return {
...d,
start: new Date(d.start),
end: new Date(d.end),
};
});
calendarStore.setCalendarEvents(evs);
setInitialized(true);
};
const handleSelect = (event, e) => {
const { start, end } = event;
const data = { title: '', subject: '', start, end, allDay: false };
setShowAddModal(true);
setShowEditModal(false);
setCalendarEvent(data);
};
const handleSelectEvent = (event, e) => {
setShowAddModal(false);
setShowEditModal(true);
let { id, title, subject, start, end, allDay } = event;
start = new Date(start);
end = new Date(end);
const data = { id, title, subject, start, end, allDay };
setCalendarEvent(data);
};
const handleDragEvent = (event, e) => {
setShowAddModal(false);
setShowEditModal(false);
let { id, title, subject, start, end, allDay } = event;
start = new Date(start);
end = new Date(end);
const data = { id, title, subject, start, end, allDay };
setCalendarEvent(data);
};

useEffect(() => {
if (!initialized) {
getCalendarEvents();
}
});

return (
<div className="page">
<Modal show={showAddModal} onHide={hideModals}>
<Modal.Header closeButton>
<Modal.Title>Ajouter une session</Modal.Title>
</Modal.Header>
<Modal.Body>
<CalendarForm
calendarStore={calendarStore}
calendarEvent={calendarEvent}
onCancel={hideModals.bind(this)}
edit={false}
/>
</Modal.Body>
</Modal>
<Modal show={showEditModal} onHide={hideModals}>
<Modal.Header closeButton>
<Modal.Title>Editer la session</Modal.Title>
</Modal.Header>
<Modal.Body>
<CalendarForm
calendarStore={calendarStore}
calendarEvent={calendarEvent}
onCancel={hideModals.bind(this)}
edit={true}
/>
</Modal.Body>
</Modal>
<DnDCalendar
localizer={localizer}
events={calendarStore.calendarEvents}
startAccessor="start"
endAccessor="end"
defaultView="week"
selectable={true}
resizable={true}
onEventDrop={handleDragEvent}
style={{ height: '70vh' }}
onSelectSlot={handleSelect}
onSelectEvent={handleSelectEvent}
min={
new Date(today.getFullYear(), today.getMonth(), today.getDate(), 8)
}
max={
new Date(today.getFullYear(), today.getMonth(), today.getDate(), 23)
}
messages={{
month: 'mois',
week: 'semaine',
day: 'jour',
today: "aujourd'hui",
next: 'suiv.',
previous: 'préc.',
}}
resource="Test ressource"
eventPropGetter={(event) => ({
style: {
backgroundColor: event.isDone === true ? '#ad4ca4' : '#3174ad',
},
})}
/>
</div>
);
};

export default observer(HomePage);

最佳答案

拖放不会显式更新您的 events .您必须提供 onEventDrop prop,带有方法签名 ({event, start, end, isAllDay}) => update your events .

const onEventDrop = ({event, start, end, isAllDay}) => {
const updatedEvent = {...event, start, end, isAllDay};
// Any other logic. If async saving your change, you'll probably
// do the next line in a `.then()`
setEvents((prevEvents) => {
const filtered = prevEvents.filter((item) => item.id !== event.id);
return [...filtered, updatedEvent];
});
};

关于javascript - 通过拖放响应大日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62882026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com