Fix tax settings update

This commit is contained in:
dominik-zeglen 2019-11-06 14:11:10 +01:00
parent 45a56bb175
commit 62131d58fc
3 changed files with 107 additions and 57 deletions

View file

@ -46,6 +46,7 @@ const styles = (theme: Theme) =>
background: theme.palette.background.default, background: theme.palette.background.default,
borderTop: "1px solid transparent", borderTop: "1px solid transparent",
boxShadow: `0 -5px 5px 0 ${theme.overrides.MuiCard.root.borderColor}`, boxShadow: `0 -5px 5px 0 ${theme.overrides.MuiCard.root.borderColor}`,
height: "100%",
transition: `box-shadow ${theme.transitions.duration.shortest}ms` transition: `box-shadow ${theme.transitions.duration.shortest}ms`
}, },
spacer: { spacer: {

View file

@ -2,10 +2,12 @@ import React from "react";
import { useIntl } from "react-intl"; import { useIntl } from "react-intl";
import AppHeader from "@saleor/components/AppHeader"; import AppHeader from "@saleor/components/AppHeader";
import { ConfirmButtonTransitionState } from "@saleor/components/ConfirmButton";
import { Container } from "@saleor/components/Container"; import { Container } from "@saleor/components/Container";
import Form from "@saleor/components/Form"; import Form from "@saleor/components/Form";
import Grid from "@saleor/components/Grid"; import Grid from "@saleor/components/Grid";
import PageHeader from "@saleor/components/PageHeader"; import PageHeader from "@saleor/components/PageHeader";
import SaveButtonBar from "@saleor/components/SaveButtonBar";
import { sectionNames } from "@saleor/intl"; import { sectionNames } from "@saleor/intl";
import { maybe } from "../../../misc"; import { maybe } from "../../../misc";
import { CountryList_shop } from "../../types/CountryList"; import { CountryList_shop } from "../../types/CountryList";
@ -19,6 +21,7 @@ export interface FormData {
} }
export interface CountryListPageProps { export interface CountryListPageProps {
disabled: boolean; disabled: boolean;
saveButtonBarState: ConfirmButtonTransitionState;
shop: CountryList_shop; shop: CountryList_shop;
onBack: () => void; onBack: () => void;
onRowClick: (code: string) => void; onRowClick: (code: string) => void;
@ -28,6 +31,7 @@ export interface CountryListPageProps {
const CountryListPage: React.FC<CountryListPageProps> = ({ const CountryListPage: React.FC<CountryListPageProps> = ({
disabled, disabled,
saveButtonBarState,
shop, shop,
onBack, onBack,
onRowClick, onRowClick,
@ -44,6 +48,7 @@ const CountryListPage: React.FC<CountryListPageProps> = ({
return ( return (
<Form initial={initialForm} onSubmit={onSubmit}> <Form initial={initialForm} onSubmit={onSubmit}>
{({ change, data, submit }) => ( {({ change, data, submit }) => (
<>
<Container> <Container>
<AppHeader onBack={onBack}> <AppHeader onBack={onBack}>
{intl.formatMessage(sectionNames.configuration)} {intl.formatMessage(sectionNames.configuration)}
@ -54,13 +59,7 @@ const CountryListPage: React.FC<CountryListPageProps> = ({
description: "header" description: "header"
})} })}
/> />
<Grid> <Grid variant="inverted">
<div>
<CountryList
countries={maybe(() => shop.countries)}
onRowClick={onRowClick}
/>
</div>
<div> <div>
<TaxConfiguration <TaxConfiguration
data={data} data={data}
@ -69,8 +68,21 @@ const CountryListPage: React.FC<CountryListPageProps> = ({
onTaxFetch={onTaxFetch} onTaxFetch={onTaxFetch}
/> />
</div> </div>
<div>
<CountryList
countries={maybe(() => shop.countries)}
onRowClick={onRowClick}
/>
</div>
</Grid> </Grid>
</Container> </Container>
<SaveButtonBar
disabled={disabled}
state={saveButtonBarState}
onCancel={onBack}
onSave={submit}
/>
</>
)} )}
</Form> </Form>
); );

View file

@ -1,23 +1,57 @@
import React from "react"; import React from "react";
import { useIntl } from "react-intl";
import useNavigator from "@saleor/hooks/useNavigator"; import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
import { commonMessages } from "@saleor/intl";
import { configurationMenuUrl } from "../../configuration"; import { configurationMenuUrl } from "../../configuration";
import { maybe } from "../../misc"; import { getMutationState, maybe } from "../../misc";
import CountryListPage from "../components/CountryListPage"; import CountryListPage from "../components/CountryListPage";
import { TypedFetchTaxes, TypedUpdateTaxSettings } from "../mutations"; import { TypedFetchTaxes, TypedUpdateTaxSettings } from "../mutations";
import { TypedCountryListQuery } from "../queries"; import { TypedCountryListQuery } from "../queries";
import { FetchTaxes } from "../types/FetchTaxes";
import { UpdateTaxSettings } from "../types/UpdateTaxSettings";
import { countryTaxRatesUrl } from "../urls"; import { countryTaxRatesUrl } from "../urls";
export const CountryList: React.FC = () => { export const CountryList: React.FC = () => {
const intl = useIntl();
const navigate = useNavigator(); const navigate = useNavigator();
const notify = useNotifier();
const handleUpdateTaxSettings = (data: UpdateTaxSettings) => {
if (data.shopSettingsUpdate.errors.length === 0) {
notify({
text: intl.formatMessage(commonMessages.savedChanges)
});
}
};
const handleFetchTaxes = (data: FetchTaxes) => {
if (data.shopFetchTaxRates.errors.length === 0) {
notify({
text: intl.formatMessage({
defaultMessage: "Successfully fetched tax rates"
})
});
}
};
return ( return (
<TypedUpdateTaxSettings> <TypedUpdateTaxSettings onCompleted={handleUpdateTaxSettings}>
{(updateTaxSettings, updateTaxSettingsOpts) => ( {(updateTaxSettings, updateTaxSettingsOpts) => (
<TypedFetchTaxes> <TypedFetchTaxes onCompleted={handleFetchTaxes}>
{(fetchTaxes, fetchTaxesOpts) => ( {(fetchTaxes, fetchTaxesOpts) => (
<TypedCountryListQuery displayLoader={true}> <TypedCountryListQuery displayLoader={true}>
{({ data, loading }) => ( {({ data, loading }) => {
const updateTaxSettingsTransitionState = getMutationState(
updateTaxSettingsOpts.called,
updateTaxSettingsOpts.loading,
maybe(
() => updateTaxSettingsOpts.data.shopSettingsUpdate.errors
)
);
return (
<CountryListPage <CountryListPage
disabled={ disabled={
loading || loading ||
@ -30,7 +64,8 @@ export const CountryList: React.FC = () => {
updateTaxSettings({ updateTaxSettings({
variables: { variables: {
input: { input: {
chargeTaxesOnShipping: formData.chargeTaxesOnShipping, chargeTaxesOnShipping:
formData.chargeTaxesOnShipping,
displayGrossPrices: formData.showGross, displayGrossPrices: formData.showGross,
includeTaxesInPrices: formData.includeTax includeTaxesInPrices: formData.includeTax
} }
@ -38,6 +73,7 @@ export const CountryList: React.FC = () => {
}) })
} }
onTaxFetch={fetchTaxes} onTaxFetch={fetchTaxes}
saveButtonBarState={updateTaxSettingsTransitionState}
shop={maybe(() => ({ shop={maybe(() => ({
...data.shop, ...data.shop,
countries: data.shop.countries.filter( countries: data.shop.countries.filter(
@ -45,7 +81,8 @@ export const CountryList: React.FC = () => {
) )
}))} }))}
/> />
)} );
}}
</TypedCountryListQuery> </TypedCountryListQuery>
)} )}
</TypedFetchTaxes> </TypedFetchTaxes>