feat: random post funtzionalitatea sartzen du
This commit is contained in:
1 parent
bfb912c56b
commit
a3e2be718d
3 files changed
+46
-3
No files matched your search
@@ -1,6 +1,5 @@
|
||||
---
|
||||
import type { CollectionEntry } from "astro:content";
|
||||
import Post from "../components/Post.astro";
|
||||
import PostGrid from "./PostGrid.astro";
|
||||
|
||||
interface Props {
|
||||
@@ -17,7 +16,7 @@ let posts = allPosts
|
||||
new Date(b.data.updated || b.data.added).valueOf() -
|
||||
new Date(a.data.updated || a.data.added).valueOf(),
|
||||
)
|
||||
.slice(0, 8);
|
||||
.slice(0, 7);
|
||||
---
|
||||
|
||||
<content>
|
||||
|
||||
@@ -7,6 +7,11 @@ interface Props {
|
||||
|
||||
const { posts } = Astro.props;
|
||||
|
||||
const postSlugs = posts.reduce<string[]>(
|
||||
(slugs, { data: { slug } }) => [slug, ...slugs],
|
||||
[],
|
||||
);
|
||||
|
||||
function getTiltDegrees() {
|
||||
const degreeCollection = [0.5, 1, 1.5, 2];
|
||||
const randomKey = Math.floor(Math.random() * degreeCollection.length);
|
||||
@@ -35,8 +40,32 @@ function getTiltDegrees() {
|
||||
</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>
|
||||
|
||||
<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">
|
||||
ul.posts-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 {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
|
||||
@@ -28,7 +28,7 @@ const tags = getTags(posts);
|
||||
|
||||
<br />
|
||||
|
||||
<h3>Honatx nire azken postak</h3>
|
||||
<h3>Honatx editatutako azken postak</h3>
|
||||
</Container>
|
||||
|
||||
<HomePosts allPosts={posts} />
|
||||
|
||||
Reference in new issue
Block a user