@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
140 lines (138 loc) • 5.11 kB
JavaScript
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Grid from '@mui/material/Grid';
import { AudiencesFormSection } from './AudiencesFormSection';
import SecondaryButton from '../SecondaryButton';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import PrimaryButton from '../PrimaryButton';
import React from 'react';
import { makeStyles } from 'tss-react/mui';
import Input from '../FormEngineControls/Input';
import Dropdown from '../FormEngineControls/Dropdown';
import CheckboxGroup from '../FormEngineControls/CheckboxGroup';
import DateTime from '../FormEngineControls/DateTime';
import { Alert } from '@mui/material';
const useStyles = makeStyles()((theme) => ({
panelMargin: {
margin: theme.spacing(1)
},
actionButtons: {
margin: '10px 0',
textAlign: 'right',
'& .MuiButton-root': {
marginRight: theme.spacing(1)
}
}
}));
const controlsMap = {
dropdown: Dropdown,
'checkbox-group': CheckboxGroup,
'date-time': DateTime,
input: Input
};
const messages = defineMessages({
controlNotFound: {
id: 'audiencesPanel.undefinedControlType',
defaultMessage: 'Unknown control type'
}
});
const getDefaultModel = (fields) => {
const props = {};
Object.keys(fields).forEach((fieldId) => {
const propValue = fields[fieldId].defaultValue;
props[fieldId] = propValue;
});
return props;
};
const UndefinedControlType = () => {
const { formatMessage } = useIntl();
return React.createElement(Alert, { severity: 'warning', children: formatMessage(messages.controlNotFound) });
};
export function AudiencesPanelUI(props) {
const { classes } = useStyles();
const { model, modelApplying, onChange, onSaveModel, fields } = props;
const onFieldChange = (fieldId, type) => (value) => {
let props;
if (type === 'date-time') {
const timezone = value.tz();
value = value.toISOString();
props = Object.assign(Object.assign({}, model), { [fieldId]: value, [`${fieldId}_tz`]: timezone });
} else {
props = Object.assign(Object.assign({}, model), { [fieldId]: value });
}
onChange(props);
};
return React.createElement(
React.Fragment,
null,
React.createElement(
React.Fragment,
null,
React.createElement(
Grid,
{ className: classes.panelMargin },
Object.keys(fields).map((fieldId) => {
var _a, _b;
const type = fields[fieldId].type;
const Control = (_a = controlsMap[type]) !== null && _a !== void 0 ? _a : UndefinedControlType;
const controlProps = {
field: fields[fieldId],
value: (_b = model[fieldId]) !== null && _b !== void 0 ? _b : undefined,
onChange: onFieldChange(fieldId, type),
disabled: modelApplying
};
if (controlProps.field.type === 'date-time' && model[`${fieldId}_tz`]) {
controlProps['timezone'] = model[`${fieldId}_tz`];
}
return React.createElement(
AudiencesFormSection,
{ field: fields[fieldId], key: fieldId, showDivider: true },
React.createElement(Control, Object.assign({}, controlProps))
);
})
),
React.createElement(
Grid,
{ className: classes.actionButtons },
React.createElement(
SecondaryButton,
{ variant: 'contained', onClick: () => onChange(getDefaultModel(fields)) },
React.createElement(FormattedMessage, { id: 'audiencesPanel.defaults', defaultMessage: 'Defaults' })
),
React.createElement(
PrimaryButton,
{ onClick: () => onSaveModel() },
React.createElement(FormattedMessage, { id: 'audiencesPanel.apply', defaultMessage: 'Apply' })
)
)
)
);
}