0

I'm trying to achieve making a suspend user button via updating the values of the user the status to Suspended, but the problem is the status is defined but other values are undefined did I do something wrong or is there any way to update the values to make the other variable like a name not required?

This is what I mean:

enter image description here

This is my code:

const User = (props) => (
  <>
    <DropdownButton id="dropdown-basic-button" title="Action">
      <Dropdown.Item>
        <a
          href="user"
          onClick={() => {
            props.onSubmit(props.user[0]);
          }}
        >
          <i className="fas fa-trash"></i> Suspend
        </a>
      </Dropdown.Item>
    </DropdownButton>
  </>
);

export default class Users extends Component {
  constructor(props) {
    super(props);

    this.onSubmit = this.onSubmit.bind(this);

    this.state = { users: [] };
  }

  componentDidMount() {
    axios
      .get("http://localhost:5000/users/")
      .then((response) => {
        this.setState({ users: response.data });
      })
      .catch((error) => {
        console.log(error);
      });
  }



  onSubmit(id) {
    const user = {
      name: this.state.name,
      password: this.state.password,
      email: this.state.email,
      storeName: this.state.storeName,
      storeUrl: this.state.storeUrl,
      date: this.state.date,
      status: "Suspended",
    };

    console.log(user);

    axios
      .post("http://localhost:5000/users/update/" + id, user)
      .then((res) => console.log(res.data));
  }
  userList(currentuser) {
    return (
      <User
        user={currentuser}
        key={currentuser[0]}
        onSubmit={this.onSubmit}
      />
    );
  }
  render() {
    const columns = [
      {
        name: "_id",
        options: {
          display: false,
        },
      },
      {
        name: "name",
        label: "Name",
        options: {
          filter: true,
          sort: true,
        },
      },
      {
        name: "Action",
        options: {
          customBodyRender: (value, tableMeta, updateValue) => {
            return <>{this.userList(tableMeta.rowData)}</>;
          },
        },
      },
    ];
    const { users } = this.state;
    return (
      <>
          <MUIDataTable data={users} columns={columns} />
      </>
    );
  }
}
6
  • What is the this.state.storeUrl? the state has the only user propriety Commented Jan 17, 2021 at 9:00
  • hi @vincenzopalazzo it is one of the values like name, email, storeName Commented Jan 17, 2021 at 9:13
  • ops I miss some code here sorry! Commented Jan 17, 2021 at 9:30
  • 1
    no problem HEHHEHE Commented Jan 17, 2021 at 9:33
  • The state of User is { users: [] }, yet in the onSubmit you do this: this.state.name .... Commented Jan 17, 2021 at 9:38

1 Answer 1

1

You didn't define nor set the User's individual attributes' values in the state! So, no wonder they show up as undefined, when you try to read them...

The simplest solution would be:

onSubmit(id) {
    //let user = this.state.users.find(user => user.id === id); // find by id
    let user = this.state.users[id]; // find by index

    if (user) {
        user.status = 'Suspended';

        console.log(user);

        axios
          .post("http://localhost:5000/users/update/" + id, user)
          .then((res) => console.log(res.data));
    }
  }
Sign up to request clarification or add additional context in comments.

12 Comments

Hi thanks for answering really appreciate it
Cannot set property 'status' of undefined this is the error when I put this code
You have to put it all in an if-statement (edited my answer). Also, modify the find function so that it matches the user you're working on.
Hi @k-wasilewski what do u mean modify?
In the body of a js find function you define the criteria for an object you're looking for. I assumed any of your User entities (received from api) has an id attribute, but since you say the function returns undefined, probably you have to define the criteria differently.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.