Skip to content

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

Add collection translation view

dominik-zeglen2bc5641e34fe
diff --git a/saleor/static/dashboard-next/translations/components/TranslationsCollectionsPage/TranslationsCollectionsPage.tsx b/saleor/static/dashboard-next/translations/components/TranslationsCollectionsPage/TranslationsCollectionsPage.tsx
new file mode 100644
index 0000000000..c2940b4292
--- /dev/null
+++ b/saleor/static/dashboard-next/translations/components/TranslationsCollectionsPage/TranslationsCollectionsPage.tsx
@@ -0,0 +1,117 @@
+import * as React from "react";
+
+import CardSpacer from "../../../components/CardSpacer";
+import { ConfirmButtonTransitionState } from "../../../components/ConfirmButton";
+import Container from "../../../components/Container";
+import PageHeader from "../../../components/PageHeader";
+import i18n from "../../../i18n";
+import { maybe } from "../../../misc";
+import { CollectionTranslationFragment } from "../../types/CollectionTranslationFragment";
+import TranslationFields from "../TranslationFields";
+
+export interface TranslationsCollectionsPageProps {
+ activeField: string;
+ disabled: boolean;
+ languageCode: string;
+ collection: CollectionTranslationFragment;
+ saveButtonState: ConfirmButtonTransitionState;
+ onEdit: (field: string) => void;
+ onSubmit: (field: string, data: string) => void;
+}
+
+export const fieldNames = {
+ descriptionJson: "description",
+ name: "name",
+ seoDescription: "seoDescription",
+ seoTitle: "seoTitle"
+};
+
+const TranslationsCollectionsPage: React.StatelessComponent<
+ TranslationsCollectionsPageProps
+> = ({
+ activeField,
+ disabled,
+ languageCode,
+ collection,
+ saveButtonState,
+ onEdit,
+ onSubmit
+}) => (
+ <Container>
+ <PageHeader
+ title={i18n.t(
+ 'Translation Collection "{{ collectionName }}" - {{ languageCode }}',
+ {
+ collectionName: maybe(() => collection.name, "..."),
+ context: "collection translation page title",
+ languageCode
+ }
+ )}
+ />
+ <TranslationFields
+ activeField={activeField}
+ disabled={disabled}
+ initialState={true}
+ title={i18n.t("General Information")}
+ fields={[
+ {
+ displayName: i18n.t("Collection Name"),
+ name: fieldNames.name,
+ translation: maybe(() =>
+ collection.translation ? collection.translation.name : null
+ ),
+ type: "short" as "short",
+ value: maybe(() => collection.name)
+ },
+ {
+ displayName: i18n.t("Description"),
+ name: fieldNames.descriptionJson,
+ translation: maybe(() =>
+ collection.translation
+ ? collection.translation.descriptionJson
+ : null
+ ),
+ type: "rich" as "rich",
+ value: maybe(() => collection.descriptionJson)
+ }
+ ]}
+ saveButtonState={saveButtonState}
+ onEdit={onEdit}
+ onSubmit={onSubmit}
+ />
+ <CardSpacer />
+ <TranslationFields
+ activeField={activeField}
+ disabled={disabled}
+ initialState={true}
+ title={i18n.t("Search Engine Preview")}
+ fields={[
+ {
+ displayName: i18n.t("Search Engine Title"),
+ name: fieldNames.seoTitle,
+ translation: maybe(() =>
+ collection.translation ? collection.translation.seoTitle : null
+ ),
+ type: "short" as "short",
+ value: maybe(() => collection.seoTitle)
+ },
+ {
+ displayName: i18n.t("Search Engine Description"),
+ name: fieldNames.seoDescription,
+ translation: maybe(() =>
+ collection.translation
+ ? collection.translation.seoDescription
+ : null
+ ),
+ type: "long" as "long",
+ value: maybe(() => collection.seoDescription)
+ }
+ ]}
+ saveButtonState={saveButtonState}
+ onEdit={onEdit}
+ onSubmit={onSubmit}
+ />
+ </Container>
+);
+TranslationsCollectionsPage.displayName = "TranslationsCollectionsPage";
+export default TranslationsCollectionsPage;
diff --git a/saleor/static/dashboard-next/translations/components/TranslationsCollectionsPage/index.ts b/saleor/static/dashboard-next/translations/components/TranslationsCollectionsPage/index.ts
new file mode 100644
index 0000000000..0b98c4c6e3
--- /dev/null
+++ b/saleor/static/dashboard-next/translations/components/TranslationsCollectionsPage/index.ts
@@ -0,0 +1,2 @@
+export { default } from "./TranslationsCollectionsPage";
+export * from "./TranslationsCollectionsPage";
diff --git a/saleor/static/dashboard-next/translations/index.tsx b/saleor/static/dashboard-next/translations/index.tsx
index c53d1a3bf0..b0f9d6cc50 100644
--- a/saleor/static/dashboard-next/translations/index.tsx
+++ b/saleor/static/dashboard-next/translations/index.tsx
@@ -14,6 +14,9 @@ import {
import TranslationsCategoriesComponent, {
TranslationsCategoriesQueryParams
} from "./views/TranslationsCategories";
+import TranslationsCollectionsComponent, {
+ TranslationsCollectionsQueryParams
+} from "./views/TranslationsCollections";
import TranslationsEntitiesComponent, {
TranslationsEntitiesListQueryParams
} from "./views/TranslationsEntities";
@@ -62,6 +65,22 @@ const TranslationsCategories: React.FC<TranslationsEntityRouteProps> = ({
/>
);
};
+const TranslationsCollections: React.FC<TranslationsEntityRouteProps> = ({
+ location,
+ match
+}) => {
+ const qs = parseQs(location.search.substr(1));
+ const params: TranslationsCollectionsQueryParams = {
+ activeField: qs.activeField
+ };
+ return (
+ <TranslationsCollectionsComponent
+ id={decodeURIComponent(match.params.id)}
+ languageCode={LanguageCodeEnum[match.params.languageCode]}
+ params={params}
+ />
+ );
+};
const TranslationsProducts: React.FC<TranslationsEntityRouteProps> = ({
location,
match
@@ -111,6 +130,15 @@ const TranslationsRouter: React.FC = () => (
)}
component={TranslationsCategories}
/>
+ <Route
+ exact
+ path={languageEntityPath(
+ ":languageCode",
+ TranslatableEntities.collections,
+ ":id"
+ )}
+ component={TranslationsCollections}
+ />
</Switch>
</>
);
diff --git a/saleor/static/dashboard-next/translations/mutations.ts b/saleor/static/dashboard-next/translations/mutations.ts
index db07450d72..f7e8857c13 100644
--- a/saleor/static/dashboard-next/translations/mutations.ts
+++ b/saleor/static/dashboard-next/translations/mutations.ts
@@ -3,12 +3,17 @@ import gql from "graphql-tag";
import { TypedMutation } from "../mutations";
import {
categoryTranslationFragment,
+ collectionTranslationFragment,
productTranslationFragment
} from "./queries";
import {
UpdateCategoryTranslations,
UpdateCategoryTranslationsVariables
} from "./types/UpdateCategoryTranslations";
+import {
+ UpdateCollectionTranslations,
+ UpdateCollectionTranslationsVariables
+} from "./types/UpdateCollectionTranslations";
import {
UpdateProductTranslations,
UpdateProductTranslationsVariables
@@ -59,3 +64,26 @@ export const TypedUpdateCategoryTranslations = TypedMutation<
UpdateCategoryTranslations,
UpdateCategoryTranslationsVariables
>(updateCategoryTranslations);
+
+const updateCollectionTranslations = gql`
+ ${collectionTranslationFragment}
+ mutation UpdateCollectionTranslations(
+ $id: ID!
+ $input: TranslationInput!
+ $language: LanguageCodeEnum!
+ ) {
+ collectionTranslate(id: $id, input: $input, languageCode: $language) {
+ errors {
+ field
+ message
+ }
+ collection {
+ ...CollectionTranslationFragment
+ }
+ }
+ }
+`;
+export const TypedUpdateCollectionTranslations = TypedMutation<
+ UpdateCollectionTranslations,
+ UpdateCollectionTranslationsVariables
+>(updateCollectionTranslations);
diff --git a/saleor/static/dashboard-next/translations/queries.ts b/saleor/static/dashboard-next/translations/queries.ts
index 3223ff1eb8..cb8c774a23 100644
--- a/saleor/static/dashboard-next/translations/queries.ts
+++ b/saleor/static/dashboard-next/translations/queries.ts
@@ -9,6 +9,10 @@ import {
CategoryTranslations,
CategoryTranslationsVariables
} from "./types/CategoryTranslations";
+import {
+ CollectionTranslationDetails,
+ CollectionTranslationDetailsVariables
+} from "./types/CollectionTranslationDetails";
import {
CollectionTranslations,
CollectionTranslationsVariables
@@ -187,3 +191,16 @@ export const TypedCategoryTranslationDetails = TypedQuery<
CategoryTranslationDetails,
CategoryTranslationDetailsVariables
>(categoryTranslationDetails);
+
+const collectionTranslationDetails = gql`
+ ${collectionTranslationFragment}
+ query CollectionTranslationDetails($id: ID!, $language: LanguageCodeEnum!) {
+ collection(id: $id) {
+ ...CollectionTranslationFragment
+ }
+ }
+`;
+export const TypedCollectionTranslationDetails = TypedQuery<
+ CollectionTranslationDetails,
+ CollectionTranslationDetailsVariables
+>(collectionTranslationDetails);
diff --git a/saleor/static/dashboard-next/translations/types/CollectionTranslationDetails.ts b/saleor/static/dashboard-next/translations/types/CollectionTranslationDetails.ts
new file mode 100644
index 0000000000..cc8d7472c2
--- /dev/null
+++ b/saleor/static/dashboard-next/translations/types/CollectionTranslationDetails.ts
@@ -0,0 +1,43 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+import { LanguageCodeEnum } from "./../../types/globalTypes";
+
+// ====================================================
+// GraphQL query operation: CollectionTranslationDetails
+// ====================================================
+
+export interface CollectionTranslationDetails_collection_translation_language {
+ __typename: "LanguageDisplay";
+ language: string;
+}
+
+export interface CollectionTranslationDetails_collection_translation {
+ __typename: "CollectionTranslation";
+ id: string;
+ descriptionJson: any;
+ language: CollectionTranslationDetails_collection_translation_language;
+ name: string;
+ seoDescription: string | null;
+ seoTitle: string | null;
+}
+
+export interface CollectionTranslationDetails_collection {
+ __typename: "Collection";
+ id: string;
+ name: string;
+ descriptionJson: any;
+ seoDescription: string | null;
+ seoTitle: string | null;
+ translation: CollectionTranslationDetails_collection_translation | null;
+}
+
+export interface CollectionTranslationDetails {
+ collection: CollectionTranslationDetails_collection | null;
+}
+
+export interface CollectionTranslationDetailsVariables {
+ id: string;
+ language: LanguageCodeEnum;
+}
diff --git a/saleor/static/dashboard-next/translations/types/UpdateCollectionTranslations.ts b/saleor/static/dashboard-next/translations/types/UpdateCollectionTranslations.ts
new file mode 100644
index 0000000000..8a1ec15232
--- /dev/null
+++ b/saleor/static/dashboard-next/translations/types/UpdateCollectionTranslations.ts
@@ -0,0 +1,56 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+import { TranslationInput, LanguageCodeEnum } from "./../../types/globalTypes";
+
+// ====================================================
+// GraphQL mutation operation: UpdateCollectionTranslations
+// ====================================================
+
+export interface UpdateCollectionTranslations_collectionTranslate_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface UpdateCollectionTranslations_collectionTranslate_collection_translation_language {
+ __typename: "LanguageDisplay";
+ language: string;
+}
+
+export interface UpdateCollectionTranslations_collectionTranslate_collection_translation {
+ __typename: "CollectionTranslation";
+ id: string;
+ descriptionJson: any;
+ language: UpdateCollectionTranslations_collectionTranslate_collection_translation_language;
+ name: string;
+ seoDescription: string | null;
+ seoTitle: string | null;
+}
+
+export interface UpdateCollectionTranslations_collectionTranslate_collection {
+ __typename: "Collection";
+ id: string;
+ name: string;
+ descriptionJson: any;
+ seoDescription: string | null;
+ seoTitle: string | null;
+ translation: UpdateCollectionTranslations_collectionTranslate_collection_translation | null;
+}
+
+export interface UpdateCollectionTranslations_collectionTranslate {
+ __typename: "CollectionTranslate";
+ errors: UpdateCollectionTranslations_collectionTranslate_errors[] | null;
+ collection: UpdateCollectionTranslations_collectionTranslate_collection | null;
+}
+
+export interface UpdateCollectionTranslations {
+ collectionTranslate: UpdateCollectionTranslations_collectionTranslate | null;
+}
+
+export interface UpdateCollectionTranslationsVariables {
+ id: string;
+ input: TranslationInput;
+ language: LanguageCodeEnum;
+}
diff --git a/saleor/static/dashboard-next/translations/views/TranslationsCollections.tsx b/saleor/static/dashboard-next/translations/views/TranslationsCollections.tsx
new file mode 100644
index 0000000000..4b3643dee4
--- /dev/null
+++ b/saleor/static/dashboard-next/translations/views/TranslationsCollections.tsx
@@ -0,0 +1,107 @@
+import { stringify as stringifyQs } from "qs";
+import * as React from "react";
+
+import useNavigator from "../../hooks/useNavigator";
+import useNotifier from "../../hooks/useNotifier";
+import i18n from "../../i18n";
+import { getMutationState, maybe } from "../../misc";
+import { LanguageCodeEnum, TranslationInput } from "../../types/globalTypes";
+import TranslationsCollectionsPage, {
+ fieldNames
+} from "../components/TranslationsCollectionsPage";
+import { TypedUpdateCollectionTranslations } from "../mutations";
+import { TypedCollectionTranslationDetails } from "../queries";
+import { UpdateCollectionTranslations } from "../types/UpdateCollectionTranslations";
+
+export interface TranslationsCollectionsQueryParams {
+ activeField: string;
+}
+export interface TranslationsCollectionsProps {
+ id: string;
+ languageCode: LanguageCodeEnum;
+ params: TranslationsCollectionsQueryParams;
+}
+
+const TranslationsCollections: React.FC<TranslationsCollectionsProps> = ({
+ id,
+ languageCode,
+ params
+}) => {
+ const navigate = useNavigator();
+ const notify = useNotifier();
+
+ const onEdit = (field: string) =>
+ navigate(
+ "?" +
+ stringifyQs({
+ activeField: field
+ }),
+ true
+ );
+ const onUpdate = (data: UpdateCollectionTranslations) => {
+ if (data.collectionTranslate.errors.length === 0) {
+ notify({
+ text: i18n.t("Translation Saved")
+ });
+ navigate("?", true);
+ }
+ };
+
+ return (
+ <TypedCollectionTranslationDetails
+ variables={{ id, language: languageCode }}
+ >
+ {collectionTranslations => (
+ <TypedUpdateCollectionTranslations onCompleted={onUpdate}>
+ {(updateTranslations, updateTranslationsOpts) => {
+ const handleSubmit = (field: string, data: string) => {
+ const input: TranslationInput = {};
+ if (field === fieldNames.descriptionJson) {
+ input.descriptionJson = JSON.stringify(data);
+ } else if (field === fieldNames.name) {
+ input.name = data;
+ } else if (field === fieldNames.seoDescription) {
+ input.seoDescription = data;
+ } else if (field === fieldNames.seoTitle) {
+ input.seoTitle = data;
+ }
+ updateTranslations({
+ variables: {
+ id,
+ input,
+ language: languageCode
+ }
+ });
+ };
+
+ const saveButtonState = getMutationState(
+ updateTranslationsOpts.called,
+ updateTranslationsOpts.loading,
+ maybe(
+ () => updateTranslationsOpts.data.collectionTranslate.errors,
+ []
+ )
+ );
+
+ return (
+ <TranslationsCollectionsPage
+ activeField={params.activeField}
+ disabled={
+ collectionTranslations.loading ||
+ updateTranslationsOpts.loading
+ }
+ languageCode={languageCode}
+ saveButtonState={saveButtonState}
+ onEdit={onEdit}
+ onSubmit={handleSubmit}
+ collection={maybe(() => collectionTranslations.data.collection)}
+ />
+ );
+ }}
+ </TypedUpdateCollectionTranslations>
+ )}
+ </TypedCollectionTranslationDetails>
+ );
+};
+TranslationsCollections.displayName = "TranslationsCollections";
+export default TranslationsCollections;