How to call one of multiple functions - JavaScript?

后端 未结 2 1969
我在风中等你
我在风中等你 2020-12-18 17:23

I have three functions:

  1. Change Email
  2. Change password
  3. Change otherData

And One Button to call them , when the user changes his da

2条回答
  •  [愿得一人]
    2020-12-18 17:44

    I think you have two options, option add more variables to state which you can use to compare the new data with and option two use three boolean values, one for password, email and other.

    Option 1

    constructor(props) {
      super(props);
      this.state = {
        currentPassword: "",
        newPassword: "",
        email: '',
        currentUser: {
          username: "",
          email: "",
          city: "",
          mobileNumber: "",
        },
        username: '',
        city: '',
        mobileNumber: '',
        data: {},
        loading: true
      }
    }
    
    _updateProfileData = async () => {
    
      const { currentPassword, email, currentUser, newPassword,
        mobileNumber, username, city } = this.state;
    
      if (currentPassword === "") {
        alert("please write your current password first!")
        return;
      }
    
      if (email !== currentUser.email) {
        // email changed update
        await this.changeEmail();
      }
    
      if (newPassword !== currentPassword) {
        // password changed update
        await this.changePassword();
      }
    
      if (city !== currentUser.city || mobileNumber !== currentUser.mobileNumber || username !== currentUser.username) {
        await this._updateData();
      }
    }
    
    async componentDidMount() {
      try {
        const userId = firebase.auth().currentUser.uid;
        await this.setState({ userId });
        console.log("@uid", this.state.userId);
        let recentPostsRef = firebase.database().ref(`users/${userId}`);
        await recentPostsRef.once('value').then(snapshot => {
          const currentUser = snapshot.val();
          this.setState({ currentUser: currentUser, email: currentUser.email, username: currentUser.username, city: currentUser.city, mobileNumber: currentUser.mobileNumber, loading: false })
          console.log(this.state.currentUser)
        }).catch((error) => console.log("@error", error));
      } catch (error) {
        console.log("@CError", error);
      }
    }
    

    Option 2, have three booleans, passwordChanged,emailChanged,otherChanged when the user types in one of the inputs, set the value to true and in your _updateProfileData check if the value is true, then update the value.

    constructor(props) {
      super(props);
      this.state = {
        currentPassword: "",
        newPassword: "",
        currentUser: {
          username: "",
          email: "",
          city: "",
          mobileNumber: "",
        },
        data: {},
        // boolean values for email, password and other
        passwordChanged: false,
        emailChanged: false,
        otherChanged: false,
        loading: true
      }
    }
    
    _updateProfileData = async () => {
    
      const { currentPassword, passwordChanged, emailChanged, otherChanged } = this.state;
    
      if (currentPassword === "") {
        alert("please write your current password first!")
        return;
      }
    
      if (emailChanged) {
        // email changed update
        await this.changeEmail();
      }
    
      if (passwordChanged) {
        // password changed update
        await this.changePassword();
      }
    
      if (otherChanged) {
        await this._updateData();
      }
    }
    

提交回复
热议问题