-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
115 lines (87 loc) · 3.94 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
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta name="referrer" content="same-origin">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Où c'est qu'on mange ?</title>
<meta name="description" content=" ">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link crossorigin="anonymous" rel="stylesheet" href="https://rawgit.com/twbs/bootstrap/v4-dev/dist/css/bootstrap-reboot.css">
<script defer crossorigin="anonymous" src="https://cdn.polyfill.io/v2/polyfill.min.js?features=default"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
const jsonURL = 'https://rawgit.com/newick/ou-cest-quon-bouffe/data/places.json'
fetch(jsonURL)
.then(response => response.json())
.then(jsonRes => {
var data = jsonRes;
for (let j of data) {
var item = document.createElement("article")
var name = document.createElement("h2")
var address = document.createElement("address")
var placeName = j.name
var placeAddress = j.address
name.textContent = placeName
address.textContent = placeAddress
item.append(name, address)
list.append(item)
}
})
const form = document.querySelector("form")
form.addEventListener('submit', function (e) {
e.preventDefault();
const apiURL = 'https://api.github.com/repos/newick/ou-cest-quon-bouffe'
const branchName = 'data'
const fileName = 'places.json'
const token = ''
const url = apiURL.concat('/contents/', fileName, '?ref=', branchName, '&access_token=', token)
const data = [{ name: 'plop', address: 'là bas' }];
// Github requires few things to be able to commit:
// - a path = file name
const commitPath = fileName
// - a message
const commitMessage = "nouvel endroit"
// - convert content to base64:
// JSON.stringify transforms JSON array to a string
const string = JSON.stringify(data)
// btoa function transforms a string to a base64 string
const commitBase64 = btoa(string)
// - the sha of the file to edit
fetch(url)
.then(response => response.json())
.then(jsonRes => {
const commitSha = jsonRes.sha;
// - the branch name
const commitBranch = branchName
fetch(url, {
method: 'PUT',
body: JSON.stringify ({
message: commitMessage,
content: commitBase64,
sha: commitSha,
branch: commitBranch,
path: commitPath
})
})
.then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', response))
})
})
})
</script>
</head>
<body>
<h2>Ajouter un lieu</h2>
<form>
<label for="name">Nom</label>
<input type="text" name="name" id="name">
<label for="address">Adresse</label>
<input type="text" name="address" id="address">
<button type="submit">Balance</button>
</form>
<h2>Lieux déjà listés</h2>
<section id="list"></section>
</body>
</html>