diff --git a/pages/admin/categories.tsx b/pages/admin/categories.tsx new file mode 100644 index 0000000..c183371 --- /dev/null +++ b/pages/admin/categories.tsx @@ -0,0 +1,129 @@ +import { useState, useEffect } from 'react' +import type { NextPage } from 'next' +import Head from 'next/head' +import Link from 'next/link' + +interface Category { + id: number + name: string + slug: string + description: string + color: string + priority: number + is_active: boolean +} + +const CategoriesManagement: NextPage = () => { + const [categories, setCategories] = useState([]) + const [loading, setLoading] = useState(true) + + useEffect(() => { + fetchCategories() + }, []) + + const fetchCategories = async () => { + try { + const response = await fetch('/api/admin/categories') + if (response.ok) { + const data = await response.json() + setCategories(data) + } + } catch (error) { + console.error('Failed to fetch categories:', error) + } finally { + setLoading(false) + } + } + + const toggleCategory = async (id: number, isActive: boolean) => { + try { + const response = await fetch(`/api/admin/categories/${id}`, { + method: 'PATCH', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ is_active: !isActive }) + }) + if (response.ok) { + fetchCategories() + } + } catch (error) { + console.error('Failed to update category:', error) + } + } + + return ( +
+ + Správa kategórií - Infohliadka + + +
+

Správa kategórií

+ + {loading ? ( +
Loading...
+ ) : ( + + + + + + + + + + + + {categories.map((category) => ( + + + + + + + + ))} + +
NázovPrioritaFarbaAktívnaAkcie
+ {category.name} +
+ {category.description} +
+
+ {category.priority} + +
+ {category.color} +
+ {category.is_active ? '✅' : '❌'} + + +
+ )} + +
+ ← Späť na dashboard +
+
+
+ ) +} + +export default CategoriesManagement \ No newline at end of file diff --git a/pages/api/admin/categories/[id].ts b/pages/api/admin/categories/[id].ts new file mode 100644 index 0000000..f865ea4 --- /dev/null +++ b/pages/api/admin/categories/[id].ts @@ -0,0 +1,39 @@ +import type { NextApiRequest, NextApiResponse } from 'next' +import sqlite3 from 'sqlite3' +import path from 'path' + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + if (req.method !== 'PATCH') { + return res.status(405).json({ error: 'Method not allowed' }) + } + + const { id } = req.query + const { is_active } = req.body + + const dbPath = path.join(process.cwd(), 'database', 'antihoax.db') + const db = new sqlite3.Database(dbPath) + + try { + await new Promise((resolve, reject) => { + db.run( + 'UPDATE categories SET is_active = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?', + [is_active, id], + function(err) { + if (err) reject(err) + else resolve() + } + ) + }) + + return res.status(200).json({ success: true }) + + } catch (error) { + console.error('Database error:', error) + return res.status(500).json({ error: 'Internal server error' }) + } finally { + db.close() + } +} \ No newline at end of file diff --git a/pages/api/admin/categories/index.ts b/pages/api/admin/categories/index.ts new file mode 100644 index 0000000..7513de8 --- /dev/null +++ b/pages/api/admin/categories/index.ts @@ -0,0 +1,35 @@ +import type { NextApiRequest, NextApiResponse } from 'next' +import sqlite3 from 'sqlite3' +import path from 'path' + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + if (req.method !== 'GET') { + return res.status(405).json({ error: 'Method not allowed' }) + } + + const dbPath = path.join(process.cwd(), 'database', 'antihoax.db') + const db = new sqlite3.Database(dbPath) + + try { + const categories = await new Promise((resolve, reject) => { + db.all( + 'SELECT * FROM categories ORDER BY priority DESC, name ASC', + (err, rows) => { + if (err) reject(err) + else resolve(rows) + } + ) + }) + + return res.status(200).json(categories) + + } catch (error) { + console.error('Database error:', error) + return res.status(500).json({ error: 'Internal server error' }) + } finally { + db.close() + } +} \ No newline at end of file