
* Use generated hooks in apps * Remove unused files * Use proper types in apps * Use generated hooks in attributes * Use generated hooks in auth module * Use generated hooks in categories * Use generated hooks in channels * Use generated types in collections * Remove legacy types from background tasks * Use generated hooks in customers * Use generated hooks in discounts * Use generated hook in file upload * Use generated types in gift cards * Use generated types in home * Use generated hooks in navigation * Use generated hooks in orders * Use generated hooks in pages * Use generated hooks in page types * Use generated hooks in permission groups * Use generated hooks in plugins * Use generated hooks in products * Use fragment to mark product variants * Improve code a bit * Use generated hooks in page types * Use generated types in searches * Use generated hooks in shipping * Use generated hooks in site settings * Use generated hooks in staff members * Use generated hooks in taxes * Place all gql generated files in one directory * Use generated hooks in translations * Use global types from new generated module * Use generated hooks in warehouses * Use generated hooks in webhooks * Use generated fragment types * Unclutter types * Remove hoc components * Split hooks and types * Fetch introspection file * Delete obsolete schema file * Fix rebase artifacts * Fix autoreplace * Fix auth provider tests * Fix urls * Remove leftover types * Fix rebase artifacts
52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import { Node, useProductVariantSetDefaultMutation } from "@saleor/graphql";
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
|
import { getProductErrorMessage } from "@saleor/utils/errors";
|
|
import { useIntl } from "react-intl";
|
|
|
|
function useOnSetDefaultVariant(productId: string, variant: Node) {
|
|
const notify = useNotifier();
|
|
const intl = useIntl();
|
|
|
|
const [productVariantSetDefault] = useProductVariantSetDefaultMutation({
|
|
onCompleted: data => {
|
|
const errors = data.productVariantSetDefault.errors;
|
|
if (errors.length) {
|
|
errors.map(error =>
|
|
notify({
|
|
status: "error",
|
|
text: getProductErrorMessage(error, intl)
|
|
})
|
|
);
|
|
} else {
|
|
const defaultVariant = data.productVariantSetDefault.product.variants.find(
|
|
variant =>
|
|
variant.id ===
|
|
data.productVariantSetDefault.product.defaultVariant.id
|
|
);
|
|
if (defaultVariant) {
|
|
notify({
|
|
status: "success",
|
|
text: intl.formatMessage(
|
|
{
|
|
defaultMessage: "Variant {name} has been set as default."
|
|
},
|
|
{ name: defaultVariant.name }
|
|
)
|
|
});
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
const onSetDefaultVariant = (selectedVariant = null) => {
|
|
productVariantSetDefault({
|
|
variables: {
|
|
productId,
|
|
variantId: variant ? variant.id : selectedVariant.id
|
|
}
|
|
});
|
|
};
|
|
|
|
return onSetDefaultVariant;
|
|
}
|
|
export default useOnSetDefaultVariant;
|