Skip to content

Commit

Permalink
Ensure cost uses latest me.name and not a cached name from session
Browse files Browse the repository at this point in the history
disable submit button while form is submitting
  • Loading branch information
SatsAllDay committed Oct 26, 2023
1 parent fcc41ca commit 547b4ba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions api/resolvers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ export const getNameCost = async ({ name, me, models }) => {
if (me?.name === name) {
return 0
}

if (me && (await models.user.findUnique({ where: { id: me.id } })).name === name) {
// This handles the case where a user has changed their nym in the current session, so `me.name` is old
return 0
}

const distanceResult = await models.$queryRawUnsafe('select name, levenshtein(name, $1) as dist from users where id <> $2 order by dist asc limit 1;', name, me?.id ?? -1)
const { dist } = distanceResult[0]
let cost = 100000 / Math.pow(10, dist - 1)
Expand Down Expand Up @@ -539,6 +545,8 @@ export default {
models.$queryRawUnsafe('SELECT 1 FROM edit_nym($1::INTEGER, $2::TEXT, $3::BIGINT);', me.id, name, cost),
{ models, lnd, me, hash, hmac }
)
// update name in server session
me.name = name
return name
} catch (error) {
if (error.code === 'P2002') {
Expand Down
2 changes: 1 addition & 1 deletion components/fee-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export const EditNymFeeButton = ({ onClick }) => {
return (
<div className={styles.feeButton}>
<ActionTooltip overlayText={numWithUnits(cost, { abbreviate: false })}>
<SubmitButton variant='secondary' onClick={onClick}>save{cost > 0 && show && <small> {numWithUnits(cost, { abbreviate: false, format: true })}</small>}</SubmitButton>
<SubmitButton variant='secondary' onClick={onClick} disabled={formik?.isSubmitting}>save{cost > 0 && show && <small> {numWithUnits(cost, { abbreviate: false, format: true })}</small>}</SubmitButton>
</ActionTooltip>
{cost > 0 && show &&
<Info>
Expand Down

0 comments on commit 547b4ba

Please sign in to comment.