Skip to content

Commit

Permalink
fix: urls formed by get url filters function (#1473)
Browse files Browse the repository at this point in the history
* fix: urls formed by get url filters function

* fix: prevent navigation and add function to reuse
  • Loading branch information
andyesp authored Dec 20, 2023
1 parent 325b5eb commit 43dd15f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/components/Common/Typography/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import classNames from 'classnames'
import { withPrefix } from 'gatsby'

import { toGovernancePathname } from '../../../helpers/browser'
import { navigate } from '../../../utils/locations'

import './Link.css'
Expand Down Expand Up @@ -30,7 +31,7 @@ export default function Link({ target, rel, href, onClick, className, ...props }

const isBlank = e.currentTarget.target === TARGET_BLANK
if (isLocal && href && !isBlank && !isMetaClick(e)) {
const internalPath = href.replace('/governance', '')
const internalPath = toGovernancePathname(href)
e.preventDefault()
navigate(internalPath)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ export default function GrantRequestDueDiligenceSection({
key={`${item.concept}-${index}`}
title={item.concept}
subtitle={t('page.proposal_view.grant.breakdown_subtitle', { duration: item.duration })}
extra={Number(item.estimatedBudget).toLocaleString(undefined, CURRENCY_FORMAT_OPTIONS)}
extra={Number(item.estimatedBudget).toLocaleString(
undefined,
CURRENCY_FORMAT_OPTIONS as Intl.NumberFormatOptions
)}
onClick={() => {
setSelectedBudgetBreakdownConcept(item)
setModalOpen(true)
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export function scrollToAnchor(anchor: string, pixelsOverAnchor = 0) {
})
}
}

export function toGovernancePathname(pathname: string) {
return pathname.replace('/governance', '')
}
12 changes: 9 additions & 3 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FormatNumberOptions } from 'react-intl'

import { ChainId } from '@dcl/schemas/dist/dapps/chain-id'
import logger from 'decentraland-gatsby/dist/entities/Development/logger'
import isEthereumAddress from 'validator/lib/isEthereumAddress'
Expand All @@ -6,13 +8,15 @@ import isURL from 'validator/lib/isURL'
import { DEFAULT_CHAIN_ID } from '../constants'
import { clientEnv } from '../utils/clientEnv'

import { toGovernancePathname } from './browser'

export const CURRENCY_FORMAT_OPTIONS = {
style: 'currency',
currency: 'USD',
maximumFractionDigits: 0,
} as any
} as FormatNumberOptions

export function inBackground(fun: () => Promise<any>) {
export function inBackground(fun: () => Promise<unknown>) {
Promise.resolve()
.then(fun)
.then((result) => logger.log('Completed background task', { result: JSON.stringify(result) }))
Expand Down Expand Up @@ -53,7 +57,8 @@ export function getUrlFilters<T>(filterKey: string, params: URLSearchParams, val
newParams.delete('subtype')
}
const stringParams = newParams.toString()
return `${location.pathname}${stringParams === '' ? '' : '?' + stringParams}`
const pathname = toGovernancePathname(location.pathname)
return `${pathname}${stringParams === '' ? '' : '?' + stringParams}`
}

export const fetchWithTimeout = async (url: string, timeout = 10000, options?: RequestInit) => {
Expand All @@ -75,6 +80,7 @@ export const fetchWithTimeout = async (url: string, timeout = 10000, options?: R

export const isHttpsURL = (url: string) => isURL(url, { protocols: ['https'], require_protocol: true })

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function disableOnWheelInput(event: any) {
// Prevent the input value change
event.target.blur()
Expand Down
14 changes: 9 additions & 5 deletions src/hooks/usePreventNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useRef } from 'react'

import { globalHistory, useLocation } from '@reach/router'

import { toGovernancePathname } from '../helpers/browser'
import locations, { navigate } from '../utils/locations'

import useFormatMessage from './useFormatMessage'
Expand All @@ -16,7 +17,8 @@ function usePreventNavigation(isActive: boolean) {
event.preventDefault()
event.returnValue = ''
} else if (!window.confirm(t('navigation.exit'))) {
navigate(`${currentLocation.pathname}${currentLocation.search}`)
const pathname = toGovernancePathname(currentLocation.pathname)
navigate(`${pathname}${currentLocation.search}`)
} else {
confirmBack.current = true
navigate(location)
Expand All @@ -31,17 +33,19 @@ function usePreventNavigation(isActive: boolean) {

window.addEventListener('beforeunload', handleBeforeUnload)

const unsuscribe = globalHistory.listen(({ action, location }) => {
const unsubscribe = globalHistory.listen(({ action, location }) => {
const pathname = toGovernancePathname(location.pathname)

if (
isActive &&
(action === 'POP' || (action === 'PUSH' && location.pathname === locations.proposals() && !confirmBack.current))
(action === 'POP' || (action === 'PUSH' && pathname === locations.proposals() && !confirmBack.current))
) {
preventNavigation(`${location.pathname}${location.search}`)
preventNavigation(`${pathname}${location.search}`)
}
})

return () => {
unsuscribe()
unsubscribe()
window.removeEventListener('beforeunload', handleBeforeUnload)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down

0 comments on commit 43dd15f

Please sign in to comment.