Skip to content

Commit

Permalink
Add post_add
Browse files Browse the repository at this point in the history
  • Loading branch information
The-DevBlog committed Feb 14, 2024
1 parent 35d3f27 commit 3c19755
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 12 deletions.
5 changes: 1 addition & 4 deletions devblog/devblog/client/src/components/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::{
use chrono::{Local, TimeZone};
use gloo::console::log;
use stylist::Style;
use web_sys::MouseEvent;
use yew::{function_component, html, use_effect_with, use_state, Callback, Html, Properties};
use yew::{function_component, html, use_state, Callback, Html, Properties};
use yewdux::use_store_value;

const STYLE: &str = include_str!("styles/comment.css");
Expand Down Expand Up @@ -39,7 +38,6 @@ pub fn comment(props: &Props) -> Html {
let on_edit_save = move |value: String| {
log!("SAVE EDIT COMMENT");
content_clone.set(value);
// props.comment.content = "HOOOOYAHHHH".to_string();
};

html! {
Expand Down Expand Up @@ -78,7 +76,6 @@ pub fn comment(props: &Props) -> Html {
}

// CONTENT
// <p>{&props.comment.content}</p>
<p>{content.deref()}</p>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion devblog/devblog/client/src/components/navbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub fn navbar() -> Html {
<div class="nav-menus-container">
<Link<Route> to={Route::Home}>{"Home"}</Link<Route>>
<Link<Route> to={Route::Posts}>{"Posts"}</Link<Route>>
<Link<Route> to={Route::AddPost}>{"AddPost"}</Link<Route>>
<Link<Route> to={Route::About}>{"About"}</Link<Route>>

if store.admin {
Expand Down
141 changes: 134 additions & 7 deletions devblog/devblog/client/src/pages/post_add.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,144 @@
use stylist::Style;
use std::{fs::File, ops::Deref};

use crate::{router::Route, store::Store, Api};
use gloo::console::log;
use gloo_net::http::{Headers, Method};
// use gloo::file::File;
use stylist::{css, Style};
use yew::prelude::*;
use yew_router::components::Link;
use yewdux::use_store_value;

const STYLE: &str = include_str!("styles/signUp.css");
const STYLE: &str = include_str!("styles/postAdd.css");

#[function_component(AddPost)]
pub fn add_post() -> Html {
let style = Style::new(STYLE).unwrap();
let description = use_state(|| String::default());
let char_count = use_state(|| 0);
// let files = use_state(|| vec![File::open("Hello.txt").unwrap()]);
let loading = use_state(|| false);
let devblog = use_state(|| false);
let devblog_status = use_state(|| 0);
let devblog_err = use_state(|| String::default());
let mastodon = use_state(|| false);
let mastodon_status = use_state(|| 0);
let mastodon_err = use_state(|| String::default());
let discord = use_state(|| false);
let discord_status = use_state(|| 0);
let discord_err = use_state(|| String::default());
let store = use_store_value::<Store>();

// let char_count_clone = char_count.clone();
use_effect(move || {
// char_count_clone.set(description.len()); // causing page to not load
log!("FIRST RENDER");
});

let onsubmit = {
let store = store.clone();
let discord = *discord.clone();
let mastodon = *mastodon.clone();
let devblog = *devblog.clone();
Callback::from(move |e: SubmitEvent| {
e.prevent_default();
loading.set(true);
let auth = format!("Bearer {}", store.token);
let hdrs = Headers::new();
hdrs.append("Authorization", &auth);
hdrs.append("content-type", "application/json");

wasm_bindgen_futures::spawn_local(async move {
let response = Api::AddPost.fetch(Some(hdrs), None, Method::POST).await;

if let Some(res) = response {
if discord {}
if mastodon {}
if devblog {}
};
});
})
};

let upload_status = |platform: String, upload: bool, status: i32, err: String| -> Html {
html! {
<h4 class={match upload {true => css!("color: green;"), false => css!("color: red;")}}>{platform}{" Upload Status: "}
<span class={if (status).to_string().starts_with('2') {css!("color: green;")} else {css!("color: red;")}}>
{status}
</span>
if status != 200 {
<p>{err}</p>
}
</h4>
}
};

let upload_to = |platform: String, upload: bool, callback: Callback<Event>| -> Html {
html! {
<li>
<label>
<input
type="checkbox"
checked={upload}
onchange={callback}/>
{platform}
</label>
</li>
}
};

let onchange = { Callback::from(move |e: Event| {}) };

html! {
<section class={style}>
<div class="create-post">
<h1>{"Add Post Page"}</h1>
</div>
</section>
if store.admin {
<section class={style}>
<div class="create-post">
<form {onsubmit}>
<div class="upload-platforms">

// PLATFORM UPLOAD OPTIONS
<p>{"Upload to:"}</p>
<ul>
{upload_to("Discord".to_string(), *discord, onchange.clone())}
{upload_to("Mastodon".to_string(), *mastodon, onchange.clone())}
{upload_to("DevBlog".to_string(), *devblog, onchange)}
</ul>

<span class="loader">{"Loading..."}</span>

// UPLOAD STATUSES
<div class="upload-status">
{upload_status("Discord".to_string(), *discord.clone(), *discord_status, discord_err.deref().clone())}
{upload_status("Mastodon".to_string(), *mastodon.clone(), *mastodon_status, mastodon_err.deref().clone())}
{upload_status("DevBlog".to_string(), *devblog.clone(), *devblog_status, devblog_err.deref().clone())}
</div>
</div>

// NEW POST INFO
<div class="new-post-info">
<label>{"Image"}
<input type="file"
required=true
multiple=true/>
</label>

<label>{"Description"}
<p>{"Mastodon character limit: "}{*char_count}{"/500"}</p>
<p>{"Discord character limit: "}{*char_count}{"/2000"}</p>
<div class="addpost-description">
<textarea placeholder="Write description here..."/>
</div>
</label>

<p>{"Preview:"}</p>
<div class="post-preview">
<span>{"preview content:"}</span>
</div>
<button>{"Create Post"}</button>
</div>
</form>
</div>
</section>
}
}
}
9 changes: 9 additions & 0 deletions devblog/devblog/client/src/pages/posts.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
use crate::{
components::{pager::Pager, post::Post},
helpers::{self, CustomCallback},
router::Route,
store::Store,
Api, PostModel,
};
use gloo_net::http::Method;
use stylist::Style;
use yew::prelude::*;
use yew_router::components::Link;
use yewdux::use_store_value;

const STYLE: &str = include_str!("styles/posts.css");

#[function_component(Posts)]
pub fn posts() -> Html {
let style = Style::new(STYLE).unwrap();
let store = use_store_value::<Store>();
let loading = use_state(|| true);
let page_num = use_state(|| 1i32);
let posts = use_state(|| vec![PostModel::default()]);
Expand Down Expand Up @@ -76,6 +81,10 @@ pub fn posts() -> Html {
<div class="posts">
<Pager page_num={*page_num} on_click={&on_pager_click} total_pages={*pages_count}/>

if store.admin {
<Link<Route> to={Route::AddPost}>{"Create Post"}</Link<Route>>
}

// ALL POSTS
if *loading {
{for posts.iter().enumerate().map(|(idx, post)| html! {
Expand Down

0 comments on commit 3c19755

Please sign in to comment.