Skip to content

Commit 2f8905d

Browse files
committed
Fetching one random top story from HN and rendering it.
1 parent 41f13bd commit 2f8905d

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

src/apiManager.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { OfflineDictionary } from './integrations/offlineDic';
22
import type {
33
DefinitionProvider,
44
DictionaryWord,
5+
HNItem,
56
PartOfSpeech,
67
PartOfSpeechProvider,
78
Synonym,
@@ -89,6 +90,30 @@ export default class APIManager {
8990
}
9091
}
9192

93+
/**
94+
* Sends a request with the passed query to the chosen API and returns the Result
95+
*
96+
* @param query - The term you want to look up
97+
* @returns The API Response of the chosen API as Promise<DictionaryWord>
98+
*/
99+
public async requestTopHN(): Promise<HNItem> {
100+
//Get the currently enabled API
101+
let itemIds: Array<Number>;
102+
try {
103+
const url = "https://hacker-news.firebaseio.com/v0/topstories.json";
104+
const response = await fetch(url);
105+
itemIds = (await response.json()) as Array<Number>
106+
} catch (error) {
107+
return Promise.reject(error);
108+
}
109+
110+
const itemId = itemIds[Math.floor(Math.random() * itemIds.slice(0, 15).length)]
111+
const itemResponse = await fetch(`https://hacker-news.firebaseio.com/v0/item/${itemId}.json?print=pretty`);
112+
const hnItem = (await itemResponse.json()) as HNItem
113+
114+
return hnItem;
115+
}
116+
92117
/**
93118
* Sends a request with the passed query to the chosen API and returns the resulting Synonyms
94119
*

src/integrations/types.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ export enum PartOfSpeech {
2222
Adjective,
2323
Adverb,
2424
}
25+
26+
27+
// {
28+
// "by" : "norvig",
29+
// "id" : 2921983,
30+
// "kids" : [ 2922097, 2922429, 2924562, 2922709, 2922573, 2922140, 2922141 ],
31+
// "parent" : 2921506,
32+
// "text" : "Aw shucks, guys ... you make me blush with your compliments.<p>Tell you what, Ill make a deal: I'll keep writing if you keep reading. K?",
33+
// "time" : 1314211127,
34+
// "type" : "comment"
35+
// }
36+
37+
export interface HNItem {
38+
by: string;
39+
id: Number;
40+
title: string;
41+
url: string;
42+
}
43+
2544
export interface DictionaryWord {
2645
word: string;
2746
origin?: string;

src/ui/dictionary/dictionaryView.svelte

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import type APIManager from "src/apiManager";
3-
import type { DictionaryWord } from "src/integrations/types";
3+
import type { DictionaryWord, HNItem } from "src/integrations/types";
44
import type LocalDictionaryBuilder from "src/localDictionaryBuilder";
55
66
import PhoneticComponent from "./phoneticComponent.svelte";
@@ -14,13 +14,18 @@
1414
1515
export let query: string = "";
1616
let promise: Promise<DictionaryWord>;
17+
let promiseTopHN: Promise<HNItem>;
1718
1819
function search() {
1920
if (query.trim()) {
2021
promise = manager.requestDefinitions(query);
2122
}
2223
}
2324
25+
function fetchTopHN() {
26+
promiseTopHN = manager.requestTopHN();
27+
}
28+
2429
function languageModal() {
2530
dispatchEvent(new Event("dictionary-open-language-switcher"));
2631
}
@@ -29,6 +34,10 @@
2934
search();
3035
});
3136
37+
addEventListener("obsidian-hackernews-fetchTopHN", () => {
38+
fetchTopHN();
39+
});
40+
3241
function handleKeyDown(e: KeyboardEvent) {
3342
if (e.key === "Enter") {
3443
search();
@@ -52,6 +61,25 @@
5261
><i class="searchIcon" alt="Search" /></button
5362
>
5463
</div>
64+
<br />
65+
<button class="dictionary-button" on:click={fetchTopHN}
66+
>Fetch TopHN</button
67+
>
68+
{#if promiseTopHN}
69+
{#await promiseTopHN}
70+
<div class="center" />
71+
{:then data}
72+
<div class="results">
73+
<div class="container">
74+
<p>{ data.title }</p>
75+
<p>{ data.url }</p>
76+
</div>
77+
</div>
78+
{:catch error}
79+
<ErrorComponent {error} />
80+
{/await}
81+
{/if}
82+
5583
{#if promise && query.trim()}
5684
{#await promise}
5785
<div class="center">

src/ui/dictionary/dictionaryView.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default class DictionaryView extends ItemView {
4747
localDictionary: this.plugin.localDictionary,
4848
}
4949
});
50+
dispatchEvent(new Event("obsidian-hackernews-fetchTopHN"));
5051
addEventListener('dictionary-open-language-switcher', () => {
5152
new LanguageChooser(this.app, this.plugin).open();
5253
});

0 commit comments

Comments
 (0)