saleor-dashboard/src/plugins/components/PluginsDetailsPage/PluginsDetailsPage.tsx

195 lines
6.3 KiB
TypeScript
Raw Normal View History

2019-11-07 15:54:05 +00:00
import makeStyles from "@material-ui/core/styles/makeStyles";
2019-08-26 17:08:32 +00:00
import Typography from "@material-ui/core/Typography";
import AppHeader from "@saleor/components/AppHeader";
import { ConfirmButtonTransitionState } from "@saleor/components/ConfirmButton";
import Container from "@saleor/components/Container";
import Form from "@saleor/components/Form";
import Grid from "@saleor/components/Grid";
import PageHeader from "@saleor/components/PageHeader";
import SaveButtonBar from "@saleor/components/SaveButtonBar";
2019-08-30 13:10:07 +00:00
import { sectionNames } from "@saleor/intl";
2019-08-27 12:36:19 +00:00
import { maybe } from "@saleor/misc";
import { UserError } from "@saleor/types";
2019-08-30 11:46:50 +00:00
import { ConfigurationItemInput } from "@saleor/types/globalTypes";
2019-08-27 12:36:19 +00:00
import React from "react";
2019-08-30 13:10:07 +00:00
import { useIntl } from "react-intl";
2019-08-27 12:36:19 +00:00
2019-11-07 15:54:05 +00:00
import CardSpacer from "@saleor/components/CardSpacer";
import Hr from "@saleor/components/Hr";
import { ChangeEvent } from "@saleor/hooks/useForm";
import { isSecretField } from "@saleor/plugins/utils";
2019-08-30 11:46:50 +00:00
import { Plugin_plugin } from "../../types/Plugin";
2019-11-07 15:54:05 +00:00
import PluginAuthorization from "../PluginAuthorization";
2019-08-26 17:08:32 +00:00
import PluginInfo from "../PluginInfo";
import PluginSettings from "../PluginSettings";
export interface FormData {
active: boolean;
2019-08-30 11:46:50 +00:00
configuration: ConfigurationItemInput[];
2019-08-26 17:08:32 +00:00
}
2019-08-28 13:02:45 +00:00
export interface PluginsDetailsPageProps {
2019-08-26 17:08:32 +00:00
disabled: boolean;
errors: UserError[];
plugin: Plugin_plugin;
2019-08-26 17:08:32 +00:00
saveButtonBarState: ConfirmButtonTransitionState;
onBack: () => void;
2019-11-07 15:54:05 +00:00
onClear: (field: string) => void;
onEdit: (field: string) => void;
2019-08-26 17:08:32 +00:00
onSubmit: (data: FormData) => void;
}
2019-11-07 15:54:05 +00:00
const useStyles = makeStyles(
{
spacer: {
gridColumnEnd: "span 2"
}
},
{
name: "PluginsDetailsPage"
}
);
const PluginsDetailsPage: React.FC<PluginsDetailsPageProps> = props => {
const {
disabled,
errors,
plugin,
saveButtonBarState,
onBack,
onClear,
onEdit,
onSubmit
} = props;
const classes = useStyles(props);
2019-08-30 13:10:07 +00:00
const intl = useIntl();
2019-08-26 17:08:32 +00:00
const initialForm: FormData = {
active: maybe(() => plugin.active, false),
2019-11-07 15:54:05 +00:00
configuration: maybe(() =>
2019-11-12 13:08:00 +00:00
plugin.configuration
.filter(field => !isSecretField(plugin.configuration, field.name))
.map(field => ({
...field,
value: field.value || ""
}))
2019-11-07 15:54:05 +00:00
)
2019-08-26 17:08:32 +00:00
};
2019-08-28 08:29:06 +00:00
2019-08-26 17:08:32 +00:00
return (
<Form errors={errors} initial={initialForm} onSubmit={onSubmit}>
2019-08-27 12:36:19 +00:00
{({ data, errors, hasChanged, submit, set, triggerChange }) => {
2019-11-07 15:54:05 +00:00
const onChange = (event: ChangeEvent) => {
const newData = {
active: data.active,
configuration: data.configuration
};
2019-08-26 17:08:32 +00:00
const { name, value } = event.target;
name === "active"
? (newData.active = value)
: (newData.active = data.active);
if (newData.configuration) {
2019-08-29 11:23:23 +00:00
newData.configuration.map(item => {
if (item.name === name) {
item.value = value;
}
});
}
2019-08-26 17:08:32 +00:00
triggerChange();
set(newData);
};
return (
<Container>
2019-08-30 13:10:07 +00:00
<AppHeader onBack={onBack}>
{intl.formatMessage(sectionNames.plugins)}
</AppHeader>
2019-08-26 17:08:32 +00:00
<PageHeader
2019-08-30 14:15:32 +00:00
title={intl.formatMessage(
{
defaultMessage: "{pluginName} Details",
description: "header"
},
{
pluginName: maybe(() => plugin.name, "...")
}
)}
2019-08-26 17:08:32 +00:00
/>
<Grid variant="inverted">
<div>
<Typography variant="h6">
2019-08-30 13:10:07 +00:00
{intl.formatMessage({
defaultMessage: "Plugin Information and Status",
2019-08-30 14:15:32 +00:00
description: "section header"
2019-08-30 13:10:07 +00:00
})}
2019-08-26 17:08:32 +00:00
</Typography>
<Typography>
2019-08-30 13:10:07 +00:00
{intl.formatMessage({
defaultMessage:
2019-11-07 15:54:05 +00:00
"These are general information about your store. They define what is the URL of your store and what is shown in browsers taskbar."
2019-08-30 13:10:07 +00:00
})}
2019-08-26 17:08:32 +00:00
</Typography>
</div>
2019-08-30 10:54:18 +00:00
<PluginInfo
data={data}
2019-08-30 11:46:50 +00:00
description={maybe(() => plugin.description, "")}
name={maybe(() => plugin.name, "")}
2019-08-30 10:54:18 +00:00
onChange={onChange}
/>
2019-08-26 17:08:32 +00:00
{data.configuration && (
<>
2019-11-07 15:54:05 +00:00
<Hr className={classes.spacer} />
2019-08-26 17:08:32 +00:00
<div>
<Typography variant="h6">
2019-08-30 13:10:07 +00:00
{intl.formatMessage({
defaultMessage: "Plugin Settings",
2019-08-30 14:15:32 +00:00
description: "section header"
2019-08-30 13:10:07 +00:00
})}
2019-08-26 17:08:32 +00:00
</Typography>
<Typography>
2019-08-30 13:10:07 +00:00
{intl.formatMessage({
defaultMessage:
2019-08-30 14:15:32 +00:00
"This adress will be used to generate invoices and calculate shipping rates. Email adress you provide here will be used as a contact adress for your customers."
2019-08-30 13:10:07 +00:00
})}
2019-08-26 17:08:32 +00:00
</Typography>
</div>
2019-11-07 15:54:05 +00:00
<div>
<PluginSettings
data={data}
fields={maybe(() => plugin.configuration, [])}
errors={errors}
disabled={disabled}
onChange={onChange}
/>
{maybe(() =>
plugin.configuration.some(field =>
isSecretField(plugin.configuration, field.name)
)
) && (
2019-11-12 13:08:00 +00:00
<>
<CardSpacer />
<PluginAuthorization
fields={plugin.configuration}
onClear={onClear}
onEdit={onEdit}
/>
</>
2019-11-07 15:54:05 +00:00
)}
</div>
2019-08-26 17:08:32 +00:00
</>
)}
</Grid>
<SaveButtonBar
disabled={disabled || !hasChanged}
state={saveButtonBarState}
onCancel={onBack}
onSave={submit}
/>
</Container>
);
}}
</Form>
);
};
2019-08-28 13:02:45 +00:00
PluginsDetailsPage.displayName = "PluginsDetailsPage";
export default PluginsDetailsPage;