|
| 1 | +import { apdate } from "journalize"; |
| 2 | + |
| 3 | +import imgproxy from "../utils/imgproxy-url.js"; |
| 4 | +import { sendGAEvent } from "../utils/google-analytics.js"; |
| 5 | +import searchAPI from "../utils/search-api.js"; |
| 6 | +import { debouncer } from "../utils/timers.js"; |
| 7 | + |
| 8 | +function roundUp(n, by) { |
| 9 | + return by * Math.ceil(n / by); |
| 10 | +} |
| 11 | + |
| 12 | +function normalize(obj) { |
| 13 | + return { |
| 14 | + title: obj["link-title"] || obj.hed, |
| 15 | + url: obj.URL || "", |
| 16 | + byline: obj.byline || "", |
| 17 | + imageCredit: obj["image-credit"] || "", |
| 18 | + imageDescription: obj["image-description"] || "", |
| 19 | + kicker: obj.kicker || "", |
| 20 | + publishedISO: obj["pub-date"] || "", |
| 21 | + get imageSource() { |
| 22 | + return obj["image-url"] || ""; |
| 23 | + }, |
| 24 | + get published() { |
| 25 | + return apdate(new Date(this.publishedISO)); |
| 26 | + }, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +const magicPixel = |
| 31 | + "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="; |
| 32 | + |
| 33 | +export default function searchArticles() { |
| 34 | + return { |
| 35 | + query: "", |
| 36 | + results: null, |
| 37 | + error: null, |
| 38 | + isLoading: false, |
| 39 | + magicPixel, |
| 40 | + |
| 41 | + init() { |
| 42 | + const bouncedSearch = debouncer({ milliseconds: 500 }, () => |
| 43 | + searchAPI(this.query) |
| 44 | + .then((results) => { |
| 45 | + this.error = null; |
| 46 | + if (results) { |
| 47 | + this.results = results; |
| 48 | + } |
| 49 | + }) |
| 50 | + .catch((error) => { |
| 51 | + this.isLoading = false; |
| 52 | + this.error = error; |
| 53 | + }) |
| 54 | + .finally(() => { |
| 55 | + this.isLoading = false; |
| 56 | + }) |
| 57 | + ); |
| 58 | + |
| 59 | + this.$watch("query", () => { |
| 60 | + this.isLoading = true; |
| 61 | + bouncedSearch(); |
| 62 | + }); |
| 63 | + }, |
| 64 | + |
| 65 | + get stories() { |
| 66 | + if (!this.results || !this.results.hits) { |
| 67 | + return []; |
| 68 | + } |
| 69 | + return this.results.hits.map(normalize); |
| 70 | + }, |
| 71 | + |
| 72 | + get resultsCount() { |
| 73 | + return this.results?.nbHits ?? 0; |
| 74 | + }, |
| 75 | + |
| 76 | + get resultsText() { |
| 77 | + let nHits = this.resultsCount; |
| 78 | + if (!nHits) { |
| 79 | + return "No search results"; |
| 80 | + } |
| 81 | + if (nHits === 1) { |
| 82 | + return "Got one search result."; |
| 83 | + } |
| 84 | + let nStories = this.results?.hits?.length ?? 0; |
| 85 | + let more = nHits > nStories ? `Showing first ${nStories}.` : ""; |
| 86 | + return `Got ${nHits} search results. ${more}`; |
| 87 | + }, |
| 88 | + |
| 89 | + loadImage(ev) { |
| 90 | + let el = ev.target; |
| 91 | + let { src } = el.dataset; |
| 92 | + if (el.src !== this.magicPixel || src === "") { |
| 93 | + return; |
| 94 | + } |
| 95 | + let { width = 0, height = 0 } = el; |
| 96 | + let aspectRatio = height / width; |
| 97 | + width = roundUp(window.devicePixelRatio * width, 100); |
| 98 | + height = Math.round(aspectRatio * width); |
| 99 | + el.src = imgproxy(src, { width, height }); |
| 100 | + }, |
| 101 | + |
| 102 | + analytics($event) { |
| 103 | + let { href = "" } = $event.target; |
| 104 | + sendGAEvent({ |
| 105 | + eventCategory: "Internal Link", |
| 106 | + eventAction: "Search", |
| 107 | + eventLabel: href, |
| 108 | + transport: "beacon", |
| 109 | + }); |
| 110 | + }, |
| 111 | + }; |
| 112 | +} |
0 commit comments