Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Move site settings dialog path to qs

dominik-zeglen8414908d8446
diff --git a/saleor/static/dashboard-next/siteSettings/index.tsx b/saleor/static/dashboard-next/siteSettings/index.tsx
index 9aef4200f5..96168d4c8d 100644
--- a/saleor/static/dashboard-next/siteSettings/index.tsx
+++ b/saleor/static/dashboard-next/siteSettings/index.tsx
@@ -1,10 +1,18 @@
+import { parse as parseQs } from "qs";
import * as React from "react";
import { Route, RouteComponentProps } from "react-router-dom";
-import { siteSettingsPath } from "./urls";
-import SiteSettings from "./views/";
+import { siteSettingsPath, SiteSettingsUrlQueryParams } from "./urls";
+import SiteSettingsComponent from "./views/";
-export const SiteSettingsSection: React.StatelessComponent<
- RouteComponentProps<{}>
-> = () => <Route path={siteSettingsPath} component={SiteSettings} />;
+const SiteSettings: React.FC<RouteComponentProps> = ({ location }) => {
+ const qs = parseQs(location.search.substr(1));
+ const params: SiteSettingsUrlQueryParams = qs;
+
+ return <SiteSettingsComponent params={params} />;
+};
+
+export const SiteSettingsSection: React.FC = () => {
+ return <Route path={siteSettingsPath} component={SiteSettings} />;
+};
export default SiteSettingsSection;
diff --git a/saleor/static/dashboard-next/siteSettings/urls.ts b/saleor/static/dashboard-next/siteSettings/urls.ts
index 48d3f82a18..201d086a79 100644
--- a/saleor/static/dashboard-next/siteSettings/urls.ts
+++ b/saleor/static/dashboard-next/siteSettings/urls.ts
@@ -1,9 +1,11 @@
-import * as urlJoin from "url-join";
+import { stringify as stringifyQs } from "qs";
+
+import { Dialog } from "../types";
const siteSettingsSection = "/site-settings";
export const siteSettingsPath = siteSettingsSection;
-export const siteSettingsUrl = siteSettingsPath;
-
-export const siteSettingsAddKeyPath = urlJoin(siteSettingsSection, "addKey");
-export const siteSettingsAddKeyUrl = siteSettingsAddKeyPath;
+export type SiteSettingsUrlDialog = "add-key";
+export type SiteSettingsUrlQueryParams = Dialog<SiteSettingsUrlDialog>;
+export const siteSettingsUrl = (params?: SiteSettingsUrlQueryParams) =>
+ siteSettingsPath + "?" + stringifyQs(params);
diff --git a/saleor/static/dashboard-next/siteSettings/views/index.tsx b/saleor/static/dashboard-next/siteSettings/views/index.tsx
index e6f6e85e1f..8bd8e1eac5 100644
--- a/saleor/static/dashboard-next/siteSettings/views/index.tsx
+++ b/saleor/static/dashboard-next/siteSettings/views/index.tsx
@@ -1,10 +1,9 @@
import * as React from "react";
-import { Route } from "react-router-dom";
-import Messages from "../../components/messages";
-import Navigator from "../../components/Navigator";
import { WindowTitle } from "../../components/WindowTitle";
import { configurationMenuUrl } from "../../configuration";
+import useNavigator from "../../hooks/useNavigator";
+import useNotifier from "../../hooks/useNotifier";
import i18n from "../../i18n";
import { getMutationState, maybe } from "../../misc";
import { AuthorizationKeyType } from "../../types/globalTypes";
@@ -23,191 +22,180 @@ import { TypedSiteSettingsQuery } from "../queries";
import { AuthorizationKeyAdd } from "../types/AuthorizationKeyAdd";
import { AuthorizationKeyDelete } from "../types/AuthorizationKeyDelete";
import { ShopSettingsUpdate } from "../types/ShopSettingsUpdate";
-import {
- siteSettingsAddKeyPath,
- siteSettingsAddKeyUrl,
- siteSettingsUrl
-} from "../urls";
+import { siteSettingsUrl, SiteSettingsUrlQueryParams } from "../urls";
+
+export interface SiteSettingsProps {
+ params: SiteSettingsUrlQueryParams;
+}
+
+export const SiteSettings: React.StatelessComponent<SiteSettingsProps> = ({
+ params
+}) => {
+ const navigate = useNavigator();
+ const notify = useNotifier();
+
+ const handleAddKeySuccess = (data: AuthorizationKeyAdd) => {
+ if (!maybe(() => data.authorizationKeyAdd.errors.length)) {
+ notify({
+ text: i18n.t("Authorization key added", {
+ context: "notification"
+ })
+ });
+ navigate(siteSettingsUrl);
+ }
+ };
+ const handleDeleteKeySuccess = (data: AuthorizationKeyDelete) => {
+ if (!maybe(() => data.authorizationKeyDelete.errors.length)) {
+ notify({
+ text: i18n.t("Authorization key deleted", {
+ context: "notification"
+ })
+ });
+ } else {
+ notify({
+ text: i18n.t("Could not delete authorization key: {{ message }}", {
+ context: "notification",
+ message: data.authorizationKeyDelete.errors[0].message
+ })
+ });
+ }
+ };
+ const handleSiteSettingsSuccess = (data: ShopSettingsUpdate) => {
+ if (
+ (!data.shopDomainUpdate.errors ||
+ data.shopDomainUpdate.errors.length === 0) &&
+ (!data.shopSettingsUpdate.errors ||
+ data.shopSettingsUpdate.errors.length === 0)
+ ) {
+ notify({
+ text: i18n.t("Site settings updated", {
+ context: "notification"
+ })
+ });
+ }
+ };
+ return (
+ <TypedSiteSettingsQuery displayLoader>
+ {siteSettings => (
+ <TypedAuthorizationKeyAdd onCompleted={handleAddKeySuccess}>
+ {(addAuthorizationKey, addAuthorizationKeyOpts) => (
+ <TypedAuthorizationKeyDelete onCompleted={handleDeleteKeySuccess}>
+ {(deleteAuthorizationKey, _) => (
+ <TypedShopSettingsUpdate
+ onCompleted={handleSiteSettingsSuccess}
+ >
+ {(updateShopSettings, updateShopSettingsOpts) => {
+ const errors = [
+ ...maybe(
+ () =>
+ updateShopSettingsOpts.data.shopDomainUpdate.errors,
+ []
+ ),
+ ...maybe(
+ () =>
+ updateShopSettingsOpts.data.shopSettingsUpdate.errors,
+ []
+ )
+ ];
+ const loading =
+ siteSettings.loading ||
+ addAuthorizationKeyOpts.loading ||
+ updateShopSettingsOpts.loading;
-export const SiteSettings: React.StatelessComponent<{}> = () => (
- <Navigator>
- {navigate => (
- <Messages>
- {pushMessage => {
- const handleAddKeySuccess = (data: AuthorizationKeyAdd) => {
- if (!maybe(() => data.authorizationKeyAdd.errors.length)) {
- pushMessage({
- text: i18n.t("Authorization key added", {
- context: "notification"
- })
- });
- navigate(siteSettingsUrl);
- }
- };
- const handleDeleteKeySuccess = (data: AuthorizationKeyDelete) => {
- if (!maybe(() => data.authorizationKeyDelete.errors.length)) {
- pushMessage({
- text: i18n.t("Authorization key deleted", {
- context: "notification"
- })
- });
- } else {
- pushMessage({
- text: i18n.t(
- "Could not delete authorization key: {{ message }}",
- {
- context: "notification",
- message: data.authorizationKeyDelete.errors[0].message
- }
- )
- });
- }
- };
- const handleSiteSettingsSuccess = (data: ShopSettingsUpdate) => {
- if (
- (!data.shopDomainUpdate.errors ||
- data.shopDomainUpdate.errors.length === 0) &&
- (!data.shopSettingsUpdate.errors ||
- data.shopSettingsUpdate.errors.length === 0)
- ) {
- pushMessage({
- text: i18n.t("Site settings updated", {
- context: "notification"
- })
- });
- }
- };
- return (
- <TypedSiteSettingsQuery displayLoader>
- {siteSettings => (
- <TypedAuthorizationKeyAdd onCompleted={handleAddKeySuccess}>
- {(addAuthorizationKey, addAuthorizationKeyOpts) => (
- <TypedAuthorizationKeyDelete
- onCompleted={handleDeleteKeySuccess}
- >
- {(deleteAuthorizationKey, _) => (
- <TypedShopSettingsUpdate
- onCompleted={handleSiteSettingsSuccess}
- >
- {(updateShopSettings, updateShopSettingsOpts) => {
- const errors = [
- ...maybe(
- () =>
- updateShopSettingsOpts.data.shopDomainUpdate
- .errors,
- []
- ),
- ...maybe(
- () =>
- updateShopSettingsOpts.data.shopSettingsUpdate
- .errors,
- []
- )
- ];
- const loading =
- siteSettings.loading ||
- addAuthorizationKeyOpts.loading ||
- updateShopSettingsOpts.loading;
+ const handleAuthenticationKeyAdd = (
+ data: SiteSettingsKeyDialogForm
+ ) =>
+ addAuthorizationKey({
+ variables: {
+ input: {
+ key: data.key,
+ password: data.password
+ },
+ keyType: data.type
+ }
+ });
+ const handleUpdateShopSettings = (
+ data: SiteSettingsPageFormData
+ ) =>
+ updateShopSettings({
+ variables: {
+ shopDomainInput: {
+ domain: data.domain,
+ name: data.name
+ },
+ shopSettingsInput: {
+ description: data.description
+ }
+ }
+ });
- const handleAuthenticationKeyAdd = (
- data: SiteSettingsKeyDialogForm
- ) =>
- addAuthorizationKey({
- variables: {
- input: {
- key: data.key,
- password: data.password
- },
- keyType: data.type
- }
- });
- const handleUpdateShopSettings = (
- data: SiteSettingsPageFormData
- ) =>
- updateShopSettings({
- variables: {
- shopDomainInput: {
- domain: data.domain,
- name: data.name
- },
- shopSettingsInput: {
- description: data.description
- }
- }
- });
+ const formTransitionState = getMutationState(
+ updateShopSettingsOpts.called,
+ updateShopSettingsOpts.loading,
+ [
+ ...maybe(
+ () =>
+ updateShopSettingsOpts.data.shopDomainUpdate.errors,
+ []
+ ),
+ ...maybe(
+ () =>
+ updateShopSettingsOpts.data.shopSettingsUpdate
+ .errors,
+ []
+ )
+ ]
+ );
- const formTransitionState = getMutationState(
- updateShopSettingsOpts.called,
- updateShopSettingsOpts.loading,
- [
- ...maybe(
- () =>
- updateShopSettingsOpts.data.shopDomainUpdate
- .errors,
- []
- ),
- ...maybe(
- () =>
- updateShopSettingsOpts.data
- .shopSettingsUpdate.errors,
- []
- )
- ]
- );
- return (
- <>
- <WindowTitle title={i18n.t("Site settings")} />
- <SiteSettingsPage
- disabled={loading}
- errors={errors}
- shop={maybe(() => siteSettings.data.shop)}
- onBack={() => navigate(configurationMenuUrl)}
- onKeyAdd={() =>
- navigate(siteSettingsAddKeyUrl)
- }
- onKeyRemove={keyType =>
- deleteAuthorizationKey({
- variables: { keyType }
- })
- }
- onSubmit={handleUpdateShopSettings}
- saveButtonBarState={formTransitionState}
- />
- <Route
- path={siteSettingsAddKeyPath}
- render={({ match }) => (
- <SiteSettingsKeyDialog
- errors={maybe(
- () =>
- addAuthorizationKeyOpts.data
- .authorizationKeyAdd.errors,
- []
- )}
- initial={{
- key: "",
- password: "",
- type: AuthorizationKeyType.FACEBOOK
- }}
- open={!!match}
- onClose={() => navigate(siteSettingsUrl)}
- onSubmit={handleAuthenticationKeyAdd}
- />
- )}
- />
- </>
- );
+ return (
+ <>
+ <WindowTitle title={i18n.t("Site settings")} />
+ <SiteSettingsPage
+ disabled={loading}
+ errors={errors}
+ shop={maybe(() => siteSettings.data.shop)}
+ onBack={() => navigate(configurationMenuUrl)}
+ onKeyAdd={() =>
+ navigate(
+ siteSettingsUrl({
+ action: "add-key"
+ })
+ )
+ }
+ onKeyRemove={keyType =>
+ deleteAuthorizationKey({
+ variables: { keyType }
+ })
+ }
+ onSubmit={handleUpdateShopSettings}
+ saveButtonBarState={formTransitionState}
+ />
+ <SiteSettingsKeyDialog
+ errors={maybe(
+ () =>
+ addAuthorizationKeyOpts.data.authorizationKeyAdd
+ .errors,
+ []
+ )}
+ initial={{
+ key: "",
+ password: "",
+ type: AuthorizationKeyType.FACEBOOK
}}
- </TypedShopSettingsUpdate>
- )}
- </TypedAuthorizationKeyDelete>
- )}
- </TypedAuthorizationKeyAdd>
+ open={params.action === "add-key"}
+ onClose={() => navigate(siteSettingsUrl())}
+ onSubmit={handleAuthenticationKeyAdd}
+ />
+ </>
+ );
+ }}
+ </TypedShopSettingsUpdate>
)}
- </TypedSiteSettingsQuery>
- );
- }}
- </Messages>
- )}
- </Navigator>
-);
+ </TypedAuthorizationKeyDelete>
+ )}
+ </TypedAuthorizationKeyAdd>
+ )}
+ </TypedSiteSettingsQuery>
+ );
+};
export default SiteSettings;