Disable auto scroll to the top on assign references (#1510) (#1524)

* add resetScroll behavior to navigator hook

* disable scroll to top on modal close

* refactor useNavigator hook

* fix linter issue

* fix no default value of destructured parameter
This commit is contained in:
Michał Droń 2021-10-21 10:34:56 +02:00 committed by GitHub
parent fe3de902b1
commit e879a0c1d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 74 additions and 61 deletions

View file

@ -24,7 +24,7 @@ const NewPassword: React.FC<RouteComponentProps> = ({ location }) => {
data.setPassword.csrfToken, data.setPassword.csrfToken,
data.setPassword.user data.setPassword.user
); );
navigate("/", true); navigate("/", { replace: true });
} }
}; };

View file

@ -132,7 +132,7 @@ export const CategoryList: React.FC<CategoryListProps> = ({ params }) => {
const handleCategoryBulkDelete = (data: CategoryBulkDelete) => { const handleCategoryBulkDelete = (data: CategoryBulkDelete) => {
if (data.categoryBulkDelete.errors.length === 0) { if (data.categoryBulkDelete.errors.length === 0) {
navigate(categoryListUrl(), true); navigate(categoryListUrl(), { replace: true });
refetch(); refetch();
reset(); reset();
} }

View file

@ -120,7 +120,7 @@ export const CollectionDetails: React.FC<CollectionDetailsProps> = ({
defaultMessage: "Added product to collection" defaultMessage: "Added product to collection"
}) })
}); });
navigate(collectionUrl(id), true); navigate(collectionUrl(id), { replace: true });
} }
} }
} }

View file

@ -149,7 +149,9 @@ export const CustomerDetailsView: React.FC<CustomerDetailsViewProps> = ({
/> />
<ActionDialog <ActionDialog
confirmButtonState={removeCustomerOpts.status} confirmButtonState={removeCustomerOpts.status}
onClose={() => navigate(customerUrl(id), true)} onClose={() =>
navigate(customerUrl(id), { replace: true })
}
onConfirm={() => removeCustomer()} onConfirm={() => removeCustomer()}
title={intl.formatMessage({ title={intl.formatMessage({
defaultMessage: "Delete Customer", defaultMessage: "Delete Customer",

View file

@ -74,7 +74,7 @@ export const SaleCreateView: React.FC<SaleCreateProps> = ({ params }) => {
defaultMessage: "Successfully created sale" defaultMessage: "Successfully created sale"
}) })
}); });
navigate(saleUrl(data.saleCreate.sale.id), true); navigate(saleUrl(data.saleCreate.sale.id), { replace: true });
} }
}; };

View file

@ -159,7 +159,7 @@ export const SaleDetails: React.FC<SaleDetailsProps> = ({ id, params }) => {
status: "success", status: "success",
text: intl.formatMessage(messages.saleDetailsSaleDeleteDialog) text: intl.formatMessage(messages.saleDetailsSaleDeleteDialog)
}); });
navigate(saleListUrl(), true); navigate(saleListUrl(), { replace: true });
} }
}; };

View file

@ -79,7 +79,7 @@ export const VoucherCreateView: React.FC<VoucherCreateProps> = ({ params }) => {
defaultMessage: "Successfully created voucher" defaultMessage: "Successfully created voucher"
}) })
}); });
navigate(voucherUrl(data.voucherCreate.voucher.id), true); navigate(voucherUrl(data.voucherCreate.voucher.id), { replace: true });
} }
}; };

View file

@ -169,7 +169,7 @@ export const VoucherDetails: React.FC<VoucherDetailsProps> = ({
defaultMessage: "Deleted voucher" defaultMessage: "Deleted voucher"
}) })
}); });
navigate(voucherListUrl(), true); navigate(voucherListUrl(), { replace: true });
} }
}; };

View file

