upload image and text with form

System Information
  • Strapi Version:
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

Hello , I’m learning React with Strapi.

I made a form to add data to the database. I manage to send my img in the upload api and the text data in the Animals api, however, I can’t manage to link the two. Ie to make sure that the image also goes on the animal that I add.

Thank you all for your future help.

import React from "react";
import { useForm } from "react-hook-form";
import axios from "axios";

class AddAnimal extends React.Component {
    
  constructor(props) {
    super(props);

    this.state = {
      modifiedData: {
        nom: '',
        description: '',
        
       image:'',
        races: [],
        images :[],
   
      },
      allImage:[],
      allRaces: [],
      error: null,
    };
    
  }

 
  onImageChange = event => {
    console.log(event.target.files);

    this.setState({
      images: event.target.files,
      image: event.target.files,

    
    });
  };
  
  componentDidMount = async () => {
    try {
      const response = await axios.get('http://localhost:1337/api/races');
      this.setState({ allRaces: response.data.data });
    } catch (error) {
      this.setState({ error });
    }
  };



  handleInputChange = ({ target: { name, value } },event) => {
    this.setState(prev => ({
      ...prev,
      modifiedData: {
        ...prev.modifiedData,
        
        [name]: value,
       
   
        
      },
    }),
    )
    //    this.setState({
    //   images: event.target.files,
      
    // });
  };

  handleSubmit = async e => {
    e.preventDefault();
    
    const formData = new FormData();
    Array.from(this.state.images).forEach(image => {
      formData.append('files', image);
    });
    try {
      const response = await axios.post('http://localhost:1337/api/animaux?populate=*',
      {
        headers: { 'Content-Type': 'multipart/form-data' },
        "data": {
          nom:  this.state.modifiedData.nom,
          Description: this.state.modifiedData.description,
          // image : formData.append('files',this.setState.images),
          races: this.state.modifiedData.races
          
        },


    
      })
      axios
      .post(`http://localhost:1337/api/upload`, formData, {
        headers: { 'Content-Type': 'multipart/form-data' },
       })    
      

       console.log( this.setState.images)
  

      console.log(response);
      
    } catch (error) {
      this.setState({ error });
    }
    
  };

// if (files) {
//       // automatically uploads the files based on the entry and the model
//       await uploadFiles(user, files, { model: strapi.plugins['users-permissions'].models.user })
//     }
  
  renderCheckbox = race => {
    const {
      modifiedData: { races },
    } = this.state;
    const isChecked = races.includes(race.id);
    const handleChange = () => {
      if (!races.includes(race.id)) {
        this.handleInputChange({
          target: { name: 'races', value: races.concat(race.id) },
        });
      } else {
        this.handleInputChange({
          target: {
            name: 'races',
            value: races.filter(v => v !== race.id),
          },
        });
      }
    };

    return (
      <div key={race.id}>
        <label htmlFor={race.id}>{race.attributes.nom}</label>
        <input
          type="checkbox"
          checked={isChecked}
          onChange={handleChange}
          name="races"
          id={race.id}
        />
      </div>
    );
  };

  render() {
    const { error, allRaces, modifiedData } = this.state;


    if (error) {
      return <div>An error occured: {error.message}</div>;
    }

    return (
      <div className="App">
        <form onSubmit={this.handleSubmit}>
          <h3>Ajouter un animal</h3>
          <br />
          <label>
            Name:
            <input
              type="text"
              name="nom"
              onChange={this.handleInputChange}
              value={modifiedData.name}
            />
          </label>
          <label>
            Description:
            <input
              type="text"
              name="description"
              onChange={this.handleInputChange}
              value={modifiedData.description}
            />
          </label>
          <input type="file" name="image"  value={this.images} onChange={this.onImageChange}/>
          <div>
            <br />
            <b>Select races</b>
            {allRaces.map(this.renderCheckbox)}
          </div>
          <br />
          <button type="submit">Ajouter</button>
        </form>
      </div>
    );
  }
}

export default AddAnimal;