|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +--- |
| 4 | + |
| 5 | +<script src="https://accounts.google.com/gsi/client" async defer></script> |
| 6 | + |
| 7 | +<style> |
| 8 | + .cert-container { |
| 9 | + display: grid; |
| 10 | + grid-template-columns: 300px 1fr; |
| 11 | + gap: 2rem; |
| 12 | + padding-top: 90px; |
| 13 | + padding-left: 2rem; |
| 14 | + padding-right: 2rem; |
| 15 | + padding-bottom: 1rem; |
| 16 | + } |
| 17 | + .user-info { |
| 18 | + margin-bottom: 1rem; |
| 19 | + } |
| 20 | + #certificate-frame { |
| 21 | + border: 1px solid #ccc; |
| 22 | + width: 100%; |
| 23 | + height: 80vh; |
| 24 | + } |
| 25 | +</style> |
| 26 | + |
| 27 | +<div class="cert-container"> |
| 28 | + <div> |
| 29 | + <div class="user-info" id="user-info"> |
| 30 | + <div id="g_id_onload" |
| 31 | + data-client_id="820930631538-pcpv8qid6rpvgeirjjksbjp06u1f2oi6.apps.googleusercontent.com" |
| 32 | + data-callback="handleCredentialResponse"> |
| 33 | + </div> |
| 34 | + <div class="g_id_signin" |
| 35 | + data-type="standard" |
| 36 | + data-size="large" |
| 37 | + data-theme="outline" |
| 38 | + data-text="sign_in_with" |
| 39 | + data-shape="rect" |
| 40 | + data-logo_alignment="left"> |
| 41 | + </div> |
| 42 | + </div> |
| 43 | + |
| 44 | + <select id="events-dropdown" style="display:none; width:100%; padding:0.5rem;"> |
| 45 | + <option value="">Select your event certificate</option> |
| 46 | + </select> |
| 47 | + </div> |
| 48 | + |
| 49 | + <div> |
| 50 | + <iframe id="certificate-frame"></iframe> |
| 51 | + </div> |
| 52 | +</div> |
| 53 | + |
| 54 | +<script> |
| 55 | + let googleUser = null; |
| 56 | + |
| 57 | + async function handleCredentialResponse(response) { |
| 58 | + const data = parseJwt(response.credential); |
| 59 | + googleUser = data; |
| 60 | + document.getElementById('user-info').innerHTML = |
| 61 | + `<p><strong>${data.name}</strong><br>${data.email}</p>`; |
| 62 | + await loadEvents(data.sub); |
| 63 | + } |
| 64 | + |
| 65 | + function parseJwt(token) { |
| 66 | + const base64Url = token.split('.')[1]; |
| 67 | + const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); |
| 68 | + const jsonPayload = decodeURIComponent(atob(base64).split('').map(c => |
| 69 | + '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) |
| 70 | + ).join('')); |
| 71 | + return JSON.parse(jsonPayload); |
| 72 | + } |
| 73 | + |
| 74 | + async function loadEvents(userId) { |
| 75 | + try { |
| 76 | + const res = await fetch('https://xrxfvavgbcphhywznrfq.supabase.co/functions/v1/my-certificates', { |
| 77 | + method: 'POST', |
| 78 | + headers: {'Content-Type': 'application/json'}, |
| 79 | + body: JSON.stringify({ user_id: userId }) |
| 80 | + }); |
| 81 | + if (!res.ok) throw new Error('Failed fetching events'); |
| 82 | + const events = await res.json(); |
| 83 | + |
| 84 | + const dropdown = document.getElementById('events-dropdown'); |
| 85 | + dropdown.innerHTML = '<option value="">Select your event certificate</option>'; |
| 86 | + events.forEach(id => { |
| 87 | + dropdown.innerHTML += `<option value="${id}">${id}</option>`; |
| 88 | + }); |
| 89 | + dropdown.style.display = 'block'; |
| 90 | + |
| 91 | + dropdown.addEventListener('change', e => { |
| 92 | + if (e.target.value) loadCertificate(e.target.value); |
| 93 | + }); |
| 94 | + |
| 95 | + } catch (e) { |
| 96 | + console.error(e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + async function loadCertificate(eventId) { |
| 101 | + // Since Jekyll builds everything statically, we must load the collection index at build time. |
| 102 | + // We'll expose them via `site.certificates` below: |
| 103 | + // const allCertificates = {{ site.certificates | jsonify }}; |
| 104 | + // const cert = allCertificates.find(c => c.id === eventId); |
| 105 | + // if (!cert) { |
| 106 | + // alert('Certificate not found'); |
| 107 | + // return; |
| 108 | + // } |
| 109 | + |
| 110 | + // const svgPath = `{{ '/assets/certificates/' | relative_url }}${cert.certificate}`; |
| 111 | + // document.getElementById('certificate-frame').src = svgPath; |
| 112 | + } |
| 113 | +</script> |
0 commit comments