-
-
Notifications
You must be signed in to change notification settings - Fork 963
Expand file tree
/
Copy pathCategoryFact.jsx
More file actions
106 lines (100 loc) · 3.28 KB
/
CategoryFact.jsx
File metadata and controls
106 lines (100 loc) · 3.28 KB
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
import React, { useState } from 'react';
import axios from 'axios';
import MathImage from '../components/images/math.png';
import YearImage from '../components/images/year.png';
import DateImage from '../components/images/date.png';
import { CategoryFacts } from './DigitsDelightsConstant';
import './Categoryfact.css';
function CategoryFact() {
const [fact, setFact] = useState(CategoryFacts);
// const [error, setError] = useState('');
const [selectedVoice, setSelectedVoice] = useState('Zira');
const [showSelecter, setShowSelecter] = useState(false);
const speechHandler = (msg) => {
let voices = [];
window.speechSynthesis.onvoiceschanged = () => {
voices = window.speechSynthesis.getVoices();
};
// This for loop helps to give the selected voice
for (let i = 0; i < voices.length; i++) {
if (voices[i].name === selectedVoice) {
msg.voice = voices[i];
}
}
window.speechSynthesis.cancel();
window.speechSynthesis.speak(msg);
};
const getFact = async (type) => {
const options = {
method: 'GET',
url: `https://numbersapi.p.rapidapi.com/random/${type}`,
params: { fragment: 'true', json: 'true' },
headers: {
'X-RapidAPI-Key': process.env.REACT_APP_DIGITSDELIGHT_APIKEY,
'X-RapidAPI-Host': 'numbersapi.p.rapidapi.com'
}
};
try {
const { data } = await axios.request(options);
setFact(data.number + ' is ' + data.text);
speechHandler(new SpeechSynthesisUtterance(data.number + ' is ' + data.text));
setShowSelecter(true);
} catch (error) {
// setError('Error fetching fact. Please try again later.');
}
};
const handleVoiceChange = ({ target }) => setSelectedVoice(target.value);
return (
<div>
<div className="digits-delight-voice-selection-container" style={{ marginTop: '2px' }}>
<select
className={`digits-voice-select ${showSelecter ? 'opacity-1' : 'opacity-0'}`}
placeholder="Select the voices"
value={selectedVoice}
onChange={handleVoiceChange}
>
{window.speechSynthesis.getVoices().map((voice) => (
<option key={voice.name} value={voice.name}>
{voice.name}
</option>
))}
</select>
</div>
<div className="digits-category-facts">
{fact && <p className="get-digits-facts">{fact}</p>}
</div>
<div>
<ul className="thumb">
<li>
<button
className="randomButton tooltip math"
tooltip-text="Random Math Facts"
onClick={() => getFact('math')}
>
<img src={MathImage} />
</button>
</li>
<li>
<button
className="randomButton tooltip date"
tooltip-text="Facts By Date"
onClick={() => getFact('date')}
>
<img src={DateImage} />
</button>
</li>
<li>
<button
className="randomButton tooltip year"
tooltip-text="Facts By Year"
onClick={() => getFact('year')}
>
<img src={YearImage} />
</button>
</li>
</ul>
</div>
</div>
);
}
export default CategoryFact;