-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
124 lines (111 loc) · 3.82 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gestion des clients</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
</head>
<body>
<div class="container">
<header class="row">
<h1>Gestion des clients</h1>
</header>
<hr>
<section class="row">
<h2>Ajouter/ modifier un client</h2>
<form action="" class="d-flex justify-content-center">
<input type="hidden" id="id" name="id">
<input type="text" class="form-control" id="nom" placeholder="nom" name="nom">
<input type="text"class="form-control mx-4" id="tel" placeholder="tel" name="tel">
<input type="submit" class="btn btn-outline-dark"></form>
</section>
<hr>
<section class="row">
<h2>Liste de clients</h2>
<div class="row clients">
pas de client pour l'instant
</div>
</section>
</div>
<script src="https://www.gstatic.com/firebasejs/8.9.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.9.1/firebase-database.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "AIzaSyBF-J7yOiletpmkJm1UaXanRrTqn1S4j14",
authDomain: "doranco-2335f.firebaseapp.com",
databaseURL: "https://doranco-2335f-default-rtdb.europe-west1.firebasedatabase.app",
projectId: "doranco-2335f",
storageBucket: "doranco-2335f.appspot.com",
messagingSenderId: "442878492623",
appId: "1:442878492623:web:c2b1bc84f28168e283dcba"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
<script>
//ajouter un nouvel enregistrement dans remplir le form => remplir la base
document.querySelector("form").addEventListener("submit", e=>{
e.preventDefault();
let id = e.target.id.value;
let nom = e.target.nom.value;
let tel = e.target.tel.value;
if(id.length == 0 ){
//permet de sélectionner et/ou créer table cleints
const refDB = firebase.database().ref("/clients");
const nouveauClient = refDB.push();
nouveauClient.set({
nom:nom,
tel: tel
});
}else{
//modif
const refDB = firebase.database().ref(`/clients/${id}`);
let clientModifie = {
id: id,
nom: nom,
tel :tel
}
refDB.update(clientModifie);
}
e.target.reset();
e.target.id.value="";
});
///addicher la liste des clients dans la table clients de la bdd firebase :
window.addEventListener("DOMContentLoaded",() =>{
const refDB = firebase.database().ref("/clients");
refDB.on("value",(clients) =>{
let data = clients.val();
let html ="";
for(let id in data){
html +=`<article class="col-4">
<ul>
<li>${data[id].nom}</li>
<li>${data[id].tel}</li>
<li>
<button class="btn btn-outline-warning btn-sm" onclick="modifier('${id}')">modifier</button>
<button class="btn btn-outline-danger btn-sm" onclick="supprimer('${id}')">supprimer</button>
</li>
</ul>
</article>`
}
document.querySelector(".clients").innerHTML = html;
})
});
//conceptd'observable -> RxJS
function supprimer(id){
firebase.database().ref(`/clients/${id}`).remove();
}
function modifier(id){
const refDB = firebase.database().ref(`/clients/${id}`);
refDB.once("value", client =>{
document.querySelector("#id").value = id;
document.querySelector("#nom").value = client.val().nom;
document.querySelector("#tel").value = client.val().tel;
})
}
</script>
</body>
</html>