2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-11-06 13:11:10 +00:00
|
|
|
import { useIntl } from "react-intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
2019-11-06 13:11:10 +00:00
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
|
|
|
import { commonMessages } from "@saleor/intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { configurationMenuUrl } from "../../configuration";
|
2019-12-06 17:11:46 +00:00
|
|
|
import { maybe } from "../../misc";
|
2019-06-19 14:40:52 +00:00
|
|
|
import CountryListPage from "../components/CountryListPage";
|
|
|
|
import { TypedFetchTaxes, TypedUpdateTaxSettings } from "../mutations";
|
|
|
|
import { TypedCountryListQuery } from "../queries";
|
2019-11-06 13:11:10 +00:00
|
|
|
import { FetchTaxes } from "../types/FetchTaxes";
|
|
|
|
import { UpdateTaxSettings } from "../types/UpdateTaxSettings";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { countryTaxRatesUrl } from "../urls";
|
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
export const CountryList: React.FC = () => {
|
2019-11-06 13:11:10 +00:00
|
|
|
const intl = useIntl();
|
2019-06-19 14:40:52 +00:00
|
|
|
const navigate = useNavigator();
|
2019-11-06 13:11:10 +00:00
|
|
|
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"
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
return (
|
2019-11-06 13:11:10 +00:00
|
|
|
<TypedUpdateTaxSettings onCompleted={handleUpdateTaxSettings}>
|
2019-06-19 14:40:52 +00:00
|
|
|
{(updateTaxSettings, updateTaxSettingsOpts) => (
|
2019-11-06 13:11:10 +00:00
|
|
|
<TypedFetchTaxes onCompleted={handleFetchTaxes}>
|
2019-06-19 14:40:52 +00:00
|
|
|
{(fetchTaxes, fetchTaxesOpts) => (
|
|
|
|
<TypedCountryListQuery displayLoader={true}>
|
2019-12-06 17:11:46 +00:00
|
|
|
{({ data, loading }) => (
|
|
|
|
<CountryListPage
|
|
|
|
disabled={
|
|
|
|
loading ||
|
|
|
|
fetchTaxesOpts.loading ||
|
|
|
|
updateTaxSettingsOpts.loading
|
|
|
|
}
|
|
|
|
onBack={() => navigate(configurationMenuUrl)}
|
|
|
|
onRowClick={code => navigate(countryTaxRatesUrl(code))}
|
|
|
|
onSubmit={formData =>
|
|
|
|
updateTaxSettings({
|
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
chargeTaxesOnShipping: formData.chargeTaxesOnShipping,
|
|
|
|
displayGrossPrices: formData.showGross,
|
|
|
|
includeTaxesInPrices: formData.includeTax
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
2019-12-06 17:11:46 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
onTaxFetch={fetchTaxes}
|
|
|
|
saveButtonBarState={updateTaxSettingsOpts.state}
|
|
|
|
shop={maybe(() => ({
|
|
|
|
...data.shop,
|
|
|
|
countries: data.shop.countries.filter(
|
|
|
|
country => country.vat
|
|
|
|
)
|
|
|
|
}))}
|
|
|
|
/>
|
|
|
|
)}
|
2019-06-19 14:40:52 +00:00
|
|
|
</TypedCountryListQuery>
|
|
|
|
)}
|
|
|
|
</TypedFetchTaxes>
|
|
|
|
)}
|
|
|
|
</TypedUpdateTaxSettings>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default CountryList;
|