Skip to content

Commit

Permalink
chore: use URL func to create url
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhart1o1 committed Feb 15, 2024
1 parent f7d07c8 commit 99ae14d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
43 changes: 24 additions & 19 deletions apps/pay/components/Memo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { ACTIONS, ACTION_TYPE } from "../../app/reducer"

import styles from "./memo.module.css"

import { extractSearchParams } from "@/utils/utils"

interface Props {
state: React.ComponentState
dispatch: React.Dispatch<ACTION_TYPE>
Expand All @@ -17,30 +15,37 @@ interface Props {
const Memo = ({ state, dispatch }: Props) => {
const searchParams = useSearchParams()
const { username } = useParams()
const { amount, sats, unit, memo, display } = extractSearchParams(searchParams)
const amount = searchParams.get("amount") || "0"
const sats = searchParams.get("sats") || "0"
const display = searchParams.get("display") || "USD"
const memo = searchParams.get("memo") || ""

const unit = searchParams.get("unit")
const [openModal, setOpenModal] = React.useState<boolean>(false)
const [note, setNote] = React.useState<string>(memo?.toString() || "")

const handleSetMemo = () => {
if (unit === "SAT" || unit === "CENT") {
window.history.pushState(
{},
"",
`${username}?${new URLSearchParams({
amount: amount,
sats: sats,
unit: unit,
memo: note,
display,
}).toString()}`,
)
const params = new URLSearchParams({
amount,
sats,
unit,
memo: note,
display,
})

const currentUrl = new URL(window.location.toString())
currentUrl.search = params.toString()
window.history.pushState({}, "", currentUrl.toString())
} else {
window.history.pushState(
{},
"",
`${username}?${new URLSearchParams({ memo: note, display }).toString()}`,
)
const params = new URLSearchParams({
memo: note,
display,
})

const currentUrl = new URL(window.location.toString())
currentUrl.search = params.toString()
window.history.pushState({}, "", currentUrl.toString())
}
handleClose()
}
Expand Down
44 changes: 27 additions & 17 deletions apps/pay/components/ParsePOSPayment/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,19 @@ function ParsePayment({
username: initialUsername,
}
if (initialQuery !== newQuery) {
const newUrl = `${username}?${new URLSearchParams({
const params = new URLSearchParams({
amount: initialAmount,
sats: initialSats,
unit: initialUnit,
memo: memo ?? "",
display: initialDisplay,
currency: defaultWalletCurrency,
}).toString()}`
router.replace(newUrl, {
})
const newUrl = new URL(window.location.toString())
newUrl.pathname = `/${username}`
newUrl.search = params.toString()

router.replace(newUrl.toString(), {
scroll: true,
})
}
Expand Down Expand Up @@ -144,16 +148,19 @@ function ParsePayment({
const toggleCurrency = () => {
const newUnit = unit === AmountUnit.Sat ? AmountUnit.Cent : AmountUnit.Sat
prevUnit.current = (unit as AmountUnit) || AmountUnit.Cent
router.replace(
`${username}?${new URLSearchParams({
currency: defaultWalletCurrency,
unit: newUnit,
memo,
display,
amount,
sats,
}).toString()}`,
)
const params = new URLSearchParams({
currency: defaultWalletCurrency,
unit: newUnit,
memo,
display,
amount,
sats,
})

const newUrl = new URL(window.location.toString())
newUrl.pathname = `/${username}`
newUrl.search = params.toString()
router.replace(newUrl.toString())
}

// Update Params From Current Amount
Expand Down Expand Up @@ -200,14 +207,17 @@ function ParsePayment({
amount: amt,
sats: satsAmt,
currency: defaultWalletCurrency,
unit: unit,
memo: memo,
display: display,
unit,
memo,
display,
}

const initialQuery = extractSearchParams(searchParams)
if (initialQuery !== newQuery && !skipRouterPush) {
router.replace(`${username}?${new URLSearchParams(newQuery).toString()}`)
const newUrl = new URL(window.location.toString())
newUrl.pathname = `/${username}`
newUrl.search = new URLSearchParams(newQuery).toString()
router.replace(newUrl.toString())
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down

0 comments on commit 99ae14d

Please sign in to comment.