recipes/frontend/src/shopping_list.rs
2025-03-18 20:03:25 +01:00

51 lines
1.2 KiB
Rust

use chrono::{Datelike, Days, Months, NaiveDate};
use common::ron_api;
use gloo::storage::{LocalStorage, Storage};
use ron::ser::{PrettyConfig, to_string_pretty};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::{calendar, request};
#[derive(Error, Debug)]
pub enum Error {
#[error("Request error: {0}")]
Request(#[from] request::Error),
}
type Result<T> = std::result::Result<T, Error>;
#[derive(Clone, Copy)]
pub struct ShoppingList {
is_local: bool,
}
impl ShoppingList {
pub fn new(is_local: bool) -> Self {
Self { is_local }
}
pub async fn get_items(&self) -> Result<Vec<ron_api::ShoppingListItem>> {
if self.is_local {
Ok(vec![]) // TODO
} else {
Ok(request::get("shopping_list/get_list", ()).await?)
}
}
pub async fn set_item_checked(&self, item_id: i64, is_checked: bool) -> Result<()> {
if self.is_local {
todo!();
} else {
request::patch(
"shopping_list/set_checked",
ron_api::Value {
id: item_id,
value: is_checked,
},
)
.await
.map_err(Error::from)
}
}
}