fix: qr generator preview now shows dynamically generated shortlink instead of destination URL
This commit is contained in:
@@ -12,13 +12,15 @@ export default function CreateQRPage() {
|
|||||||
const [destinationUrl, setDestinationUrl] = useState('https://ck-qr.com');
|
const [destinationUrl, setDestinationUrl] = useState('https://ck-qr.com');
|
||||||
const [qrName, setQrName] = useState('');
|
const [qrName, setQrName] = useState('');
|
||||||
const [customSlug, setCustomSlug] = useState('');
|
const [customSlug, setCustomSlug] = useState('');
|
||||||
|
const [randomSlug, setRandomSlug] = useState('');
|
||||||
const [isGenerating, setIsGenerating] = useState(false);
|
const [isGenerating, setIsGenerating] = useState(false);
|
||||||
|
const baseUrl = typeof window !== 'undefined' ? (process.env.NEXT_PUBLIC_APP_URL || 'https://qr.houseofwebsites.ai') : 'https://qr.houseofwebsites.ai';
|
||||||
|
|
||||||
const [qrOptions, setQrOptions] = useState<Options>({
|
const [qrOptions, setQrOptions] = useState<Options>({
|
||||||
width: 300,
|
width: 300,
|
||||||
height: 300,
|
height: 300,
|
||||||
type: 'svg',
|
type: 'svg',
|
||||||
data: 'https://ck-qr.com',
|
data: `${baseUrl}/l/preview`, // Will be updated in useEffect
|
||||||
margin: 10,
|
margin: 10,
|
||||||
qrOptions: {
|
qrOptions: {
|
||||||
typeNumber: 0,
|
typeNumber: 0,
|
||||||
@@ -43,11 +45,18 @@ export default function CreateQRPage() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Keep QR data in sync with URL input
|
// Generate random slug on mount to prevent SSR hydration errors
|
||||||
const handleUrlChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
React.useEffect(() => {
|
||||||
const newUrl = e.target.value;
|
const generated = Math.random().toString(36).substring(2, 8);
|
||||||
setDestinationUrl(newUrl);
|
setRandomSlug(generated);
|
||||||
setQrOptions(prev => ({ ...prev, data: newUrl || 'https://ck-qr.com' }));
|
setQrOptions(prev => ({ ...prev, data: `${baseUrl}/l/${generated}` }));
|
||||||
|
}, [baseUrl]);
|
||||||
|
|
||||||
|
// Keep QR data in sync with custom slug
|
||||||
|
const handleSlugChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
const val = e.target.value.replace(/[^a-zA-Z0-9-_]/g, '');
|
||||||
|
setCustomSlug(val);
|
||||||
|
setQrOptions(prev => ({ ...prev, data: `${baseUrl}/l/${val || randomSlug}` }));
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleGenerate = async () => {
|
const handleGenerate = async () => {
|
||||||
@@ -56,7 +65,7 @@ export default function CreateQRPage() {
|
|||||||
// Generate Dynamic QR via API
|
// Generate Dynamic QR via API
|
||||||
const response = await api.post('/qr/generate', {
|
const response = await api.post('/qr/generate', {
|
||||||
name: qrName,
|
name: qrName,
|
||||||
customSlug: customSlug,
|
customSlug: customSlug || randomSlug, // Pass the previewed slug so the backend uses it!
|
||||||
type: 'dynamic',
|
type: 'dynamic',
|
||||||
dataType: 'URL',
|
dataType: 'URL',
|
||||||
destinationUrl: destinationUrl,
|
destinationUrl: destinationUrl,
|
||||||
@@ -114,7 +123,7 @@ export default function CreateQRPage() {
|
|||||||
<input
|
<input
|
||||||
type="url"
|
type="url"
|
||||||
value={destinationUrl}
|
value={destinationUrl}
|
||||||
onChange={handleUrlChange}
|
onChange={e => setDestinationUrl(e.target.value)}
|
||||||
placeholder="https://your-website.com"
|
placeholder="https://your-website.com"
|
||||||
className="w-full border-gray-300 rounded-lg shadow-sm p-3 border focus:ring-indigo-500 focus:border-indigo-500 text-gray-900 bg-white"
|
className="w-full border-gray-300 rounded-lg shadow-sm p-3 border focus:ring-indigo-500 focus:border-indigo-500 text-gray-900 bg-white"
|
||||||
/>
|
/>
|
||||||
@@ -130,8 +139,8 @@ export default function CreateQRPage() {
|
|||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
value={customSlug}
|
value={customSlug}
|
||||||
onChange={e => setCustomSlug(e.target.value.replace(/[^a-zA-Z0-9-_]/g, ''))}
|
onChange={handleSlugChange}
|
||||||
placeholder="my-promo"
|
placeholder={randomSlug || "my-promo"}
|
||||||
className="flex-1 w-full p-3 focus:outline-none text-gray-900 bg-white"
|
className="flex-1 w-full p-3 focus:outline-none text-gray-900 bg-white"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user