Skip to content

Commit f0a8fa7

Browse files
(Admin) Jonathan Ruda(Admin) Jonathan Ruda
authored andcommitted
Implement same logic on missing flows
1 parent 9b11ad4 commit f0a8fa7

2 files changed

Lines changed: 87 additions & 24 deletions

File tree

client/components/mma/accountoverview/updateAmount/SupporterPlusUpdateAmountForm.tsx

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ export const SupporterPlusUpdateAmountForm = (
131131
const [otherAmount, setOtherAmount] = useState<number | null>(
132132
defaultOtherAmount,
133133
);
134+
const [otherAmountInput, setOtherAmountInput] = useState<string>(
135+
defaultOtherAmount?.toString() ?? '',
136+
);
134137
const [isOtherAmountSelected, setIsOtherAmountSelected] =
135138
useState<boolean>(false);
136139
const [hasInteractedWithOtherAmount, setHasInteractedWithOtherAmount] =
@@ -230,6 +233,40 @@ export const SupporterPlusUpdateAmountForm = (
230233
}
231234
};
232235

236+
const onChangeHandler = (event: React.ChangeEvent<HTMLInputElement>) => {
237+
const next = event.target.value.trim();
238+
239+
if (next === '') {
240+
setOtherAmountInput('');
241+
setOtherAmount(null);
242+
return;
243+
}
244+
245+
// Whole-string match: 1+ digits, optional '.' or ',', and 0–2 fractional digits (allows '2', '2.', '2,', '2.5', '2,50').
246+
if (/^\d+(?:[.,]\d{0,2})?$/.test(next)) {
247+
const normalizedValue = next.replace(',', '.');
248+
249+
setOtherAmountInput(normalizedValue);
250+
setOtherAmount(Number(normalizedValue));
251+
}
252+
};
253+
254+
const onBlurHandler = () => {
255+
let processed = otherAmountInput;
256+
257+
if (processed.endsWith('.')) {
258+
processed = processed.slice(0, -1);
259+
}
260+
261+
// Remove leading zeros
262+
if (processed !== '' && processed !== '0') {
263+
processed = processed.replace(/^0+(?=\d)/, '');
264+
}
265+
266+
setOtherAmountInput(processed);
267+
setOtherAmount(processed === '' ? null : Number(processed));
268+
};
269+
233270
if (showUpdateLoader) {
234271
return <DefaultLoadingView loadingMessage="Updating..." />;
235272
}
@@ -335,15 +372,11 @@ export const SupporterPlusUpdateAmountForm = (
335372
errorMessage) ||
336373
undefined
337374
}
338-
type="number"
339-
value={otherAmount?.toString() || ''}
340-
onChange={(event) =>
341-
setOtherAmount(
342-
event.target.value
343-
? Number(event.target.value)
344-
: null,
345-
)
346-
}
375+
type="text"
376+
inputMode="decimal"
377+
value={otherAmountInput}
378+
onChange={onChangeHandler}
379+
onBlur={onBlurHandler}
347380
/>
348381
</div>
349382
)}

client/components/mma/upgrade/UpgradeSupportAmountForm.tsx

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export const UpgradeSupportAmountForm = ({
118118
const [otherAmountSelected, setOtherAmountSelected] = useState<
119119
number | null
120120
>(null);
121+
const [otherAmountInput, setOtherAmountInput] = useState<string>('');
121122

122123
const [hasInteractedWithOtherAmount, setHasInteractedWithOtherAmount] =
123124
useState<boolean>(false);
@@ -151,6 +152,45 @@ export const UpgradeSupportAmountForm = ({
151152
priceConfig.minAmount,
152153
]);
153154

155+
const onChangeHandler = (event: React.ChangeEvent<HTMLInputElement>) => {
156+
const next = event.target.value.trim();
157+
158+
if (next === '') {
159+
setOtherAmountInput('');
160+
setOtherAmountSelected(null);
161+
setChosenAmount(null);
162+
return;
163+
}
164+
165+
// Whole-string match: 1+ digits, optional '.' or ',', and 0–2 fractional digits (allows '2', '2.', '2,', '2.5', '2,50').
166+
if (/^\d+(?:[.,]\d{0,2})?$/.test(next)) {
167+
const normalizedValue = next.replace(',', '.');
168+
169+
setOtherAmountInput(normalizedValue);
170+
setOtherAmountSelected(Number(normalizedValue));
171+
setChosenAmount(Number(normalizedValue));
172+
}
173+
174+
setContinuedToConfirmation(false);
175+
};
176+
177+
const onBlurHandler = () => {
178+
let processed = otherAmountInput;
179+
180+
if (processed.endsWith('.')) {
181+
processed = processed.slice(0, -1);
182+
}
183+
184+
// Remove leading zeros
185+
if (processed !== '' && processed !== '0') {
186+
processed = processed.replace(/^0+(?=\d)/, '');
187+
}
188+
189+
setOtherAmountInput(processed);
190+
setOtherAmountSelected(processed === '' ? null : Number(processed));
191+
setChosenAmount(processed === '' ? null : Number(processed));
192+
};
193+
154194
return (
155195
<>
156196
<Stack space={3}>
@@ -208,23 +248,13 @@ export const UpgradeSupportAmountForm = ({
208248
errorMessage) ||
209249
undefined
210250
}
211-
type="number"
251+
type="text"
252+
inputMode="decimal"
212253
width={30}
213-
value={otherAmountSelected?.toString() || ''}
254+
value={otherAmountInput}
255+
onChange={onChangeHandler}
256+
onBlur={onBlurHandler}
214257
onWheel={(event) => event.currentTarget.blur()}
215-
onChange={(event) => {
216-
setChosenAmount(
217-
event.target.value
218-
? Number(event.target.value)
219-
: null,
220-
);
221-
setOtherAmountSelected(
222-
event.target.value
223-
? Number(event.target.value)
224-
: null,
225-
);
226-
setContinuedToConfirmation(false);
227-
}}
228258
/>
229259
</div>
230260
)}

0 commit comments

Comments
 (0)