feat: add create post requests

This commit is contained in:
Kasper Juul Hermansen 2025-01-19 22:28:20 +01:00
parent c0e8d0f4ee
commit aa64a850e6
Signed by: kjuulh
SSH Key Fingerprint: SHA256:RjXh0p7U6opxnfd3ga/Y9TCo18FYlHFdSpRIV72S/QM
5 changed files with 126 additions and 213 deletions

234
Cargo.lock generated
View File

@ -75,6 +75,7 @@ dependencies = [
"serde_json",
"server_fn",
"thiserror 2.0.11",
"uuid",
"web-sys",
]
@ -249,9 +250,9 @@ checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "bitflags"
version = "2.7.0"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1be3f42a67d6d345ecd59f675f3f012d6974981560836e938c22b424b85ce1be"
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
[[package]]
name = "brotli"
@ -300,9 +301,9 @@ checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3"
[[package]]
name = "cc"
version = "1.2.8"
version = "1.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0cf6e91fde44c773c6ee7ec6bba798504641a8bc2eb7e37a04ffbf4dfaa55a"
checksum = "13208fcbb66eaeffe09b99fffbe1af420f00a7b35aa99ad683dfc1aa76145229"
dependencies = [
"jobserver",
"libc",
@ -537,16 +538,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "erased-serde"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24e2389d65ab4fab27dc2a5de7b191e1f6617d1f1c8855c0dc569c94a4cbb18d"
dependencies = [
"serde",
"typeid",
]
[[package]]
name = "errno"
version = "0.3.10"
@ -1220,9 +1211,9 @@ dependencies = [
[[package]]
name = "leptos"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8a90c679094979aa12927e8e925fe8eead1420d69420b2d8c6540863937ca75"
checksum = "21c31c9d022c77702c53e02830d08b28320aca9c0899a19c443096c114623fa5"
dependencies = [
"any_spawner",
"base64",
@ -1258,9 +1249,9 @@ dependencies = [
[[package]]
name = "leptos_axum"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae4386e955d6ba7c3c8d09583e99044050f6fb9a48b8d4096af948fdb3345434"
checksum = "43b613d5784037baee42a11d21bc263adfc1a55e416556a3d5bfe39c7b87fadf"
dependencies = [
"any_spawner",
"axum",
@ -1282,9 +1273,9 @@ dependencies = [
[[package]]
name = "leptos_config"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c712e1ed3283d1acb842cef4cbcce7b2987a347cc1bf7141195e01f92bb8590d"
checksum = "5d874993c7664d757677d056c8f46b5cb5365fe622005e1bf26050f4996e7e52"
dependencies = [
"config",
"regex",
@ -1295,9 +1286,9 @@ dependencies = [
[[package]]
name = "leptos_dom"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99803be421344a2184fd5796e1a7645c2090738b2ab5d1a856084816853ec322"
checksum = "a462aaeec85bc4ecfb26bf324437b92690bf3add1e30eb29b3acc08b20e8b4cb"
dependencies = [
"js-sys",
"or_poisoned",
@ -1310,9 +1301,9 @@ dependencies = [
[[package]]
name = "leptos_hot_reload"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92413c6d93a22d8c4e75b2e7da3867af173e8d9ca49ce170b631e7c2766eef48"
checksum = "07eb295ad2f3b2af190da62af339b84fd01ce3c71702f09eb69a57310fcf0c6d"
dependencies = [
"anyhow",
"camino",
@ -1328,9 +1319,9 @@ dependencies = [
[[package]]
name = "leptos_integration_utils"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "692fbf6dc5358d766932c6e58b30df53cfde916a9f4d20d4bb0cfc1bbf7f85a1"
checksum = "a8652fcd7a1744f85403b95c5520143f3b962d640c8450b8514f9530fb5c4b76"
dependencies = [
"futures",
"hydration_context",
@ -1343,9 +1334,9 @@ dependencies = [
[[package]]
name = "leptos_macro"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20bcb2afa03e0614c64eec4a95ec2986fd3c59358daa0f50006e081bc1bd1067"
checksum = "90291b25ee576bc9c299d3371cc8f09bf60ea939a8de61fa8b744650aff76e24"
dependencies = [
"attribute-derive",
"cfg-if",
@ -1365,9 +1356,9 @@ dependencies = [
[[package]]
name = "leptos_meta"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a54d4b942a474e38b606ff0bfe8712f093300bd053adb9dd7a028a14ca3fbfac"
checksum = "7250991b2077ef5869e999c74cf4990926a3c3919b50c9937e101c1c874102db"
dependencies = [
"futures",
"indexmap",
@ -1381,9 +1372,9 @@ dependencies = [
[[package]]
name = "leptos_router"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69ff7d8c058b4bd7512fa58224b684d5da10d5f056171b8e27d516e0d36e1a5d"
checksum = "9a193dbd62b9617a5d7d199ea70c570da01a1bbe798e617373b6351845be6778"
dependencies = [
"any_spawner",
"either_of",
@ -1406,9 +1397,9 @@ dependencies = [
[[package]]
name = "leptos_router_macro"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15a48035e8d796233a5b43322aeab9387f946f914dacdf17a1981c3910879b19"
checksum = "34bc3f80ad810b22058f12d278bb0bf929779cc0bc1289a06980d896f62743f0"
dependencies = [
"proc-macro-error2",
"proc-macro2",
@ -1417,9 +1408,9 @@ dependencies = [
[[package]]
name = "leptos_server"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fb23bd110ac04c7276aae3d8ba523f94cf06989d00b4e76eaee89451b06b494"
checksum = "18caffe32c245ddb35697edd898ccb3393efce67672a707a14eebd0db2e8249a"
dependencies = [
"any_spawner",
"base64",
@ -1471,12 +1462,9 @@ dependencies = [
[[package]]
name = "log"
version = "0.4.24"
version = "0.4.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6ea2a48c204030ee31a7d7fc72c93294c92fe87ecb1789881c9543516e1a0d"
dependencies = [
"value-bag",
]
checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f"
[[package]]
name = "manyhow"
@ -1537,9 +1525,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "miniz_oxide"
version = "0.8.2"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ffbe83022cedc1d264172192511ae958937694cd57ce297164951b8b3568394"
checksum = "b8402cab7aefae129c6977bb0ff1b8fd9a04eb5b51efc50a70bea51cda0c7924"
dependencies = [
"adler2",
]
@ -1816,9 +1804,9 @@ dependencies = [
[[package]]
name = "prettyplease"
version = "0.2.27"
version = "0.2.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "483f8c21f64f3ea09fe0f30f5d48c3e8eefe5dac9129f0075f76593b4c1da705"
checksum = "6924ced06e1f7dfe3fa48d57b9f74f55d8915f5036121bef647ef4b204895fac"
dependencies = [
"proc-macro2",
"syn",
@ -1965,9 +1953,9 @@ dependencies = [
[[package]]
name = "reactive_graph"
version = "0.1.3"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bee22d7574c73fbfd47d828ee14dc67ca65606ade81de2f8d1691741072a93b"
checksum = "9fbf210c04505e128fb7f64acecc23c71f82f56c7d481b190e1010b7bada2cb9"
dependencies = [
"any_spawner",
"async-lock",
@ -2276,20 +2264,11 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_fmt"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1d4ddca14104cd60529e8c7f7ba71a2c8acd8f7f5cfcdc2faf97eeb7c3010a4"
dependencies = [
"serde",
]
[[package]]
name = "serde_json"
version = "1.0.135"
version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b0d7ba2887406110130a978386c4e1befb98c674b4fba677954e4db976630d9"
checksum = "336a0c23cf42a38d9eaa7cd22c7040d04e1228a19a933890805ffd00a16437d2"
dependencies = [
"itoa",
"memchr",
@ -2341,9 +2320,9 @@ dependencies = [
[[package]]
name = "server_fn"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0b9f0d2eecb2bf4f909661acc731009e3657574dec93a0ec9f114e250f74bc4"
checksum = "f5dd7fcccd3ef2081da086c1f8595b506627abbbbc9f64be0141d2251219570e"
dependencies = [
"axum",
"bytes",
@ -2377,9 +2356,9 @@ dependencies = [
[[package]]
name = "server_fn_macro"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee7723bef57b4353cd9939e280d3b5b2ebe45b4a4630c9e9e97a6fa4b84e8b1c"
checksum = "e0bbac4f01a714b0490247ac625bdb7055548210556c39e8f56a2dbbe3abc70b"
dependencies = [
"const_format",
"convert_case",
@ -2391,9 +2370,9 @@ dependencies = [
[[package]]
name = "server_fn_macro_default"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87663ec10f17fbe8f6c53adc2d038df3304bfd17aaaab22f777810a9e6e05fff"
checksum = "f07dfd1744a5f5612f00f69fe035b0bfafdf12bb46d76e785673078a9e56b170"
dependencies = [
"server_fn_macro",
"syn",
@ -2478,84 +2457,6 @@ version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "sval"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6dc0f9830c49db20e73273ffae9b5240f63c42e515af1da1fceefb69fceafd8"
[[package]]
name = "sval_buffer"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "429922f7ad43c0ef8fd7309e14d750e38899e32eb7e8da656ea169dd28ee212f"
dependencies = [
"sval",
"sval_ref",
]
[[package]]
name = "sval_dynamic"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68f16ff5d839396c11a30019b659b0976348f3803db0626f736764c473b50ff4"
dependencies = [
"sval",
]
[[package]]
name = "sval_fmt"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c01c27a80b6151b0557f9ccbe89c11db571dc5f68113690c1e028d7e974bae94"
dependencies = [
"itoa",
"ryu",
"sval",
]
[[package]]
name = "sval_json"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0deef63c70da622b2a8069d8600cf4b05396459e665862e7bdb290fd6cf3f155"
dependencies = [
"itoa",
"ryu",
"sval",
]
[[package]]
name = "sval_nested"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a39ce5976ae1feb814c35d290cf7cf8cd4f045782fe1548d6bc32e21f6156e9f"
dependencies = [
"sval",
"sval_buffer",
"sval_ref",
]
[[package]]
name = "sval_ref"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb7c6ee3751795a728bc9316a092023529ffea1783499afbc5c66f5fabebb1fa"
dependencies = [
"sval",
]
[[package]]
name = "sval_serde"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a5572d0321b68109a343634e3a5d576bf131b82180c6c442dee06349dfc652a"
dependencies = [
"serde",
"sval",
"sval_nested",
]
[[package]]
name = "syn"
version = "2.0.96"
@ -2622,9 +2523,9 @@ dependencies = [
[[package]]
name = "tachys"
version = "0.1.3"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "761f12c13d74f1b723e3c53ff70fe291d15fe51cc4b6586aafea974f0b647043"
checksum = "d777e4426a597296b020edcb5c3d8f25a3ccd8adfd22eb5154ac81da946aef9f"
dependencies = [
"any_spawner",
"const_str_slice_concat",
@ -2963,12 +2864,6 @@ dependencies = [
"syn",
]
[[package]]
name = "typeid"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e13db2e0ccd5e14a544e8a246ba2312cd25223f616442d7f2cb0e3db614236e"
[[package]]
name = "unicase"
version = "2.8.1"
@ -3030,47 +2925,12 @@ checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "uuid"
version = "1.11.1"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b913a3b5fe84142e269d63cc62b64319ccaf89b748fc31fe025177f767a756c4"
checksum = "744018581f9a3454a9e15beb8a33b017183f1e7c0cd170232a2d1453b23a51c4"
dependencies = [
"getrandom",
]
[[package]]
name = "value-bag"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ef4c4aa54d5d05a279399bfa921ec387b7aba77caf7a682ae8d86785b8fdad2"
dependencies = [
"value-bag-serde1",
"value-bag-sval2",
]
[[package]]
name = "value-bag-serde1"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bb773bd36fd59c7ca6e336c94454d9c66386416734817927ac93d81cb3c5b0b"
dependencies = [
"erased-serde",
"serde",
"serde_fmt",
]
[[package]]
name = "value-bag-sval2"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53a916a702cac43a88694c97657d449775667bcd14b70419441d05b7fea4a83a"
dependencies = [
"sval",
"sval_buffer",
"sval_dynamic",
"sval_fmt",
"sval_json",
"sval_ref",
"sval_serde",
]
[[package]]

View File

@ -9,14 +9,14 @@ lto = true
opt-level = 'z'
[workspace.dependencies]
leptos = { version = "0.7.2", features = ["nightly"] }
leptos_meta = { version = "0.7.2" }
leptos_router = { version = "0.7.2", features = ["nightly"] }
leptos_axum = { version = "0.7.2" }
server_fn = { version = "0.7.2", features = [] }
leptos = { version = "0.7.4", features = ["nightly"] }
leptos_meta = { version = "0.7.4" }
leptos_router = { version = "0.7.4", features = ["nightly"] }
leptos_axum = { version = "0.7.4" }
server_fn = { version = "0.7.4", features = [] }
serde = { version = "1", features = ["derive"] }
axum = "0.8"
axum = "0.7.9"
cfg-if = "1"
console_error_panic_hook = "0.1.7"
console_log = "1"
@ -28,6 +28,7 @@ tokio = { version = "1.37.0", features = ["full"] }
tower = { version = "0.5.0", features = ["full"] }
tower-http = { version = "0.6", features = ["full"] }
wasm-bindgen = "=0.2.100"
uuid = { version = "1.1.2", features = ["serde", "v4"] }
# See https://github.com/akesson/cargo-leptos for documentation of all the parameters.

View File

@ -18,6 +18,7 @@ serde.workspace = true
http.workspace = true
cfg-if.workspace = true
thiserror.workspace = true
uuid.workspace = true
reqwest = { version = "0.12.11", optional = true, features = ["json"] }
serde_json = { version = "1.0.134", optional = true }
web-sys = { version = "0.3.76", features = [

View File

@ -49,7 +49,9 @@ pub fn ErrorTemplate(
cfg_if! { if #[cfg(feature="ssr")] {
let response = use_context::<ResponseOptions>();
if let Some(response) = response {
response.set_status(errors[0].status_code());
if let Some(resp) = errors.first() {
response.set_status(resp.status_code());
}
}
}}

View File

@ -1,3 +1,5 @@
use std::any::Any;
use crate::error_template::{AppError, ErrorTemplate};
use leptos::{either::Either, ev::SubmitEvent, html, prelude::*};
@ -71,7 +73,18 @@ fn smooth_scroll_to_bottom() {
#[component]
pub fn HomePage() -> impl IntoView {
let messages = Resource::new(|| (), move |_| get_messages());
let conversation_id = uuid::Uuid::new_v4();
let send_message = ServerAction::<SendMessage>::new();
let messages = Resource::new(
move || (send_message.version().get()),
move |_| {
get_messages(GetMessagesRequest {
conversation_id: conversation_id.clone(),
})
},
);
let existing_messages = move || {
Suspend::new(async move {
@ -111,19 +124,30 @@ pub fn HomePage() -> impl IntoView {
})
};
let (_, set_messages) = signal(
message::get_messages()
.into_iter()
.enumerate()
.map(|(index, value)| (index, ArcRwSignal::new(value)))
.collect::<Vec<_>>(),
);
// let (_, set_messages) = signal(
// message::get_messages()
// .into_iter()
// .enumerate()
// .map(|(index, value)| (index, ArcRwSignal::new(value)))
// .collect::<Vec<_>>(),
// );
let (input, set_input) = signal("".to_string());
let on_submit = move |ev: SubmitEvent| {
// stop the page from reloading!
ev.prevent_default();
leptos::logging::log!("sending request");
send_message.dispatch(SendMessage {
request: SendMessageRequest {
conversation_id: Some(conversation_id.clone()),
role: "user".into(),
content: input.get(),
},
});
set_input.set("".into());
// let messages_len = messages.get().len();
// let mut messages = set_messages.write();
// messages.push((
@ -134,8 +158,6 @@ pub fn HomePage() -> impl IntoView {
// }),
// ));
// set_input.set("".into());
// request_animation_frame(move || {
// smooth_scroll_to_bottom();
// });
@ -196,20 +218,47 @@ pub fn HomePage() -> impl IntoView {
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct GetMessagesRequest {
conversation_id: uuid::Uuid,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Messages {
pub messages: Vec<Message>,
}
#[server]
pub async fn get_messages() -> Result<Messages, ServerFnError> {
let messages: Messages =
reqwest::get("https://lebusiness-service.prod.kjuulh.app/api/messages")
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
.json()
.await
.map_err(|e| ServerFnError::new(e.to_string()))?;
pub async fn get_messages(req: GetMessagesRequest) -> Result<Messages, ServerFnError> {
let messages: Vec<Message> = reqwest::get(format!(
"https://lebusiness-service.prod.kjuulh.app/api/messages?conversation_id={}",
req.conversation_id,
))
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
.json()
.await
.map_err(|e| ServerFnError::new(e.to_string()))?;
Ok(messages)
Ok(Messages { messages })
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct SendMessageRequest {
conversation_id: Option<uuid::Uuid>,
role: String,
content: String,
}
#[server]
pub async fn send_message(request: SendMessageRequest) -> Result<(), ServerFnError> {
let client = reqwest::Client::new();
client
.post("https://lebusiness-service.prod.kjuulh.app/api/messages")
.json(&request)
.send()
.await
.map_err(|e| ServerFnError::new(e.to_string()))?;
Ok(())
}