fix: cannot delete event in calendar (#1277)

* fix: calendar cannot delete event

* feat: close detail after delete event

* fix: make sure the DeleteEvent button only appears in the calendar details
This commit is contained in:
Dunqing 2022-12-21 21:13:35 +08:00 committed by GitHub
parent ee7185c51c
commit 3e2a884a3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 19 deletions

View File

@ -4,7 +4,7 @@ import { observer, RecursionField, Schema, useFieldSchema } from '@formily/react
import { parseExpression } from 'cron-parser'; import { parseExpression } from 'cron-parser';
import get from 'lodash/get'; import get from 'lodash/get';
import moment from 'moment'; import moment from 'moment';
import React, { useMemo, useState } from 'react'; import React, { useCallback, useMemo, useState } from 'react';
import { Calendar as BigCalendar, momentLocalizer } from 'react-big-calendar'; import { Calendar as BigCalendar, momentLocalizer } from 'react-big-calendar';
import * as dates from 'react-big-calendar/lib/utils/dates'; import * as dates from 'react-big-calendar/lib/utils/dates';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
@ -16,10 +16,14 @@ import Header from './components/Header';
import { CalendarToolbarContext } from './context'; import { CalendarToolbarContext } from './context';
import './style.less'; import './style.less';
import type { ToolbarProps } from './types'; import type { ToolbarProps } from './types';
import { formatDate } from './utils';
const Weeks = ['month', 'week', 'day'] as const; const Weeks = ['month', 'week', 'day'] as const;
const localizer = momentLocalizer(moment); const localizer = momentLocalizer(moment);
export const DeleteEventContext = React.createContext({
close: () => {},
});
function Toolbar(props: ToolbarProps) { function Toolbar(props: ToolbarProps) {
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
@ -168,13 +172,20 @@ const CalendarRecordViewer = (props) => {
}, null), }, null),
[], [],
); );
const close = useCallback(() => {
setVisible(false);
}, []);
return ( return (
eventSchema && ( eventSchema && (
<DeleteEventContext.Provider value={{ close }}>
<ActionContext.Provider value={{ visible, setVisible }}> <ActionContext.Provider value={{ visible, setVisible }}>
<RecordProvider record={record}> <RecordProvider record={record}>
<RecursionField schema={eventSchema} name={eventSchema.name} /> <RecursionField schema={eventSchema} name={eventSchema.name} />
</RecordProvider> </RecordProvider>
</ActionContext.Provider> </ActionContext.Provider>
</DeleteEventContext.Provider>
) )
); );
}; };
@ -225,7 +236,8 @@ export const Calendar: any = observer((props: any) => {
if (!record) { if (!record) {
return; return;
} }
record.__event = event; record.__event = { ...event, start: formatDate(moment(event.start)), end: formatDate(moment(event.end)) };
setRecord(record); setRecord(record);
setVisible(true); setVisible(true);
}} }}

View File

