import { stringify as stringifyQs } from "qs"; import * as React from "react"; import useNavigator from "@saleor/hooks/useNavigator"; import usePaginator, { createPaginationState } from "@saleor/hooks/usePaginator"; import useShop from "@saleor/hooks/useShop"; import { PAGINATE_BY } from "../../config"; import { maybe } from "../../misc"; import { Pagination } from "../../types"; import TranslationsEntitiesList from "../components/TranslationsEntitiesList"; import TranslationsEntitiesListPage, { TranslationsEntitiesListFilterTab } from "../components/TranslationsEntitiesListPage"; import { TypedCategoryTranslations, TypedCollectionTranslations, TypedPageTranslations, TypedProductTranslations, TypedProductTypeTranslations, TypedSaleTranslations, TypedVoucherTranslations } from "../queries"; import { AttributeTranslationFragment } from "../types/AttributeTranslationFragment"; import { languageEntityUrl, languageListUrl, TranslatableEntities } from "../urls"; export type TranslationsEntitiesListQueryParams = Pagination & { tab: TranslationsEntitiesListFilterTab; }; interface TranslationsEntitiesProps { language: string; params: TranslationsEntitiesListQueryParams; } function sumTranslations( acc: number, attr: AttributeTranslationFragment ): number { const accAfterNameTranslation = acc + (attr.translation && attr.translation.name !== null ? 1 : 0); const count = accAfterNameTranslation + attr.values.reduce( (acc2, attrValue) => acc2 + (attrValue.translation && attrValue.translation.name !== null ? 1 : 0), 0 ); return count; } const TranslationsEntities: React.FC = ({ language, params }) => { const navigate = useNavigator(); const paginate = usePaginator(); const shop = useShop(); if (Object.keys(TranslatableEntities).indexOf(params.tab) === -1) { navigate( "?" + stringifyQs({ tab: TranslatableEntities.categories }), true ); } const filterCallbacks = { onCategoriesTabClick: () => navigate( "?" + stringifyQs({ tab: TranslatableEntities.categories }) ), onCollectionsTabClick: () => navigate( "?" + stringifyQs({ tab: TranslatableEntities.collections }) ), onPagesTabClick: () => navigate( "?" + stringifyQs({ tab: TranslatableEntities.pages }) ), onProductTypesTabClick: () => navigate( "?" + stringifyQs({ tab: TranslatableEntities.productTypes }) ), onProductsTabClick: () => navigate( "?" + stringifyQs({ tab: TranslatableEntities.products }) ), onSalesTabClick: () => navigate( "?" + stringifyQs({ tab: TranslatableEntities.sales }) ), onVouchersTabClick: () => navigate( "?" + stringifyQs({ tab: TranslatableEntities.vouchers }) ) }; const lang = maybe(() => shop.languages.find(languageFromList => languageFromList.code === language) ); const paginationState = createPaginationState(PAGINATE_BY, params); return ( navigate(languageListUrl)} > {params.tab === "categories" ? ( {({ data, loading }) => { const { loadNextPage, loadPreviousPage, pageInfo } = paginate( maybe(() => data.categories.pageInfo), paginationState, params ); return ( data.categories.edges .map(edge => edge.node) .map(node => ({ completion: { current: node.translation ? [ node.translation.descriptionJson, node.translation.name, node.translation.seoDescription, node.translation.seoTitle ].reduce( (acc, field) => acc + (field !== null ? 1 : 0), 0 ) : 0, max: 4 }, id: node.id, name: node.name })) )} onRowClick={id => navigate( languageEntityUrl( language, TranslatableEntities.categories, id ) ) } onNextPage={loadNextPage} onPreviousPage={loadPreviousPage} pageInfo={pageInfo} /> ); }} ) : params.tab === "products" ? ( {({ data, loading }) => { const { loadNextPage, loadPreviousPage, pageInfo } = paginate( maybe(() => data.products.pageInfo), paginationState, params ); return ( data.products.edges .map(edge => edge.node) .map(node => ({ completion: { current: node.translation ? [ node.translation.descriptionJson, node.translation.name, node.translation.seoDescription, node.translation.seoTitle ].reduce( (acc, field) => acc + (field !== null ? 1 : 0), 0 ) : 0, max: 4 }, id: node.id, name: node.name })) )} onRowClick={id => navigate( languageEntityUrl( language, TranslatableEntities.products, id ) ) } onNextPage={loadNextPage} onPreviousPage={loadPreviousPage} pageInfo={pageInfo} /> ); }} ) : params.tab === "collections" ? ( {({ data, loading }) => { const { loadNextPage, loadPreviousPage, pageInfo } = paginate( maybe(() => data.collections.pageInfo), paginationState, params ); return ( data.collections.edges .map(edge => edge.node) .map(node => ({ completion: { current: node.translation ? [ node.translation.descriptionJson, node.translation.name, node.translation.seoDescription, node.translation.seoTitle ].reduce( (acc, field) => acc + (field !== null ? 1 : 0), 0 ) : 0, max: 4 }, id: node.id, name: node.name })) )} onRowClick={id => navigate( languageEntityUrl( language, TranslatableEntities.collections, id ) ) } onNextPage={loadNextPage} onPreviousPage={loadPreviousPage} pageInfo={pageInfo} /> ); }} ) : params.tab === "sales" ? ( {({ data, loading }) => { const { loadNextPage, loadPreviousPage, pageInfo } = paginate( maybe(() => data.sales.pageInfo), paginationState, params ); return ( data.sales.edges .map(edge => edge.node) .map(node => ({ completion: { current: node.translation ? +!!node.translation.name : 0, max: 1 }, id: node.id, name: node.name })) )} onRowClick={id => navigate( languageEntityUrl(language, TranslatableEntities.sales, id) ) } onNextPage={loadNextPage} onPreviousPage={loadPreviousPage} pageInfo={pageInfo} /> ); }} ) : params.tab === "vouchers" ? ( {({ data, loading }) => { const { loadNextPage, loadPreviousPage, pageInfo } = paginate( maybe(() => data.vouchers.pageInfo), paginationState, params ); return ( data.vouchers.edges .map(edge => edge.node) .map(node => ({ completion: { current: node.translation ? +!!node.translation.name : 0, max: 1 }, id: node.id, name: node.name })) )} onRowClick={id => navigate( languageEntityUrl( language, TranslatableEntities.vouchers, id ) ) } onNextPage={loadNextPage} onPreviousPage={loadPreviousPage} pageInfo={pageInfo} /> ); }} ) : params.tab === "pages" ? ( {({ data, loading }) => { const { loadNextPage, loadPreviousPage, pageInfo } = paginate( maybe(() => data.pages.pageInfo), paginationState, params ); return ( data.pages.edges .map(edge => edge.node) .map(node => ({ completion: { current: node.translation ? [ node.translation.contentJson, node.translation.seoDescription, node.translation.seoTitle, node.translation.title ].reduce( (acc, field) => acc + (field !== null ? 1 : 0), 0 ) : 0, max: 4 }, id: node.id, name: node.title })) )} onRowClick={id => navigate( languageEntityUrl(language, TranslatableEntities.pages, id) ) } onNextPage={loadNextPage} onPreviousPage={loadPreviousPage} pageInfo={pageInfo} /> ); }} ) : params.tab === "productTypes" ? ( {({ data, loading }) => { const { loadNextPage, loadPreviousPage, pageInfo } = paginate( maybe(() => data.productTypes.pageInfo), paginationState, params ); return ( data.productTypes.edges .map(edge => edge.node) .map(node => ({ completion: { current: node.productAttributes && node.variantAttributes ? maybe(() => node.productAttributes, []).reduce( sumTranslations, 0 ) + maybe(() => node.variantAttributes, []).reduce( sumTranslations, 0 ) : 0, max: node.productAttributes && node.variantAttributes ? node.productAttributes.reduce( (acc, attr) => acc + attr.values.length, node.productAttributes.length ) + node.variantAttributes.reduce( (acc, attr) => acc + attr.values.length, node.variantAttributes.length ) : 0 }, id: node.id, name: node.name })) )} onRowClick={id => navigate( languageEntityUrl( language, TranslatableEntities.productTypes, id ) ) } onNextPage={loadNextPage} onPreviousPage={loadPreviousPage} pageInfo={pageInfo} /> ); }} ) : null} ); }; TranslationsEntities.displayName = "TranslationsEntities"; export default TranslationsEntities;