2022-12-18 22:58:00 +01:00
|
|
|
import Widgets from '../../../../widgets';
|
2022-12-10 22:14:31 +01:00
|
|
|
import { Button, Group, MultiSelect, Stack, Switch, TextInput } from '@mantine/core';
|
|
|
|
|
import { ContextModalProps } from '@mantine/modals';
|
|
|
|
|
import { useTranslation } from 'next-i18next';
|
|
|
|
|
import { useState } from 'react';
|
2022-12-16 21:01:06 +01:00
|
|
|
import { useConfigContext } from '../../../../config/provider';
|
|
|
|
|
import { useConfigStore } from '../../../../config/store';
|
2022-12-18 22:58:00 +01:00
|
|
|
import { IWidget } from '../../../../widgets/widgets';
|
2022-12-10 22:14:31 +01:00
|
|
|
|
2022-12-18 22:58:00 +01:00
|
|
|
export type WidgetEditModalInnerProps = {
|
|
|
|
|
integration: string;
|
|
|
|
|
options: IWidget<string, any>['properties'];
|
2022-12-10 22:14:31 +01:00
|
|
|
};
|
|
|
|
|
|
2022-12-18 22:58:00 +01:00
|
|
|
type IntegrationOptionsValueType = IWidget<string, any>['properties'][string];
|
|
|
|
|
|
2022-12-16 21:01:06 +01:00
|
|
|
export const WidgetsEditModal = ({
|
2022-12-10 22:14:31 +01:00
|
|
|
context,
|
|
|
|
|
id,
|
|
|
|
|
innerProps,
|
2022-12-16 21:01:06 +01:00
|
|
|
}: ContextModalProps<WidgetEditModalInnerProps>) => {
|
2022-12-18 22:58:00 +01:00
|
|
|
const { t } = useTranslation([`modules/${innerProps.integration}`, 'common']);
|
2022-12-10 22:14:31 +01:00
|
|
|
const [moduleProperties, setModuleProperties] = useState(innerProps.options);
|
2022-12-18 22:58:00 +01:00
|
|
|
const items = Object.entries(moduleProperties ?? {}) as [string, IntegrationOptionsValueType][];
|
2022-12-10 22:14:31 +01:00
|
|
|
|
|
|
|
|
const { name: configName } = useConfigContext();
|
|
|
|
|
const updateConfig = useConfigStore((x) => x.updateConfig);
|
|
|
|
|
|
|
|
|
|
if (!configName || !innerProps.options) return null;
|
|
|
|
|
|
|
|
|
|
const handleChange = (key: string, value: IntegrationOptionsValueType) => {
|
|
|
|
|
setModuleProperties((prev) => {
|
2022-12-17 00:28:46 +09:00
|
|
|
const copyOfPrev: any = { ...prev };
|
2022-12-10 22:14:31 +01:00
|
|
|
copyOfPrev[key] = value;
|
|
|
|
|
return copyOfPrev;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-18 22:58:00 +01:00
|
|
|
const getMutliselectData = (option: string) => {
|
|
|
|
|
const currentWidgetDefinition = Widgets[innerProps.integration as keyof typeof Widgets];
|
|
|
|
|
if (!Widgets) return [];
|
|
|
|
|
|
|
|
|
|
const options = currentWidgetDefinition.options as any;
|
|
|
|
|
return options[option]?.data ?? [];
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-10 22:14:31 +01:00
|
|
|
const handleSave = () => {
|
2022-12-17 00:28:46 +09:00
|
|
|
updateConfig(configName, (prev) => ({
|
|
|
|
|
...prev,
|
2022-12-19 17:03:39 +01:00
|
|
|
widgets: {
|
|
|
|
|
...prev.widgets,
|
2022-12-17 00:28:46 +09:00
|
|
|
[innerProps.integration]:
|
2022-12-19 17:03:39 +01:00
|
|
|
'properties' in (prev.widgets[innerProps.integration] ?? {})
|
2022-12-17 00:28:46 +09:00
|
|
|
? {
|
2022-12-19 17:03:39 +01:00
|
|
|
...prev.widgets[innerProps.integration],
|
2022-12-17 00:28:46 +09:00
|
|
|
properties: moduleProperties,
|
|
|
|
|
}
|
2022-12-19 17:03:39 +01:00
|
|
|
: prev.widgets[innerProps.integration],
|
2022-12-17 00:28:46 +09:00
|
|
|
},
|
|
|
|
|
}));
|
2022-12-10 22:14:31 +01:00
|
|
|
context.closeModal(id);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Stack>
|
|
|
|
|
{items.map(([key, value]) => (
|
|
|
|
|
<>
|
|
|
|
|
{typeof value === 'boolean' ? (
|
|
|
|
|
<Switch
|
2022-12-18 22:58:00 +01:00
|
|
|
label={t(`descriptor.settings.${key}.label`)}
|
2022-12-10 22:14:31 +01:00
|
|
|
checked={value}
|
|
|
|
|
onChange={(ev) => handleChange(key, ev.currentTarget.checked)}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
{typeof value === 'string' ? (
|
|
|
|
|
<TextInput
|
2022-12-18 22:58:00 +01:00
|
|
|
label={t(`descriptor.settings.${key}.label`)}
|
2022-12-10 22:14:31 +01:00
|
|
|
value={value}
|
|
|
|
|
onChange={(ev) => handleChange(key, ev.currentTarget.value)}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
{typeof value === 'object' && Array.isArray(value) ? (
|
|
|
|
|
<MultiSelect
|
2022-12-18 22:58:00 +01:00
|
|
|
data={getMutliselectData(key)}
|
|
|
|
|
label={t(`descriptor.settings.${key}.label`)}
|
2022-12-10 22:14:31 +01:00
|
|
|
value={value}
|
2022-12-18 22:58:00 +01:00
|
|
|
onChange={(v) => handleChange(key, v)}
|
2022-12-10 22:14:31 +01:00
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</>
|
|
|
|
|
))}
|
|
|
|
|
|
|
|
|
|
<Group position="right">
|
|
|
|
|
<Button onClick={() => context.closeModal(id)} variant="light">
|
|
|
|
|
{t('common:actions.cancel')}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button onClick={handleSave}>{t('common:actions.save')}</Button>
|
|
|
|
|
</Group>
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
};
|