Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[APP-1007]: Add parse ref code from ref link #5252

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -176,8 +191,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
Loading