Skip to content

Commit c7919cf

Browse files
committed
✨ Connect with the API
1 parent d526887 commit c7919cf

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

src/config/api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
baseURL: 'http://localhost:8000'
3+
}

src/pages/Result/index.tsx

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import React, { useMemo } from 'react';
1+
import React, { useEffect, useMemo, useState } from 'react';
22
import { MdRefresh } from 'react-icons/md';
33
import { Link, useLocation } from 'react-router-dom';
4+
import api from '../../config/api';
45

56
import './styles.scss';
67

@@ -12,6 +13,33 @@ const Result: React.FC = () => {
1213
const query = useQuery();
1314
const factorialNumber = Number(query.get("factorial"));
1415

16+
const [loading, setLoading] = useState<boolean>(true);
17+
const [errors, setErrors] = useState<string>("");
18+
const [factorialResult, setFactorialResult] = useState<string>("");
19+
20+
useEffect(() => {
21+
setLoading(true);
22+
setErrors("");
23+
setFactorialResult("");
24+
25+
async function getFactorial() {
26+
if (factorialNumber) {
27+
const response = await fetch(`${api.baseURL}/factorial?number=${factorialNumber}`)
28+
if (response.status === 200) {
29+
const { result } = await response.json();
30+
setFactorialResult(result);
31+
setLoading(false);
32+
} else {
33+
const { errors } = await response.json();
34+
setErrors(errors[0].msg);
35+
setLoading(false);
36+
}
37+
}
38+
}
39+
40+
getFactorial();
41+
}, [factorialNumber])
42+
1543
const multipliers = useMemo(() => {
1644
if (factorialNumber <= 20) {
1745
let string = "";
@@ -33,24 +61,42 @@ const Result: React.FC = () => {
3361
}
3462

3563
return (
36-
<div className="container">
64+
<div id="result-page" className="container">
3765
<h2 className="title">
3866
Resultado do cálculo de {factorialNumber} fatorial é de:
3967
</h2>
4068

41-
<div className="factorial-result">
42-
<p className="multipliers">{multipliers}</p>
43-
<h4 className="result">
44-
51391281291
45-
</h4>
46-
</div>
47-
48-
<Link to="/" className="back-button">
49-
<div className="back">
50-
<MdRefresh size={22} />
51-
<span>Novo cálculo</span>
52-
</div>
53-
</Link>
69+
{
70+
loading ?
71+
<>
72+
<p>Aguarde, computando...</p>
73+
</>
74+
:
75+
<>
76+
{
77+
errors ?
78+
<>
79+
<p><strong>ERRO:</strong> {errors}</p>
80+
</>
81+
:
82+
<>
83+
<div className="factorial-result">
84+
<p className="multipliers">{multipliers}</p>
85+
<h4 className="result">
86+
{factorialResult}
87+
</h4>
88+
</div>
89+
90+
</>
91+
}
92+
<Link to="/" className="back-button">
93+
<div className="back">
94+
<MdRefresh size={22} />
95+
<span>Novo cálculo</span>
96+
</div>
97+
</Link>
98+
</>
99+
}
54100
</div>
55101
);
56102
}

src/pages/Result/styles.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
#result-page {
2+
justify-content: normal;
3+
margin: 50px 0px;
4+
height: auto;
5+
width: auto;
6+
}
7+
18
div.factorial-result {
29
background-color: var(--grey6);
310
padding: 20px;
@@ -8,6 +15,7 @@ div.factorial-result {
815
margin: 0px;
916
line-height: 45px;
1017
color: var(--grey1);
18+
word-break: break-all;
1119
}
1220

1321
p.multipliers {

0 commit comments

Comments
 (0)