fix(examples): implement CR's suggestions

Refactored the function into a reactive declaration.

Added similar reactive declaration in `Item.svelte` too.

Note that the conditional is slightly different in each case:
- in `Summary.svelte`, the API response gives correct type 'ask' for 'Ask HN'-type posts,
- but in `Item.svelte`, a different API call resolves these 'Ask HN'-type posts with type 'link'.

In both cases, however, 'item.domain' is undefined.
pull/4640/head
Robin Cussol 6 years ago
parent 2029de45ba
commit bd73d8decb

@ -3,6 +3,8 @@
export let item; export let item;
export let returnTo; export let returnTo;
$: url = !item.domain ? `https://news.ycombinator.com/${item.url}` : item.url;
</script> </script>
<style> <style>
@ -24,7 +26,7 @@
<a href={returnTo}>&laquo; back</a> <a href={returnTo}>&laquo; back</a>
<article> <article>
<a href="{item.url}"> <a href="{url}">
<h1>{item.title}</h1> <h1>{item.title}</h1>
<small>{item.domain}</small> <small>{item.domain}</small>
</a> </a>

@ -8,13 +8,7 @@
return `${c} ${c === 1 ? 'comment' : 'comments'}`; return `${c} ${c === 1 ? 'comment' : 'comments'}`;
} }
function item_url() { $: url = item.type === "ask" ? `https://news.ycombinator.com/${item.url}` : item.url;
const { url, type } = item;
if (type === "ask") {
return `https://news.ycombinator.com/${url}`;
}
return url;
}
</script> </script>
<style> <style>
@ -41,6 +35,6 @@
<article> <article>
<span>{i + offset + 1}</span> <span>{i + offset + 1}</span>
<h2><a target="_blank" href={item_url()}>{item.title}</a></h2> <h2><a target="_blank" href={url}>{item.title}</a></h2>
<p class="meta"><a href="#/item/{item.id}">{comment_text()}</a> by {item.user} {item.time_ago}</p> <p class="meta"><a href="#/item/{item.id}">{comment_text()}</a> by {item.user} {item.time_ago}</p>
</article> </article>

Loading…
Cancel
Save