2019-08-26 17:08:32 +00:00
|
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
2019-08-29 11:01:06 +00:00
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
|
|
|
import React from "react";
|
2019-09-02 10:31:34 +00:00
|
|
|
import { useIntl } from "react-intl";
|
2019-08-30 13:10:07 +00:00
|
|
|
|
2019-11-07 15:54:05 +00:00
|
|
|
import { ConfigurationItemInput } from "@saleor/types/globalTypes";
|
2019-08-26 17:08:32 +00:00
|
|
|
import { getMutationState, maybe } from "../../misc";
|
2019-08-27 12:36:19 +00:00
|
|
|
import PluginsDetailsPage from "../components/PluginsDetailsPage";
|
2019-08-28 14:53:57 +00:00
|
|
|
import { TypedPluginUpdate } from "../mutations";
|
2019-08-26 17:08:32 +00:00
|
|
|
import { TypedPluginsDetailsQuery } from "../queries";
|
2019-11-07 15:54:05 +00:00
|
|
|
import { Plugin_plugin_configuration } from "../types/Plugin";
|
|
|
|
import {
|
|
|
|
pluginsListUrl,
|
|
|
|
pluginsUrl,
|
|
|
|
PluginsUrlQueryParams,
|
|
|
|
PluginUrlDialog
|
|
|
|
} from "../urls";
|
|
|
|
import { isSecretField } from "../utils";
|
2019-08-26 17:08:32 +00:00
|
|
|
|
|
|
|
export interface PluginsDetailsProps {
|
|
|
|
id: string;
|
2019-11-07 15:54:05 +00:00
|
|
|
params: PluginsUrlQueryParams;
|
2019-08-26 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 15:54:05 +00:00
|
|
|
export function getConfigurationInput(
|
|
|
|
config: Plugin_plugin_configuration[] | null,
|
|
|
|
input: ConfigurationItemInput[] | null
|
|
|
|
): ConfigurationItemInput[] | null {
|
|
|
|
if (config === null || input === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return input
|
|
|
|
.filter(field => !isSecretField(config, field.name))
|
|
|
|
.map(field => ({
|
|
|
|
name: field.name,
|
|
|
|
value: field.value.toString()
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
export const PluginsDetails: React.FC<PluginsDetailsProps> = ({
|
|
|
|
id,
|
|
|
|
params
|
|
|
|
}) => {
|
2019-08-26 17:08:32 +00:00
|
|
|
const navigate = useNavigator();
|
2019-08-29 11:01:06 +00:00
|
|
|
const notify = useNotifier();
|
2019-08-30 13:10:07 +00:00
|
|
|
const intl = useIntl();
|
2019-08-26 17:08:32 +00:00
|
|
|
|
2019-11-07 15:54:05 +00:00
|
|
|
const closeModal = () =>
|
|
|
|
navigate(
|
|
|
|
pluginsUrl(id, {
|
|
|
|
...params,
|
|
|
|
action: undefined,
|
|
|
|
field: undefined
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
const openModal = (action: PluginUrlDialog, field?: string) =>
|
|
|
|
navigate(
|
|
|
|
pluginsUrl(id, {
|
|
|
|
...params,
|
|
|
|
action,
|
|
|
|
field
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2019-08-26 17:08:32 +00:00
|
|
|
return (
|
2019-08-28 14:53:57 +00:00
|
|
|
<TypedPluginUpdate>
|
|
|
|
{(pluginUpdate, pluginUpdateOpts) => (
|
2019-08-26 17:08:32 +00:00
|
|
|
<TypedPluginsDetailsQuery variables={{ id }}>
|
2019-11-07 15:54:05 +00:00
|
|
|
{pluginDetails => {
|
2019-08-26 17:08:32 +00:00
|
|
|
const formTransitionState = getMutationState(
|
2019-08-28 14:53:57 +00:00
|
|
|
pluginUpdateOpts.called,
|
|
|
|
pluginUpdateOpts.loading,
|
|
|
|
maybe(() => pluginUpdateOpts.data.pluginUpdate.errors)
|
2019-08-26 17:08:32 +00:00
|
|
|
);
|
|
|
|
|
2019-08-29 11:01:06 +00:00
|
|
|
const formErrors = maybe(
|
|
|
|
() => pluginUpdateOpts.data.pluginUpdate.errors,
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (formErrors.length) {
|
|
|
|
formErrors.map(error => {
|
|
|
|
notify({
|
|
|
|
text: error.message
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (pluginUpdateOpts.data) {
|
|
|
|
notify({
|
2019-08-30 13:10:07 +00:00
|
|
|
text: intl.formatMessage({
|
|
|
|
defaultMessage: "Succesfully updated plugin settings",
|
|
|
|
description: "plugin success message"
|
|
|
|
})
|
2019-08-29 11:01:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 17:08:32 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<WindowTitle
|
2019-11-07 15:54:05 +00:00
|
|
|
title={maybe(() => pluginDetails.data.plugin.name)}
|
2019-08-26 17:08:32 +00:00
|
|
|
/>
|
|
|
|
<PluginsDetailsPage
|
2019-11-07 15:54:05 +00:00
|
|
|
disabled={pluginDetails.loading}
|
2019-08-29 11:01:06 +00:00
|
|
|
errors={formErrors}
|
2019-08-26 17:08:32 +00:00
|
|
|
saveButtonBarState={formTransitionState}
|
2019-11-07 15:54:05 +00:00
|
|
|
plugin={maybe(() => pluginDetails.data.plugin)}
|
2019-08-26 17:08:32 +00:00
|
|
|
onBack={() => navigate(pluginsListUrl())}
|
2019-11-07 15:54:05 +00:00
|
|
|
onClear={field => openModal("clear", field)}
|
|
|
|
onEdit={field => openModal("edit", field)}
|
|
|
|
onSubmit={formData =>
|
2019-08-28 14:53:57 +00:00
|
|
|
pluginUpdate({
|
2019-08-26 17:08:32 +00:00
|
|
|
variables: {
|
|
|
|
id,
|
|
|
|
input: {
|
|
|
|
active: formData.active,
|
2019-11-07 15:54:05 +00:00
|
|
|
configuration: getConfigurationInput(
|
|
|
|
pluginDetails.data.plugin.configuration,
|
|
|
|
formData.configuration
|
|
|
|
)
|
2019-08-26 17:08:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-07 15:54:05 +00:00
|
|
|
})
|
|
|
|
}
|
2019-08-26 17:08:32 +00:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedPluginsDetailsQuery>
|
|
|
|
)}
|
2019-08-28 14:53:57 +00:00
|
|
|
</TypedPluginUpdate>
|
2019-08-26 17:08:32 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
PluginsDetails.displayName = "PluginsDetails";
|
|
|
|
export default PluginsDetails;
|