Skip to content

Commit

Permalink
Merge pull request #527 from Iterable/support_user_id
Browse files Browse the repository at this point in the history
add support for userId in sample app
  • Loading branch information
evantk91 authored Apr 13, 2024
2 parents 34f2a81 + 28ae093 commit 4bdaf22
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
26 changes: 22 additions & 4 deletions SampleApp/javascript/js/SettingsTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ import { Iterable } from '@iterable/react-native-sdk';
class SettingsTab extends Component {
constructor(props) {
super(props);
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
this.onLoginTapped = () => {
console.log("onLoginTapped");
Iterable.setEmail(this.state.email);
if (emailRegex.test(this.state.email)) {
Iterable.setEmail(this.state.email);
this.updateState();
} else {
Iterable.setUserId(this.state.email);
}
this.updateState();
};
this.onLogoutTapped = () => {
console.log("onLogoutTapped");
Iterable.setEmail(undefined);
if (emailRegex.test(this.state.email)) {
Iterable.setEmail(undefined);
} else{
Iterable.setUserId(undefined);
}
this.updateState();
};
this.state = { isLoggedIn: false };
Expand Down Expand Up @@ -42,8 +52,9 @@ class SettingsTab extends Component {
}
renderLoggedOut() {
console.log("renderLoggedOut");
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
return (React.createElement(View, { style: styles.emailContainer },
React.createElement(TextInput, { value: this.state.email, style: styles.emailTextInput, autoCapitalize: "none", autoCompleteType: "email", onChangeText: (text) => this.setState({ isLoggedIn: false, email: text }), placeholder: "user@example.com" }),
React.createElement(TextInput, { value: this.state.email, style: styles.emailTextInput, autoCapitalize: "none", autoCompleteType: emailRegex.test(this.state.email) ? "email" : "none", onChangeText: (text) => this.setState({ isLoggedIn: false, email: text }), placeholder: "user@example.com/userId" }),
React.createElement(Button, { title: "Login", onPress: this.onLoginTapped })));
}
updateState() {
Expand All @@ -53,7 +64,14 @@ class SettingsTab extends Component {
this.setState({ isLoggedIn: true, email: email });
}
else {
this.setState({ isLoggedIn: false, email: undefined });
Iterable.getUserId().then(userId => {
console.log("gotUserId: " + userId);
if(userId) {
this.setState({ isLoggedIn: true, email: userId });
} else {
this.setState({ isLoggedIn: false, email: undefined });
}
})
}
});
}
Expand Down
29 changes: 24 additions & 5 deletions SampleApp/typescript/ts/SettingsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ class SettingsTab extends Component<Props, State> {

private renderLoggedOut() {
console.log("renderLoggedOut")
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
return (
<View style={styles.emailContainer}>
<TextInput
value={this.state.email}
style={styles.emailTextInput}
autoCapitalize="none"
autoCompleteType="email"
autoCompleteType= {emailRegex.test(this.state.email) ? "email" : "none"}
onChangeText={(text) => this.setState({ isLoggedIn: false, email: text })}
placeholder="user@example.com" />
placeholder="user@example.com/userId" />
<Button
title="Login"
onPress={this.onLoginTapped}
Expand All @@ -75,13 +76,24 @@ class SettingsTab extends Component<Props, State> {

private onLoginTapped = () => {
console.log("onLoginTapped")
Iterable.setEmail(this.state.email)
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
if (emailRegex.test(this.state.email)) {
Iterable.setEmail(this.state.email)
} else{
Iterable.setUserId(this.state.email)
}
this.updateState()
}

private onLogoutTapped = () => {
console.log("onLogoutTapped")
Iterable.setEmail(undefined)

const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (emailRegex.test(this.state.email)) {
Iterable.setEmail(undefined)
} else {
Iterable.setUserId(undefined)
}
this.updateState()
}

Expand All @@ -91,7 +103,14 @@ class SettingsTab extends Component<Props, State> {
if (email) {
this.setState({ isLoggedIn: true, email: email })
} else {
this.setState({ isLoggedIn: false, email: undefined })
Iterable.getUserId().then(userId => {
console.log("gotUserId: " + userId)
if (userId) {
this.setState({ isLoggedIn: true, email: userId })
} else {
this.setState({ isLoggedIn: false, email: undefined })
}
})
}
})
}
Expand Down

0 comments on commit 4bdaf22

Please sign in to comment.