use crate::types::*; use webcomment_common::{api::*, types::*}; use gloo::{console, net::http}; use parking_lot::RwLock; use std::collections::HashMap; pub struct ApiInner { pub admin_psw: Option, pub comments: HashMap, pub url: String, } pub struct Api { pub inner: RwLock, } impl Api { pub async fn get_comments_by_topic(&self, topic: String) { match http::Request::post(&format!("{}/api/comments_by_topic", &self.inner.read().url)) .body( serde_json::to_string(&queries::CommentsByTopic { mutation_token: None, topic, }) .unwrap(), ) .send() .await { Ok(resp) => { let Ok(Ok(resps::Response::CommentsByTopic(resp))) = resp.json::().await else { // TODO error return; }; let mut inner = self.inner.write(); for comment in resp.approved_comments { let Ok(comment_id) = CommentId::from_base64(&comment.id) else { continue }; inner.comments.insert( comment_id, StoredComment { author: comment.author, email: comment.email, last_edit_time: comment.last_edit_time, post_time: comment.post_time, text: comment.text, }, ); } } Err(e) => console::log!("get_comments_by_topic: {e:?}"), } } }