Skip to content

Commit

Permalink
[APP-1007]: Add parse ref code from ref link (#5252)
Browse files Browse the repository at this point in the history
* ref link pasting working

* set max length to 7

* rm comment

---------

Co-authored-by: Ben Goldberg <[email protected]>
  • Loading branch information
walmat and benisgold authored Dec 14, 2023
1 parent b0d8a26 commit 6797a8a
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/screens/points/content/ReferralContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ import { ActionButton } from '@/screens/points/components/ActionButton';
import { PointsIconAnimation } from '../components/PointsIconAnimation';
import { usePointsReferralCode } from '@/resources/points';
import { analyticsV2 } from '@/analytics';
import Clipboard from '@react-native-community/clipboard';

const parseReferralCodeFromLink = (code: string) => {
if (!code.startsWith('https://rainbow.me/points?ref=')) return;

const [, refCode] = code.split('=');
if (!refCode) return;

const trimmed = refCode.replace(/-/g, '').slice(0, 6).toLocaleUpperCase();

return trimmed;
};

export default function ReferralContent() {
const { accentColor } = useAccountAccentColor();
Expand All @@ -61,7 +73,7 @@ export default function ReferralContent() {

const validateReferralCode = useCallback(
async (code: string) => {
if (code.length !== 6) return;
if (code.length !== 6) return false;
const res = await metadataPOSTClient.validateReferral({
code,
});
Expand All @@ -87,7 +99,10 @@ export default function ReferralContent() {
setReferralCode(code);
textInputRef.current?.blur();
haptics.notificationSuccess();
return true;
}

return false;
},
[deeplinked]
);
Expand Down Expand Up @@ -178,8 +193,23 @@ export default function ReferralContent() {
});

const onChangeText = useCallback(
(code: string) => {
async (code: string) => {
if (goingBack) return;
if (code === 'https:/') {
const url = await Clipboard.getString();
const codeFromUrl = parseReferralCodeFromLink(url);
if (codeFromUrl) {
const formattedCode =
codeFromUrl.slice(0, 3) + '-' + codeFromUrl.slice(3, 7);
setReferralCodeDisplay(formattedCode);
if (formattedCode.length !== 7) {
setStatus('incomplete');
} else {
validateReferralCode(codeFromUrl);
}
}
return;
}

const rawCode = code.replace(/-/g, '').slice(0, 6).toLocaleUpperCase();
let formattedCode = rawCode;
Expand Down

0 comments on commit 6797a8a

Please sign in to comment.