✨ push changes
This commit is contained in:
@@ -1,34 +1,39 @@
|
||||
use std::rc::Rc;
|
||||
use std::ops::Range;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use yew::prelude::*;
|
||||
use yew_router::prelude::*;
|
||||
use yew_router::components::Link;
|
||||
|
||||
use crate::Route;
|
||||
|
||||
const ELLIPSIS: &str = "\u{02026}";
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct Author {
|
||||
pub seed: u64,
|
||||
pub seed: u8,
|
||||
pub name: String,
|
||||
pub keywords: Vec<String>,
|
||||
pub image_url: String,
|
||||
pub about: String
|
||||
}
|
||||
impl BlogEntry for Author {
|
||||
fn from_entry(entry: &mut Entry) -> Self {
|
||||
return match entry.seed {
|
||||
1 => Self {seed: entry.seed, name: "iouring".to_string(), keywords: vec!["meow".to_string(), "mrrp".to_string(), "mew".to_string()], image_url: "".to_string() },
|
||||
_ => Self {seed: entry.seed, name: "none".to_string(), keywords: vec!["meow".to_string(), "mrrp".to_string(), "mew".to_string()], image_url: "".to_string()}
|
||||
};
|
||||
0 => Self {
|
||||
seed: entry.seed,
|
||||
name: "iouring".to_string(),
|
||||
image_url: "profile.avif".to_string(),
|
||||
about: "sup".to_string(),
|
||||
keywords: vec![
|
||||
"meow".to_string(),
|
||||
"mrrp".to_string(),
|
||||
"mew".to_string()
|
||||
]
|
||||
},
|
||||
_ => Self {
|
||||
seed: u8::MAX,
|
||||
name: "not found".to_string(),
|
||||
image_url: "".to_string(),
|
||||
about: "".to_string(),
|
||||
keywords: vec![]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct PostMeta {
|
||||
pub seed: u64,
|
||||
pub seed: u8,
|
||||
pub title: String,
|
||||
pub author: Author,
|
||||
pub keywords: Vec<String>,
|
||||
@@ -36,12 +41,21 @@ pub struct PostMeta {
|
||||
}
|
||||
impl BlogEntry for PostMeta {
|
||||
fn from_entry(entry: &mut Entry) -> Self {
|
||||
return Self {
|
||||
seed: entry.seed,
|
||||
title: "awawa title".to_string(),
|
||||
author: Author::from_entry(entry),
|
||||
keywords: vec!["meow".to_string(), "mrrp".to_string(), "mew".to_string()],
|
||||
image_url: "".to_string()
|
||||
return match entry.seed {
|
||||
0 => Self {
|
||||
seed: entry.seed,
|
||||
title: "awawa title".to_string(),
|
||||
author: Author::from_entry(entry),
|
||||
keywords: vec!["meow".to_string(), "mrrp".to_string(), "mew".to_string()],
|
||||
image_url: "testimage.jpg".to_string()
|
||||
},
|
||||
_ => Self {
|
||||
seed: entry.seed,
|
||||
title: "not found".to_string(),
|
||||
author: Author::from_entry(entry),
|
||||
keywords: vec![],
|
||||
image_url: "".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,7 +67,10 @@ pub struct Post {
|
||||
}
|
||||
impl BlogEntry for Post {
|
||||
fn from_entry(entry: &mut Entry) -> Self {
|
||||
return Self { meta: PostMeta::from_entry(entry), content: vec![PostPart::from_entry(entry)] }
|
||||
return Self {
|
||||
meta: PostMeta::from_entry(entry),
|
||||
content: vec![PostPart::from_entry(entry)]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +81,10 @@ pub enum PostPart {
|
||||
}
|
||||
impl BlogEntry for PostPart {
|
||||
fn from_entry(entry: &mut Entry) -> Self {
|
||||
return Self::Quote(Quote::from_entry(entry));
|
||||
// TODO: ... | add proper logic
|
||||
|
||||
return Self::Section(Section::from_entry(entry))
|
||||
// return Self::Quote(Quote::from_entry(entry))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +96,22 @@ pub struct Section {
|
||||
}
|
||||
impl BlogEntry for Section {
|
||||
fn from_entry(entry: &mut Entry) -> Self {
|
||||
return Self { title: "awawa title".to_string(), paragraphs: vec!["meow".to_string(), "mrrp".to_string(), "mew".to_string()], image_url: "".to_string() }
|
||||
return match entry.seed {
|
||||
0 => Self {
|
||||
title: "awawa title".to_string(),
|
||||
image_url: "".to_string(),
|
||||
paragraphs: vec![
|
||||
"meow 1".to_string(),
|
||||
"mrrp 2".to_string(),
|
||||
"mew 3".to_string()
|
||||
]
|
||||
},
|
||||
_ => Self {
|
||||
title: "not found".to_string(),
|
||||
image_url: "".to_string(),
|
||||
paragraphs: vec![]
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,332 +127,18 @@ impl BlogEntry for Quote {
|
||||
}
|
||||
|
||||
pub struct Entry {
|
||||
pub seed: u64
|
||||
pub seed: u8
|
||||
}
|
||||
|
||||
impl Entry {
|
||||
pub fn from_seed(seed: u64) -> Self {
|
||||
pub fn from_seed(seed: u8) -> Self {
|
||||
Self { seed }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait BlogEntry: Sized {
|
||||
fn from_entry(entry: &mut Entry) -> Self;
|
||||
fn from_seed(seed: u64) -> Self {
|
||||
fn from_seed(seed: u8) -> Self {
|
||||
Self::from_entry(&mut Entry::from_seed(seed))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Properties)]
|
||||
pub struct PropsAuthorCard {
|
||||
pub seed: u64,
|
||||
}
|
||||
|
||||
pub struct AuthorCard {
|
||||
author: Author,
|
||||
}
|
||||
impl Component for AuthorCard {
|
||||
type Message = ();
|
||||
type Properties = PropsAuthorCard;
|
||||
|
||||
fn create(ctx: &Context<Self>) -> Self {
|
||||
Self {
|
||||
author: Author::from_seed(ctx.props().seed),
|
||||
}
|
||||
}
|
||||
|
||||
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
|
||||
self.author = Author::from_seed(ctx.props().seed);
|
||||
true
|
||||
}
|
||||
|
||||
fn view(&self, _ctx: &Context<Self>) -> Html {
|
||||
let Self { author } = self;
|
||||
html! {
|
||||
<div class="card">
|
||||
<div class="card-content">
|
||||
<div class="media">
|
||||
<div class="media-left">
|
||||
<figure class="image is-128x128">
|
||||
<img alt="this should normally show an image mew,," src={author.image_url.clone()} />
|
||||
</figure>
|
||||
</div>
|
||||
<div class="media-content">
|
||||
<p class="title is-3">{ &author.name }</p>
|
||||
<p>
|
||||
{ "I like " }
|
||||
<b>{ author.keywords.join(", ") }</b>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<footer class="card-footer">
|
||||
<Link<Route> classes={classes!("card-footer-item")} to={Route::Author { id: author.seed }}>
|
||||
{ "Profile" }
|
||||
</Link<Route>>
|
||||
</footer>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Properties)]
|
||||
pub struct PropsPostCard {
|
||||
pub seed: u64,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
pub struct PostMetaState {
|
||||
inner: PostMeta,
|
||||
}
|
||||
|
||||
impl Reducible for PostMetaState {
|
||||
type Action = u64;
|
||||
|
||||
fn reduce(self: Rc<Self>, action: u64) -> Rc<Self> {
|
||||
Self { inner: PostMeta::from_seed(action.into()), }.into()
|
||||
}
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn PostCard(props: &PropsPostCard) -> Html {
|
||||
let seed = props.seed;
|
||||
|
||||
let post = use_reducer_eq(|| PostMetaState {
|
||||
inner: PostMeta::from_seed(seed.into()),
|
||||
});
|
||||
|
||||
{
|
||||
let post_dispatcher = post.dispatcher();
|
||||
use_effect_with(seed, move |seed| {
|
||||
post_dispatcher.dispatch(*seed);
|
||||
|
||||
|| {}
|
||||
});
|
||||
}
|
||||
|
||||
let post = &post.inner;
|
||||
|
||||
html! {
|
||||
<div class="card">
|
||||
<div class="card-image">
|
||||
<figure class="image is-2by1">
|
||||
<img alt="This post's image" src={post.image_url.clone()} loading="lazy" />
|
||||
</figure>
|
||||
</div>
|
||||
<div class="card-content">
|
||||
<Link<Route> classes={classes!("title", "is-block")} to={Route::Entry { id: post.seed }}>
|
||||
{ &post.title }
|
||||
</Link<Route>>
|
||||
<Link<Route> classes={classes!("subtitle", "is-block")} to={Route::Author { id: post.author.seed }}>
|
||||
{ &post.author.name }
|
||||
</Link<Route>>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
|
||||
pub struct PageQuery {
|
||||
pub page: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Properties)]
|
||||
pub struct PropsNavigation {
|
||||
pub page: u64,
|
||||
pub total_pages: u64,
|
||||
pub route_to_page: Route,
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn RelNavButtons(props: &PropsNavigation) -> Html {
|
||||
let PropsNavigation {
|
||||
page,
|
||||
total_pages,
|
||||
route_to_page: to,
|
||||
} = props.clone();
|
||||
|
||||
html! {
|
||||
<>
|
||||
<Link<Route, PageQuery>
|
||||
classes={classes!("pagination-previous")}
|
||||
disabled={page==1}
|
||||
query={Some(PageQuery{page: page - 1})}
|
||||
to={to.clone()}
|
||||
>
|
||||
{ "Previous" }
|
||||
</Link<Route, PageQuery>>
|
||||
<Link<Route, PageQuery>
|
||||
classes={classes!("pagination-next")}
|
||||
disabled={page==total_pages}
|
||||
query={Some(PageQuery{page: page + 1})}
|
||||
{to}
|
||||
>
|
||||
{ "Next page" }
|
||||
</Link<Route, PageQuery>>
|
||||
</>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Properties, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct RenderLinksProps {
|
||||
range: Range<u64>,
|
||||
len: usize,
|
||||
max_links: usize,
|
||||
props: PropsNavigation,
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn RenderLinks(props: &RenderLinksProps) -> Html {
|
||||
let RenderLinksProps {
|
||||
range,
|
||||
len,
|
||||
max_links,
|
||||
props,
|
||||
} = props.clone();
|
||||
|
||||
let mut range = range;
|
||||
|
||||
if len > max_links {
|
||||
let last_link =
|
||||
html! {<RenderLink to_page={range.next_back().unwrap()} props={props.clone()} />};
|
||||
// remove 1 for the ellipsis and 1 for the last link
|
||||
let links = range
|
||||
.take(max_links - 2)
|
||||
.map(|page| html! {<RenderLink to_page={page} props={props.clone()} />});
|
||||
html! {
|
||||
<>
|
||||
{ for links }
|
||||
<li><span class="pagination-ellipsis">{ ELLIPSIS }</span></li>
|
||||
{ last_link }
|
||||
</>
|
||||
}
|
||||
} else {
|
||||
html! {
|
||||
for page in range {
|
||||
<RenderLink to_page={page} props={props.clone()} />
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Properties, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct RenderLinkProps {
|
||||
to_page: u64,
|
||||
props: PropsNavigation,
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn RenderLink(props: &RenderLinkProps) -> Html {
|
||||
let RenderLinkProps { to_page, props } = props.clone();
|
||||
|
||||
let PropsNavigation {
|
||||
page,
|
||||
route_to_page,
|
||||
..
|
||||
} = props;
|
||||
|
||||
let is_current_class = if to_page == page { "is-current" } else { "" };
|
||||
|
||||
html! {
|
||||
<li>
|
||||
<Link<Route, PageQuery>
|
||||
classes={classes!("pagination-link", is_current_class)}
|
||||
to={route_to_page}
|
||||
query={Some(PageQuery{page: to_page})}
|
||||
>
|
||||
{ to_page }
|
||||
</Link<Route, PageQuery>>
|
||||
</li>
|
||||
}
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn Links(props: &PropsNavigation) -> Html {
|
||||
const LINKS_PER_SIDE: usize = 3;
|
||||
|
||||
let PropsNavigation {
|
||||
page, total_pages, ..
|
||||
} = *props;
|
||||
|
||||
let pages_prev = page.checked_sub(1).unwrap_or_default() as usize;
|
||||
let pages_next = (total_pages - page) as usize;
|
||||
|
||||
let links_left = LINKS_PER_SIDE.min(pages_prev)
|
||||
// if there are less than `LINKS_PER_SIDE` to the right, we add some more on the left.
|
||||
+ LINKS_PER_SIDE.checked_sub(pages_next).unwrap_or_default();
|
||||
let links_right = 2 * LINKS_PER_SIDE - links_left;
|
||||
|
||||
html! {
|
||||
<>
|
||||
<RenderLinks range={ 1..page } len={pages_prev} max_links={links_left} props={props.clone()} />
|
||||
<RenderLink to_page={page} props={props.clone()} />
|
||||
<RenderLinks range={ page + 1..total_pages + 1 } len={pages_next} max_links={links_right} props={props.clone()} />
|
||||
</>
|
||||
}
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn Pagination(props: &PropsNavigation) -> Html {
|
||||
html! {
|
||||
<nav class="pagination is-right" role="navigation" aria-label="pagination">
|
||||
<RelNavButtons ..{props.clone()} />
|
||||
<ul class="pagination-list">
|
||||
<Links ..{props.clone()} />
|
||||
</ul>
|
||||
</nav>
|
||||
}
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn Nav() -> Html {
|
||||
let navbar_active = use_state_eq(|| false);
|
||||
|
||||
let toggle_navbar = {
|
||||
let navbar_active = navbar_active.clone();
|
||||
|
||||
Callback::from(move |_| {
|
||||
navbar_active.set(!*navbar_active);
|
||||
})
|
||||
};
|
||||
|
||||
let active_class = if !*navbar_active { "is-active" } else { "" };
|
||||
|
||||
html! {
|
||||
<nav class="navbar is-primary" role="navigation" aria-label="main navigation">
|
||||
<div class="navbar-brand">
|
||||
<h1 class="navbar-item is-size-3">{ "Yew Blog" }</h1>
|
||||
|
||||
<button class={classes!("navbar-burger", "burger", active_class)}
|
||||
aria-label="menu" aria-expanded="false"
|
||||
onclick={toggle_navbar}
|
||||
>
|
||||
<span aria-hidden="true"></span>
|
||||
<span aria-hidden="true"></span>
|
||||
<span aria-hidden="true"></span>
|
||||
</button>
|
||||
</div>
|
||||
<div class={classes!("navbar-menu", active_class)}>
|
||||
<div class="navbar-start">
|
||||
<Link<Route> classes={classes!("navbar-item")} to={Route::Entries}> // TODO: ...
|
||||
{ "Home" }
|
||||
</Link<Route>>
|
||||
<Link<Route> classes={classes!("navbar-item")} to={Route::Entries}>
|
||||
{ "Posts" }
|
||||
</Link<Route>>
|
||||
|
||||
<div class="navbar-item has-dropdown is-hoverable">
|
||||
<div class="navbar-link">
|
||||
{ "More" }
|
||||
</div>
|
||||
<div class="navbar-dropdown">
|
||||
<Link<Route> classes={classes!("navbar-item")} to={Route::Authors}>
|
||||
{ "Meet the authors" }
|
||||
</Link<Route>>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user