@ -1,18 +1,21 @@
import { observer } from '@formily/react'; import { observer } from '@formily/react';
import { Modal, Radio, Space, Typography } from 'antd'; import { Modal, Radio, Space, Typography } from 'antd';
import moment from 'moment'; import moment from 'moment';
import React, { useState } from 'react'; import React, { useContext, useState } from 'react';
import { createPortal } from 'react-dom'; import { createPortal } from 'react-dom';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useBlockRequestContext, useFilterByTk } from '../../../block-provider'; import { useBlockRequestContext, useFilterByTk } from '../../../block-provider';
import { useRecord } from '../../../record-provider'; import { useRecord } from '../../../record-provider';
import { useActionContext } from '../action'; import { useActionContext } from '../action';
import { DeleteEventContext } from './Calendar';
import { formatDate } from './utils';
const { Text } = Typography; const { Text } = Typography;
export const DeleteEvent = observer(() => { export const DeleteEvent = observer(() => {
const { visible, setVisible } = useActionContext(); const { visible, setVisible } = useActionContext();
const { exclude = [], cron, ...record } = useRecord(); const { exclude = [], cron, ...record } = useRecord();
const startDate = moment(record.__parent.__event.start).format(); const { close } = useContext(DeleteEventContext);
const startDate = formatDate(moment(record.__parent.__event.start));
const filterByTk = useFilterByTk(); const filterByTk = useFilterByTk();
const { resource, service, __parent } = useBlockRequestContext(); const { resource, service, __parent } = useBlockRequestContext();
const [value, onChange] = useState(startDate); const [value, onChange] = useState(startDate);
@ -35,6 +38,7 @@ export const DeleteEvent = observer(() => {
__parent?.service?.refresh?.(); __parent?.service?.refresh?.();
service?.refresh?.(); service?.refresh?.();
setVisible?.(false, true); setVisible?.(false, true);
close();
}; };
const { t } = useTranslation(); const { t } = useTranslation();

View File

@ -1,5 +1,10 @@
import { createContext } from 'react'; import { createContext, useContext } from 'react';
import type { ToolbarProps } from './types'; import type { ToolbarProps } from './types';
export const CalendarToolbarContext = createContext<ToolbarProps>(null); export const CalendarToolbarContext = createContext<ToolbarProps>(null);
export const CalendarContext = createContext(null); export const CalendarContext = createContext(null);
export const DeleteEventContext = createContext(null);
export const useDeleteEvent = () => {
return useContext(DeleteEventContext);
}

View File

@ -19,3 +19,7 @@ export const getLunarDay = (date: moment.MomentInput) => {
const result = solarLunar.solar2lunar(md.year(), md.month() + 1, md.date()); const result = solarLunar.solar2lunar(md.year(), md.month() + 1, md.date());
return typeof result !== 'number' ? result.lunarFestival || result.term || result.dayCn : result; return typeof result !== 'number' ? result.lunarFestival || result.term || result.dayCn : result;
}; };
export const formatDate = (date: moment.Moment) => {
return date.format('YYYY-MM-DDTHH:mm:ss.SSSZ')
}

View File

@ -1,3 +1,4 @@
import { useFieldSchema } from '@formily/react';
import { isPlainObj } from '@formily/shared'; import { isPlainObj } from '@formily/shared';
import React, { createContext, useContext } from 'react'; import React, { createContext, useContext } from 'react';
import { SchemaComponentOptions } from '../schema-component'; import { SchemaComponentOptions } from '../schema-component';
@ -13,7 +14,8 @@ export interface SchemaInitializerProviderProps {
initializers?: Record<string, any>; initializers?: Record<string, any>;
} }
export const useSchemaInitializer = (name: string) => { export const useSchemaInitializer = (name: string, props = {}) => {
const fieldSchema = useFieldSchema();
const initializers = useContext(SchemaInitializerContext); const initializers = useContext(SchemaInitializerContext);
const render = (component?: any, props?: any) => { const render = (component?: any, props?: any) => {
return component && React.createElement(component, props); return component && React.createElement(component, props);
@ -23,7 +25,8 @@ export const useSchemaInitializer = (name: string) => {
return { exists: false, render: (props?: any) => render(null) }; return { exists: false, render: (props?: any) => render(null) };
} }
const initializer = initializers?.[name]; const initializer = initializers?.[name || fieldSchema?.['x-initializer']];
const initializerProps = { ...props, ...fieldSchema?.['x-initializer-props'] };
if (!initializer) { if (!initializer) {
return { exists: false, render: (props?: any) => render(null) }; return { exists: false, render: (props?: any) => render(null) };
@ -34,14 +37,14 @@ export const useSchemaInitializer = (name: string) => {
exists: true, exists: true,
render: (props?: any) => { render: (props?: any) => {
const component = (initializer as any).component || SchemaInitializer.Button; const component = (initializer as any).component || SchemaInitializer.Button;
return render(component, { ...initializer, ...props }); return render(component, { ...initializer, ...initializerProps, ...props });
}, },
}; };
} }
return { return {
exists: true, exists: true,
render: (props?: any) => render(initializer, props), render: (props?: any) => render(initializer, { ...initializerProps, ...props }),
}; };
}; };

View File

@ -104,7 +104,8 @@ const useRelationFields = () => {
return relationFields; return relationFields;
}; };
const useInheritFields = () => { const useInheritFields = (props) => {
const { actionInitializers } = props;
const collection = useCollection(); const collection = useCollection();
const { getChildrenCollections } = useCollectionManager(); const { getChildrenCollections } = useCollectionManager();
const childrenCollections = getChildrenCollections(collection.name); const childrenCollections = getChildrenCollections(collection.name);
@ -116,14 +117,14 @@ const useInheritFields = () => {
component: 'RecordReadPrettyFormBlockInitializer', component: 'RecordReadPrettyFormBlockInitializer',
icon: false, icon: false,
targetCollection: c, targetCollection: c,
actionInitializers: 'CalendarFormActionInitializers', actionInitializers,
}; };
}); });
}; };
export const RecordBlockInitializers = (props: any) => { export const RecordBlockInitializers = (props: any) => {
const { t } = useTranslation(); const { t } = useTranslation();
const { insertPosition, component } = props; const { insertPosition, component, actionInitializers } = props;
return ( return (
<SchemaInitializer.Button <SchemaInitializer.Button
wrap={gridRowColWrap} wrap={gridRowColWrap}
@ -141,7 +142,7 @@ export const RecordBlockInitializers = (props: any) => {
type: 'item', type: 'item',
title: '{{t("Details")}}', title: '{{t("Details")}}',
component: 'RecordReadPrettyFormBlockInitializer', component: 'RecordReadPrettyFormBlockInitializer',
actionInitializers: 'CalendarFormActionInitializers', actionInitializers,
}, },
{ {
key: 'form', key: 'form',
@ -154,7 +155,7 @@ export const RecordBlockInitializers = (props: any) => {
{ {
type: 'itemGroup', type: 'itemGroup',
title: '{{t("Children collection blocks")}}', title: '{{t("Children collection blocks")}}',
children: useInheritFields(), children: useInheritFields(props),
}, },
{ {
type: 'itemGroup', type: 'itemGroup',

View File

@ -986,6 +986,9 @@ export const createCalendarBlockSchema = (options) => {
grid: { grid: {
type: 'void', type: 'void',
'x-component': 'Grid', 'x-component': 'Grid',
'x-initializer-props': {
actionInitializers: 'CalendarFormActionInitializers'
},
'x-initializer': 'RecordBlockInitializers', 'x-initializer': 'RecordBlockInitializers',
properties: {}, properties: {},
}, },