Implement option to update billing address

This commit is contained in:
2023-04-27 20:20:56 +02:00
parent 8ffd478ca9
commit 15f999869a
5 changed files with 65 additions and 51 deletions

@ -36,6 +36,7 @@ namespace Elwig.Windows {
RadioButtonInputs = Utils.FindAllChildren<RadioButton>(this, ExemptInputs).ToArray();
foreach (var tb in TextBoxInputs)
Valid[tb] = true;
ValidateRequiredInputs();
}
abstract protected void UpdateButtons();
@ -119,9 +120,7 @@ namespace Elwig.Windows {
ValidateRequiredInputs();
}
protected bool IsValid() {
return Valid.All(kv => kv.Value) && ComboBoxInputs.All(cb => cb.ItemsSource == null || cb.SelectedItem != null);
}
protected bool IsValid => Valid.All(kv => kv.Value);
protected bool InputHasChanged(Control input) {
if (!OriginalValues.ContainsKey(input)) {
@ -139,39 +138,30 @@ namespace Elwig.Windows {
}
}
protected bool HasChanged() {
return !IsValid() ||
TextBoxInputs.Any(InputHasChanged) ||
ComboBoxInputs.Any(InputHasChanged) ||
CheckBoxInputs.Any(InputHasChanged) ||
RadioButtonInputs.Any(InputHasChanged);
protected bool HasChanged =>
!IsValid ||
TextBoxInputs.Any(InputHasChanged) ||
ComboBoxInputs.Any(InputHasChanged) ||
CheckBoxInputs.Any(InputHasChanged) ||
RadioButtonInputs.Any(InputHasChanged);
protected void UpdatePlz(TextBox plzInput, bool plzInputValid, ComboBox ortInput) {
var item = ortInput.SelectedItem;
var list = plzInputValid && plzInput.Text.Length == 4 ? Context.Postleitzahlen.Find(int.Parse(plzInput.Text))?.Orte.ToList() : null;
ortInput.ItemsSource = list;
ortInput.SelectedItem = (list != null && item != null && list.Contains(item)) ? item : null;
ComboBox_SelectionChanged(ortInput, new());
}
protected void UpdatePlz(TextBox plzInput, ComboBox ortInput) {
if (plzInput.Text.Length == 4) {
ortInput.ItemsSource = Context.Postleitzahlen.Find(int.Parse(plzInput.Text))?.Orte.ToList();
} else {
ortInput.ItemsSource = null;
}
ortInput.SelectedItem = null;
if (ortInput.ItemsSource != null) {
Utils.SetInputInvalid(ortInput);
} else {
Utils.ClearInputState(ortInput);
}
ValidateInput(plzInput, SenderIsRequired(plzInput) || (ortInput.ItemsSource != null));
UpdateButtons();
protected bool InputTextChanged(TextBox input, Func<TextBox, bool, ValidationResult> checker) {
return InputTextChanged(input, (tb, required, ctx) => checker(tb, required));
}
protected void InputTextChanged(TextBox input, Func<TextBox, bool, ValidationResult> checker) {
InputTextChanged(input, (tb, required, ctx) => checker(tb, required));
protected bool InputTextChanged(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> checker) {
return InputTextChanged(input, checker(input, SenderIsRequired(input), Context));
}
protected void InputTextChanged(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> checker) {
InputTextChanged(input, checker(input, SenderIsRequired(input), Context));
}
protected void InputTextChanged(TextBox input, ValidationResult res) {
protected bool InputTextChanged(TextBox input, ValidationResult res) {
ValidateInput(input, res.IsValid);
if (res.IsValid) {
if (InputHasChanged(input)) {
@ -183,19 +173,21 @@ namespace Elwig.Windows {
Utils.SetInputInvalid(input);
}
UpdateButtons();
return res.IsValid;
}
protected void InputLostFocus(TextBox input, Func<TextBox, bool, ValidationResult> checker, string? msg = null) {
InputLostFocus(input, (tb, requiered, ctx) => checker(tb, requiered), msg);
protected bool InputLostFocus(TextBox input, Func<TextBox, bool, ValidationResult> checker, string? msg = null) {
return InputLostFocus(input, (tb, requiered, ctx) => checker(tb, requiered), msg);
}
protected void InputLostFocus(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> checker, string? msg = null) {
InputLostFocus(input, checker(input, SenderIsRequired(input), Context), msg);
protected bool InputLostFocus(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> checker, string? msg = null) {
return InputLostFocus(input, checker(input, SenderIsRequired(input), Context), msg);
}
protected void InputLostFocus(TextBox input, ValidationResult res, string? msg = null) {
protected bool InputLostFocus(TextBox input, ValidationResult res, string? msg = null) {
if (!res.IsValid)
MessageBox.Show(res.ErrorContent.ToString(), msg ?? res.ErrorContent.ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
return res.IsValid;
}
protected void CheckBox_Changed(object sender, RoutedEventArgs evt) {
@ -240,12 +232,16 @@ namespace Elwig.Windows {
protected void ComboBox_SelectionChanged(object sender, RoutedEventArgs evt) {
var input = (ComboBox)sender;
if (input.ItemsSource != null && input.SelectedItem == null) {
if (input.ItemsSource != null && input.SelectedItem == null && RequiredInputs.Contains(input)) {
ValidateInput(input, false);
Utils.SetInputInvalid(input);
} else if (InputHasChanged(input)) {
Utils.SetInputChanged(input);
} else {
Utils.ClearInputState(input);
ValidateInput(input, true);
if (InputHasChanged(input)) {
Utils.SetInputChanged(input);
} else {
Utils.ClearInputState(input);
}
}
UpdateButtons();
}
@ -272,14 +268,14 @@ namespace Elwig.Windows {
protected void PlzInput_TextChanged(object sender, RoutedEventArgs evt) {
var plz = (TextBox)sender;
InputTextChanged(plz, Validator.CheckPlz);
UpdatePlz(plz, Utils.FindNextSibling<ComboBox>(plz));
var valid = InputTextChanged(plz, Validator.CheckPlz);
UpdatePlz(plz, valid, Utils.FindNextSibling<ComboBox>(plz));
}
protected void PlzInput_LostFocus(object sender, RoutedEventArgs evt) {
var plz = (TextBox)sender;
InputLostFocus(plz, Validator.CheckPlz);
UpdatePlz(plz, Utils.FindNextSibling<ComboBox>(plz));
var valid = InputLostFocus(plz, Validator.CheckPlz);
UpdatePlz(plz, valid, Utils.FindNextSibling<ComboBox>(plz));
}
protected void EmailInput_TextChanged(object sender, RoutedEventArgs evt) {