Skip to content

Commit

Permalink
Introduce LoginArgs instead of passing username and password around…
Browse files Browse the repository at this point in the history
… in tuples or individually.

* where variables are frequently/always used together it indicates a structure should be used to hold them.
  • Loading branch information
hydra committed Feb 4, 2025
1 parent 7258a9c commit f08b425
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions examples/forms-signup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,20 @@ struct SignupFormFields {
}

impl SignupFormFields {
pub fn result(&self) -> (String, MaskedString) {
(self.username.get(), self.password.get())
pub fn result(&self) -> LoginArgs {
LoginArgs {
username: self.username.get(),
password: self.password.get(),
}
}
}

#[derive(Debug)]
struct LoginArgs {
username: String,
password: MaskedString,
}

#[derive(Default)]
struct SignupForm {
state: Dynamic::<NewUserState>,
Expand Down Expand Up @@ -92,10 +101,9 @@ impl SignupForm {
let app_state = app_state.clone();
let api = api.clone();
let api_errors = api_errors.clone();
move |(username, password)| {
move |login_args: LoginArgs| {
handle_login(
username,
password,
login_args,
&api,
&app_state,
&form_state,
Expand Down Expand Up @@ -283,24 +291,23 @@ fn logged_in(username: &str, app_state: &Dynamic<AppState>) -> impl MakeWidget {
}

fn handle_login(
username: String,
password: MaskedString,
login_args: LoginArgs,
api: &channel::Sender<FakeApiRequest>,
app_state: &Dynamic<AppState>,
form_state: &Dynamic<NewUserState>,
api_errors: &Dynamic<Map<SignupField, String>>,
) {
let request = FakeApiRequestKind::SignUp {
username: username.clone(),
password,
username: login_args.username.clone(),
password: login_args.password,
};

let response = request
.send_to(api);

match response {
FakeApiResponse::SignUpSuccess => {
app_state.set(AppState::LoggedIn { username });
app_state.set(AppState::LoggedIn { username: login_args.username });
form_state.set(NewUserState::Done);
}
FakeApiResponse::SignUpFailure(errors) => {
Expand Down

0 comments on commit f08b425

Please sign in to comment.