feat: random post funtzionalitatea sartzen du

This commit is contained in:
txarli committed 2026-11-12 12:26:19 +01:00
1 parent bfb912c56b
commit a3e2be718d
3 files changed
+46 -3

No files matched your search

+1 -2
View File
@@ -1,6 +1,5 @@
--- ---
import type { CollectionEntry } from "astro:content"; import type { CollectionEntry } from "astro:content";
import Post from "../components/Post.astro";
import PostGrid from "./PostGrid.astro"; import PostGrid from "./PostGrid.astro";
interface Props { interface Props {
@@ -17,7 +16,7 @@ let posts = allPosts
new Date(b.data.updated || b.data.added).valueOf() - new Date(b.data.updated || b.data.added).valueOf() -
new Date(a.data.updated || a.data.added).valueOf(), new Date(a.data.updated || a.data.added).valueOf(),
) )
.slice(0, 8); .slice(0, 7);
--- ---
<content> <content>
+44
View File
@@ -7,6 +7,11 @@ interface Props {
const { posts } = Astro.props; const { posts } = Astro.props;
const postSlugs = posts.reduce<string[]>(
(slugs, { data: { slug } }) => [slug, ...slugs],
[],
);
function getTiltDegrees() { function getTiltDegrees() {
const degreeCollection = [0.5, 1, 1.5, 2]; const degreeCollection = [0.5, 1, 1.5, 2];
const randomKey = Math.floor(Math.random() * degreeCollection.length); const randomKey = Math.floor(Math.random() * degreeCollection.length);
@@ -35,8 +40,32 @@ function getTiltDegrees() {
</li> </li>
)) ))
} }
<li class="post post--random" style={`--tilt: ${getTiltDegrees()}deg;`}>
<button
type="button"
role="link"
id="random-post-button"
class="title title--button"
>
Random post-a
</button>
<br />
<span class="excerpt">Bestela, irakurri ausaz aukeratutako post bat 🎲</span
>
</li>
</ul> </ul>
<script is:inline define:vars={{ postSlugs }}>
document.querySelectorAll("#random-post-button").forEach((button) => {
button.addEventListener("click", () => {
const randomSlug =
postSlugs[Math.floor(Math.random() * postSlugs.length)];
window.location.assign("/post/" + randomSlug);
});
});
</script>
<style lang="scss"> <style lang="scss">
ul.posts-list, ul.posts-list,
ul.tags-list { ul.tags-list {
@@ -85,6 +114,21 @@ function getTiltDegrees() {
} }
} }
.post--random {
background-color: var(--blue);
}
.title--button {
background-color: transparent;
border: none;
cursor: pointer;
padding: 0;
text-decoration-thickness: 0.3ex;
text-underline-offset: 0.3ex;
text-decoration: underline;
text-decoration-color: var(--yellow);
}
.tags { .tags {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
+1 -1
View File
@@ -28,7 +28,7 @@ const tags = getTags(posts);
<br /> <br />
<h3>Honatx nire azken postak</h3> <h3>Honatx editatutako azken postak</h3>
</Container> </Container>
<HomePosts allPosts={posts} /> <HomePosts allPosts={posts} />