Fix branch

This commit is contained in:
Adrian Woźniak 2023-10-11 11:21:33 +02:00
parent 1f18ba54d3
commit d4135d425f
3 changed files with 59 additions and 27 deletions

View File

@ -28,6 +28,9 @@ pub fn mount(config: &mut ServiceConfig) {
config.service(root).service( config.service(root).service(
scope("/parking-spaces") scope("/parking-spaces")
.service(parking_space_rent_form) .service(parking_space_rent_form)
.service(parking_space_rent_edit)
.service(parking_space_rent_create)
.service(parking_space_rent_update)
.service(form_show) .service(form_show)
.service(all_parking_spaces) .service(all_parking_spaces)
.service(create) .service(create)
@ -459,6 +462,7 @@ async fn update(
struct ParkingSpaceRentForm { struct ParkingSpaceRentForm {
pub id: Option<i32>, pub id: Option<i32>,
pub price: Option<i32>, pub price: Option<i32>,
pub price_f: Option<f64>,
} }
#[derive(Debug, Template)] #[derive(Debug, Template)]
@ -557,11 +561,39 @@ async fn parking_space_rent_form(
async fn parking_space_rent_edit(_id: Path<i32>) -> HttpResponse { async fn parking_space_rent_edit(_id: Path<i32>) -> HttpResponse {
todo!() todo!()
} }
#[post("/parking-space-rents/create")] #[post("/{parking_space_id}/parking-space-rents/create")]
async fn parking_space_rent_create(_form: Form<ParkingSpaceRentForm>) -> HttpResponse { async fn parking_space_rent_create(form: Form<ParkingSpaceRentForm>) -> HttpResponse {
let mut form = form.into_inner();
form.price = form.price_f.map(|n| n * 100.0).map(|n| n as i32);
todo!() todo!()
} }
#[put("/parking-space-rents/update")] #[put("/{parking_space_id}/parking-space-rents/update")]
async fn parking_space_rent_update(_form: Form<ParkingSpaceRentForm>) -> HttpResponse { async fn parking_space_rent_update(form: Form<ParkingSpaceRentForm>) -> HttpResponse {
let mut form = form.into_inner();
form.price = form.price_f.map(|n| n * 100.0).map(|n| n as i32);
todo!() todo!()
} }
fn render_rent_form(form: ParkingSpaceRentForm, req: &HttpRequest) -> String {
let body = ParkingSpaceRentFormTemplate {
parking_space,
location,
form,
lang,
t: t.into_inner(),
};
let main = Main {
body,
title: Blank,
opts: MainOpts {
show: true,
search: None,
session,
},
};
if is_partial(&req) {
main.render()
} else {
Layout { main }.render()
}
}

View File

@ -6,12 +6,12 @@
<div class="max-w-md w-full p-6 bg-white rounded-lg shadow-lg"> <div class="max-w-md w-full p-6 bg-white rounded-lg shadow-lg">
<form <form
{% if let Some(id) = form.id %} {% if let Some(id) = form.id %}
hx-put='/parking-space-rents/update/{{id}}' hx-put='/parking-spaces/{{parking_space.id}}/parking-space-rents/update/{{id}}'
{% else %} {% else %}
hx-post='/parking-space-rents/create' hx-post='/parking-spaces/{{parking_space.id}}/parking-space-rents/create'
{% endif %} {% endif %}
hx-headers="{'Accept':'text/html-partial'}" hx-headers="{'Accept':'text/html-partial'}"
> >
{% if let Some(id) = form.id %} {% if let Some(id) = form.id %}
<input type="hidden" value="{{id}}" name="id" /> <input type="hidden" value="{{id}}" name="id" />
{% endif %} {% endif %}
@ -22,39 +22,33 @@
<label <label
for="price" for="price"
class="block mb-2 text-sm text-gray-600" class="block mb-2 text-sm text-gray-600"
> >
{{ "Price"|t(lang,t) }} {{ "Price"|t(lang,t) }}
</label> </label>
<div class="flex w-full"> <div class="flex w-full">
<input <input
id="price-major" id="price"
name="price-major" name="price_f"
class="px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-cyan-500" class="px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-cyan-500"
type="number" type="number"
step="0.01"
min="0.00"
max="1000000.00"
required required
{% if let Some(v) = form.price %} value="{{ v / 100 }}" {% endif %} {% if let Some(v) = form.price %} value="{{ v|display_price }}" {% endif %}
/>
<span class="m-1 text-lg font-medium">.</span>
<input
id="price-minor
name="price-minor"
class="px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-cyan-500"
type="number"
required
{% if let Some(v) = form.price %} value="{{ v % 100 }}" {% endif %}
/> />
<span class="m-1">PLN</span> <span class="m-1">PLN</span>
</div> </div>
</div> </div>
<div class="mb-6"> <div class="mb-6">
<input <input
type="submit" type="submit"
{% if let Some(id) = form.id %} {% if let Some(id) = form.id %}
value="{{"Update parking space rent"|t(lang,t)}}" value="{{"Update parking space rent"|t(lang,t)}}"
{% else %} {% else %}
value="{{"Register parking space rent"|t(lang,t)}}" value="{{"Register parking space rent"|t(lang,t)}}"
{% endif %} {% endif %}
class="w-64 bg-cyan-600 text-white py-2 rounded-lg mx-auto block focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 mb-2" class="w-64 bg-cyan-600 text-white py-2 rounded-lg mx-auto block focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-cyan-500 mb-2"
/> />
</div> </div>
</form> </form>

View File

@ -14,3 +14,9 @@ pub fn skip_if<T: std::fmt::Display + PartialEq + Copy>(n: &T, skip: T) -> Resul
n.to_string() n.to_string()
}) })
} }
#[tracing::instrument]
pub fn display_price(n: &&i32) -> Result<String> {
let n: i32 = **n;
Ok(format!("{}.{}", n / 100, n % 100))
}