diff --git a/README.md b/README.md index 58beeacc..fc69b54c 100644 --- a/README.md +++ b/README.md @@ -1,70 +1,74 @@ -# Getting Started with Create React App +# Let's Shop -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). +## Introduction -## Available Scripts +The Let's Shop app is designed to assist senior citizens in managing their grocery shopping lists efficiently. This user-friendly application leverages the WebSpeech API to allow users to add items to their cart by speaking the name of the product. Additionally, users have the option to manually enter the name of the product. The app aims to simplify the shopping experience for seniors and make it more accessible. -In the project directory, you can run: +## Features -### `npm start` +### 1. Speech-to-Text for Spoken Items -Runs the app in the development mode.\ -Open [http://localhost:3000](http://localhost:3000) to view it in your browser. +- The app provides a speech recognition feature, allowing users to verbally specify the items they want to add to their shopping cart. +- The spoken items are then checked against an items database, and a corresponding product is mapped if a match is found. -The page will reload when you make changes.\ -You may also see any lint errors in the console. +### 2. Autocomplete for Manual Entry -### `npm test` +- Users can manually enter the name of the product using the autocomplete feature, ensuring accuracy and convenience in item selection. -Launches the test runner in the interactive watch mode.\ -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. +### 3. Edit Spoken Content -### `npm run build` +- To accommodate variations in pronunciation and speech recognition accuracy, users have the option to edit the transcribed content. -Builds the app for production to the `build` folder.\ -It correctly bundles React in production mode and optimizes the build for the best performance. +### 4. Add to Cart Button -The build is minified and the filenames include the hashes.\ -Your app is ready to be deployed! +- The app includes an "Add to Cart" button (represented by a plus sign icon) to add selected items to the cart, allowing users to build their shopping list easily. -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. +### 5. Show Cart -### `npm run eject` +- Users can view the list of items they've added to their cart by clicking the "Show Cart" button (represented by a cart icon). -**Note: this is a one-way operation. Once you `eject`, you can't go back!** +### 6. Print List -If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. +- The app offers a "Print List" feature, allowing users to generate a physical shopping list for reference while shopping in-store. -Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. +### 7. Share List via Email -You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. +- Users can share their shopping list with the grocery store or family members via email, making it a collaborative and convenient shopping experience. -## Learn More +### Login Feature -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). +- The app includes a login feature that allows users to access their shopping lists securely. +- Users can log in using their username and password. -To learn React, check out the [React documentation](https://reactjs.org/). +## Authentication -### Code Splitting +- The login feature makes use of the Authentication API at [https://fakestoreapi.com/auth/login](https://fakestoreapi.com/auth/login). +- The predefined username and password for logging in are as follows: + - Username: "mor_2314" + - Password: "83r5^_" -This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) +## Routes -### Analyzing the Bundle Size +- `/`: This route leads to the login page where users can log in using their credentials. +- `/cart`: This route provides access to the text-to-speech and cart functionality, allowing users to interact with their shopping list. -This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) +## APIs Used in the Project -### Making a Progressive Web App +- **Get a List of All Available Products**: The app retrieves a list of all available products from [https://dummyjson.com/products](https://dummyjson.com/products). This API is used to populate the product database and provide a wide selection of items for users to add to their shopping lists. -This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) +- **Authentication API**: The authentication feature of the app utilizes the Authentication API at [https://fakestoreapi.com/auth/login](https://fakestoreapi.com/auth/login) to securely authenticate users and grant access to their shopping lists. -### Advanced Configuration +The Let's Shop app is designed to make grocery shopping more accessible and user-friendly for seniors. Enjoy a seamless and efficient shopping experience with the convenience of speech recognition, manual entry features, and secure login functionality. -This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) +## How to Run the App -### Deployment +Follow these steps to run the Let's Shop app on your local machine: + + +```bash + npm i + npm i --force (incase of any dependency issues) + npm start -This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) -### `npm run build` fails to minify -This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) diff --git a/package-lock.json b/package-lock.json index 89f4e0cd..6f686953 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,8 +16,12 @@ "@testing-library/jest-dom": "5.16.5", "@testing-library/react": "13.4.0", "@testing-library/user-event": "13.5.0", - "react": "18.2.0", - "react-dom": "18.2.0", + "react": "^18.2.0", + "react-autosuggest": "^10.1.0", + "react-dom": "^18.2.0", + "react-mic": "^12.4.6", + "react-router": "^6.16.0", + "react-router-dom": "^6.16.0", "react-scripts": "5.0.1", "web-vitals": "2.1.4" } @@ -4530,6 +4534,14 @@ "url": "https://opencollective.com/popperjs" } }, + "node_modules/@remix-run/router": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.9.0.tgz", + "integrity": "sha512-bV63itrKBC0zdT27qYm6SDZHlkXwFL1xMBuhkn+X7l0+IIhNaH5wuuvZKp6eKhCD4KFhujhfhCT1YxXW6esUIA==", + "engines": { + "node": ">=14.0.0" + } + }, "node_modules/@rollup/plugin-babel": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/@rollup/plugin-babel/-/plugin-babel-5.3.1.tgz", @@ -8293,6 +8305,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/es6-promise": { + "version": "4.2.8", + "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.8.tgz", + "integrity": "sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w==" + }, "node_modules/escalade": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", @@ -17139,6 +17156,21 @@ "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz", "integrity": "sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==" }, + "node_modules/react-autosuggest": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/react-autosuggest/-/react-autosuggest-10.1.0.tgz", + "integrity": "sha512-/azBHmc6z/31s/lBf6irxPf/7eejQdR0IqnZUzjdSibtlS8+Rw/R79pgDAo6Ft5QqCUTyEQ+f0FhL+1olDQ8OA==", + "dependencies": { + "es6-promise": "^4.2.8", + "prop-types": "^15.7.2", + "react-themeable": "^1.1.0", + "section-iterator": "^2.0.0", + "shallow-equal": "^1.2.1" + }, + "peerDependencies": { + "react": ">=16.3.0" + } + }, "node_modules/react-dev-utils": { "version": "12.0.1", "resolved": "https://registry.npmjs.org/react-dev-utils/-/react-dev-utils-12.0.1.tgz", @@ -17267,6 +17299,28 @@ "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", "integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==" }, + "node_modules/react-mic": { + "version": "12.4.6", + "resolved": "https://registry.npmjs.org/react-mic/-/react-mic-12.4.6.tgz", + "integrity": "sha512-2/DZoz7thR2nJyekF10zBvs/7a8HhUQ4L8MV6BpC+Q/T8G1MvpRHGSHjSlVtnbzaCMDJ3R1MdThoLu15WuVh/g==", + "dependencies": { + "prop-types": "^15.5.10", + "react-ga": "^2.2.0" + }, + "peerDependencies": { + "prop-types": "^15.5.10", + "react": "16.x" + } + }, + "node_modules/react-mic/node_modules/react-ga": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/react-ga/-/react-ga-2.7.0.tgz", + "integrity": "sha512-AjC7UOZMvygrWTc2hKxTDvlMXEtbmA0IgJjmkhgmQQ3RkXrWR11xEagLGFGaNyaPnmg24oaIiaNPnEoftUhfXA==", + "peerDependencies": { + "prop-types": "^15.6.0", + "react": "^15.6.2 || ^16.0" + } + }, "node_modules/react-refresh": { "version": "0.11.0", "resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.11.0.tgz", @@ -17275,6 +17329,36 @@ "node": ">=0.10.0" } }, + "node_modules/react-router": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/react-router/-/react-router-6.16.0.tgz", + "integrity": "sha512-VT4Mmc4jj5YyjpOi5jOf0I+TYzGpvzERy4ckNSvSh2RArv8LLoCxlsZ2D+tc7zgjxcY34oTz2hZaeX5RVprKqA==", + "dependencies": { + "@remix-run/router": "1.9.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "react": ">=16.8" + } + }, + "node_modules/react-router-dom": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.16.0.tgz", + "integrity": "sha512-aTfBLv3mk/gaKLxgRDUPbPw+s4Y/O+ma3rEN1u8EgEpLpPe6gNjIsWt9rxushMHHMb7mSwxRGdGlGdvmFsyPIg==", + "dependencies": { + "@remix-run/router": "1.9.0", + "react-router": "6.16.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "react": ">=16.8", + "react-dom": ">=16.8" + } + }, "node_modules/react-scripts": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-5.0.1.tgz", @@ -17347,6 +17431,22 @@ } } }, + "node_modules/react-themeable": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/react-themeable/-/react-themeable-1.1.0.tgz", + "integrity": "sha512-kl5tQ8K+r9IdQXZd8WLa+xxYN04lLnJXRVhHfdgwsUJr/SlKJxIejoc9z9obEkx1mdqbTw1ry43fxEUwyD9u7w==", + "dependencies": { + "object-assign": "^3.0.0" + } + }, + "node_modules/react-themeable/node_modules/object-assign": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-3.0.0.tgz", + "integrity": "sha512-jHP15vXVGeVh1HuaA2wY6lxk+whK/x4KBG88VXeRma7CCun7iGD5qPc4eYykQ9sdQvg8jkwFKsSxHln2ybW3xQ==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/react-transition-group": { "version": "4.4.5", "resolved": "https://registry.npmjs.org/react-transition-group/-/react-transition-group-4.4.5.tgz", @@ -17928,6 +18028,11 @@ "url": "https://opencollective.com/webpack" } }, + "node_modules/section-iterator": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/section-iterator/-/section-iterator-2.0.0.tgz", + "integrity": "sha512-xvTNwcbeDayXotnV32zLb3duQsP+4XosHpb/F+tu6VzEZFmIjzPdNk6/O+QOOx5XTh08KL2ufdXeCO33p380pQ==" + }, "node_modules/select-hose": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/select-hose/-/select-hose-2.0.0.tgz", @@ -18125,6 +18230,11 @@ "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==" }, + "node_modules/shallow-equal": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/shallow-equal/-/shallow-equal-1.2.1.tgz", + "integrity": "sha512-S4vJDjHHMBaiZuT9NPb616CSmLf618jawtv3sufLl6ivK8WocjAo58cXwbRV1cgqxH0Qbv+iUt6m05eqEa2IRA==" + }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", diff --git a/package.json b/package.json index ca2a9136..95635d6d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "shopping-list-app", - "version": "0.1.0", + "version": "0.1.1", "private": true, "dependencies": { "@emotion/react": "11.11.0", @@ -11,8 +11,12 @@ "@testing-library/jest-dom": "5.16.5", "@testing-library/react": "13.4.0", "@testing-library/user-event": "13.5.0", - "react": "18.2.0", - "react-dom": "18.2.0", + "react": "^18.2.0", + "react-autosuggest": "^10.1.0", + "react-dom": "^18.2.0", + "react-mic": "^12.4.6", + "react-router": "^6.16.0", + "react-router-dom": "^6.16.0", "react-scripts": "5.0.1", "web-vitals": "2.1.4" }, diff --git a/public/add-to-cart.png b/public/add-to-cart.png new file mode 100644 index 00000000..eda88a07 Binary files /dev/null and b/public/add-to-cart.png differ diff --git a/public/add.png b/public/add.png new file mode 100644 index 00000000..3d0b46fd Binary files /dev/null and b/public/add.png differ diff --git a/public/logo.png b/public/logo.png new file mode 100644 index 00000000..17a46508 Binary files /dev/null and b/public/logo.png differ diff --git a/public/microphone.png b/public/microphone.png new file mode 100644 index 00000000..7ed7d52d Binary files /dev/null and b/public/microphone.png differ diff --git a/public/printImg.png b/public/printImg.png new file mode 100644 index 00000000..2b05b708 Binary files /dev/null and b/public/printImg.png differ diff --git a/public/shareImg.png b/public/shareImg.png new file mode 100644 index 00000000..a3476d5b Binary files /dev/null and b/public/shareImg.png differ diff --git a/src/App.js b/src/App.js index 96d24665..9344da69 100644 --- a/src/App.js +++ b/src/App.js @@ -1,98 +1,19 @@ -import React, { useEffect, useState } from 'react' - -import CssBaseline from '@mui/material/CssBaseline' - -import { - createTheme, - ThemeProvider, - responsiveFontSizes, -} from '@mui/material/styles' - -import Stack from '@mui/material/Stack' - -import Content from './components/Content' -import Loading from './components/Loading' - - -let theme = createTheme({ - palette: { - primary: { - main: '#de6720', - }, - secondary: { - main: '#0077ea', - }, - }, - components: { - MuiCssBaseline: { - styleOverrides: { - html: { - WebkitFontSmoothing: 'auto', - }, - body: { - background: '#fefefe', - overflow: 'hidden', - padding: '5px', - width: '450px', - color: '#333', - }, - }, - }, - MuiLinearProgress: { - styleOverrides: { - root: { - backgroundColor: 'rgba(222, 103, 32, 0.25)', - }, - bar: { - backgroundColor: 'rgba(222, 103, 32, 1)', - }, - }, - }, - }, -}) -theme = responsiveFontSizes(theme) - - -const App = () => { - - const [loading, setLoading] = useState(false) - - useEffect(() => { - - // show a loading indicator - setLoading(true) - - setTimeout(() => { - - // hide loading indicator - setLoading(false) - }, 1000) // 1 second - - // hide loading indicator - return () => { - setLoading(false) - } - }, []) - - const renderLoading = () => { - if ( !loading ) { return } - return - } - - const renderContent = () => { - if ( loading ) { return } - return - } +import React from 'react'; +import Login from './components/Login'; +import SpeechToText from './components/SpeechToText'; +import { BrowserRouter, Routes, Route } from 'react-router-dom'; +function App() { return ( - - - - {renderLoading()} - {renderContent()} - - - ) + <> + + + }> + }> + + + + ); } -export default App +export default App; diff --git a/src/components/CartItemList.css b/src/components/CartItemList.css new file mode 100644 index 00000000..341c76fe --- /dev/null +++ b/src/components/CartItemList.css @@ -0,0 +1,135 @@ + +.cart-item-list { + /* padding:10%; */ + margin:5%; + background-color: #f7f7f7; + border: 1px solid #ddd; + padding: 20px; + border-radius: 5px; + margin-top: 20px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); +} +.item-quantity { + font-size: 16px; + color: #666; + +} +.item-image{ + width: 50px; + height: 50px; +} +.cart-header { + font-size: 24px; + margin-bottom: 20px; + color: #333; +} + +.cart-list { + list-style: none; + padding: 0; +} + +.cart-item { + border: 1px solid #ccc; + padding: 10px; + margin-bottom: 10px; + border-radius: 5px; + display: flex; + flex-direction: column; +} + +.item-title { + font-weight: bold; + font-size: 18px; + margin-bottom: 10px; + color: #333; +} + +.item-details { + display: flex; + flex-direction: column; +} + +.item-id, +.item-price, +.item-rating,.item-quantity { + font-size: 16px; + margin-bottom: 5px; + color: #666; +} + +@media (max-width: 768px) { + .cart-item { + flex-direction: column; + } + + .item-title { + font-size: 16px; + } + + .item-id, + .item-price, + .item-rating { + font-size: 14px; + } + + .cart-buttons { + display: flex; + justify-content: space-between; + align-items: center; + margin-top: 20px; + margin-bottom: 10px; + } + + .cart-button { + width: 100px; + height: 40px; + background-color: #22847f; + color: #fff; + border: none; + border-radius: 5px; + cursor: pointer; + transition: background-color 0.3s; + display: flex; + align-items: center; + justify-content: center; + } + + .cart-button i { + margin-right: 5px; + } + + .cart-button:hover { + background-color: #0056b3; + } +} + + +@media (min-width: 769px) { + .cart-buttons { + display: flex; + gap: 10px; + } + + .cart-button { + width: auto; + height: auto; + background-color: #22847f; + color: #fff; + border: none; + border-radius: 5px; + cursor: pointer; + transition: background-color 0.3s; + display: flex; + align-items: center; + padding: 5px 10px; + } + + .cart-button i { + margin-right: 5px; + } + + /* .cart-button:hover { + background-color: #0056b3; + } */ +} diff --git a/src/components/Content.js b/src/components/Content.js index 44b93897..2444a61e 100644 --- a/src/components/Content.js +++ b/src/components/Content.js @@ -10,7 +10,7 @@ const Content = ({ socketConnection }) => { return ( - {renderHello()} + {/* {renderHello()} */} ) } diff --git a/src/components/Login.js b/src/components/Login.js new file mode 100644 index 00000000..bff8713a --- /dev/null +++ b/src/components/Login.js @@ -0,0 +1,79 @@ +import React, { useState } from 'react'; +import './login.css'; +import { useNavigate } from 'react-router'; + +function Login() { + const navigate = useNavigate(); + + const [username, setUsername] = useState('mor_2314'); + const [password, setPassword] = useState('83r5^_'); + const [message, setMessage] = useState(''); + + const handleUsernameChange = (event) => { + const { value } = event.target; + setUsername(value); + }; + + const handlePasswordChange = (event) => { + const { value } = event.target; + setPassword(value); + }; + + const handleLogin = (event) => { + event.preventDefault(); + + fetch('https://fakestoreapi.com/auth/login', { + method: 'POST', + body: JSON.stringify({ + username: username, + password: password, + }), + headers: { + 'Content-Type': 'application/json', + }, + }) + .then((response) => response.json()) + .then((data) => { + if (data.token) { + + setMessage('Login successful!'); + + navigate('/cart'); + } else { + + setMessage('Login failed. Please check your credentials.'); + } + }) + .catch((error) => { + console.error('Error:', error); + setMessage('Login failed. Please check your credentials.'); + }); + }; + + return ( +
+

