Skip to content

Instantly share code, notes, and snippets.

@Ladvace
Created October 31, 2021 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ladvace/2bc0e26690ab2a114b6a302a726ffc7e to your computer and use it in GitHub Desktop.
Save Ladvace/2bc0e26690ab2a114b6a302a726ffc7e to your computer and use it in GitHub Desktop.
<script context="module">
// this script fetch the posts from the api
// https://svelte.dev/docs#script_context_module
// this is runned on load (check svelKit doc)
export async function load({ fetch }) {
let articles;
try {
// here you should type your dev.to username
articles = await fetch(`https://dev.to/api/articles?username=ladvace`);
articles = await articles.json();
} catch (e) {
console.error(e);
}
// you can pass the `articles` via props like that
return {
props: {
articles
}
};
}
</script>
<script>
// in this second script you are going to declare the articles fetched in the first script and eventually filter them as I did
export let articles;
// here I blacklist some posts adding the id of the post (from the API) in this array
const blackListedArticles = [432439];
const filteredArticles = articles.filter((article) => !blackListedArticles.includes(article.id));
</script>
<div class="articlesContainer">
<div class="articles">
<h1>Articles</h1>
// if you don't need to filter your posts just pass the `articles` instead if `filteredArticles`
{#each filteredArticles as article}
// here on click you we are passing the article of the id
<a href={`/blog/${article.id}`}>
<div class="article">
<div class="header">
<h2>{article.title} </h2>
<h4>Tags: {article.tags}</h4>
</div>
<p>
{article.description}
</p>
</div>
</a>
{/each}
{#if filteredArticles.length === 0}
<div>No Articles</div>
{/if}
</div
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment