@@ -245,52 +245,57 @@ async function deleteExpense(e) {
245245 }
246246}
247247
248- // async function editExpense(e) {
249- // try {
250- // const token = localStorage.getItem("token");
251- // const categoryValue = document.getElementById("categoryBtn");
252- // const descriptionValue = document.getElementById("descriptionValue");
253- // const amountValue = document.getElementById("amountValue");
254- // const addExpenseBtn = document.getElementById("submitBtn");
255- // if (e.target.classList.contains("edit")) {
256- // let tr = e.target.parentElement.parentElement;
257- // let id = tr.children[0].textContent;
258- // //Fill the input values with the existing values
259- // const res = await axios.get(
260- // "http://localhost:3000/expense/getAllExpenses",
261- // { headers: { Authorization: token } }
262- // );
263- // res.data.forEach((expense) => {
264- // if (expense.id == id) {
265- // categoryValue.textContent = expense.category;
266- // descriptionValue.value = expense.description;
267- // amountValue.value = expense.amount;
268- // addExpenseBtn.textContent = "Update";
269-
270- // // const form = document.getElementById("form1");
271- // addExpenseBtn.removeEventListener("click", addExpense);
272-
273- // addExpenseBtn.addEventListener("click", async function update(e) {
274- // e.preventDefault();
275- // console.log("request to backend for edit");
276- // const res = await axios.post(
277- // `http://localhost:3000/expense/editExpense/${id}`,
278- // {
279- // category: categoryValue.textContent.trim(),
280- // description: descriptionValue.value,
281- // amount: amountValue.value,
282- // },
283- // { headers: { Authorization: token } }
284- // );
285- // window.location.reload();
286- // });
287- // }
288- // });
289- // }
290- // } catch {
291- // (err) => console.log(err);
292- // }
293- // }
248+ table . addEventListener ( "click" , ( e ) => {
249+ editExpense ( e ) ;
250+ } ) ;
251+
252+ async function editExpense ( e ) {
253+ try {
254+ // const token = localStorage.getItem("token");
255+ const categoryValue = document . getElementById ( "categoryBtn" ) ;
256+ const descriptionValue = document . getElementById ( "descriptionValue" ) ;
257+ const amountValue = document . getElementById ( "amountValue" ) ;
258+ const addExpenseBtn = document . getElementById ( "submitBtn" ) ;
259+
260+ if ( e . target . classList . contains ( "edit" ) ) {
261+ let tr = e . target . parentElement . parentElement ;
262+ let id = tr . children [ 0 ] . textContent ;
263+ //Fill the input values with the existing values
264+ const res = await axios . get (
265+ "http://localhost:9000/expense/getAllExpenses" // here not need of api call we can acess all the data from tr.children[1] tr.children[2].... so on
266+ // ,{ headers: { Authorization: token } }
267+ ) ;
268+ res . data . forEach ( ( expense ) => {
269+ if ( expense . id == id ) {
270+ categoryValue . textContent = expense . category ;
271+ descriptionValue . value = expense . description ;
272+ amountValue . value = expense . amount ;
273+ addExpenseBtn . textContent = "Update" ;
274+
275+ // const form = document.getElementById("form1");
276+ addExpenseBtn . removeEventListener ( "click" , addExpense ) ;
277+
278+ addExpenseBtn . addEventListener ( "click" , async function update ( e ) {
279+ e . preventDefault ( ) ;
280+ // console.log("request to backend for edit");
281+ const res = await axios . post (
282+ `http://localhost:9000/expense/updateExpense/${ id } ` ,
283+ {
284+ category : categoryValue . textContent . trim ( ) ,
285+ description : descriptionValue . value ,
286+ amount : amountValue . value ,
287+ }
288+ //, { headers: { Authorization: token } }
289+ ) ;
290+ window . location . reload ( ) ;
291+ } ) ;
292+ }
293+ } ) ;
294+ }
295+ } catch {
296+ ( err ) => console . log ( err ) ;
297+ }
298+ }
294299
295300// async function buyPremium(e) {
296301// const token = localStorage.getItem("token");
@@ -354,8 +359,4 @@ async function deleteExpense(e) {
354359
355360// document.addEventListener("DOMContentLoaded", isPremiumUser);
356361
357-
358- // table.addEventListener("click", (e) => {
359- // editExpense(e);
360- // });
361362// logoutBtn.addEventListener("click", logout);
0 commit comments