Let's Shop

+ Logo +
+ + + +
{message}
+
+
+ ); +} + +export default Login; diff --git a/src/components/SpeechToText.css b/src/components/SpeechToText.css new file mode 100644 index 00000000..61d3f551 --- /dev/null +++ b/src/components/SpeechToText.css @@ -0,0 +1,174 @@ + +.container { + text-align: center; + margin: 2rem auto; + max-width: 80%; + padding: 0 20px; + box-sizing: border-box; + display: flex; + flex-direction: column; + align-items: center; + position: relative; +} + +.header { + font-size: 24px; + margin-bottom: 1rem; + color: #333; +} + + +.record-button { + width: 80px; + height: 80px; + /* background-color: #007bff; */ + color: #fff; + border: none; + padding: 10px; + font-size: 18px; + cursor: pointer; + border-radius: 50%; + outline: none; + display: inline-flex; + align-items: center; + justify-content: center; + transition: background-color 0.3s; + position: relative; + z-index: 1; +} + +.record-button.listening { + background-color: #dc3545; +} + +.mic-image { + width: 30px; + height: 30px; +} + +.transcript-container { + margin-top: 1rem; + position: relative; +} + +.transcript-header { + font-size: 18px; + margin-bottom: 1rem; + color: #333; +} + +.transcript-textarea { + width: 100%; + height: 50px; + padding: 10px; + border: 1px solid #ccc; + border-radius: 5px; + font-size: 16px; + resize: none; + z-index: 1; +} + +.suggestions-container { + position: absolute; + width: 100%; + max-height: 150px; + overflow-y: auto; + background-color: #fff; + border: 1px solid #ccc; + border-radius: 5px; + box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1); + z-index: 1000; + top: calc(100% + 10px); + left: 0; + display: none; + list-style: none; + padding: 0; +} + +.suggestion { + + cursor: pointer; + transition: background-color 0.3s; +} + +.suggestion:hover { + background-color: #f0f0f0; +} + + +.add-to-cart-button, +.show-list-button { + background-color: rgb(147, 173, 184); + border: none; + cursor: pointer; + outline: none; + /* margin: 1rem; */ + margin-right: 0.5rem; + padding: 1%; + width: 50%; + position: relative; + z-index: 1; +} + +.button-icon { + width: 30px; + height: 30px; +} + +.button-container { + display: flex; + flex-direction: row; + margin-top: 1rem; + width: 50%; +} + +.input-method-container { + display: flex; + flex-direction: row; + gap: 20px; + align-items: center; + justify-content: center; +} + +.input-method-container label { + display: flex; + align-items: center; +} + +.input-method-container input[type="radio"] { + margin-right: 8px; +} + +@media (max-width: 768px) { + .container { + padding: 0 10px; + } + + .record-button { + font-size: 16px; + padding: 8px; + width: 50px; + height: 50px; + } + + .mic-image { + width: 16px; + height: 16px; + } + + .transcript-textarea { + height: 30px; + font-size: 14px; + } + + .add-to-cart-button, + .show-list-button { + font-size: 16px; + padding: 8px 16px; + } + + .button-icon { + width: 24px; + height: 24px; + } +} diff --git a/src/components/SpeechToText.js b/src/components/SpeechToText.js new file mode 100644 index 00000000..8967b17d --- /dev/null +++ b/src/components/SpeechToText.js @@ -0,0 +1,217 @@ +import React, { useState, useEffect } from 'react'; +import './SpeechToText.css'; +import Autosuggest from 'react-autosuggest'; +import CartItemList from './cartItems'; + +const recognition = new window.webkitSpeechRecognition(); +recognition.continuous = true; +recognition.interimResults = true; +recognition.lang = 'en-US'; + +function SpeechToText() { + const [listening, setListening] = useState(false); + const [transcript, setTranscript] = useState(''); + const [isDisabled, setIsDisabled] = useState(false); + const [inputMethod, setInputMethod] = useState('speak'); + const [manualInput, setManualInput] = useState(''); + const [cartItems, setCartItems] = useState([]); + const [showCart, setShowCart] = useState(false); + const [suggestions, setSuggestions] = useState([]); + + useEffect(() => { + recognition.onresult = handleRecognitionResult; + recognition.onerror = handleRecognitionError; + return () => { + recognition.onresult = null; + recognition.onerror = null; + recognition.stop(); + }; + }, []); + + const toggleCart = () => { + setShowCart(!showCart); + }; + + const handleRecognitionResult = (event) => { + let newTranscript = transcript; + for (let i = event.resultIndex; i < event.results.length; i++) { + newTranscript += event.results[i][0].transcript + ' '; + } + setTranscript(newTranscript); + }; + + const handleRecognitionError = (event) => { + console.error('Speech recognition error:', event.error); + stopRecording(); + }; + + const startRecording = () => { + setTranscript(''); + recognition.start(); + setListening(true); + setIsDisabled(true); + }; + + const stopRecording = () => { + recognition.stop(); + setListening(false); + setIsDisabled(false); + }; + + const handleTextareaChange = (e) => { + if (inputMethod === 'speak') { + setTranscript(e.target.value); + } else if (inputMethod === 'type') { + setManualInput(e.target.value); + } + }; + + const fetchGroceries = () => { + const url = 'https://dummyjson.com/products'; + return fetch(url) + .then((res) => res.json()) + .then((data) => { + return data; + }); + }; + + const addToCart = async () => { + let product; + const data = await fetchGroceries(); + if (inputMethod === 'speak') { + console.log('Item added to cart:', transcript); + product = searchProductByWord(transcript, data.products); + } else if (inputMethod === 'type') { + console.log('Item added to cart:', manualInput); + product = searchProductByWord(manualInput, data.products); + } + + if (!product || (transcript.trim().length == 0 && manualInput.trim().length == 0)) { + console.log('No matching products found.'); + alert('No matching products found.'); + } else { + console.log(`ID: ${product.id}`); + console.log(`Title: ${product.title}`); + console.log(`Price: $${product.price}`); + console.log(`Rating: ${product.rating}`); + console.log('------------------------'); + setCartItems([...cartItems, product]); + alert(product.title + ' - Added to the cart'); + } + }; + + const handleRadioChange = (e) => { + setInputMethod(e.target.value); + setTranscript(''); + setManualInput(''); + }; + + const getCartItems = () => { + console.log('cart items', cartItems); + }; + + function searchProductByWord(word, productsData) { + const cleanedWord = word.replace(/^\s+|\s+$/g, ''); + const foundProduct = productsData.find((product) => + product.title.toLowerCase().includes(cleanedWord.toLowerCase()) + ); + + return foundProduct; + } + + const renderSuggestion = (suggestion) => ( +
{suggestion.title}
+ ); + + const handleInputChange = (event, { newValue }) => { + setManualInput(newValue); + }; + + const onSuggestionsFetchRequested = async ({ value }) => { + const data = await fetchGroceries(); + const filteredProducts = data.products.filter((product) => + product.title.toLowerCase().includes(value.toLowerCase()) + ); + + setSuggestions(filteredProducts.slice(0, 5)); + }; + + const onSuggestionsClearRequested = () => { + setSuggestions([]); + }; + + return ( +
+
+

Please Select Input Method

+
+ + +
+ {inputMethod === 'speak' && ( +
+

Transcript:

+