From 1f606911aca161c9ecbb393ca0f637c103916ce8 Mon Sep 17 00:00:00 2001 From: Devendra Singh <21f1006853@ds.study.iitm.ac.in> Date: Thu, 20 Feb 2025 01:50:02 +0530 Subject: [PATCH] Formated App and TextForm components for improved readability and added grammar check functionality and first letter capital functionality --- src/App.js | 60 ++++++++++++++----------- src/components/TextForm.js | 91 +++++++++++++++++++++++++++----------- 2 files changed, 100 insertions(+), 51 deletions(-) diff --git a/src/App.js b/src/App.js index ed583f4..1346457 100644 --- a/src/App.js +++ b/src/App.js @@ -10,28 +10,36 @@ import { Route } from "react-router-dom"; - + function App() { const [mode, setMode] = useState('light'); // Whether dark mode is enabled or not const [alert, setAlert] = useState(null); - const showAlert = (message, type)=>{ - setAlert({ - msg: message, - type: type - }) + const showAlert = (message, type) => { + setAlert({ + msg: message, + type: type + }) + if (type === 'danger') { setTimeout(() => { - setAlert(null); + setAlert(null); + }, 10000); + + } else { + + setTimeout(() => { + setAlert(null); }, 1500); + } } - const toggleMode = ()=>{ - if(mode === 'light'){ + const toggleMode = () => { + if (mode === 'light') { setMode('dark'); document.body.style.backgroundColor = '#042743'; showAlert("Dark mode has been enabled", "success"); } - else{ + else { setMode('light'); document.body.style.backgroundColor = 'white'; showAlert("Light mode has been enabled", "success"); @@ -39,23 +47,23 @@ function App() { } return ( <> - - - -
- - {/* /users --> Component 1 + + + +
+ + {/* /users --> Component 1 /users/home --> Component 2 */} - - - - - - - -
-
- + + + + + + +
+
+
+ ); } diff --git a/src/components/TextForm.js b/src/components/TextForm.js index 07549d6..6ae4ee7 100644 --- a/src/components/TextForm.js +++ b/src/components/TextForm.js @@ -1,32 +1,32 @@ -import React, {useState} from 'react' +import React, { useState } from 'react' export default function TextForm(props) { - const handleUpClick = ()=>{ + const handleUpClick = () => { let newText = text.toUpperCase(); setText(newText) props.showAlert("Converted to uppercase!", "success"); } - const handleLoClick = ()=>{ + const handleLoClick = () => { let newText = text.toLowerCase(); setText(newText) props.showAlert("Converted to lowercase!", "success"); } - const handleClearClick = ()=>{ + const handleClearClick = () => { let newText = ''; setText(newText); props.showAlert("Text Cleared!", "success"); } - const handleOnChange = (event)=>{ - setText(event.target.value) + const handleOnChange = (event) => { + setText(event.target.value) } // Credits: A const handleCopy = () => { - navigator.clipboard.writeText(text); + navigator.clipboard.writeText(text); props.showAlert("Copied to Clipboard!", "success"); } @@ -37,29 +37,70 @@ export default function TextForm(props) { props.showAlert("Extra spaces removed!", "success"); } - const [text, setText] = useState(''); + // Credits: Dev3ndr4 + const handleFirstCapital = () => { + let newText = text.split('. ').map(sentence => { + // Trim any extra spaces and capitalize the first letter of each sentence + return sentence.charAt(0).toUpperCase() + sentence.slice(1); + }).join('. '); + + setText(newText); + props.showAlert("Capitalized First Letter of Each Sentence!", "success"); + } + + // Credits: Dev3ndr4 + const handleGrammarCheck = async () => { + const response = await fetch('https://api.languagetool.org/v2/check', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: new URLSearchParams({ + text: text, // The text to check + language: 'en-US', // Specify language + }), + }); + + const data = await response.json(); + + if (data.matches.length > 0) { + let errors = data.matches.map(match => { + return `Error: ${match.message} at position ${match.offset}`; + }).join('\n'); + + props.showAlert(`Grammar Issues Found: \n${errors}`, "danger"); + } else { + props.showAlert("No grammar issues found!", "success"); + } + }; + + + const [text, setText] = useState(''); // text = "new text"; // Wrong way to change the state // setText("new text"); // Correct way to change the state return ( <> -
-

{props.heading}

-
- +
+

{props.heading}

+
+ +
+ + + + + + + + +
+
+

Your text summary

+

{text.split(/\s+/).filter((element) => { return element.length !== 0 }).length} words and {text.length} characters

+

{0.008 * text.split(/\s+/).filter((element) => { return element.length !== 0 }).length} Minutes read

+

Preview

+

{text.length > 0 ? text : "Nothing to preview!"}

- - - - - -
-
-

Your text summary

-

{text.split(/\s+/).filter((element)=>{return element.length!==0}).length} words and {text.length} characters

-

{0.008 * text.split(/\s+/).filter((element)=>{return element.length!==0}).length} Minutes read

-

Preview

-

{text.length>0?text:"Nothing to preview!"}

-
) }