Setup frontend

This commit is contained in:
Manik Maity
2024-11-05 22:12:03 +05:30
parent b5686b2961
commit 0d38e35655
9 changed files with 1304 additions and 365 deletions

228
README.md
View File

@@ -1,228 +0,0 @@
### System Design LivePoll
#### 1. **Features**
- **User Authentication:** Users can log in, create polls, and vote. Authenticated users can bookmark polls.
- **Create Polls:** Users can create polls with options for others to vote on.
- **Real-Time Voting & Visualization:** Live updates for ongoing polls and dynamic visualizations using charts.
- **Bookmark Polls:** Users can bookmark polls to view or participate in later.
- **View Past Participation:** A history of polls the user participated in.
#### 2. **Frontend (React)**
- **React Components**:
- **Login/Register Page:** For user authentication.
- **Dashboard:** Displays polls created by the user and polls they bookmarked.
- **Poll Creation Page:** Allows users to create a new poll with title, description, and options.
- **Poll Display Page:** Shows the poll with voting options, live updates on votes, and a visualization of the voting progress (e.g., bar chart or pie chart).
- **Bookmarks Page:** Displays a list of bookmarked polls.
- **Real-Time Voting Updates**:
- Use **Socket.io** or **WebSockets** to enable real-time updates. When a user votes, the data is sent to the server and then broadcast to other connected clients, updating the visualization immediately.
#### 3. **Backend (Node.js + Express)**
- **API Endpoints**:
- **Auth Routes**:
- `POST /api/auth/register`: User registration.
- `POST /api/auth/login`: User login.
- **Poll Routes**:
- `POST /api/polls`: Create a new poll.
- `GET /api/polls/:pollId`: Get poll details, including live vote count.
- `POST /api/polls/:pollId/vote`: Submit a vote for a poll.
- `GET /api/polls/bookmarked`: Retrieve all bookmarked polls for a user.
- `POST /api/polls/:pollId/bookmark`: Bookmark a poll.
- **User Routes**:
- `GET /api/user/history`: Get a list of polls the user has participated in.
- **Real-Time Updates**:
- **Socket.io**: When a vote is cast, the server broadcasts the updated vote count to all connected clients subscribed to that poll. This ensures real-time updates on each client.
#### 4. **Database (MongoDB)**
- **Collections**:
- **Users**: Stores user information, bookmarks, and participated polls.
- **Polls**: Contains details of each poll, options, votes, and the creators user ID.
- **Votes**: Each document records a vote with a user ID, poll ID, and option ID.
- **Bookmarks**: Stores references to polls a user has bookmarked.
- **Schema Design**:
- **User Schema**:
```javascript
{
_id: ObjectId,
username: String,
password: String, // hashed
bookmarks: [pollId],
history: [pollId]
}
```
- **Poll Schema**:
```javascript
{
_id: ObjectId,
title: String,
description: String,
options: [
{ optionId: ObjectId, text: String, voteCount: Number }
],
creatorId: ObjectId,
createdAt: Date
}
```
- **Vote Schema**:
```javascript
{
_id: ObjectId,
userId: ObjectId,
pollId: ObjectId,
optionId: ObjectId
}
```
#### 5. **Real-Time Voting with Socket.io**
- **Workflow**:
- When a user votes on a poll, the frontend triggers a `vote` event via **Socket.io**.
- The server receives this event, updates the vote count in the database, and broadcasts the updated vote count to all clients connected to that poll.
- This enables real-time visualization as each client receives the updated poll data without refreshing the page.
#### 6. **Chart Visualization**
- **Frontend Visualization**:
- Use **Chart.js** or **D3.js** to create charts that display poll results.
- As Socket.io sends updates, the poll display page refreshes the chart data dynamically, showing live vote changes.
#### 7. **Bookmarking & Poll History**
- **Bookmarking a Poll**:
- When a user bookmarks a poll, the poll ID is added to their bookmarks in the `Users` collection.
- The bookmarks page fetches all polls that match the bookmarked poll IDs and displays them.
- **Poll History**:
- Each vote by a user is recorded, with the poll ID added to their `history` field in the `Users` collection.
- This allows users to view past polls they participated in.
### System Diagram
A basic diagram for this system would include:
1. **Frontend (React)**: Login/Register Page, Dashboard, Poll Creation Page, Poll Display Page with charts, Bookmark and History pages.
2. **Backend (Express)**: API endpoints for user actions (vote, create poll, bookmark), real-time voting handled by Socket.io.
3. **Database (MongoDB)**: Stores collections for users, polls, votes, and bookmarks.
4. **Socket.io/WebSocket**: Handles real-time updates from the server to the clients as votes come in.
This setup should provide a responsive and interactive polling application using the MERN stack!Here's a high-level system design for a **real-time polling app** using the **MERN (MongoDB, Express.js, React, Node.js) stack** that meets the outlined requirements.
### System Design Overview
#### 1. **Features**
- **User Authentication:** Users can log in, create polls, and vote. Authenticated users can bookmark polls.
- **Create Polls:** Users can create polls with options for others to vote on.
- **Real-Time Voting & Visualization:** Live updates for ongoing polls and dynamic visualizations using charts.
- **Bookmark Polls:** Users can bookmark polls to view or participate in later.
- **View Past Participation:** A history of polls the user participated in.
#### 2. **Frontend (React)**
- **React Components**:
- **Login/Register Page:** For user authentication.
- **Dashboard:** Displays polls created by the user and polls they bookmarked.
- **Poll Creation Page:** Allows users to create a new poll with title, description, and options.
- **Poll Display Page:** Shows the poll with voting options, live updates on votes, and a visualization of the voting progress (e.g., bar chart or pie chart).
- **Bookmarks Page:** Displays a list of bookmarked polls.
- **Real-Time Voting Updates**:
- Use **Socket.io** or **WebSockets** to enable real-time updates. When a user votes, the data is sent to the server and then broadcast to other connected clients, updating the visualization immediately.
#### 3. **Backend (Node.js + Express)**
- **API Endpoints**:
- **Auth Routes**:
- `POST /api/auth/register`: User registration.
- `POST /api/auth/login`: User login.
- **Poll Routes**:
- `POST /api/polls`: Create a new poll.
- `GET /api/polls/:pollId`: Get poll details, including live vote count.
- `POST /api/polls/:pollId/vote`: Submit a vote for a poll.
- `GET /api/polls/bookmarked`: Retrieve all bookmarked polls for a user.
- `POST /api/polls/:pollId/bookmark`: Bookmark a poll.
- **User Routes**:
- `GET /api/user/history`: Get a list of polls the user has participated in.
- **Real-Time Updates**:
- **Socket.io**: When a vote is cast, the server broadcasts the updated vote count to all connected clients subscribed to that poll. This ensures real-time updates on each client.
#### 4. **Database (MongoDB)**
- **Collections**:
- **Users**: Stores user information, bookmarks, and participated polls.
- **Polls**: Contains details of each poll, options, votes, and the creators user ID.
- **Votes**: Each document records a vote with a user ID, poll ID, and option ID.
- **Bookmarks**: Stores references to polls a user has bookmarked.
- **Schema Design**:
- **User Schema**:
```javascript
{
_id: ObjectId,
username: String,
password: String, // hashed
bookmarks: [pollId],
history: [pollId]
}
```
- **Poll Schema**:
```javascript
{
_id: ObjectId,
title: String,
description: String,
options: [
{ optionId: ObjectId, text: String, voteCount: Number }
],
creatorId: ObjectId,
createdAt: Date
}
```
- **Vote Schema**:
```javascript
{
_id: ObjectId,
userId: ObjectId,
pollId: ObjectId,
optionId: ObjectId
}
```
#### 5. **Real-Time Voting with Socket.io**
- **Workflow**:
- When a user votes on a poll, the frontend triggers a `vote` event via **Socket.io**.
- The server receives this event, updates the vote count in the database, and broadcasts the updated vote count to all clients connected to that poll.
- This enables real-time visualization as each client receives the updated poll data without refreshing the page.
#### 6. **Chart Visualization**
- **Frontend Visualization**:
- Use **Chart.js** or **D3.js** to create charts that display poll results.
- As Socket.io sends updates, the poll display page refreshes the chart data dynamically, showing live vote changes.
#### 7. **Bookmarking & Poll History**
- **Bookmarking a Poll**:
- When a user bookmarks a poll, the poll ID is added to their bookmarks in the `Users` collection.
- The bookmarks page fetches all polls that match the bookmarked poll IDs and displays them.
- **Poll History**:
- Each vote by a user is recorded, with the poll ID added to their `history` field in the `Users` collection.
- This allows users to view past polls they participated in.
### System Diagram
A basic diagram for this system would include:
1. **Frontend (React)**: Login/Register Page, Dashboard, Poll Creation Page, Poll Display Page with charts, Bookmark and History pages.
2. **Backend (Express)**: API endpoints for user actions (vote, create poll, bookmark), real-time voting handled by Socket.io.
3. **Database (MongoDB)**: Stores collections for users, polls, votes, and bookmarks.
4. **Socket.io/WebSocket**: Handles real-time updates from the server to the clients as votes come in.

