Skip to content

Commit

Permalink
Improve order flow (#467)
Browse files Browse the repository at this point in the history
* Simplify listing page form

* Make shipping option required in listing form input
  • Loading branch information
yzernik authored Jul 25, 2022
1 parent 06a255e commit 4417f94
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 66 deletions.
44 changes: 10 additions & 34 deletions src/listing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ struct Context {
base_context: BaseContext,
flash: Option<(String, String)>,
listing_display: ListingDisplay,
selected_shipping_option: Option<ShippingOption>,
user: Option<User>,
admin_user: Option<AdminUser>,
}
Expand All @@ -26,7 +25,6 @@ impl Context {
pub async fn raw(
mut db: Connection<Db>,
listing_id: &str,
shipping_option_id: Option<&str>,
flash: Option<(String, String)>,
user: Option<User>,
admin_user: Option<AdminUser>,
Expand All @@ -50,19 +48,10 @@ impl Context {
}
};

let maybe_shipping_option = match shipping_option_id {
Some(sid) => {
let shipping_option = ShippingOption::single_by_public_id(&mut db, sid).await;
shipping_option.ok()
}
None => None,
};

Ok(Context {
base_context,
flash,
listing_display,
selected_shipping_option: maybe_shipping_option,
user,
admin_user,
})
Expand All @@ -77,15 +66,12 @@ async fn submit(
) -> Result<Flash<Redirect>, Flash<Redirect>> {
match submit_listing(&mut db, id, user).await {
Ok(_) => Ok(Flash::success(
Redirect::to(uri!("/listing", index(id, Some("")))),
Redirect::to(uri!("/listing", index(id))),
"Marked as submitted".to_string(),
)),
Err(e) => {
error_!("Mark submitted({}) error: {}", id, e);
Err(Flash::error(
Redirect::to(uri!("/listing", index(id, Some("")))),
e,
))
Err(Flash::error(Redirect::to(uri!("/listing", index(id))), e))
}
}
}
Expand Down Expand Up @@ -128,15 +114,12 @@ async fn approve(
) -> Result<Flash<Redirect>, Flash<Redirect>> {
match approve_listing(&mut db, id).await {
Ok(_) => Ok(Flash::success(
Redirect::to(uri!("/listing", index(id, Some("")))),
Redirect::to(uri!("/listing", index(id))),
"Marked as approved".to_string(),
)),
Err(e) => {
error_!("Mark approved({}) error: {}", id, e);
Err(Flash::error(
Redirect::to(uri!("/listing", index(id, Some("")))),
e,
))
Err(Flash::error(Redirect::to(uri!("/listing", index(id))), e))
}
}
}
Expand Down Expand Up @@ -170,15 +153,12 @@ async fn reject(
) -> Result<Flash<Redirect>, Flash<Redirect>> {
match reject_listing(&mut db, id).await {
Ok(_) => Ok(Flash::success(
Redirect::to(uri!("/listing", index(id, Some("")))),
Redirect::to(uri!("/listing", index(id))),
"Marked as rejected".to_string(),
)),
Err(e) => {
error_!("Mark rejected({}) error: {}", id, e);
Err(Flash::error(
Redirect::to(uri!("/listing", index(id, Some("")))),
e,
))
Err(Flash::error(Redirect::to(uri!("/listing", index(id,))), e))
}
}
}
Expand Down Expand Up @@ -212,15 +192,12 @@ async fn remove(
) -> Result<Flash<Redirect>, Flash<Redirect>> {
match remove_listing(&mut db, id, user, admin_user).await {
Ok(_) => Ok(Flash::success(
Redirect::to(uri!("/listing", index(id, Some("")))),
Redirect::to(uri!("/listing", index(id))),
"Marked as removed".to_string(),
)),
Err(e) => {
error_!("Mark removed({}) error: {}", id, e);
Err(Flash::error(
Redirect::to(uri!("/listing", index(id, Some("")))),
e,
))
Err(Flash::error(Redirect::to(uri!("/listing", index(id))), e))
}
}
}
Expand All @@ -247,19 +224,18 @@ async fn remove_listing(
Ok(())
}

#[get("/<id>?<shipping_option_id>")]
#[get("/<id>")]
async fn index(
flash: Option<FlashMessage<'_>>,
id: &str,
shipping_option_id: Option<&str>,
db: Connection<Db>,
user: Option<User>,
admin_user: Option<AdminUser>,
) -> Template {
let flash = flash.map(FlashMessage::into_inner);
Template::render(
"listing",
Context::raw(db, id, shipping_option_id, flash, user, admin_user).await,
Context::raw(db, id, flash, user, admin_user).await,
)
}

Expand Down
22 changes: 14 additions & 8 deletions src/prepare_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl Context {
let seller_user_settings = UserSettings::single(&mut db, listing_display.listing.user_id)
.await
.map_err(|_| "failed to get visited user settings.")?;
println!("quantity: {:?}", quantity);
Ok(Context {
base_context,
flash,
Expand Down Expand Up @@ -92,7 +93,7 @@ async fn new(
Err(Flash::error(
Redirect::to(uri!(
"/prepare_order",
index(id, Some(order_info.shipping_option_id), Some(1))
index(id, order_info.shipping_option_id, 1)
)),
e,
))
Expand Down Expand Up @@ -223,20 +224,25 @@ fn divide_round_up(dividend: u64, divisor: u64) -> u64 {
async fn index(
flash: Option<FlashMessage<'_>>,
id: &str,
shipping_option_id: Option<&str>,
quantity: Option<i32>,
shipping_option_id: &str,
quantity: usize,
db: Connection<Db>,
user: User,
admin_user: Option<AdminUser>,
) -> Template {
// TODO: Don't use unwrap.
let sid = shipping_option_id.unwrap();
let quantity = quantity.unwrap_or(0);

let flash = flash.map(FlashMessage::into_inner);
Template::render(
"prepareorder",
Context::raw(db, id, sid, quantity, flash, user, admin_user).await,
Context::raw(
db,
id,
shipping_option_id,
quantity.try_into().unwrap(),
flash,
user,
admin_user,
)
.await,
)
}

Expand Down
28 changes: 11 additions & 17 deletions templates/listing.html.tera
Original file line number Diff line number Diff line change
Expand Up @@ -83,38 +83,32 @@
<h3>{{ listing_display.listing.title }}</h3>
<p><b>Seller</b>: <a href="/user/{{listing_display.user.username}}">{{ listing_display.user.username }}</a></p>
<p><b>Price</b>: {{ listing_display.listing.price_sat }} sats</p>
{% if selected_shipping_option %}
<p><b>Price per unit with shipping</b>: {{ listing_display.listing.price_sat + selected_shipping_option.price_sat }} sats</p>
<p><b>Shipping Option</b>: {{ selected_shipping_option.title }} ({{ selected_shipping_option.price_sat }} sats)</p>
<p><b>Shipping Option Description</b>: {{ selected_shipping_option.description }}</p>
{% endif %}

{% if admin_user or user and listing_display.listing.user_id == user.id %}
<p><b>Fee Rate</b>: Market will collect a {{ listing_display.listing.fee_rate_basis_points / 100 }}% fee rate</p>
{% endif %}

<form action="" method="get">
<label for="shipping_options">Choose a shipping option:</label>
<select id="shipping_option_id" name="shipping_option_id">
<p><b>Description</b>: {{ listing_display.listing.description }}</p>

{% if listing_display.listing.approved %}
<form action="/prepare_order/{{ listing_display.listing.public_id }}" method="get">

<label for="shipping_options">choose a shipping option:</label>
<select required id="shipping_option_id" name="shipping_option_id">
<option disabled selected value> -- select an option -- </option>
{% for shipping_option in listing_display.shipping_options %}
<option value="{{ shipping_option.public_id }}" {% if selected_shipping_option and shipping_option.id == selected_shipping_option.id %}selected{% endif %}>{{ shipping_option.title }} ({{ shipping_option.price_sat }} sats)</option>
{% endfor %}
</select>
<input type="submit" value="Update Shipping Option">
</form>

{% if selected_shipping_option %}
{% if listing_display.listing.approved %}
<form action="/prepare_order/{{ listing_display.listing.public_id }}" method="get">
<input type="hidden" id="shipping_option_id" name="shipping_option_id" value="{{ selected_shipping_option.public_id }}">
<input type="hidden" id="quantity" name="quantity" value="1">
<label for="quantity">Choose quantity:</label>
<input type="number" placeholder="enter the quantity of items..."
name="quantity" id="quantity" value="1"
class="u-full-width" />
<input type="submit" value="Prepare Order">
</form>
{% endif %}
{% endif %}

<p><b>Description</b>: {{ listing_display.listing.description }}</p>
</div>
</div>
</div>
Expand Down
12 changes: 5 additions & 7 deletions templates/prepareorder.html.tera
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@
<p><a href="/listing/{{ listing_display.listing.public_id }}?shipping_option_id={{ selected_shipping_option.public_id }}">Back to Listing</a></p>

<p><b>Listing</b>: {{ listing_display.listing.title }} ({{ listing_display.listing.price_sat }} sats)</p>
<p><b>Quantity</b>: {{ quantity }}</p>
<p><b>Shipping Option</b>: {{ selected_shipping_option.title }} ({{ selected_shipping_option.price_sat }} sats)</p>
<p><b>Shipping option description</b>: {{ selected_shipping_option.description }}</p>

<p><b>Price per unit with shipping</b>: {{ listing_display.listing.price_sat }} sats + {{ selected_shipping_option.price_sat }} sats = <b>{{ selected_shipping_option.price_sat + listing_display.listing.price_sat }} sats</b></p>
<p><b>Total Price</b>: {{ quantity }} x ({{ listing_display.listing.price_sat }} sats + {{ selected_shipping_option.price_sat }} sats) = <b>{{ quantity * (selected_shipping_option.price_sat + listing_display.listing.price_sat) }} sats</b></p>

<p><b>Seller PGP Key</b>: {% if seller_user_settings.pgp_key %}<pre>{{ seller_user_settings.pgp_key }}</pre>{% else %}Not set{% endif %}</p>

<form action="/prepare_order/{{ listing_display.listing.public_id }}/new" method="post">
<input type="hidden" id="shipping_option_id" name="shipping_option_id" value="{{ selected_shipping_option.public_id }}">

<label for="quantity">Choose quantity:</label>
<input type="number" placeholder="enter the quantity of items..."
name="quantity" id="quantity" value="{{ quantity }}"
class="u-full-width" />

<input type="hidden"
name="quantity" id="quantity" value="{{ quantity }}" />
<label for="shipping_instructions">Please enter your name and address as it would apper on a letter (encrypt with the seller PGP key):</label>
<textarea placeholder="shipping instructions..."
name="shipping_instructions" id="shipping_instructions" placeholder="enter shipping instructions ..." value=""
Expand Down

0 comments on commit 4417f94

Please sign in to comment.