@ -2,8 +2,11 @@ import useRouter from "use-react-router";
export type UseNavigatorResult = ( export type UseNavigatorResult = (
url: string, url: string,
replace?: boolean, opts?: {
preserveQs?: boolean replace?: boolean;
preserveQs?: boolean;
resetScroll?: boolean;
}
) => void; ) => void;
function useNavigator(): UseNavigatorResult { function useNavigator(): UseNavigatorResult {
const { const {
@ -11,15 +14,19 @@ function useNavigator(): UseNavigatorResult {
history history
} = useRouter(); } = useRouter();
return (url: string, replace = false, preserveQs = false) => { return (
url: string,
{ replace = false, preserveQs = false, resetScroll = true } = {}
) => {
const targetUrl = preserveQs ? url + search : url; const targetUrl = preserveQs ? url + search : url;
if (replace) { if (replace) {
history.replace(targetUrl); history.replace(targetUrl);
} else { } else {
history.push(targetUrl); history.push(targetUrl);
} }
if (resetScroll) {
window.scrollTo({ behavior: "smooth", top: 0 }); window.scrollTo({ behavior: "smooth", top: 0 });
}
}; };
} }

View file

@ -5,5 +5,5 @@ import useNavigator from "./useNavigator";
export const usePaginationReset = (url: string, rowNumber: number) => { export const usePaginationReset = (url: string, rowNumber: number) => {
const navigate = useNavigator(); const navigate = useNavigator();
useEffect(() => navigate(url, true), [rowNumber]); useEffect(() => navigate(url, { replace: true }), [rowNumber]);
}; };

View file

@ -52,7 +52,7 @@ function usePaginator() {
after: pageInfo.endCursor, after: pageInfo.endCursor,
before: undefined before: undefined
}), }),
true { replace: true }
); );
const loadPreviousPage = () => const loadPreviousPage = () =>
@ -63,7 +63,7 @@ function usePaginator() {
after: undefined, after: undefined,
before: pageInfo.startCursor before: pageInfo.startCursor
}), }),
true { replace: true }
); );
const newPageInfo = pageInfo const newPageInfo = pageInfo

View file

@ -77,7 +77,7 @@ const MenuDetails: React.FC<MenuDetailsProps> = ({ id, params }) => {
action: undefined, action: undefined,
id: undefined id: undefined
}), }),
true { replace: true }
); );
const handleItemClick = (id: string, type: MenuItemType) => { const handleItemClick = (id: string, type: MenuItemType) => {

View file

@ -56,7 +56,7 @@ export function handleDelete(
status: "success", status: "success",
text: intl.formatMessage(commonMessages.savedChanges) text: intl.formatMessage(commonMessages.savedChanges)
}); });
navigate(menuListUrl(), true); navigate(menuListUrl(), { replace: true });
} }
} }

View file

@ -66,7 +66,7 @@ const MenuList: React.FC<MenuListProps> = ({ params }) => {
id: undefined, id: undefined,
ids: undefined ids: undefined
}), }),
true { replace: true }
); );
const paginationState = createPaginationState(settings.rowNumber, params); const paginationState = createPaginationState(settings.rowNumber, params);

View file

