Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 110d961

Browse files
authored
[FEAT] Add the API to update the background color of a square on TypeScript challenge (#13)
1 parent 9e5dad3 commit 110d961

File tree

8 files changed

+57
-13
lines changed

8 files changed

+57
-13
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,6 @@ If it's necessary (because the exercise is too long), no need to implement all t
6262
<img title="Token" src="images/token.png">
6363
</p>
6464

65-
- Add a new api method that updates the background color of a square, without redrawing all the grid. \
66-
It should be called with: `<library object>.updateBackgroundColor(“16”, “Pink”);` \
67-
The result should be something like this:
68-
<p align="center">
69-
<img title="Background color" src="images/background-color.png">
70-
</p>
71-
7265
- Add a new api method that adds a handler on click on square. \
7366
It should be called with: `<library object>.addHandler(squareLabel, (event) => {....});`
7467

images/background-color.png

4.05 KB
Loading

images/start_library_rendering.png

4.63 KB
Loading

library/ts-rollup/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ The result should be something like this:
2222
<img title="Grid" src="../../images/grid.png">
2323
</p>
2424

25+
### Update the background color of a square, without redrawing all the grid
26+
```javascript
27+
bingo.updateBackgroundColor('36', 'Pink');
28+
```
29+
The result should be something like this:
30+
<p align="center">
31+
<img title="Grid" src="../../images/background-color.png">
32+
</p>
33+
2534
## Technical part
2635

2736
The project is configured with Jest for the tests, Playwright & Jest-image-snapshot for the visual tests, ESLint for the code formatting.

library/ts-rollup/dev/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ <h2>Fun starts here</h2>
1212

1313
<section class="flex-column-container">
1414
<div>
15-
<div id="controls-panel">
15+
<div id="controls-panel" class="flex-row-container">
1616
<input id="btn-new-grid" type="button" title="Generate new Grid" value="New Grid" class="button"/>
17+
18+
<div class="flex-column-container">
19+
<label id="background" for="field-update-bckgd">Update the background of this square</label>
20+
<input id="field-update-bckgd" type="text" maxLength="3" placeholder="put square number"/>
21+
</div>
1722
</div>
1823

1924
<!-- container used to display the BINGO -->

library/ts-rollup/dev/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ function startBingoApp(): Bingo {
1111

1212
const bingo = startBingoApp();
1313

14+
// reset grid
1415
document.getElementById('btn-new-grid').addEventListener('click', () => {
1516
bingo.resetGrid();
1617
});
18+
19+
// update background
20+
const updateBackground = document.getElementById(
21+
'field-update-bckgd',
22+
) as HTMLInputElement;
23+
updateBackground.onchange = () => {
24+
bingo.updateBackgroundColor(updateBackground.value, 'Pink');
25+
updateBackground.value = ''; // reset
26+
};

library/ts-rollup/dev/static/css/main.css

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
margin-top: 75px;
99
}
1010

11+
.flex-row-container {
12+
display: flex;
13+
flex-direction: row;
14+
justify-content: space-around;
15+
}
16+
1117
.info-centered {
1218
margin: 0 auto;
1319
width: 100%;
1420
text-align: center;
15-
font-family: monospace;
1621
}
1722

1823
#controls-panel {
@@ -32,8 +37,13 @@
3237
background-color: LightGreen;
3338
color: #121e12;
3439
font-size: small;
40+
font-family: monospace;
41+
}
42+
43+
body {
44+
font-family: monospace;
3545
}
3646

3747
label {
38-
font-size: 0.65em;
48+
font-size: small;
3949
}

library/ts-rollup/src/index.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BingoGraph } from './graph';
2+
import { mxgraph } from './initializer';
23

34
const titleHeight = 70;
45

@@ -58,10 +59,11 @@ export class Bingo {
5859
columnsCount < columnNumber;
5960
columnsCount++
6061
) {
62+
const randomInteger = getRandomInteger();
6163
this.graph.insertVertex(
6264
grid,
63-
null,
64-
`${getRandomInteger()}`,
65+
convertLabelToCellId(randomInteger),
66+
`${randomInteger}`,
6567
columnsCount * columnWidth,
6668
(rowsCount + 1) * rowHeight - (rowHeight - titleHeight),
6769
columnWidth,
@@ -74,11 +76,26 @@ export class Bingo {
7476
this.graph.getModel().endUpdate();
7577
}
7678
}
79+
80+
public updateBackgroundColor(label: string, fillColor: string): void {
81+
const cell = this.graph.getModel().getCell(convertLabelToCellId(label));
82+
if (!cell) {
83+
return;
84+
}
85+
86+
const style = cell.getStyle();
87+
const newStyle = `${style};${mxgraph.mxConstants.STYLE_FILLCOLOR}=${fillColor}`;
88+
this.graph.getModel().setStyle(cell, newStyle);
89+
}
7790
}
7891

7992
// naive implementation, doesn't prevent duplicates in grid
80-
export function getRandomInteger(): number {
93+
function getRandomInteger(): number {
8194
const min = 1;
8295
const max = 90;
8396
return Math.floor(Math.random() * (max - min + 1)) + min;
8497
}
98+
99+
function convertLabelToCellId(label: string | number): string {
100+
return `number_${label}`;
101+
}

0 commit comments

Comments
 (0)