-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (144 loc) · 4.47 KB
/
index.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* ~~~~ Constants ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
const headers = ["Nom", "Population", "Surface", "Département", "Région"];
const keys = ["nom", "population", "surface", "codeDepartement", "codeRegion"];
const types = ["string", "number", "number", "string", "string"];
/* ~~~~ Helpers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
const getSearchParams = (form) => {
const formData = new FormData(form);
const searchParams = new URLSearchParams(formData);
return searchParams;
};
const getLocalURL = (searchParams) => {
const localURL = new URL(location);
localURL.hash = "";
localURL.search = searchParams;
return localURL;
};
const getRemoteURL = (searchParams) => {
const remoteURL = new URL("https://geo.api.gouv.fr/communes");
remoteURL.search = searchParams;
return remoteURL;
};
/* ~~~~ Initialization ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
const initForm = () => {
const form = document.querySelector("header > form");
const currentURL = new URL(location);
const currentSearchParams = currentURL.searchParams;
if (currentSearchParams.has("nom")) {
const input = computeInput(currentSearchParams);
renderField(form, input);
}
return form;
};
const initTable = () => {
const main = document.querySelector("main");
const table = document.createElement("table");
main.append(table);
table.createCaption();
const head = table.createTHead();
const row = head.insertRow();
for (const header of headers) {
const cell = document.createElement("th");
row.append(cell);
cell.textContent = header;
cell.tabIndex = 0;
}
table.createTBody();
return table;
};
/* ~~~~ State computing ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
const computeState = async (searchParams) => {
const input = computeInput(searchParams);
const output = await computeOutput(searchParams);
return {
input: input,
output: output,
};
};
const computeInput = (searchParams) => {
const input = searchParams.get("nom");
return input;
};
const computeOutput = async (searchParams) => {
const remoteURL = getRemoteURL(searchParams);
const response = await fetch(remoteURL);
const output = await response.json();
return output;
};
/* ~~~~ State rendering ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
const renderState = (form, table, state) => {
const input = state.input;
const output = state.output;
renderForm(form, input);
renderTable(table, input, output);
};
const renderForm = (form, input) => {
renderField(form, input);
};
const renderTable = (table, input, output) => {
renderCaption(table, input, output);
renderBody(table, output);
};
const renderField = (form, input) => {
const elements = form.elements;
const name = elements.namedItem("nom");
name.value = input;
};
const renderCaption = (table, input, output) => {
const caption = table.caption;
const length = output.length;
const content = length === 0
? "Aucun résultat pour la recherche «\u{00a0}" + input + "\u{00a0}»"
: length === 1
? "1 résultat pour la recherche «\u{00a0}" + input + "\u{00a0}»"
: String(length) + " résultats pour la recherche «\u{00a0}" + input + "\u{00a0}»";
caption.textContent = content;
};
const renderBody = (table, output) => {
const bodies = table.tBodies;
const oldBody = bodies.item(0);
const newBody = document.createElement("tbody");
for (const item of output) {
const row = newBody.insertRow();
for (const key of keys) {
const cell = row.insertCell();
const content = item[key];
cell.textContent = content;
}
}
oldBody.replaceWith(newBody);
};
/* ~~~~ State logging ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
const logState = (state) => {
const input = state.input;
const output = state.output;
console.groupCollapsed(JSON.stringify(input) + ":");
console.table(output, ["nom", "codeDepartement", "codeRegion"]);
console.groupEnd();
};
/* ~~~~ State sorting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
const sortState = (state, column) => {
let order = 1;
if (column < 0) {
column = ~column;
order = -1;
}
const output = state.output;
const key = keys[column];
const type = types[column];
switch (type) {
case "number": {
output.sort((a, b) => {
return (a[key] - b[key]) * order;
});
break;
}
case "string": {
output.sort((a, b) => {
return a[key].localeCompare(b[key], "fr") * order;
});
break;
}
}
};
export {getSearchParams, getLocalURL, getRemoteURL, initForm, initTable, computeState, renderState, logState, sortState};