Newer
Older
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
let tableParentId = 'table';
let detailsParentId = 'details';
function getRowId(resource) {
return `${resource.id}_row`
}
function updateDetails(pokemonTypeId) {
const pt = pokemonTypes?.data?.find(item => item.id === pokemonTypeId);
const parent = document.getElementById(detailsParentId);
parent.innerHTML = `
<div class="card" id="${pt.id}_card">
<img src="${pt.imgUrl}" class="card-img-top" alt="${pt.classification}">
<div class="card-body">
<h5 class="card-title">${pt.name} #${pt.pokedexNumber}</h5>
<p class="card-text">
Japanese name: ${pt.japaneseName} </br>
Classification: ${pt.classification} </br>
Abilities: ${pt.abilities?.join(", ") || "none"} </br>
Type: ${pt.primaryType}${pt.secondaryType ? " , " + pt.secondaryType : ""}
</p>
</div>
</div>
`
}
function pokemonTypeToRow(pokemonType) {
return `
<tr id="${getRowId(pokemonType)}" onclick="updateDetails('${pokemonType?.id?.trim()}')">
<th scope="row">${pokemonType.id}</th>
<td>${pokemonType.name}</td>
<td>#${pokemonType.pokedexNumber}</td>
<td>${pokemonType.primaryType}</td>
<td>${pokemonType.secondaryType || '-'}</td>
</tr>
`
}
function createPokemonTypesTable() {
const tableParent = document.getElementById(tableParentId);
tableParent.innerHTML = `
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Pokedex Number</th>
<th scope="col">Primary Type</th>
<th scope="col">Secondary Type</th>
</tr>
</thead>
<tbody>
${
pokemonTypes.data.map(resource => `${pokemonTypeToRow(resource)}`).join("\n")
|| "no data"
}
</tbody>
</table>
`
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
}
function createPokemonTrainersTable() {
const tableParent = document.getElementById(tableParentId);
tableParent.innerHTML = `
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Created</th>
</tr>
</thead>
<tbody>
${
pokemonTrainers.data.map(resource => `${pokemonTrainerToRow(resource)}`).join("\n")
|| "no data"
}
</tbody>
</table>
`
}
function pokemonTrainerToRow(pokemonTrainer) {
return `
<tr id="${getRowId(pokemonTrainer)}" onclick="updateDetailsTrainers('${pokemonTrainer?.id?.trim()}')">
<th scope="row">${pokemonTrainer.id}</th>
<td>${pokemonTrainer.name}</td>
<td>#${pokemonTrainer.created}</td>
</tr>
`
}
function updateDetailsTrainers(trainerId) {
const trainer = pokemonTrainers?.data?.find(item => item.id === trainerId);
const parent = document.getElementById(detailsParentId);
parent.innerHTML = `
<div class="card" id="${trainer.id}_card">
<img src="${trainer.profileUrl}" class="card-img-top" alt="${trainer.name}">
<div class="card-body">
<h5 class="card-title">${trainer.name}</h5>
<p class="card-text">
ID: ${trainer.id} </br>
Created: ${trainer.created} </br>
Last Update: ${trainer.lastUpDate}
</p>
</div>
</div>
`
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
145
146
147
148
149
150
}
function createTrainer() {
const name = document.getElementById('trainerName').value;
const profileUrl = document.getElementById('trainerProfileUrl').value;
const trainer = {
name: name,
profileUrl: profileUrl
};
fetch('/pokemon/api/trainers', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(trainer),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
// Refresh the trainers table
fetch('/pokemon/api/trainers')
.then(res => res.json())
.then(data => {
pokemonTrainers = data;
createPokemonTrainersTable();
})
.catch(err => {
console.error(`Unable to fetch Pokemon Trainers: ${err.status}`);
console.error(err);
});
})
.catch((error) => {
console.error('Error:', error);
});