-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.ts
154 lines (134 loc) · 4.77 KB
/
script.ts
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
145
146
147
148
149
150
151
152
153
154
const form = document.querySelector("#player-search") as HTMLFormElement;
const position = document.querySelector("#position") as HTMLInputElement;
const fgprecent = document.querySelector(
"#field-goals-precents"
) as HTMLInputElement;
const threeprecent = document.querySelector(
"#three-precents"
) as HTMLInputElement;
const points = document.querySelector("#points") as HTMLInputElement;
const BASEURL = "https://nbaserver-q21u.onrender.com/api/";
type player = {
position: string;
twoPercent: Number;
threePercent: Number;
points: Number;
playerName?: string;
};
type FantasyPlayerElement = {
maindiv: HTMLDivElement;
heading: HTMLHeadingElement;
detaildiv: HTMLDivElement;
};
enum Positions {
PG = 0,
SG = 1,
SF = 2,
PF = 3,
C = 4,
}
const setElements = (position: string): FantasyPlayerElement => {
let elements: FantasyPlayerElement = {
maindiv: undefined as any,
heading: undefined as any,
detaildiv: undefined as any,
};
elements.maindiv = document.getElementById(
`${position}div`
) as HTMLDivElement;
elements.heading = elements.maindiv.querySelector("h3") as HTMLHeadingElement;
elements.detaildiv = elements.maindiv.querySelector("div") as HTMLDivElement;
return elements;
};
const SetPlayer = (player: player, playerelements: FantasyPlayerElement) => {
//cleaning
playerelements.detaildiv.innerHTML = "";
//starting to insert
playerelements.heading.innerText = player.playerName || "";
const playerPoints = document.createElement('h4') as HTMLHeadingElement;
playerPoints.textContent = `Points : ${player.points}`
const playerTwo = document.createElement('h4') as HTMLHeadingElement;
playerTwo.textContent = `Two Precents : ${player.twoPercent}%`
const playerThree = document.createElement('h4') as HTMLHeadingElement;
playerThree.textContent = `Three Precents : ${player.threePercent}%`
playerelements.detaildiv.appendChild(playerThree);
playerelements.detaildiv.appendChild(playerTwo);
playerelements.detaildiv.appendChild(playerPoints);
};
const team: FantasyPlayerElement[] = [
setElements("PG"),
setElements("SG"),
setElements("SF"),
setElements("PF"),
setElements("C"),
];
const postData = async (url: string = "", data: any = {}) => {
try {
const response: any = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
// Check if the response was successful
if (!response.ok) {
const message = `An error occurred: ${response.statusText}`;
throw new Error(message);
}
const responseData = await response.json(); // Parse JSON data from the response
return responseData; // Return the data from the response
} catch (error) {
console.error("Error:", error);
}
};
form.addEventListener("submit", async (e: Event) => {
e.preventDefault();
console.log("started filterring");
const filterPlayer: player = {
position: position.value,
threePercent: threeprecent.valueAsNumber,
twoPercent: fgprecent.valueAsNumber,
points: points.valueAsNumber,
};
const response = await postData(BASEURL + "filter", filterPlayer);
updateTable(response);
});
function updateTable(players: player[]) {
// Clear existing table entries
const tbody = document.querySelector(
"#main-search-tbody"
) as HTMLTableSectionElement;
tbody.innerHTML = "";
// Generate new rows for each player and append to the table
players.forEach((player) => {
const row = document.createElement("tr");
const playerCell = document.createElement("td");
playerCell.textContent = player.playerName || "";
const positionCell = document.createElement("td");
positionCell.textContent = player.position;
const pointsCell = document.createElement("td");
pointsCell.textContent = `${player.points}`;
const fgPercentCell = document.createElement("td");
fgPercentCell.textContent = `${player.twoPercent}`;
const threePercentCell = document.createElement("td");
threePercentCell.textContent = `${player.threePercent}`;
const ActionCell = document.createElement("td");
const Addbutton = document.createElement("button") as HTMLButtonElement;
Addbutton.id = "AddButton";
Addbutton.textContent = `Add ${
player.playerName?.split(" ")[0]
} to Current Team`;
Addbutton.addEventListener("click", (e: Event) => {SetPlayer(player, team[Positions[player.position as keyof typeof Positions]])});
ActionCell.appendChild(Addbutton);
// Append cells to the row
row.appendChild(playerCell);
row.appendChild(positionCell);
row.appendChild(pointsCell);
row.appendChild(fgPercentCell);
row.appendChild(threePercentCell);
row.appendChild(ActionCell);
// Append row to the tbody
tbody.appendChild(row);
});
}