|
| 1 | +interface RatingsData { |
| 2 | + snap_id: string; |
| 3 | + total_votes: number; |
| 4 | + ratings_band: string; |
| 5 | +} |
| 6 | + |
| 7 | +async function fetchSnapRatings(snapId: string): Promise<RatingsData | null> { |
| 8 | + try { |
| 9 | + const response = await fetch(`/api/snap/${snapId}/ratings`); |
| 10 | + |
| 11 | + if (!response.ok) { |
| 12 | + console.error( |
| 13 | + `Failed to fetch ratings for snap ${snapId}:`, |
| 14 | + response.status, |
| 15 | + ); |
| 16 | + return null; |
| 17 | + } |
| 18 | + |
| 19 | + const data = await response.json(); |
| 20 | + |
| 21 | + return data; |
| 22 | + } catch (error) { |
| 23 | + console.error(`Error fetching ratings for snap ${snapId}:`, error); |
| 24 | + return null; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function renderRatingsInfo( |
| 29 | + ratingsData: RatingsData | null, |
| 30 | + containerSelector: string, |
| 31 | +): void { |
| 32 | + const container = document.querySelector(containerSelector) as HTMLElement; |
| 33 | + |
| 34 | + if (!container) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + if (!ratingsData) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + const ratingsBand = ratingsData.ratings_band; |
| 43 | + let icon = ""; |
| 44 | + let text = ""; |
| 45 | + |
| 46 | + switch (ratingsBand) { |
| 47 | + case "very-good": |
| 48 | + icon = "p-icon--thumbs-up"; |
| 49 | + text = "Very good"; |
| 50 | + break; |
| 51 | + case "good": |
| 52 | + icon = "p-icon--thumbs-up"; |
| 53 | + text = "Good"; |
| 54 | + break; |
| 55 | + case "neutral": |
| 56 | + icon = "p-icon--minus"; |
| 57 | + text = "Neutral"; |
| 58 | + break; |
| 59 | + case "poor": |
| 60 | + icon = "p-icon--thumbs-down"; |
| 61 | + text = "Poor"; |
| 62 | + break; |
| 63 | + case "very-poor": |
| 64 | + icon = "p-icon--thumbs-down"; |
| 65 | + text = "Very poor"; |
| 66 | + break; |
| 67 | + default: |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const title = text.charAt(0).toUpperCase() + text.slice(1) + " rating"; |
| 72 | + const html = ` |
| 73 | + <span class="p-snap-ratings"> |
| 74 | + <span title="${title}"><i class="${icon}"></i> ${text}</span> |
| 75 | + <span> (${ratingsData.total_votes} votes)</span> |
| 76 | + </span> |
| 77 | + `; |
| 78 | + |
| 79 | + const list = document.querySelector(containerSelector); |
| 80 | + if (list) { |
| 81 | + const li = document.createElement("li"); |
| 82 | + li.className = "p-inline-list__item"; |
| 83 | + li.innerHTML = html; |
| 84 | + list.appendChild(li); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +export default function initSnapRatings( |
| 89 | + snapId: string, |
| 90 | + containerSelectors: string[], |
| 91 | +): void { |
| 92 | + if (!snapId) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + fetchSnapRatings(snapId).then((ratingsData) => { |
| 97 | + containerSelectors.forEach((selector) => { |
| 98 | + renderRatingsInfo(ratingsData, selector); |
| 99 | + }); |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +export { fetchSnapRatings, renderRatingsInfo, RatingsData }; |
0 commit comments