Exercices

LocalStorage Weather Api ToDo et LocalStorage

React+ionic

Ionic Create project 4.React Outils utiles 5.Lifecycle Methods 6.JSX

Gestion Ventes

Produit CRUD

6.Form :Ajouter un produit 8.Liste produits 9.Pagination 10.Liste produits +Pagination 11.Liste produits +Datatable 11.Liste produits +load more 12.Supprimer un produit 14.Details d'un produit 15.Recherche et Autocomplete

Reacts:Utils

Gestion des erreurs Variables d'environment Cookies Internationalization proptypes Form validation React Animation Fragment Google Maps

Utilisateur CRUD

18.Inscription upload file 19.Connexion et Les sessions 19.Connexion: JWT Token

Ventes CRUD

17.Vendre un produit 18.Facture Generate PDF 19.Statisques Charts

Redux





Exercice:useState

Créer une application en React permettant de:
  1. Ajouter un client dans un tableau
  2. Supprimer et modifier un Client
  3. Afficher la lite de clients avec un système de pagintion

UseStateCrud.js

import { useEffect, useState } from "react";

export default function UseStateCrud()
{

    const [client,setClient]=useState({
        idClient:1,
        nom:'',
        prenom:'',
        email:'',
        genre:''
    })

    const [listeClients,setListeClients]=useState([])
   const [startIndex,setStartIndex]=useState(0);
    const[modifierClicked,setModifierClicked]=useState(false)
    const getClient=(event)=>{
        setClient((preveClient)=>({
           ...preveClient,
           [event.target.name]:event.target.value
        }))

    }


    const AjouterClient=()=>{
        if(client.nom!='' && client.prenom && client.email && client.genre)
        {
            setListeClients((prevListeClient)=>([...prevListeClient,client]))
       
            setClient({
                idClient:client.idClient+1,
                nom:'',
                prenom:'',
                email:'',
                genre:''
            })
        }
        else
        {
            alert("Erreur tous les champs sont obligatoires")
        }
    }



const supprimerClient=(idClient)=>{
    setListeClients(listeClients.filter((cli=>cli.idClient!=idClient)))
}


const modifierClient=(cli)=>{
    setClient(cli)

    //afficher le bouton modifier
    setModifierClicked(true)
}



const ValiderModifierClient=()=>{
    setListeClients(listeClients.map((cli)=>{
        if(client.idClient==cli.idClient)
        {
            return client
        }
        else
        {
            return cli
        }
    }))


    //intialiser l'objet client pour ajouter un nouveau client
    setClient({
        idClient:listeClients[listeClients.length-1].idClient+1,
        nom:'',
        prenom:'',
        email:'',
        genre:''

    })


    //reafficher le bouton ajouter Client
    setModifierClicked(false)
}




    return(
        <div>
        <fieldset>
            <legend>Ajouter un client</legend>
            <table>
                <tr>
                    <td>Nom</td>
                    <td><input type="text" value={client.nom} name="nom" onChange={getClient}/></td>
                </tr>
                <tr>
                    <td>prenom</td>
                    <td><input type="text" name="prenom" value={client.prenom} onChange={getClient}/></td>
                </tr>
                <tr>
                    <td>email</td>
                    <td><input type="email" name="email"value={client.email} onChange={getClient}/></td>
                </tr>
                <tr>
                    <td>genre</td>
                    <td>
                        <input type="checkbox" name="genre"  value="Femme" checked={client.genre=="Femme"} onChange={getClient} style={{width:'25%'}}/>Femme
                        <input type="checkbox" name="genre"  value="Homme"  checked={client.genre=="Homme"} onChange={getClient} style={{width:'25%'}}/>Homme
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                       {modifierClicked? <input type="button" value="Modifier" onClick={ValiderModifierClient}/>
                       :<input type="button" value="Ajouter" onClick={AjouterClient}/>
                       
                       }

                    </td>
                </tr>
            </table>
        </fieldset>

        <h4>Liste Client</h4>
        <table>
            <thead>
                <th>Id</th>
                <th>Nom</th>
                <th>Prénom</th>
                <th>Email</th>
                <th>Genre</th>
                <th>Actions</th>
            </thead>
            <tbody>
                {
                    listeClients.slice(startIndex,startIndex+3).map((cli,index)=>{
                        if(index<3)
                        {
                        return (
                            <tr>
                                <td>{cli.idClient}</td>
                                <td>{cli.nom}</td>
                                <td>{cli.prenom}</td>
                                <td>{cli.email}</td>
                                <td>{cli.genre}</td>
                                <td>
                                    <input type="button" value="Supprimer" onClick={()=>supprimerClient(cli.idClient)}/>
                                    <input type="button" value="Modifier" onClick={()=>modifierClient(cli)}/>
                                </td>
                            </tr>
                        )
                        }
                    })
                }
               
            </tbody>
        </table>

        les pages:{
            listeClients.map((p,index)=>{
                if(index%3==0) {
                return (
                    <button onClick={()=>setStartIndex(index)}>{index/3}</button>
                )
                }
            })
        }
    </div>
    )
}