Skip to content

Commit 8a1e593

Browse files
committed
Added alerts in project
1 parent dc3317c commit 8a1e593

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/App.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
11
import { useState } from 'react';
22
import './App.css';
3+
import Alerts from './components/Alerts';
34
import Navbar from './components/Navbar';
45
import TextForm from './components/TextForm';
56
// import AboutUs from './components/AboutUs'
67

78
function App() {
8-
const [mode, setMode] = useState('light')
9+
// Stat for light and dark mode
10+
const [mode, setMode] = useState('light');
11+
12+
// State for alert
13+
const [alert, setAlert] = useState(null);
14+
15+
const showAlert = (message, type) => {
16+
setAlert({
17+
msg: message,
18+
type: type
19+
});
20+
setTimeout(() => {
21+
setAlert(null)
22+
}, 1500)
23+
}
924

1025
const toggleMode = () => {
1126
if(mode === 'light'){
1227
setMode('dark');
1328
document.body.style.backgroundColor = '#042743';
29+
showAlert("Dark mode has been enabled", "success")
1430
}
1531
else {
1632
setMode('light');
1733
document.body.style.backgroundColor = 'white';
34+
showAlert("Light mode has been enabled", "success")
1835
}
1936
}
2037
return (
2138
<>
2239
{/* Importing navbar component */}
2340
<Navbar title = "TU" aboutText= "About us" mode={mode} toggleMode={toggleMode}/>
2441

42+
{/* Alert */}
43+
<Alerts alert={alert} />
44+
2545
{/*Importing TextForm component */}
2646
<div className="container">
27-
<TextForm heading="Enter the text to analyze" mode={mode}/>
47+
<TextForm showAlert={showAlert} heading="Enter the text to analyze" mode={mode}/>
2848
</div>
2949

3050
{/* <AboutUs /> */}

src/components/Alerts.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react'
2+
3+
4+
function Alerts(props) {
5+
6+
const capitalize = (world) => {
7+
const lower = world.toLowerCase();
8+
return lower.charAt(0).toUpperCase() + lower.slice(1);
9+
}
10+
11+
return (
12+
props.alert && <div className={`alert alert-${props.alert.type} alert-dismissible fade show`} role="alert">
13+
<strong>{capitalize(props.alert.type)}:</strong> {props.alert.msg}
14+
</div>
15+
)
16+
}
17+
18+
export default Alerts

src/components/TextForm.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ export default function TextForm(props) {
66
const handleDownClick = () => {
77
let newText = text.toLowerCase()
88
setText(newText)
9+
props.showAlert("Converted to lowercase", "success")
910
}
1011

1112
const handleClearText = () => {
1213
let clearText = ''
1314
setText(clearText)
15+
props.showAlert("Text has been cleared", "success")
1416
}
1517

1618
// This is a finction for converting lower case to uppaercase
1719
const handleUpClick = () => {
1820
let newText = text.toUpperCase()
1921
setText(newText)
22+
props.showAlert("Converted to uppercase", "success")
2023
}
2124

2225
// This is a function for handling text into TextArea
@@ -30,12 +33,14 @@ export default function TextForm(props) {
3033
var text = document.getElementById("myBox");
3134
text.select();
3235
navigator.clipboard.writeText(text.value)
36+
props.showAlert("Text has been copied", "success")
3337
}
3438

3539
// Handling Extra spaces
3640
const handleExtraSpaces = () => {
3741
let newText = text.split(/[ ]+/);
3842
setText(newText.join(" "))
43+
props.showAlert("Extra space has been removed", "success")
3944
}
4045

4146

@@ -52,7 +57,7 @@ export default function TextForm(props) {
5257
<button className="btn btn-primary me-2" onClick={handleUpClick} >Convert to uppercase</button>
5358

5459
{/* Conver to lower case */}
55-
<button className="btn btn-primary me-2" onClick={handleDownClick} >Convert to uppercase</button>
60+
<button className="btn btn-primary me-2" onClick={handleDownClick} >Convert to lowercase</button>
5661

5762
{/* Copy text */}
5863
<button className="btn btn-primary me-2" onClick={handleCopy} >Copy Text</button>

0 commit comments

Comments
 (0)