@ -58,7 +58,7 @@ const OrderFulfill: React.FC<OrderFulfillProps> = ({ orderId }) => {
const [fulfillOrder, fulfillOrderOpts] = useOrderFulfill({ const [fulfillOrder, fulfillOrderOpts] = useOrderFulfill({
onCompleted: data => { onCompleted: data => {
if (data.orderFulfill.errors.length === 0) { if (data.orderFulfill.errors.length === 0) {
navigate(orderUrl(orderId), true); navigate(orderUrl(orderId), { replace: true });
notify({ notify({
status: "success", status: "success",
text: intl.formatMessage({ text: intl.formatMessage({

View file

@ -69,7 +69,7 @@ const OrderRefund: React.FC<OrderRefundProps> = ({ orderId }) => {
const [refundOrder, refundOrderOpts] = useOrderRefundMutation({ const [refundOrder, refundOrderOpts] = useOrderRefundMutation({
onCompleted: data => { onCompleted: data => {
if (data.orderRefund.errors.length === 0) { if (data.orderRefund.errors.length === 0) {
navigate(orderUrl(orderId), true); navigate(orderUrl(orderId), { replace: true });
notify({ notify({
status: "success", status: "success",
text: intl.formatMessage({ text: intl.formatMessage({
@ -86,7 +86,7 @@ const OrderRefund: React.FC<OrderRefundProps> = ({ orderId }) => {
] = useOrderFulfillmentRefundProductsMutation({ ] = useOrderFulfillmentRefundProductsMutation({
onCompleted: data => { onCompleted: data => {
if (data.orderFulfillmentRefundProducts.errors.length === 0) { if (data.orderFulfillmentRefundProducts.errors.length === 0) {
navigate(orderUrl(orderId), true); navigate(orderUrl(orderId), { replace: true });
notify({ notify({
status: "success", status: "success",
text: intl.formatMessage({ text: intl.formatMessage({

View file

@ -74,7 +74,7 @@ export const PageTypeDetails: React.FC<PageTypeDetailsProps> = ({
defaultMessage: "Page type deleted" defaultMessage: "Page type deleted"
}) })
}); });
navigate(pageTypeListUrl(), true); navigate(pageTypeListUrl(), { replace: true });
} }
} }
}); });
@ -175,7 +175,7 @@ export const PageTypeDetails: React.FC<PageTypeDetailsProps> = ({
return <NotFoundPage onBack={handleBack} />; return <NotFoundPage onBack={handleBack} />;
} }
const closeModal = () => navigate(pageTypeUrl(id), true); const closeModal = () => navigate(pageTypeUrl(id), { replace: true });
const handleSubmit = createMetadataUpdateHandler( const handleSubmit = createMetadataUpdateHandler(
data?.pageType, data?.pageType,

View file

@ -175,7 +175,8 @@ export const ProductTypeUpdate: React.FC<ProductTypeUpdateProps> = ({
return <NotFoundPage onBack={handleBack} />; return <NotFoundPage onBack={handleBack} />;
} }
const closeModal = () => navigate(productTypeUrl(id), true); const closeModal = () =>
navigate(productTypeUrl(id), { replace: true });
const handleAttributeAssignSuccess = (data: AssignProductAttribute) => { const handleAttributeAssignSuccess = (data: AssignProductAttribute) => {
if (data.productAttributeAssign.errors.length === 0) { if (data.productAttributeAssign.errors.length === 0) {
@ -217,7 +218,7 @@ export const ProductTypeUpdate: React.FC<ProductTypeUpdateProps> = ({
defaultMessage: "Product type deleted" defaultMessage: "Product type deleted"
}) })
}); });
navigate(productTypeListUrl(), true); navigate(productTypeListUrl(), { replace: true });
} }
}; };
const handleSubmit = createMetadataUpdateHandler( const handleSubmit = createMetadataUpdateHandler(

View file

@ -99,7 +99,9 @@ export const ProductImage: React.FC<ProductMediaProps> = ({
saveButtonBarState={updateResult.status} saveButtonBarState={updateResult.status}
/> />
<ActionDialog <ActionDialog
onClose={() => navigate(productImageUrl(productId, mediaId), true)} onClose={() =>
navigate(productImageUrl(productId, mediaId), { replace: true })
}
onConfirm={handleDelete} onConfirm={handleDelete}
open={params.action === "remove"} open={params.action === "remove"}
title={intl.formatMessage({ title={intl.formatMessage({

View file

@ -430,7 +430,8 @@ export const ProductUpdate: React.FC<ProductUpdateProps> = ({ id, params }) => {
productUrl(id, { productUrl(id, {
action: "assign-attribute-value", action: "assign-attribute-value",
id: attribute.id id: attribute.id
}) }),
{ resetScroll: false }
); );
const disableFormSave = const disableFormSave =
@ -619,7 +620,7 @@ export const ProductUpdate: React.FC<ProductUpdateProps> = ({ id, params }) => {
fetchReferenceProducts={searchProducts} fetchReferenceProducts={searchProducts}
fetchMoreReferenceProducts={fetchMoreReferenceProducts} fetchMoreReferenceProducts={fetchMoreReferenceProducts}
fetchMoreAttributeValues={fetchMoreAttributeValues} fetchMoreAttributeValues={fetchMoreAttributeValues}
onCloseDialog={() => navigate(productUrl(id))} onCloseDialog={() => navigate(productUrl(id), { resetScroll: false })}
onAttributeSelectBlur={searchAttributeReset} onAttributeSelectBlur={searchAttributeReset}
/> />
<ActionDialog <ActionDialog

View file

@ -100,7 +100,7 @@ const ShippingZoneDetails: React.FC<ShippingZoneDetailsProps> = ({
status: "success", status: "success",
text: intl.formatMessage(commonMessages.savedChanges) text: intl.formatMessage(commonMessages.savedChanges)
}); });
navigate(shippingZonesListUrl(), true); navigate(shippingZonesListUrl(), { replace: true });
} }
} }
}); });

View file

@ -86,7 +86,7 @@ const TranslationsAttributes: React.FC<TranslationsAttributesProps> = ({
stringifyQs({ stringifyQs({
activeField: field activeField: field
}), }),
true { replace: true }
); );
const onAttributeUpdate = (data: UpdateAttributeTranslations) => { const onAttributeUpdate = (data: UpdateAttributeTranslations) => {
if (data.attributeTranslate.errors.length === 0) { if (data.attributeTranslate.errors.length === 0) {
@ -95,7 +95,7 @@ const TranslationsAttributes: React.FC<TranslationsAttributesProps> = ({
status: "success", status: "success",
text: intl.formatMessage(commonMessages.savedChanges) text: intl.formatMessage(commonMessages.savedChanges)
}); });
navigate("?", true); navigate("?", { replace: true });
} }
}; };
const onAttributeValueUpdate = (data: UpdateAttributeValueTranslations) => { const onAttributeValueUpdate = (data: UpdateAttributeValueTranslations) => {
@ -105,11 +105,11 @@ const TranslationsAttributes: React.FC<TranslationsAttributesProps> = ({
status: "success", status: "success",
text: intl.formatMessage(commonMessages.savedChanges) text: intl.formatMessage(commonMessages.savedChanges)
}); });
navigate("?", true); navigate("?", { replace: true });
} }
}; };
const onDiscard = () => { const onDiscard = () => {
navigate("?", true); navigate("?", { replace: true });
}; };
return ( return (

View file

@ -49,7 +49,7 @@ const TranslationsCategories: React.FC<TranslationsCategoriesProps> = ({
stringifyQs({ stringifyQs({
activeField: field activeField: field
}), }),
true { replace: true }
); );
const onUpdate = (data: UpdateCategoryTranslations) => { const onUpdate = (data: UpdateCategoryTranslations) => {
if (data.categoryTranslate.errors.length === 0) { if (data.categoryTranslate.errors.length === 0) {
@ -58,11 +58,11 @@ const TranslationsCategories: React.FC<TranslationsCategoriesProps> = ({
status: "success", status: "success",
text: intl.formatMessage(commonMessages.savedChanges) text: intl.formatMessage(commonMessages.savedChanges)
}); });
navigate("?", true); navigate("?", { replace: true });
} }
}; };
const onDiscard = () => { const onDiscard = () => {
navigate("?", true); navigate("?", { replace: true });
}; };
return ( return (

View file

@ -49,7 +49,7 @@ const TranslationsCollections: React.FC<TranslationsCollectionsProps> = ({
stringifyQs({ stringifyQs({
activeField: field activeField: field
}), }),
true { replace: true }
); );
const onUpdate = (data: UpdateCollectionTranslations) => { const onUpdate = (data: UpdateCollectionTranslations) => {
if (data.collectionTranslate.errors.length === 0) { if (data.collectionTranslate.errors.length === 0) {
@ -58,11 +58,11 @@ const TranslationsCollections: React.FC<TranslationsCollectionsProps> = ({
status: "success", status: "success",
text: intl.formatMessage(commonMessages.savedChanges) text: intl.formatMessage(commonMessages.savedChanges)
}); });
navigate("?", true); navigate("?", { replace: true });
} }
}; };
const onDiscard = () => { const onDiscard = () => {
navigate("?", true); navigate("?", { replace: true });
}; };
const translation = collectionTranslations?.data?.translation; const translation = collectionTranslations?.data?.translation;

View file

@ -50,7 +50,7 @@ const TranslationsEntities: React.FC<TranslationsEntitiesProps> = ({
stringifyQs({ stringifyQs({
tab: TranslatableEntities.categories tab: TranslatableEntities.categories
}), }),
true { replace: true }
); );
} }

View file

@ -51,7 +51,7 @@ const TranslationsPages: React.FC<TranslationsPagesProps> = ({
stringifyQs({ stringifyQs({
activeField: field activeField: field
}), }),
true { replace: true }
); );
const onUpdate = (errors: unknown[]) => { const onUpdate = (errors: unknown[]) => {
@ -61,12 +61,12 @@ const TranslationsPages: React.FC<TranslationsPagesProps> = ({
status: "success", status: "success",
text: intl.formatMessage(commonMessages.savedChanges) text: intl.formatMessage(commonMessages.savedChanges)
}); });
navigate("?", true); navigate("?", { replace: true });
} }
}; };
const onDiscard = () => { const onDiscard = () => {
navigate("?", true); navigate("?", { replace: true });
}; };
return ( return (

View file

@ -54,7 +54,7 @@ const TranslationsProductVariants: React.FC<TranslationsProductVariantsProps> =
stringifyQs({ stringifyQs({
activeField: field activeField: field
}), }),
true { replace: true }
); );
const onUpdate = (errors: unknown[]) => { const onUpdate = (errors: unknown[]) => {
@ -64,12 +64,12 @@ const TranslationsProductVariants: React.FC<TranslationsProductVariantsProps> =
status: "success", status: "success",
text: intl.formatMessage(commonMessages.savedChanges) text: intl.formatMessage(commonMessages.savedChanges)
}); });
navigate("?", true); navigate("?", { replace: true });
} }
}; };
const onDiscard = () => { const onDiscard = () => {
navigate("?", true); navigate("?", { replace: true });
}; };
return ( return (

View file

@ -52,7 +52,7 @@ const TranslationsProducts: React.FC<TranslationsProductsProps> = ({
stringifyQs({ stringifyQs({
activeField: field activeField: field
}), }),
true { replace: true }
); );
const onUpdate = (errors: unknown[]) => { const onUpdate = (errors: unknown[]) => {
@ -62,12 +62,12 @@ const TranslationsProducts: React.FC<TranslationsProductsProps> = ({
status: "success", status: "success",
text: intl.formatMessage(commonMessages.savedChanges) text: intl.formatMessage(commonMessages.savedChanges)
}); });
navigate("?", true); navigate("?", { replace: true });
} }
}; };
const onDiscard = () => { const onDiscard = () => {
navigate("?", true); navigate("?", { replace: true });
}; };
return ( return (

View file

@ -48,7 +48,7 @@ const TranslationsSales: React.FC<TranslationsSalesProps> = ({
stringifyQs({ stringifyQs({
activeField: field activeField: field
}), }),
true { replace: true }
); );
const onUpdate = (data: UpdateSaleTranslations) => { const onUpdate = (data: UpdateSaleTranslations) => {
if (data.saleTranslate.errors.length === 0) { if (data.saleTranslate.errors.length === 0) {
@ -57,11 +57,11 @@ const TranslationsSales: React.FC<TranslationsSalesProps> = ({
status: "success", status: "success",
text: intl.formatMessage(commonMessages.savedChanges) text: intl.formatMessage(commonMessages.savedChanges)
}); });
navigate("?", true); navigate("?", { replace: true });
} }
}; };
const onDiscard = () => { const onDiscard = () => {
navigate("?", true); navigate("?", { replace: true });
}; };
return ( return (

View file

@ -48,7 +48,7 @@ const TranslationsShippingMethod: React.FC<TranslationsShippingMethodProps> = ({
stringifyQs({ stringifyQs({
activeField: field activeField: field
}), }),
true { replace: true }
); );
const onUpdate = (data: UpdateShippingMethodTranslations) => { const onUpdate = (data: UpdateShippingMethodTranslations) => {
if (data.shippingPriceTranslate.errors.length === 0) { if (data.shippingPriceTranslate.errors.length === 0) {
@ -57,11 +57,11 @@ const TranslationsShippingMethod: React.FC<TranslationsShippingMethodProps> = ({
status: "success", status: "success",
text: intl.formatMessage(commonMessages.savedChanges) text: intl.formatMessage(commonMessages.savedChanges)
}); });
navigate("?", true); navigate("?", { replace: true });
} }
}; };
const onDiscard = () => { const onDiscard = () => {
navigate("?", true); navigate("?", { replace: true });
}; };
return ( return (

View file

@ -49,7 +49,7 @@ const TranslationsVouchers: React.FC<TranslationsVouchersProps> = ({
stringifyQs({ stringifyQs({
activeField: field activeField: field
}), }),
true { replace: true }
); );
const onUpdate = (data: UpdateVoucherTranslations) => { const onUpdate = (data: UpdateVoucherTranslations) => {
if (data.voucherTranslate.errors.length === 0) { if (data.voucherTranslate.errors.length === 0) {
@ -58,11 +58,11 @@ const TranslationsVouchers: React.FC<TranslationsVouchersProps> = ({
status: "success", status: "success",
text: intl.formatMessage(commonMessages.savedChanges) text: intl.formatMessage(commonMessages.savedChanges)
}); });
navigate("?", true); navigate("?", { replace: true });
} }
}; };
const onDiscard = () => { const onDiscard = () => {
navigate("?", true); navigate("?", { replace: true });
}; };
return ( return (

View file

@ -28,7 +28,7 @@ function createDialogActionHandlers<
id: undefined, id: undefined,
ids: undefined ids: undefined
}), }),
true { replace: true }
); );
const open = (action: TAction, newParams?: TParams) => const open = (action: TAction, newParams?: TParams) =>
navigate( navigate(

View file

@ -18,7 +18,7 @@ function createSortHandler<T extends string>(
...getSortUrlVariables(field, params), ...getSortUrlVariables(field, params),
...DEFAULT_INITIAL_PAGINATION_DATA ...DEFAULT_INITIAL_PAGINATION_DATA
}), }),
true { replace: true }
); );
} }