| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
use makeover_layout as layout; |
| 33 |
use quasi_router::screen::Field; |
| 34 |
use quasi_router::{Action, Node, RegionKind, Slot}; |
| 35 |
|
| 36 |
|
| 37 |
pub const REGION: &str = "tip-offer"; |
| 38 |
|
| 39 |
|
| 40 |
const FORM_REGION: &str = "tip-form"; |
| 41 |
|
| 42 |
|
| 43 |
pub struct Offer<'a> { |
| 44 |
|
| 45 |
pub creator_id: &'a str, |
| 46 |
|
| 47 |
pub project_id: Option<&'a str>, |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
pub signed_in: bool, |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#[must_use] |
| 59 |
pub fn control(offer: &Offer<'_>) -> Node { |
| 60 |
if !offer.signed_in { |
| 61 |
return Node::Region( |
| 62 |
Slot::new(REGION, RegionKind::Group) |
| 63 |
.with(Node::act("Support", Action::get("/login").navigating())), |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
let mut amount = Field::new(layout::FieldKind::Number, "amount_dollars", "Tip amount") |
| 68 |
.required() |
| 69 |
.value("1") |
| 70 |
.unit("$"); |
| 71 |
amount.min = Some("1".to_owned()); |
| 72 |
amount.max = Some("9999".to_owned()); |
| 73 |
|
| 74 |
let mut message = Field::new(layout::FieldKind::Textarea, "message", "Message (optional)"); |
| 75 |
message.placeholder = Some("Add a message (optional)".to_owned()); |
| 76 |
message.max_length = Some(280); |
| 77 |
|
| 78 |
let mut action = Action::post(format!("/stripe/checkout/tip/{}", offer.creator_id)); |
| 79 |
if let Some(project) = offer.project_id { |
| 80 |
action = action.with("project_id", project); |
| 81 |
} |
| 82 |
|
| 83 |
Node::Region( |
| 84 |
Slot::new(REGION, RegionKind::Group) |
| 85 |
.showing_at_most_one(None) |
| 86 |
.with(Node::Region( |
| 87 |
Slot::new(FORM_REGION, RegionKind::Group) |
| 88 |
.label("Support") |
| 89 |
.with(Node::Form { |
| 90 |
action, |
| 91 |
submit: "Send tip".to_owned(), |
| 92 |
fields: vec![amount, message], |
| 93 |
}), |
| 94 |
)), |
| 95 |
) |
| 96 |
} |
| 97 |
|
| 98 |
#[cfg(test)] |
| 99 |
mod tests { |
| 100 |
use super::*; |
| 101 |
use quasi_webview::Webview; |
| 102 |
|
| 103 |
fn html(node: &Node) -> String { |
| 104 |
use quasi_axum::Serves as _; |
| 105 |
Webview::new().fragment(node) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
#[test] |
| 111 |
fn a_signed_out_reader_is_sent_to_sign_in() { |
| 112 |
let rendered = html(&control(&Offer { |
| 113 |
creator_id: "c1", |
| 114 |
project_id: None, |
| 115 |
signed_in: false, |
| 116 |
})); |
| 117 |
assert!(rendered.contains("/login"), "{rendered}"); |
| 118 |
assert!(!rendered.contains("amount_dollars"), "{rendered}"); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
#[test] |
| 123 |
fn the_form_keeps_the_limits_the_partial_wrote() { |
| 124 |
let rendered = html(&control(&Offer { |
| 125 |
creator_id: "c1", |
| 126 |
project_id: None, |
| 127 |
signed_in: true, |
| 128 |
})); |
| 129 |
assert!(rendered.contains("name=\"amount_dollars\""), "{rendered}"); |
| 130 |
assert!(rendered.contains("min=\"1\""), "{rendered}"); |
| 131 |
assert!(rendered.contains("max=\"9999\""), "{rendered}"); |
| 132 |
assert!(rendered.contains("maxlength=\"280\""), "{rendered}"); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
#[test] |
| 138 |
fn a_tip_from_a_project_page_carries_the_project() { |
| 139 |
let rendered = html(&control(&Offer { |
| 140 |
creator_id: "c1", |
| 141 |
project_id: Some("p9"), |
| 142 |
signed_in: true, |
| 143 |
})); |
| 144 |
assert!(rendered.contains("project_id"), "{rendered}"); |
| 145 |
assert!(rendered.contains("p9"), "{rendered}"); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
#[test] |
| 151 |
fn the_form_carries_no_hidden_token() { |
| 152 |
let rendered = html(&control(&Offer { |
| 153 |
creator_id: "c1", |
| 154 |
project_id: None, |
| 155 |
signed_in: true, |
| 156 |
})); |
| 157 |
assert!(!rendered.contains("_csrf"), "{rendered}"); |
| 158 |
assert!(!rendered.contains("type=\"hidden\""), "{rendered}"); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
#[test] |
| 164 |
fn the_offer_opens_without_a_script_of_its_own() { |
| 165 |
let rendered = html(&control(&Offer { |
| 166 |
creator_id: "c1", |
| 167 |
project_id: None, |
| 168 |
signed_in: true, |
| 169 |
})); |
| 170 |
assert!(!rendered.contains("toggleTipForm"), "{rendered}"); |
| 171 |
assert!(rendered.contains("data-shows"), "{rendered}"); |
| 172 |
} |
| 173 |
} |
| 174 |
|