Creating a single-page web application to submit records for "naming and shaming" businesses involves setting up a basic front end and back end along with a database. We'll use Docker to deploy it. Here's a simple implementation guide:
Technology Stack
- Frontend: HTML, CSS (Neumorphism design style), JavaScript
- Backend: Node.js/Express.js
- Database: MongoDB
- Containerization: Docker
1. Structure
/app
|-- /frontend
|-- index.html
|-- styles.css
|-- main.js
|-- /backend
|-- server.js
|-- Dockerfile
|-- docker-compose.yml
2. Frontend Code
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Name & Shame</title>
</head>
<body>
<div class="container">
<h1>Name & Shame</h1>
<form id="submit-form">
<input type="text" id="business-id" placeholder="Google Maps Share Link ID" required>
<select id="tags" multiple>
<option value="slow-service">Slow Service</option>
<option value="rude-staff">Rude Staff</option>
<!-- Add more predefined tags here -->
</select>
<input type="text" id="new-tag" placeholder="Add a new tag">
<textarea id="comment" maxlength="280" placeholder="Add your comment"></textarea>
<button type="submit">Submit</button>
</form>
<div id="tabs">
<button onclick="loadTab('latest')">Latest</button>
<button onclick="loadTab('top')">Top Voted</button>
</div>
<div id="display-area"></div>
</div>
<script src="main.js"></script>
</body>
</html>
styles.css
body {
font-family: Arial, sans-serif;
background-color: #e0e0e0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background: #f0f0f0;
border-radius: 10px;
box-shadow: 10px 10px 20px #bebebe, -10px -10px 20px #ffffff;
padding: 20px;
width: 300px;
}
input, select, textarea {
display: block;
width: 100%;
margin-bottom: 10px;
padding: 5px;
border: none;
border-radius: 5px;
box-shadow: inset 2px 2px 5px #bebebe, inset -2px -2px 5px #ffffff;
}
button {
border: none;
padding: 10px;
border-radius: 5px;
background: #e0e0e0;
box-shadow: 5px 5px 10px #bebebe, -5px -5px 10px #ffffff;
cursor: pointer;
}
button:active {
box-shadow: inset 1px 1px 2px #bebebe, inset -1px -1px 2px #ffffff;
}
main.js
document.getElementById('submit-form').addEventListener('submit', function(e) {
e.preventDefault();
const businessId = document.getElementById('business-id').value;
const tags = [...document.getElementById('tags').selectedOptions].map(option => option.value);
const newTag = document.getElementById('new-tag').value;
if (newTag) tags.push(newTag);
const comment = document.getElementById('comment').value;
fetch('/api/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ businessId, tags, comment })
}).then(response => response.json())
.then(data => alert(data.message))
.catch(error => alert('Error: ' + error));
});
function loadTab(tab) {
fetch(`/api/${tab}`)
.then(response => response.json())
.then(data => updateDisplayArea(data))
.catch(error => console.error('Error fetching tab:', error));
}
function updateDisplayArea(data) {
const displayArea = document.getElementById('display-area');
displayArea.innerHTML = '';
data.forEach(item => {
const entry = document.createElement('div');
const comment = document.createTextNode(item.comment);
entry.appendChild(comment);
displayArea.appendChild(entry);
});
}
3. Backend Code
server.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
let records = []; // Ideally, this will be replaced with a database connection
app.post('/api/submit', (req, res) => {
const { businessId, tags, comment } = req.body;
if (!businessId || !comment) {
return res.status(400).json({ message: 'Invalid input' });
}
const record = { businessId, tags, comment, votes: 0, timestamp: new Date() };
records.push(record);
res.json({ message: 'Submitted successfully' });
});
app.get('/api/latest', (req, res) => {
const latestRecords = records.sort((a, b) => b.timestamp - a.timestamp).slice(0, 10);
res.json(latestRecords);
});
app.get('/api/top', (req, res) => {
const topRecords = records.sort((a, b) => b.votes - a.votes).slice(0, 10);
res.json(topRecords);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
4. Docker Configuration
Dockerfile
FROM node:14
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
CMD ["node", "backend/server.js"]
docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- .:/app
Steps to Deploy
- Install Docker and Docker Compose.
- Navigate to the
app directory.
- Run the command:
docker-compose up --build.
This basic setup provides a form to submit complaints about businesses identified by Google Maps IDs, allows tagging, comments, and displays sorted entries with upvote functionality. For a production app, you'll want to replace the list of records with a proper database and enhance user authentication and input validation.
Improvements
- Add a real authentication system for user management.
- Implement a proper database like MongoDB or MySQL.
- Enhance error handling and input validation.
- Optimize and scale the app according to traffic and data security requirements.