feat: initialize Svelte frontend with Vite and TypeScript
- Added package.json for project configuration and dependencies. - Included images for the application (teh-jokur.png and vite.svg). - Created main application structure with App.svelte, CounterCard.svelte, and AddCounterCard.svelte components. - Implemented functionality for adding, editing, incrementing, and decrementing counters. - Added clickOutside utility for handling outside clicks in editing mode. - Configured TypeScript with appropriate tsconfig files for app and node. - Set up Vite configuration for building the application. - Added global styles in app.css for consistent UI design.
This commit is contained in:
33
.gitignore
vendored
Normal file
33
.gitignore
vendored
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
# Node dependencies
|
||||||
|
node_modules/
|
||||||
|
frontend/node_modules/
|
||||||
|
backend/node_modules/
|
||||||
|
|
||||||
|
# Build output
|
||||||
|
dist/
|
||||||
|
frontend/dist/
|
||||||
|
backend/dist/
|
||||||
|
|
||||||
|
# SQLite database and uploads
|
||||||
|
backend/db.sqlite
|
||||||
|
backend/uploads/
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
logs/
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
pnpm-debug.log*
|
||||||
|
|
||||||
|
# Editor directories and files
|
||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
.DS_Store
|
||||||
|
*.suo
|
||||||
|
*.ntvs*
|
||||||
|
*.njsproj
|
||||||
|
*.sln
|
||||||
|
*.sw?
|
||||||
|
|
||||||
|
# Environment files (if any)
|
||||||
|
.env
|
||||||
108
backend/index.js
Normal file
108
backend/index.js
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const sqlite3 = require('sqlite3').verbose();
|
||||||
|
const cors = require('cors');
|
||||||
|
const path = require('path');
|
||||||
|
const multer = require('multer');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
const dbPath = path.join(__dirname, 'db.sqlite');
|
||||||
|
const db = new sqlite3.Database(dbPath);
|
||||||
|
const upload = multer({ dest: path.join(__dirname, 'uploads/') });
|
||||||
|
|
||||||
|
app.use(cors());
|
||||||
|
app.use(express.json());
|
||||||
|
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
|
||||||
|
|
||||||
|
// Initialize database and ensure counters table exists
|
||||||
|
db.serialize(() => {
|
||||||
|
db.run(`CREATE TABLE IF NOT EXISTS counters (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
value INTEGER NOT NULL DEFAULT 0,
|
||||||
|
name TEXT NOT NULL DEFAULT 'Counter',
|
||||||
|
image TEXT
|
||||||
|
)`);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get all counters
|
||||||
|
app.get('/api/counters', (req, res) => {
|
||||||
|
db.all('SELECT * FROM counters', (err, rows) => {
|
||||||
|
if (err) return res.status(500).json({ error: err.message });
|
||||||
|
// Ensure id and value are numbers
|
||||||
|
const counters = rows.map(row => ({
|
||||||
|
...row,
|
||||||
|
id: Number(row.id),
|
||||||
|
value: Number(row.value)
|
||||||
|
}));
|
||||||
|
res.json(counters);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// Create a new counter with image
|
||||||
|
app.post('/api/counters', upload.single('image'), (req, res) => {
|
||||||
|
const { name = 'Counter', value = 0 } = req.body;
|
||||||
|
const image = req.file ? `/uploads/${req.file.filename}` : null;
|
||||||
|
db.run('INSERT INTO counters (name, value, image) VALUES (?, ?, ?)', [name, value, image], function (err) {
|
||||||
|
if (err) return res.status(500).json({ error: err.message });
|
||||||
|
res.json({ id: Number(this.lastID), name, value: Number(value), image });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update a counter (now supports image upload)
|
||||||
|
app.put('/api/counters/:id', upload.single('image'), (req, res) => {
|
||||||
|
const { name, value } = req.body;
|
||||||
|
const id = req.params.id;
|
||||||
|
let image = null;
|
||||||
|
|
||||||
|
// If a new image is uploaded, get its path
|
||||||
|
if (req.file) {
|
||||||
|
image = `/uploads/${req.file.filename}`;
|
||||||
|
// Optionally, delete the old image file
|
||||||
|
db.get('SELECT image FROM counters WHERE id = ?', [id], (err, row) => {
|
||||||
|
if (row && row.image) {
|
||||||
|
const oldImagePath = path.join(__dirname, '..', row.image);
|
||||||
|
fs.unlink(oldImagePath, () => { }); // Ignore errors
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build dynamic SQL and params
|
||||||
|
let fields = [];
|
||||||
|
let params = [];
|
||||||
|
if (name !== undefined) { fields.push('name = ?'); params.push(name); }
|
||||||
|
if (value !== undefined) { fields.push('value = ?'); params.push(value); }
|
||||||
|
if (image !== null) { fields.push('image = ?'); params.push(image); }
|
||||||
|
if (fields.length === 0) return res.status(400).json({ error: 'No valid fields to update.' });
|
||||||
|
|
||||||
|
params.push(id);
|
||||||
|
const sql = `UPDATE counters SET ${fields.join(', ')} WHERE id = ?`;
|
||||||
|
|
||||||
|
db.run(sql, params, function (err) {
|
||||||
|
if (err) return res.status(500).json({ error: err.message });
|
||||||
|
res.json({ updated: this.changes });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Delete a counter
|
||||||
|
app.delete('/api/counters/:id', (req, res) => {
|
||||||
|
db.run('DELETE FROM counters WHERE id = ?', [req.params.id], function (err) {
|
||||||
|
if (err) return res.status(500).json({ error: err.message });
|
||||||
|
res.json({ deleted: this.changes });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Serve static frontend files
|
||||||
|
const clientBuildPath = path.join(__dirname, '..', 'frontend', 'dist');
|
||||||
|
app.use(express.static(clientBuildPath));
|
||||||
|
|
||||||
|
// For SPA: serve index.html for any unknown route (after API and uploads)
|
||||||
|
// app.get('*', (req, res) => {
|
||||||
|
// if (req.path.startsWith('/api') || req.path.startsWith('/uploads')) return res.status(404).end();
|
||||||
|
// res.sendFile(path.join(clientBuildPath, 'index.html'));
|
||||||
|
// });
|
||||||
|
|
||||||
|
const PORT = 3000;
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
console.log(`API server running on http://localhost:${PORT}`);
|
||||||
|
});
|
||||||
2353
backend/package-lock.json
generated
Normal file
2353
backend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
18
backend/package.json
Normal file
18
backend/package.json
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"name": "server",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"description": "",
|
||||||
|
"dependencies": {
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"express": "^5.1.0",
|
||||||
|
"multer": "^2.0.2",
|
||||||
|
"sqlite3": "^5.1.7"
|
||||||
|
}
|
||||||
|
}
|
||||||
47
frontend/README.md
Normal file
47
frontend/README.md
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
# Svelte + TS + Vite
|
||||||
|
|
||||||
|
This template should help get you started developing with Svelte and TypeScript in Vite.
|
||||||
|
|
||||||
|
## Recommended IDE Setup
|
||||||
|
|
||||||
|
[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).
|
||||||
|
|
||||||
|
## Need an official Svelte framework?
|
||||||
|
|
||||||
|
Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more.
|
||||||
|
|
||||||
|
## Technical considerations
|
||||||
|
|
||||||
|
**Why use this over SvelteKit?**
|
||||||
|
|
||||||
|
- It brings its own routing solution which might not be preferable for some users.
|
||||||
|
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.
|
||||||
|
|
||||||
|
This template contains as little as possible to get started with Vite + TypeScript + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-vite` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project.
|
||||||
|
|
||||||
|
Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate.
|
||||||
|
|
||||||
|
**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**
|
||||||
|
|
||||||
|
Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information.
|
||||||
|
|
||||||
|
**Why include `.vscode/extensions.json`?**
|
||||||
|
|
||||||
|
Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project.
|
||||||
|
|
||||||
|
**Why enable `allowJs` in the TS template?**
|
||||||
|
|
||||||
|
While `allowJs: false` would indeed prevent the use of `.js` files in the project, it does not prevent the use of JavaScript syntax in `.svelte` files. In addition, it would force `checkJs: false`, bringing the worst of both worlds: not being able to guarantee the entire codebase is TypeScript, and also having worse typechecking for the existing JavaScript. In addition, there are valid use cases in which a mixed codebase may be relevant.
|
||||||
|
|
||||||
|
**Why is HMR not preserving my local component state?**
|
||||||
|
|
||||||
|
HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/rixo/svelte-hmr#svelte-hmr).
|
||||||
|
|
||||||
|
If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// store.ts
|
||||||
|
// An extremely simple external store
|
||||||
|
import { writable } from 'svelte/store'
|
||||||
|
export default writable(0)
|
||||||
|
```
|
||||||
16
frontend/index.html
Normal file
16
frontend/index.html
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Tally counter</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div id="app"></div>
|
||||||
|
<script type="module" src="/src/main.ts"></script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
1461
frontend/package-lock.json
generated
Normal file
1461
frontend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
21
frontend/package.json
Normal file
21
frontend/package.json
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"name": "tally-app",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.0.0",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "vite build",
|
||||||
|
"preview": "vite preview",
|
||||||
|
"check": "svelte-check --tsconfig ./tsconfig.app.json && tsc -p tsconfig.node.json"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@sveltejs/vite-plugin-svelte": "^6.2.1",
|
||||||
|
"@tsconfig/svelte": "^5.0.5",
|
||||||
|
"@types/node": "^24.10.0",
|
||||||
|
"svelte": "^5.43.5",
|
||||||
|
"svelte-check": "^4.3.3",
|
||||||
|
"typescript": "~5.9.3",
|
||||||
|
"vite": "^7.2.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
frontend/public/teh-jokur.png
Normal file
BIN
frontend/public/teh-jokur.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 39 KiB |
1
frontend/public/vite.svg
Normal file
1
frontend/public/vite.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
||||||
|
After Width: | Height: | Size: 1.5 KiB |
47
frontend/src/AddCounterCard.svelte
Normal file
47
frontend/src/AddCounterCard.svelte
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
export let onAdd: (name: string, value: number, image?: File) => void;
|
||||||
|
|
||||||
|
let name = "";
|
||||||
|
let value: number = 0;
|
||||||
|
let image: File | null = null;
|
||||||
|
let previewUrl: string | null = null;
|
||||||
|
|
||||||
|
function handleAdd() {
|
||||||
|
onAdd(name && name.trim() ? name : "Counter", value, image ?? undefined);
|
||||||
|
name = "";
|
||||||
|
value = 0;
|
||||||
|
image = null;
|
||||||
|
previewUrl = null;
|
||||||
|
(document.getElementById("add-image") as HTMLInputElement).value = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleImageChange(e: Event) {
|
||||||
|
const file = (e.target as HTMLInputElement).files?.[0] || null;
|
||||||
|
image = file;
|
||||||
|
previewUrl = file ? URL.createObjectURL(file) : null;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-img-container">
|
||||||
|
{#if previewUrl}
|
||||||
|
<img src={previewUrl} alt="Preview" class="card-img" />
|
||||||
|
{:else}
|
||||||
|
<div class="card-img placeholder"></div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
bind:value={name}
|
||||||
|
placeholder="Counter name"
|
||||||
|
class="input"
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
id="add-image"
|
||||||
|
type="file"
|
||||||
|
accept="image/*"
|
||||||
|
on:change={handleImageChange}
|
||||||
|
class="input"
|
||||||
|
/>
|
||||||
|
<button class="add-btn" on:click={handleAdd}>Add Counter</button>
|
||||||
|
</div>
|
||||||
163
frontend/src/App.svelte
Normal file
163
frontend/src/App.svelte
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { onMount } from "svelte";
|
||||||
|
import CounterCard from "./CounterCard.svelte";
|
||||||
|
import AddCounterCard from "./AddCounterCard.svelte";
|
||||||
|
|
||||||
|
type Counter = {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
value: number;
|
||||||
|
image?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
let counters: Counter[] = [];
|
||||||
|
let loading = true;
|
||||||
|
|
||||||
|
// API endpoint
|
||||||
|
const API = "http://localhost:3000/api/counters";
|
||||||
|
const CACHE_KEY = "tally-counters-cache";
|
||||||
|
|
||||||
|
async function fetchCounters() {
|
||||||
|
loading = true;
|
||||||
|
// Try to load from cache first
|
||||||
|
const cached = localStorage.getItem(CACHE_KEY);
|
||||||
|
if (cached) {
|
||||||
|
counters = JSON.parse(cached);
|
||||||
|
loading = false;
|
||||||
|
}
|
||||||
|
// Always fetch fresh data in the background
|
||||||
|
const res = await fetch(API);
|
||||||
|
const fresh = await res.json();
|
||||||
|
counters = fresh;
|
||||||
|
localStorage.setItem(CACHE_KEY, JSON.stringify(fresh));
|
||||||
|
loading = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
onMount(fetchCounters);
|
||||||
|
|
||||||
|
function increment(id: number) {
|
||||||
|
const counter = counters.find((c) => c.id === id);
|
||||||
|
if (!counter) return;
|
||||||
|
counter.value = Number(counter.value) + 1;
|
||||||
|
counters = [...counters]; // Trigger Svelte update
|
||||||
|
// Fire-and-forget API call
|
||||||
|
fetch(`${API}/${id}`, {
|
||||||
|
method: "PUT",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ value: counter.value }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function decrement(id: number) {
|
||||||
|
const counter = counters.find((c) => c.id === id);
|
||||||
|
if (!counter || Number(counter.value) <= 0) return;
|
||||||
|
counter.value = Number(counter.value) - 1;
|
||||||
|
counters = [...counters];
|
||||||
|
fetch(`${API}/${id}`, {
|
||||||
|
method: "PUT",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ value: counter.value }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let editingId: number | null = null;
|
||||||
|
let editName = "";
|
||||||
|
let editImage: File | null = null;
|
||||||
|
|
||||||
|
function startEdit(id: number) {
|
||||||
|
const counter = counters.find((c) => c.id === id);
|
||||||
|
if (!counter) return;
|
||||||
|
editingId = id;
|
||||||
|
editName = counter.name;
|
||||||
|
editImage = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function saveEdit(id: number) {
|
||||||
|
const counter = counters.find((c) => c.id === id);
|
||||||
|
if (!counter) return;
|
||||||
|
|
||||||
|
// Optimistically update UI
|
||||||
|
counter.name = editName;
|
||||||
|
if (editImage) {
|
||||||
|
// Show preview immediately
|
||||||
|
counter.image = URL.createObjectURL(editImage);
|
||||||
|
}
|
||||||
|
counters = [...counters];
|
||||||
|
|
||||||
|
// Prepare FormData for API
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append("name", editName);
|
||||||
|
if (editImage) formData.append("image", editImage);
|
||||||
|
|
||||||
|
// Fire-and-forget API call
|
||||||
|
fetch(`${API}/${id}`, {
|
||||||
|
method: "PUT",
|
||||||
|
body: formData,
|
||||||
|
}).then(() => fetchCounters()); // Optionally refresh to get real image URL
|
||||||
|
|
||||||
|
editingId = null;
|
||||||
|
editName = "";
|
||||||
|
editImage = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function remove(id: number) {
|
||||||
|
counters = counters.filter((c) => c.id !== id); // Remove from UI immediately
|
||||||
|
fetch(`${API}/${id}`, { method: "DELETE" });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function addCounter(name: string, value: number, image?: File) {
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append("name", name ? name : "Counter");
|
||||||
|
formData.append("value", String(value));
|
||||||
|
if (image) formData.append("image", image);
|
||||||
|
|
||||||
|
const res = await fetch(API, {
|
||||||
|
method: "POST",
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
const newCounter = await res.json();
|
||||||
|
counters = [...counters, newCounter];
|
||||||
|
}
|
||||||
|
|
||||||
|
function cancelEdit() {
|
||||||
|
editingId = null;
|
||||||
|
editName = "";
|
||||||
|
editImage = null;
|
||||||
|
counters = [...counters]; // <-- This triggers a re-render
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<main>
|
||||||
|
<h1>Tally counter</h1>
|
||||||
|
<div class="card-grid">
|
||||||
|
{#each counters as counter (counter.id)}
|
||||||
|
<CounterCard
|
||||||
|
{counter}
|
||||||
|
isEditing={editingId === counter.id}
|
||||||
|
editName={editingId === counter.id ? editName : counter.name}
|
||||||
|
editImage={editingId === counter.id ? editImage : null}
|
||||||
|
onIncrement={increment}
|
||||||
|
onDecrement={decrement}
|
||||||
|
onEdit={startEdit}
|
||||||
|
onSaveEdit={saveEdit}
|
||||||
|
onSetEditName={(val: string) => {
|
||||||
|
if (editingId === counter.id) editName = val;
|
||||||
|
}}
|
||||||
|
onSetEditImage={(val: File | null) => {
|
||||||
|
if (editingId === counter.id) editImage = val;
|
||||||
|
}}
|
||||||
|
onRemove={remove}
|
||||||
|
onCancelEdit={cancelEdit}
|
||||||
|
/>
|
||||||
|
{/each}
|
||||||
|
<AddCounterCard onAdd={addCounter} />
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.card-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
|
||||||
|
gap: 1em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
107
frontend/src/CounterCard.svelte
Normal file
107
frontend/src/CounterCard.svelte
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { clickOutside } from "./clickOutside";
|
||||||
|
|
||||||
|
export let counter: {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
value: number;
|
||||||
|
image?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export let onIncrement: (id: number) => void;
|
||||||
|
export let onDecrement: (id: number) => void;
|
||||||
|
export let onEdit: (id: number) => void;
|
||||||
|
export let onRemove: (id: number) => void;
|
||||||
|
|
||||||
|
export let isEditing: boolean = false;
|
||||||
|
export let editName: string = "";
|
||||||
|
export let editImage: File | null = null;
|
||||||
|
export let onSaveEdit: (id: number) => void;
|
||||||
|
export let onSetEditName: (name: string) => void;
|
||||||
|
export let onSetEditImage: (file: File | null) => void;
|
||||||
|
export let onCancelEdit: (id: number) => void;
|
||||||
|
|
||||||
|
// For previewing the new image
|
||||||
|
let previewUrl: string | null = null;
|
||||||
|
|
||||||
|
$: if (editImage) {
|
||||||
|
previewUrl = URL.createObjectURL(editImage);
|
||||||
|
} else {
|
||||||
|
previewUrl = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const BACKEND_URL = "http://localhost:3000";
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div
|
||||||
|
class="card"
|
||||||
|
role="group"
|
||||||
|
use:clickOutside={() => {
|
||||||
|
if (isEditing) onCancelEdit(counter.id);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{#if isEditing}
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
bind:value={editName}
|
||||||
|
class="card-title input"
|
||||||
|
placeholder="Edit name"
|
||||||
|
on:input={(e) =>
|
||||||
|
onSetEditName((e.target && (e.target as HTMLInputElement).value) || "")}
|
||||||
|
on:keydown={(e) => {
|
||||||
|
if (e.key === "Enter") onSaveEdit(counter.id);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<div class="card-img-container">
|
||||||
|
{#if previewUrl}
|
||||||
|
<img src={previewUrl} alt="Preview" class="card-img" />
|
||||||
|
{:else if counter.image}
|
||||||
|
<img
|
||||||
|
src={`${BACKEND_URL}${counter.image}`}
|
||||||
|
alt={counter.name}
|
||||||
|
class="card-img"
|
||||||
|
/>
|
||||||
|
{:else}
|
||||||
|
<div class="card-img placeholder"></div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
<input
|
||||||
|
type="file"
|
||||||
|
accept="image/*"
|
||||||
|
on:change={(e) =>
|
||||||
|
onSetEditImage(
|
||||||
|
e.target && (e.target as HTMLInputElement).files
|
||||||
|
? (e.target as HTMLInputElement).files![0]
|
||||||
|
: null,
|
||||||
|
)}
|
||||||
|
class="input"
|
||||||
|
/>
|
||||||
|
<div class="card-actions">
|
||||||
|
<button class="remove-btn" on:click={() => onRemove(counter.id)}
|
||||||
|
>Delete</button
|
||||||
|
>
|
||||||
|
<button class="save-btn" on:click={() => onSaveEdit(counter.id)}
|
||||||
|
>Save</button
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<div class="card-title">{counter.name}</div>
|
||||||
|
<div class="card-img-container">
|
||||||
|
{#if counter.image}
|
||||||
|
<img
|
||||||
|
src={`${BACKEND_URL}${counter.image}`}
|
||||||
|
alt={counter.name}
|
||||||
|
class="card-img"
|
||||||
|
/>
|
||||||
|
{:else}
|
||||||
|
<div class="card-img placeholder">No image</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
<div class="card-value">{counter.value}</div>
|
||||||
|
<div class="card-actions">
|
||||||
|
<button on:click={() => onDecrement(counter.id)}>-</button>
|
||||||
|
<button on:click={() => onEdit(counter.id)}>Edit</button>
|
||||||
|
<button on:click={() => onIncrement(counter.id)}>+</button>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
183
frontend/src/app.css
Normal file
183
frontend/src/app.css
Normal file
@@ -0,0 +1,183 @@
|
|||||||
|
:root {
|
||||||
|
font-family: 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card {
|
||||||
|
padding: 2em;
|
||||||
|
}
|
||||||
|
|
||||||
|
#app {
|
||||||
|
max-width: 1280px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 2rem;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.card {
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 1em;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card-title {
|
||||||
|
font-size: 1.2em;
|
||||||
|
margin: 0.5em 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card-img-container {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card-img {
|
||||||
|
max-width: 100px;
|
||||||
|
max-height: 100px;
|
||||||
|
border-radius: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card-img.placeholder {
|
||||||
|
width: 100px;
|
||||||
|
height: 100px;
|
||||||
|
background: transparent;
|
||||||
|
border-radius: 8px;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
font-size: 0.9em;
|
||||||
|
color: #999;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card-value {
|
||||||
|
font-size: 1.5em;
|
||||||
|
margin: 0.5em 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card-actions {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 0.5em;
|
||||||
|
margin-top: 1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.save-btn {
|
||||||
|
background: #4CAF50;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
border-radius: 6px;
|
||||||
|
padding: 0.4em 1em;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.save-btn:hover {
|
||||||
|
background: #45a049;
|
||||||
|
}
|
||||||
|
|
||||||
|
.remove-btn {
|
||||||
|
background: #c00;
|
||||||
|
color: #fff;
|
||||||
|
border: none;
|
||||||
|
border-radius: 6px;
|
||||||
|
padding: 0.4em 1em;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.remove-btn:hover {
|
||||||
|
background: #a00;
|
||||||
|
}
|
||||||
|
|
||||||
|
.input {
|
||||||
|
margin: 0.3em 0;
|
||||||
|
padding: 0.5em;
|
||||||
|
border-radius: 6px;
|
||||||
|
border: 1px solid #bbb;
|
||||||
|
width: 90%;
|
||||||
|
font-size: 1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-btn {
|
||||||
|
margin-top: 1em;
|
||||||
|
background: #4CAF50;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
border-radius: 6px;
|
||||||
|
padding: 0.6em 1.5em;
|
||||||
|
font-size: 1em;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.add-btn:hover {
|
||||||
|
background: #388e3c;
|
||||||
|
}
|
||||||
1
frontend/src/assets/svelte.svg
Normal file
1
frontend/src/assets/svelte.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="26.6" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 308"><path fill="#FF3E00" d="M239.682 40.707C211.113-.182 154.69-12.301 113.895 13.69L42.247 59.356a82.198 82.198 0 0 0-37.135 55.056a86.566 86.566 0 0 0 8.536 55.576a82.425 82.425 0 0 0-12.296 30.719a87.596 87.596 0 0 0 14.964 66.244c28.574 40.893 84.997 53.007 125.787 27.016l71.648-45.664a82.182 82.182 0 0 0 37.135-55.057a86.601 86.601 0 0 0-8.53-55.577a82.409 82.409 0 0 0 12.29-30.718a87.573 87.573 0 0 0-14.963-66.244"></path><path fill="#FFF" d="M106.889 270.841c-23.102 6.007-47.497-3.036-61.103-22.648a52.685 52.685 0 0 1-9.003-39.85a49.978 49.978 0 0 1 1.713-6.693l1.35-4.115l3.671 2.697a92.447 92.447 0 0 0 28.036 14.007l2.663.808l-.245 2.659a16.067 16.067 0 0 0 2.89 10.656a17.143 17.143 0 0 0 18.397 6.828a15.786 15.786 0 0 0 4.403-1.935l71.67-45.672a14.922 14.922 0 0 0 6.734-9.977a15.923 15.923 0 0 0-2.713-12.011a17.156 17.156 0 0 0-18.404-6.832a15.78 15.78 0 0 0-4.396 1.933l-27.35 17.434a52.298 52.298 0 0 1-14.553 6.391c-23.101 6.007-47.497-3.036-61.101-22.649a52.681 52.681 0 0 1-9.004-39.849a49.428 49.428 0 0 1 22.34-33.114l71.664-45.677a52.218 52.218 0 0 1 14.563-6.398c23.101-6.007 47.497 3.036 61.101 22.648a52.685 52.685 0 0 1 9.004 39.85a50.559 50.559 0 0 1-1.713 6.692l-1.35 4.116l-3.67-2.693a92.373 92.373 0 0 0-28.037-14.013l-2.664-.809l.246-2.658a16.099 16.099 0 0 0-2.89-10.656a17.143 17.143 0 0 0-18.398-6.828a15.786 15.786 0 0 0-4.402 1.935l-71.67 45.674a14.898 14.898 0 0 0-6.73 9.975a15.9 15.9 0 0 0 2.709 12.012a17.156 17.156 0 0 0 18.404 6.832a15.841 15.841 0 0 0 4.402-1.935l27.345-17.427a52.147 52.147 0 0 1 14.552-6.397c23.101-6.006 47.497 3.037 61.102 22.65a52.681 52.681 0 0 1 9.003 39.848a49.453 49.453 0 0 1-22.34 33.12l-71.664 45.673a52.218 52.218 0 0 1-14.563 6.398"></path></svg>
|
||||||
|
After Width: | Height: | Size: 1.9 KiB |
14
frontend/src/clickOutside.ts
Normal file
14
frontend/src/clickOutside.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
export function clickOutside(node: HTMLElement, callback: () => void) {
|
||||||
|
const handleClick = (event: MouseEvent) => {
|
||||||
|
if (!node.contains(event.target as Node)) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
document.addEventListener('mousedown', handleClick, true);
|
||||||
|
|
||||||
|
return {
|
||||||
|
destroy() {
|
||||||
|
document.removeEventListener('mousedown', handleClick, true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
10
frontend/src/main.ts
Normal file
10
frontend/src/main.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { mount } from 'svelte'
|
||||||
|
import './app.css'
|
||||||
|
/// <reference types="svelte" />
|
||||||
|
import App from './App.svelte'
|
||||||
|
|
||||||
|
const app = mount(App, {
|
||||||
|
target: document.getElementById('app')!,
|
||||||
|
})
|
||||||
|
|
||||||
|
export default app
|
||||||
4
frontend/src/svelte.d.ts
vendored
Normal file
4
frontend/src/svelte.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
declare module '*.svelte' {
|
||||||
|
import { SvelteComponentTyped } from 'svelte';
|
||||||
|
export default class Component extends SvelteComponentTyped<any, any, any> { }
|
||||||
|
}
|
||||||
8
frontend/svelte.config.js
Normal file
8
frontend/svelte.config.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'
|
||||||
|
|
||||||
|
/** @type {import("@sveltejs/vite-plugin-svelte").SvelteConfig} */
|
||||||
|
export default {
|
||||||
|
// Consult https://svelte.dev/docs#compile-time-svelte-preprocess
|
||||||
|
// for more information about preprocessors
|
||||||
|
preprocess: vitePreprocess(),
|
||||||
|
}
|
||||||
22
frontend/tsconfig.app.json
Normal file
22
frontend/tsconfig.app.json
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"extends": "@tsconfig/svelte/tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
||||||
|
"target": "ES2022",
|
||||||
|
"useDefineForClassFields": true,
|
||||||
|
"module": "ESNext",
|
||||||
|
"types": [ "svelte", "vite/client" ],
|
||||||
|
"composite": true,
|
||||||
|
// "noEmit": true,
|
||||||
|
/**
|
||||||
|
* Typecheck JS in `.svelte` and `.js` files by default.
|
||||||
|
* Disable checkJs if you'd like to use dynamic types in JS.
|
||||||
|
* Note that setting allowJs false does not prevent the use
|
||||||
|
* of JS in `.svelte` files.
|
||||||
|
*/
|
||||||
|
"allowJs": true,
|
||||||
|
"checkJs": true,
|
||||||
|
"moduleDetection": "force"
|
||||||
|
},
|
||||||
|
"include": [ "src/**/*.ts", "src/**/*.js", "src/**/*.svelte" ]
|
||||||
|
}
|
||||||
7
frontend/tsconfig.json
Normal file
7
frontend/tsconfig.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"files": [ ],
|
||||||
|
"references": [
|
||||||
|
{ "path": "./tsconfig.app.json" },
|
||||||
|
{ "path": "./tsconfig.node.json" }
|
||||||
|
]
|
||||||
|
}
|
||||||
27
frontend/tsconfig.node.json
Normal file
27
frontend/tsconfig.node.json
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
|
||||||
|
"target": "ES2023",
|
||||||
|
"lib": [ "ES2023" ],
|
||||||
|
"module": "ESNext",
|
||||||
|
"types": [ "node" ],
|
||||||
|
"skipLibCheck": true,
|
||||||
|
|
||||||
|
/* Bundler mode */
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
// "allowImportingTsExtensions": true,
|
||||||
|
"verbatimModuleSyntax": true,
|
||||||
|
"moduleDetection": "force",
|
||||||
|
// "noEmit": true,
|
||||||
|
"composite": true,
|
||||||
|
|
||||||
|
/* Linting */
|
||||||
|
"strict": true,
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"noUnusedParameters": true,
|
||||||
|
"erasableSyntaxOnly": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
"noUncheckedSideEffectImports": true
|
||||||
|
},
|
||||||
|
"include": [ "vite.config.ts" ]
|
||||||
|
}
|
||||||
7
frontend/vite.config.ts
Normal file
7
frontend/vite.config.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { defineConfig } from 'vite'
|
||||||
|
import { svelte } from '@sveltejs/vite-plugin-svelte'
|
||||||
|
|
||||||
|
// https://vite.dev/config/
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [svelte()],
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user