View File

@@ -1,5 +1,5 @@
<!doctype html>
<html lang="en">
<html lang="en" data-theme="night">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />

File diff suppressed because it is too large Load Diff

View File

@@ -18,11 +18,15 @@
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@vitejs/plugin-react": "^4.3.3",
"autoprefixer": "^10.4.20",
"daisyui": "^4.12.14",
"eslint": "^9.13.0",
"eslint-plugin-react": "^7.37.2",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.14",
"globals": "^15.11.0",
"postcss": "^8.4.47",
"tailwindcss": "^3.4.14",
"vite": "^5.4.10"
}
}

View File

@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}

View File

@@ -1,42 +0,0 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}
@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}
.card {
padding: 2em;
}
.read-the-docs {
color: #888;
}

View File

@@ -1,34 +1,13 @@
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import { } from 'react'
import './App.css'
function App() {
const [count, setCount] = useState(0)
return (
<>
<div>
<a href="https://vite.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.jsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</>
<div className="bg-slate-700">
Hello
</div>
)
}

View File

@@ -1,68 +1,3 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
@tailwind base;
@tailwind components;
@tailwind utilities;

View File

@@ -0,0 +1,17 @@
/** @type {import('tailwindcss').Config} */
export default {
content: [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [
require('daisyui'),
],
daisyui: {
themes: ["light", "dark", "night"],
},
}