Skip to content

Commit 05cb586

Browse files
committed
lesson-17
1 parent 51e2c02 commit 05cb586

File tree

5 files changed

+75
-19
lines changed

5 files changed

+75
-19
lines changed

public/app.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import { Invoice } from './classes/Invoice.js';
22
import { Payment } from './classes/Payment.js';
3-
// let docOne: HasFormatter;
4-
// let docTwo: HasFormatter;
5-
// docOne = new Invoice('yoshi', 'web work', 250);
6-
// docTwo = new Payment('mario', 'plumbing', 200);
7-
// let docs: HasFormatter[] = [];
8-
// docs.push(docOne);
9-
// docs.push(docTwo);
3+
import { ListTemplate } from './classes/ListTemplate.js';
104
const form = document.querySelector('.new-item-form');
115
console.log(form.children);
126
// inputs
137
const type = document.querySelector('#type');
148
const tofrom = document.querySelector('#tofrom');
159
const details = document.querySelector('#details');
1610
const amount = document.querySelector('#amount');
11+
// list template instance
12+
const ul = document.querySelector('ul');
13+
const list = new ListTemplate(ul);
1714
form.addEventListener('submit', (e) => {
1815
e.preventDefault();
1916
let doc;
@@ -23,5 +20,5 @@ form.addEventListener('submit', (e) => {
2320
else {
2421
doc = new Payment(tofrom.value, details.value, amount.valueAsNumber);
2522
}
26-
console.log(doc);
23+
list.render(doc, type.value, 'end');
2724
});

public/classes/ListTemplate.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export class ListTemplate {
2+
constructor(container) {
3+
this.container = container;
4+
}
5+
render(item, heading, pos) {
6+
const li = document.createElement('li');
7+
const h4 = document.createElement('h4');
8+
h4.innerText = heading;
9+
li.append(h4);
10+
const p = document.createElement('p');
11+
p.innerText = item.format();
12+
li.append(p);
13+
if (pos === 'start') {
14+
this.container.prepend(li);
15+
}
16+
else {
17+
this.container.append(li);
18+
}
19+
}
20+
}

public/classes/RenderList.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export class ListTemplate {
2+
constructor(container) {
3+
this.container = container;
4+
}
5+
render(item, heading, pos) {
6+
const li = document.createElement('li');
7+
const h4 = document.createElement('h4');
8+
h4.innerText = heading;
9+
li.append(h4);
10+
const p = document.createElement('p');
11+
p.innerText = item.format();
12+
li.append(p);
13+
if (pos === 'start') {
14+
this.container.prepend(li);
15+
}
16+
else {
17+
this.container.append(li);
18+
}
19+
}
20+
}

src/app.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
import { Invoice } from './classes/Invoice.js';
22
import { Payment } from './classes/Payment.js';
3+
import { ListTemplate } from './classes/ListTemplate.js';
34
import { HasFormatter } from './interfaces/HasFormatter.js';
45

5-
// let docOne: HasFormatter;
6-
// let docTwo: HasFormatter;
7-
8-
// docOne = new Invoice('yoshi', 'web work', 250);
9-
// docTwo = new Payment('mario', 'plumbing', 200);
10-
11-
// let docs: HasFormatter[] = [];
12-
// docs.push(docOne);
13-
// docs.push(docTwo);
14-
156
const form = document.querySelector('.new-item-form') as HTMLFormElement;
167
console.log(form.children);
178

@@ -21,6 +12,10 @@ const tofrom = document.querySelector('#tofrom') as HTMLInputElement;
2112
const details = document.querySelector('#details') as HTMLInputElement;
2213
const amount = document.querySelector('#amount') as HTMLInputElement;
2314

15+
// list template instance
16+
const ul = document.querySelector('ul')!;
17+
const list = new ListTemplate(ul);
18+
2419
form.addEventListener('submit', (e: Event) => {
2520
e.preventDefault();
2621

@@ -30,5 +25,6 @@ form.addEventListener('submit', (e: Event) => {
3025
} else {
3126
doc = new Payment(tofrom.value, details.value, amount.valueAsNumber);
3227
}
33-
console.log(doc);
28+
29+
list.render(doc, type.value, 'end');
3430
});

src/classes/ListTemplate.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { HasFormatter } from "../interfaces/HasFormatter";
2+
3+
export class ListTemplate {
4+
constructor(private container: HTMLUListElement){}
5+
6+
render(item: HasFormatter, heading: string, pos: 'start' | 'end'){
7+
const li = document.createElement('li');
8+
9+
const h4 = document.createElement('h4');
10+
h4.innerText = heading;
11+
li.append(h4);
12+
13+
const p = document.createElement('p');
14+
p.innerText = item.format();
15+
li.append(p);
16+
17+
if(pos === 'start'){
18+
this.container.prepend(li);
19+
} else {
20+
this.container.append(li);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)