Skip to content

Commit a331fce

Browse files
bug: QR code redirect fix (#118)
* fix: update seed generation script * feat: update qr code scan workflow
1 parent 1d57cb4 commit a331fce

File tree

2 files changed

+42
-49
lines changed

2 files changed

+42
-49
lines changed

client/src/pages/ApplyReferral/ApplyReferral.tsx

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef, useState } from 'react';
1+
import { useCallback, useEffect, useRef, useState } from 'react';
22

33
import { Html5Qrcode } from 'html5-qrcode';
44
import { useNavigate } from 'react-router-dom';
@@ -19,6 +19,39 @@ export default function ApplyReferral() {
1919
const scannerRef = useRef<Html5Qrcode | null>(null);
2020
const readerRef = useRef<HTMLDivElement | null>(null);
2121

22+
// Function to handle successful QR code scan
23+
const onScanSuccess = useCallback((decodedText: string) => {
24+
if (scannerRef.current) {
25+
scannerRef.current
26+
.stop()
27+
.then(() => {
28+
scannerRef.current?.clear();
29+
console.log('Scanner stopped after successful scan.');
30+
})
31+
.catch(error =>
32+
console.error('Failed to stop scanner:', error)
33+
);
34+
}
35+
setIsScanning(false);
36+
37+
// Extract referral code from scanned text
38+
const code = decodedText.trim();
39+
40+
if (!code) {
41+
alert('Invalid QR Code. Could not extract referral code.');
42+
return;
43+
}
44+
45+
// Clear any existing survey data and navigate to survey with the referral code
46+
clearSurvey();
47+
navigate(`/survey?ref=${code}`);
48+
}, []);
49+
50+
// Function to handle QR code scan failure
51+
const onScanFailure = useCallback((error: string) => {
52+
console.warn(`QR Code scan error: ${error}`);
53+
}, []);
54+
2255
// Effect to initialize the QR scanner
2356
// This effect runs when the component mounts and when isScanning changes
2457
// Defaults to back camera unless there is no back camera
@@ -67,36 +100,7 @@ export default function ApplyReferral() {
67100
}
68101
}
69102
};
70-
}, [isScanning]);
71-
72-
// Function to handle successful QR code scan
73-
const onScanSuccess = (decodedText: string) => {
74-
if (scannerRef.current) {
75-
scannerRef.current
76-
.stop()
77-
.then(() => {
78-
scannerRef.current?.clear();
79-
console.log('Scanner stopped after successful scan.');
80-
})
81-
.catch(error =>
82-
console.error('Failed to stop scanner:', error)
83-
);
84-
}
85-
setIsScanning(false);
86-
87-
// Check if the scanned text is a valid URL
88-
try {
89-
const url = new URL(decodedText);
90-
window.location.href = url.href; // Redirect user to the scanned URL
91-
} catch (error) {
92-
alert('Invalid QR Code. Please scan a valid link.');
93-
}
94-
};
95-
96-
// Function to handle QR code scan failure
97-
const onScanFailure = (error: string) => {
98-
console.warn(`QR Code scan error: ${error}`);
99-
};
103+
}, [isScanning, onScanSuccess, onScanFailure]);
100104

101105
// Function to handle referral code submission
102106
const handleStartSurvey = async () => {

scripts/generateSeeds.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env tsx
22
/**
3-
* NOTE: Make sure to set CLIENT_URL in .env to the correct deployment URL
43
* Script to generate N seeds for a given location
54
* Usage: npm run generate-seeds -- <hubName|objectId> <count>
65
* Example: npm run generate-seeds -- "Main Hub" 10
@@ -49,9 +48,9 @@ function generateTimestampFilename(locationName: string, outputDir: string): str
4948
return path.join(outputDir, filename);
5049
}
5150

52-
async function generateQRCodeBuffer(surveyCode: string, baseUrl: string, qrSize: number): Promise<Buffer> {
53-
const qrUrl = `${baseUrl}/survey?ref=${surveyCode}`;
54-
const qrDataUrl = await QRCode.toDataURL(qrUrl, {
51+
async function generateQRCodeBuffer(surveyCode: string, qrSize: number): Promise<Buffer> {
52+
// Encode only the referral code (no URL) so QR codes work across any deployment
53+
const qrDataUrl = await QRCode.toDataURL(surveyCode, {
5554
width: qrSize,
5655
margin: 1,
5756
errorCorrectionLevel: 'M',
@@ -63,8 +62,7 @@ async function addQRCodePage(
6362
doc: any,
6463
surveyCode: string,
6564
locationName: string,
66-
baseUrl: string,
67-
isFirstPage: boolean,
65+
isFirstPage: boolean
6866
): Promise<void> {
6967
if (!isFirstPage) {
7068
doc.addPage();
@@ -136,8 +134,8 @@ async function addQRCodePage(
136134
// QR Code and Referral Code
137135
const qrSize = 100;
138136
const qrX = (pageWidth - qrSize) / 2;
139-
140-
const qrBuffer = await generateQRCodeBuffer(surveyCode, baseUrl, qrSize);
137+
138+
const qrBuffer = await generateQRCodeBuffer(surveyCode, qrSize);
141139
doc.image(qrBuffer, qrX, currentY, {
142140
width: qrSize,
143141
height: qrSize,
@@ -219,7 +217,7 @@ async function generatePDF(seeds: any[], locationName: string): Promise<void> {
219217

220218
// Generate one page per seed
221219
for (let i = 0; i < seeds.length; i++) {
222-
await addQRCodePage(doc, seeds[i].surveyCode, locationName, baseUrl, i === 0);
220+
await addQRCodePage(doc, seeds[i].surveyCode, locationName, i === 0);
223221
}
224222

225223
doc.end();
@@ -331,15 +329,6 @@ async function generateSeeds(locationIdentifier: string, count: number): Promise
331329
}
332330
}
333331

334-
// Require CLIENT_URL to be set
335-
const baseUrl = process.env.CLIENT_URL;
336-
if (!baseUrl) {
337-
console.error('\n✗ Error: CLIENT_URL environment variable is not set');
338-
console.error(' Please set CLIENT_URL in your .env file before running this script.');
339-
console.error(' Example: CLIENT_URL=https://your-domain.com\n');
340-
process.exit(1);
341-
}
342-
343332
// Parse command line arguments
344333
const args = process.argv.slice(2);
345334

0 commit comments

Comments
 (0)