Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add missing prefilled data #240

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.

## UNRELEASED
### Added
* Add prefilled data to credit card input views
### Changed
### Removed
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.unit.dp
import io.snabble.sdk.Snabble
import io.snabble.sdk.ui.R
import io.snabble.sdk.ui.cart.shoppingcart.utils.rememberTextFieldManager
import io.snabble.sdk.ui.payment.creditcard.shared.country.displayName
Expand All @@ -44,20 +45,33 @@ internal fun CustomerInfoInputScreen(
countryItems: List<CountryItem>,
onBackNavigationClick: () -> Unit,
) {
var name by remember { mutableStateOf("") }
val preFilledData = Snabble.formPrefillData

var name by remember { mutableStateOf(preFilledData?.name.orEmpty()) }
var intCallingCode by remember { mutableStateOf("") }
var phoneNumber by remember { mutableStateOf("") }
var email by remember { mutableStateOf("") }
var street by remember { mutableStateOf("") }
var zip by remember { mutableStateOf("") }
var city by remember { mutableStateOf("") }
var state by remember { mutableStateOf("") }
var country: CountryItem by remember { mutableStateOf(countryItems.loadDefaultCountry()) }
var email by remember { mutableStateOf(preFilledData?.email.orEmpty()) }
var street by remember { mutableStateOf(preFilledData?.street.orEmpty()) }
var zip by remember { mutableStateOf(preFilledData?.zip.orEmpty()) }
var city by remember { mutableStateOf(preFilledData?.city.orEmpty()) }
var state by remember { mutableStateOf(preFilledData?.stateCode.orEmpty()) }

var country: CountryItem by remember {
mutableStateOf(
countryItems.loadPreSetCountry(preFilledData?.countryCode)
?: countryItems.loadDefaultCountry()
)
}

val textFieldManager = rememberTextFieldManager()

val isRequiredStateSet =
if (!countryItems.firstOrNull { it.code == country.code }?.stateItems.isNullOrEmpty()) state.isNotEmpty() else true
if (!countryItems.firstOrNull { it.code == country.code }?.stateItems.isNullOrEmpty()) {
state.isNotEmpty()
} else {
true
}

val areRequiredFieldsSet = listOf(
name,
intCallingCode,
Expand Down Expand Up @@ -166,7 +180,7 @@ internal fun CustomerInfoInputScreen(
CountrySelectionMenu(
countryItems = countryItems,
selectedCountryCode = country,
selectedStateCode = null,
selectedStateCode = state,
onCountrySelected = { countryItem, stateItem ->
country = countryItem
state = stateItem?.code.orEmpty()
Expand Down Expand Up @@ -203,6 +217,9 @@ internal fun CustomerInfoInputScreen(
}
}

private fun List<CountryItem>?.loadPreSetCountry(countryCode: String?): CountryItem? =
this?.firstOrNull { it.code == countryCode }

private fun List<CountryItem>?.loadDefaultCountry(): CountryItem =
this?.firstOrNull { it.displayName == Locale.getDefault().country.displayName }
?: this?.firstOrNull { it.code == Locale.GERMANY.displayCountry }
Expand Down