first commit

main
Jeremy 2025-06-17 21:17:40 -05:00
commit 1769e75bb6
45 changed files with 8477 additions and 0 deletions

24
.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

12
README.md Normal file
View File

@ -0,0 +1,12 @@
# React + Vite
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
Currently, two official plugins are available:
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
## Expanding the ESLint configuration
If you are developing a production application, we recommend using TypeScript with type-aware lint rules enabled. Check out the [TS template](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) for information on how to integrate TypeScript and [`typescript-eslint`](https://typescript-eslint.io) in your project.

33
eslint.config.js Normal file
View File

@ -0,0 +1,33 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
export default [
{ ignores: ['dist'] },
{
files: ['**/*.{js,jsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
parserOptions: {
ecmaVersion: 'latest',
ecmaFeatures: { jsx: true },
sourceType: 'module',
},
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...js.configs.recommended.rules,
...reactHooks.configs.recommended.rules,
'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
]

14
index.html Normal file
View File

@ -0,0 +1,14 @@
<!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" />
<link href="./output.css" rel="stylesheet">
<title>Vite + React</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>

4112
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

38
package.json Normal file
View File

@ -0,0 +1,38 @@
{
"name": "frontend",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@tailwindcss/cli": "^4.1.8",
"@tailwindcss/vite": "^4.1.8",
"axios": "^1.9.0",
"multer": "^2.0.1",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"react-icons": "^5.5.0",
"react-router-dom": "^7.6.2",
"react-toastify": "^11.0.5",
"slugify": "^1.6.6"
},
"devDependencies": {
"@eslint/js": "^9.25.0",
"@types/react": "^19.1.2",
"@types/react-dom": "^19.1.2",
"@vitejs/plugin-react": "^4.4.1",
"autoprefixer": "^10.4.21",
"eslint": "^9.25.0",
"eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.19",
"globals": "^16.0.0",
"postcss": "^8.5.4",
"tailwindcss": "^4.1.8",
"vite": "^6.3.5"
}
}

1
public/vite.svg Normal file
View 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

0
src/App.css Normal file
View File

98
src/App.jsx Normal file
View File

@ -0,0 +1,98 @@
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import axios from 'axios';
import { CartProvider } from './context/CartContext';
import { ThemeProvider } from './context/ThemeContext'; // Added import
import Home from './pages/Home';
import Register from './pages/Register';
import CreatePage from './pages/CreatePage';
import EditPage from './pages/EditPage';
import Login from './pages/Login';
import AdminDashboard from './pages/AdminDashboard';
import AdminPages from './pages/AdminPages';
import AdminPosts from './pages/AdminPosts';
import AdminOrders from './pages/AdminOrders';
import AdminFulfillments from './pages/AdminFulfillments';
import AdminShipping from './pages/AdminShipping';
import PageViewer from './pages/PageViewer';
import AdminTemplates from './pages/AdminTemplates';
import CreatePost from './pages/CreatePost';
import AdminMedia from './pages/AdminMedia';
import CreateTemplate from './pages/CreateTemplate';
import LandingPage from './pages/LandingPage';
import ContactUs from './pages/ContactUs';
import AdminForms from './pages/AdminForms';
import CreateForm from './pages/CreateForm';
import ProductsPage from './pages/ProductsPage';
import Cart from './pages/Cart';
import ProductsAdmin from './pages/ProductsAdmin';
function DynamicPage() {
const { slug } = useParams();
const [page, setPage] = useState(null);
const [error, setError] = useState('');
useEffect(() => {
axios
.get(`http://localhost:3000/pages/${slug}`)
.then((response) => setPage(response.data))
.catch(() => setError('Failed to load page'));
}, [slug]);
if (error) return <div className="container mx-auto px-6 py-8 text-red-500 dark:text-red-400">Failed to load page</div>;
if (!page) return <div className="container mx-auto px-6 py-8 text-gray-600 dark:text-gray-300">Loading...</div>;
const componentMap = {
PageViewer,
LandingPage,
ContactUs,
ProductsPage,
};
const Component = componentMap[page.component] || PageViewer;
return <Component page={page} />;
}
function App() {
return (
<CartProvider>
<ThemeProvider> {/* Wrap entire app in ThemeProvider */}
<Router>
<div className="min-h-screen bg-gray-100 dark:bg-gray-900 font-sans">
<Routes>
<Route path="/" element={<Home />} />
<Route path="/register" element={<Register />} />
<Route path="/login" element={<Login />} />
<Route path="/page/:slug" element={<DynamicPage />} />
<Route path="/cart" element={<Cart />} />
<Route path="/admin/*" element={<AdminDashboard />}>
<Route path="pages" element={<AdminPages />} />
<Route path="posts" element={<AdminPosts />} />
<Route path="orders" element={<AdminOrders />} />
<Route path="fulfillments" element={<AdminFulfillments />} />
<Route path="shipping" element={<AdminShipping />} />
<Route path="templates" element={<AdminTemplates />} />
<Route path="create-page" element={<CreatePage />} />
<Route path="edit-page/:id" element={<EditPage />} />
<Route path="create-post" element={<CreatePost />} />
<Route path="media" element={<AdminMedia />} />
<Route path="create-template" element={<CreateTemplate />} />
<Route path="create-template/:id" element={<CreateTemplate />} />
<Route path="forms" element={<AdminForms />} />
<Route path="create-form" element={<CreateForm />} />
<Route path="create-form/:id" element={<CreateForm />} />
<Route path="products" element={<ProductsAdmin />} />
</Route>
</Routes>
<ToastContainer position="top-right" autoClose={3000} theme={localStorage.getItem('theme') || 'light'} />
</div>
</Router>
</ThemeProvider>
</CartProvider>
);
}
export default App;

1
src/assets/react.svg Normal file
View 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="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>

After

Width:  |  Height:  |  Size: 4.0 KiB

View File

@ -0,0 +1,109 @@
import React, { useState, useRef } from 'react';
const CustomWYSIWYG = ({ value, onChange }) => {
const editorRef = useRef(null);
const [showLinkInput, setShowLinkInput] = useState(false);
const [linkUrl, setLinkUrl] = useState('');
const execCommand = (command, value = null) => {
document.execCommand(command, false, value);
editorRef.current.focus();
onChange(editorRef.current.innerHTML);
};
const handleBold = () => execCommand('bold');
const handleItalic = () => execCommand('italic');
const handleUnderline = () => execCommand('underline');
const handleOrderedList = () => execCommand('insertOrderedList');
const handleUnorderedList = () => execCommand('insertUnorderedList');
const handleLink = () => {
if (showLinkInput && linkUrl) {
const selection = window.getSelection();
if (!selection.rangeCount) return;
const range = selection.getRangeAt(0);
execCommand('createLink', linkUrl);
setShowLinkInput(false);
setLinkUrl('');
} else {
setShowLinkInput(true);
}
};
const handleInput = () => {
onChange(editorRef.current.innerHTML);
};
return (
<div className="border border-gray-300 rounded-md">
<div className="flex bg-gray-100 p-2 border-b border-gray-300">
<button
type="button"
onClick={handleBold}
className="px-2 py-1 mr-1 text-gray-700 hover:bg-gray-200 rounded"
title="Bold"
>
<strong>B</strong>
</button>
<button
type="button"
onClick={handleItalic}
className="px-2 py-1 mr-1 text-gray-700 hover:bg-gray-200 rounded"
title="Italic"
>
<em>I</em>
</button>
<button
type="button"
onClick={handleUnderline}
className="px-2 py-1 mr-1 text-gray-700 hover:bg-gray-200 rounded"
title="Underline"
>
<u>U</u>
</button>
<button
type="button"
onClick={handleOrderedList}
className="px-2 py-1 mr-1 text-gray-700 hover:bg-gray-200 rounded"
title="Ordered List"
>
1.
</button>
<button
type="button"
onClick={handleUnorderedList}
className="px-2 py-1 mr-1 text-gray-700 hover:bg-gray-200 rounded"
title="Unordered List"
>
</button>
<button
type="button"
onClick={handleLink}
className="px-2 py-1 text-gray-700 hover:bg-gray-200 rounded"
title="Link"
>
🔗
</button>
{showLinkInput && (
<input
type="text"
value={linkUrl}
onChange={(e) => setLinkUrl(e.target.value)}
placeholder="Enter URL"
className="ml-2 p-1 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
)}
</div>
<div
ref={editorRef}
contentEditable
onInput={handleInput}
className="p-3 min-h-[150px] bg-white text-gray-800 focus:outline-none"
dangerouslySetInnerHTML={{ __html: value }}
/>
</div>
);
};
export default CustomWYSIWYG;

View File

@ -0,0 +1,92 @@
import { useState, useEffect } from 'react';
function FormField({ field, config, value, onChange, errorMessage }) {
const [error, setError] = useState(errorMessage || '');
useEffect(() => {
setError(errorMessage || '');
}, [errorMessage]);
const validate = (val) => {
let err = '';
if (config.required && !val) {
err = 'This field is required';
} else if (config.minlength && val.length < config.minlength) {
err = `Minimum length is ${config.minlength} characters`;
} else if (config.maxlength && val.length > config.maxlength) {
err = `Maximum length is ${config.maxlength} characters`;
} else if (config.inputType === 'email' && val && !/^[^@]+@[^@]+\.[^@]+$/.test(val)) {
err = 'Invalid email format';
}
setError(err);
return err;
};
const handleChange = (e) => {
const newValue = e.target.value;
validate(newValue);
onChange(field, newValue);
};
const baseClasses = 'w-full p-3 border border-gray-300 rounded-lg focus:outline-none transition duration-200';
const focusClasses = error ? 'focus:ring-2 focus:ring-red-500' : 'focus:ring-2 focus:ring-blue-500';
const errorClasses = error ? 'border-red-500' : 'border-gray-300';
switch (config.type) {
case 'input':
return (
<div className="mb-4">
<label className="block text-gray-700 text-sm font-semibold mb-2 capitalize">{field}</label>
<input
type={config.inputType || 'text'}
value={value || ''}
onChange={handleChange}
required={config.required}
maxLength={config.maxlength}
minLength={config.minlength}
placeholder={`Enter ${field}`}
className={`${baseClasses} ${focusClasses} ${errorClasses}`}
/>
{error && <p className="text-red-500 text-sm mt-1">{error}</p>}
</div>
);
case 'textarea':
return (
<div className="mb-4">
<label className="block text-gray-700 text-sm font-semibold mb-2 capitalize">{field}</label>
<textarea
value={value || ''}
onChange={handleChange}
required={config.required}
maxLength={config.maxlength}
minLength={config.minlength}
placeholder={`Enter ${field}`}
className={`${baseClasses} h-24 ${focusClasses} ${errorClasses}`}
/>
{error && <p className="text-red-500 text-sm mt-1">{error}</p>}
</div>
);
case 'select':
return (
<div className="mb-4">
<label className="block text-gray-700 text-sm font-semibold mb-2 capitalize">{field}</label>
<select
value={value || ''}
onChange={handleChange}
required={config.required}
className={`${baseClasses} ${focusClasses} ${errorClasses}`}
>
<option value="">Select an option</option>
{(config.options || []).map((option, idx) => (
<option key={idx} value={option}>{option}</option>
))}
</select>
{error && <p className="text-red-500 text-sm mt-1">{error}</p>}
</div>
);
default:
return null;
}
}
export default FormField;

View File

@ -0,0 +1,98 @@
import { useState, useEffect } from 'react';
import axios from 'axios';
import { toast } from 'react-toastify';
const MediaPickerModal = ({ isOpen, onClose, onSelect }) => {
const [media, setMedia] = useState([]);
const [selectedImage, setSelectedImage] = useState(null);
useEffect(() => {
if (isOpen) {
const token = localStorage.getItem('token');
axios
.get('http://localhost:3000/admin/media', {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => setMedia(response.data))
.catch(() => toast.error('Failed to load media'));
}
}, [isOpen]);
const handleUpload = async (e) => {
const file = e.target.files[0];
if (!file) return;
const formData = new FormData();
formData.append('image', file);
try {
const token = localStorage.getItem('token');
const response = await axios.post('http://localhost:3000/admin/media', formData, {
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'multipart/form-data' },
});
setMedia([...media, response.data]);
toast.success('Image uploaded successfully');
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to upload image');
}
};
const handleSelect = () => {
if (selectedImage) {
onSelect(selectedImage.path);
onClose();
} else {
toast.error('Please select an image');
}
};
if (!isOpen) return null;
return (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
<div className="bg-white p-6 rounded-lg shadow-lg max-w-4xl w-full max-h-[80vh] overflow-y-auto">
<h2 className="text-2xl font-bold text-gray-800 mb-4">Select Media</h2>
<div className="mb-4">
<input
type="file"
accept="image/*"
onChange={handleUpload}
className="w-full p-3 border border-gray-300 rounded-md"
/>
</div>
<div className="grid grid-cols-4 gap-4">
{media.map((item) => (
<div
key={item.id}
className={`relative cursor-pointer border-2 ${selectedImage?.id === item.id ? 'border-blue-500' : 'border-transparent'}`}
onClick={() => setSelectedImage(item)}
>
<img
src={`http://localhost:3000${item.path}`}
alt={item.filename}
className="w-full h-32 object-cover rounded"
/>
<p className="text-xs text-gray-600 truncate">{item.filename}</p>
</div>
))}
</div>
<div className="flex justify-end mt-6">
<button
type="button"
onClick={onClose}
className="bg-gray-600 text-white px-4 py-2 rounded-md hover:bg-gray-700 mr-2"
>
Cancel
</button>
<button
type="button"
onClick={handleSelect}
className="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700"
>
Select
</button>
</div>
</div>
</div>
);
};
export default MediaPickerModal;

View File

@ -0,0 +1,45 @@
import { useState, useEffect } from 'react';
import axios from 'axios';
import { useCart } from '../context/CartContext'; // Fixed import path
function Products() {
const [products, setProducts] = useState([]);
const [error, setError] = useState('');
const { addToCart } = useCart();
useEffect(() => {
axios
.get('http://localhost:3000/products')
.then((response) => setProducts(response.data))
.catch((err) => setError('Failed to load products: ' + (err.response?.data?.error || 'Unknown error')));
}, []);
if (error) return <div className="text-red-500 text-center">{error}</div>;
return (
<div className="grid grid-cols-1 md:grid-cols-3 gap-6">
{products.map((product) => (
<div key={product.id} className="bg-white p-4 rounded-xl shadow-lg border border-gray-200">
{product.image && (
<img
src={`http://localhost:3000${product.image}`}
alt={product.name}
className="w-full h-48 object-cover rounded-md mb-4"
/>
)}
<h3 className="text-lg font-semibold text-gray-800">{product.name}</h3>
<p className="text-gray-600 mb-2">{product.description}</p>
<p className="text-gray-800 font-bold mb-4">${product.price.toFixed(2)}</p>
<button
onClick={() => addToCart(product)}
className="bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition duration-300 w-full"
>
Add to Cart
</button>
</div>
))}
</div>
);
}
export default Products;

View File

@ -0,0 +1,69 @@
import { createContext, useContext, useState, useEffect } from 'react';
const CartContext = createContext();
export function CartProvider({ children }) {
const [cart, setCart] = useState({});
useEffect(() => {
const savedCart = localStorage.getItem('cart');
if (savedCart) {
setCart(JSON.parse(savedCart));
}
}, []);
useEffect(() => {
localStorage.setItem('cart', JSON.stringify(cart));
}, [cart]);
const addToCart = (product, quantity = 1) => {
setCart((prev) => ({
...prev,
[product.id]: {
product,
quantity: (prev[product.id]?.quantity || 0) + quantity,
},
}));
};
const removeFromCart = (productId) => {
setCart((prev) => {
const newCart = { ...prev };
delete newCart[productId];
return newCart;
});
};
const updateQuantity = (productId, quantity) => {
if (quantity <= 0) {
removeFromCart(productId);
} else {
setCart((prev) => ({
...prev,
[productId]: {
...prev[productId],
quantity,
},
}));
}
};
const clearCart = () => {
setCart({});
localStorage.removeItem('cart');
};
const getTotal = () => {
return Object.values(cart).reduce((sum, item) => sum + item.product.price * item.quantity, 0).toFixed(2);
};
return (
<CartContext.Provider value={{ cart, addToCart, removeFromCart, updateQuantity, clearCart, getTotal }}>
{children}
</CartContext.Provider>
);
}
export function useCart() {
return useContext(CartContext);
}

View File

@ -0,0 +1,43 @@
import { createContext, useContext, useState, useEffect } from 'react';
const ThemeContext = createContext();
export function ThemeProvider({ children }) {
const [theme, setTheme] = useState(() => {
const savedTheme = localStorage.getItem('theme');
console.log('Initial theme:', savedTheme || 'light');
return savedTheme || 'light';
});
useEffect(() => {
console.log('Applying theme:', theme);
localStorage.setItem('theme', theme);
if (theme === 'dark') {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
}, [theme]);
const toggleTheme = () => {
setTheme((prev) => {
const newTheme = prev === 'light' ? 'dark' : 'light';
console.log('Toggling to theme:', newTheme);
return newTheme;
});
};
return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
{children}
</ThemeContext.Provider>
);
}
export function useTheme() {
const context = useContext(ThemeContext);
if (!context) {
throw new Error('useTheme must be used within a ThemeProvider');
}
return context;
}

1
src/index.css Normal file
View File

@ -0,0 +1 @@
@import "tailwindcss";

10
src/main.jsx Normal file
View File

@ -0,0 +1,10 @@
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>,
)

View File

@ -0,0 +1,265 @@
import { useEffect, useState } from 'react';
import { Outlet, Link, useNavigate } from 'react-router-dom';
import axios from 'axios';
import { useTheme } from '../context/ThemeContext';
function AdminDashboard() {
const [isAdmin, setIsAdmin] = useState(false);
const [openAccordions, setOpenAccordions] = useState({
pages: false,
posts: false,
templates: false,
forms: false,
ecommerce: false,
});
const navigate = useNavigate();
const { theme, toggleTheme } = useTheme();
useEffect(() => {
const token = localStorage.getItem('token');
if (!token) {
navigate('/login');
return;
}
axios
.get('http://localhost:3000/admin/pages', {
headers: { Authorization: `Bearer ${token}` },
})
.then(() => setIsAdmin(true))
.catch(() => {
localStorage.removeItem('token');
navigate('/login');
});
}, [navigate]);
const toggleAccordion = (section) => {
setOpenAccordions((prev) => ({ ...prev, [section]: !prev[section] }));
};
if (!isAdmin) return <div className="flex items-center justify-center min-h-screen text-gray-600 dark:text-gray-300">Loading...</div>;
return (
<div className="flex min-h-screen bg-gray-100 dark:bg-gray-900">
<aside className="fixed top-0 left-0 w-64 h-full bg-gray-800 text-white shadow-lg">
<div className="p-4">
<h1 className="text-2xl font-bold">DeComPress Admin</h1>
</div>
<nav className="mt-4">
<ul>
<li>
<button
onClick={() => toggleAccordion('pages')}
className="w-full text-left px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200 flex justify-between items-center"
>
Pages
<span>{openAccordions.pages ? '▲' : '▼'}</span>
</button>
{openAccordions.pages && (
<ul className="pl-4">
<li>
<Link
to="/admin/pages"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
All Pages
</Link>
</li>
<li>
<Link
to="/admin/create-page"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Create Page
</Link>
</li>
</ul>
)}
</li>
<li>
<button
onClick={() => toggleAccordion('posts')}
className="w-full text-left px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200 flex justify-between items-center"
>
Posts
<span>{openAccordions.posts ? '▲' : '▼'}</span>
</button>
{openAccordions.posts && (
<ul className="pl-4">
<li>
<Link
to="/admin/posts"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
All Posts
</Link>
</li>
<li>
<Link
to="/admin/create-post"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Create Post
</Link>
</li>
</ul>
)}
</li>
<li>
<button
onClick={() => toggleAccordion('templates')}
className="w-full text-left px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200 flex justify-between items-center"
>
Templates
<span>{openAccordions.templates ? '▲' : '▼'}</span>
</button>
{openAccordions.templates && (
<ul className="pl-4">
<li>
<Link
to="/admin/templates"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
All Templates
</Link>
</li>
<li>
<Link
to="/admin/create-template"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Create Template
</Link>
</li>
</ul>
)}
</li>
<li>
<button
onClick={() => toggleAccordion('forms')}
className="w-full text-left px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200 flex justify-between items-center"
>
Forms
<span>{openAccordions.forms ? '▲' : '▼'}</span>
</button>
{openAccordions.forms && (
<ul className="pl-4">
<li>
<Link
to="/admin/forms"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
All Forms
</Link>
</li>
<li>
<Link
to="/admin/create-form"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Create Form
</Link>
</li>
</ul>
)}
</li>
<li>
<button
onClick={() => toggleAccordion('ecommerce')}
className="w-full text-left px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200 flex justify-between items-center"
>
E-commerce
<span>{openAccordions.ecommerce ? '▲' : '▼'}</span>
</button>
{openAccordions.ecommerce && (
<ul className="pl-4">
<li>
<Link
to="/admin/orders"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Orders
</Link>
</li>
<li>
<Link
to="/admin/fulfillments"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Fulfillments
</Link>
</li>
<li>
<Link
to="/admin/shipping"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Shipping
</Link>
</li>
<li>
<Link
to="/admin/products"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Products
</Link>
</li>
</ul>
)}
</li>
<li>
<Link
to="/admin/media"
className="block px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Media
</Link>
</li>
<li>
<button
onClick={() => {
localStorage.removeItem('token');
navigate('/login');
}}
className="w-full text-left px-4 py-2 hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Logout
</button>
</li>
</ul>
</nav>
</aside>
<div className="flex-1 ml-64">
<header className="bg-white dark:bg-gray-800 shadow p-4 flex justify-between items-center">
<h2 className="text-xl font-semibold text-gray-800 dark:text-gray-100">Admin Dashboard</h2>
<div className="flex items-center space-x-4">
<button
onClick={() => {
console.log('Toggling theme from AdminDashboard');
toggleTheme();
}}
className="bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-100 px-3 py-1 rounded-md hover:bg-gray-300 dark:hover:bg-gray-600 transition duration-200"
>
{theme === 'light' ? 'Dark Mode' : 'Light Mode'}
</button>
<button
onClick={() => {
localStorage.removeItem('token');
navigate('/login');
}}
className="bg-red-600 text-white px-4 py-2 rounded-md hover:bg-red-700 dark:hover:bg-red-600 transition duration-200"
>
Logout
</button>
</div>
</header>
<main className="p-6 bg-gray-100 dark:bg-gray-900">
<Outlet />
</main>
</div>
</div>
);
}
export default AdminDashboard;

90
src/pages/AdminForms.jsx Normal file
View File

@ -0,0 +1,90 @@
import { useEffect, useState } from 'react';
import axios from 'axios';
import { Link } from 'react-router-dom';
import { toast } from 'react-toastify';
function AdminForms() {
const [forms, setForms] = useState([]);
useEffect(() => {
const token = localStorage.getItem('token');
axios
.get('http://localhost:3000/admin/forms', {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => setForms(response.data))
.catch(() => toast.error('Failed to load forms'));
}, []);
const handleDeleteForm = async (id) => {
if (!window.confirm('Are you sure you want to delete this form?')) return;
const token = localStorage.getItem('token');
try {
await axios.delete(`http://localhost:3000/admin/forms/${id}`, {
headers: { Authorization: `Bearer ${token}` },
});
setForms(forms.filter((f) => f.id !== id));
toast.success('Form deleted successfully');
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to delete form');
}
};
return (
<div className="bg-white p-8 rounded-xl shadow-lg border border-gray-200">
<div className="flex justify-between items-center mb-8">
<h2 className="text-3xl font-bold text-gray-800">Manage Forms</h2>
<Link
to="/admin/create-form"
className="bg-blue-600 text-white px-6 py-3 rounded-lg hover:bg-blue-700 transition duration-300 shadow-md"
>
Create New Form
</Link>
</div>
<div className="overflow-x-auto">
<table className="min-w-full bg-white border border-gray-200 rounded-lg">
<thead>
<tr className="bg-gray-50">
<th className="px-6 py-4 text-left text-sm font-semibold text-gray-700">Name</th>
<th className="px-6 py-4 text-left text-sm font-semibold text-gray-700">Fields</th>
<th className="px-6 py-4 text-left text-sm font-semibold text-gray-700">Actions</th>
</tr>
</thead>
<tbody>
{forms.map((form) => (
<tr key={form.id} className="border-t border-gray-200 hover:bg-gray-100 transition duration-200">
<td className="px-6 py-4 text-gray-800 font-medium">{form.name}</td>
<td className="px-6 py-4 text-gray-600">
{Object.entries(form.fields).map(([field, config]) => (
<div key={field} className="text-sm">
<span className="font-semibold capitalize">{field}</span>: {config.type}
{config.inputType && ` (${config.inputType})`}
{config.options && ` (Options: ${config.options.join(', ')})`}
{config.values && ` (Values: ${config.values.join(', ')})`}
</div>
))}
</td>
<td className="px-6 py-4">
<Link
to={`/admin/create-form/${form.id}`}
className="text-blue-600 hover:text-blue-800 font-medium mr-4 transition duration-200"
>
Edit
</Link>
<button
onClick={() => handleDeleteForm(form.id)}
className="text-red-600 hover:text-red-800 font-medium transition duration-200"
>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
}
export default AdminForms;

View File

@ -0,0 +1,49 @@
import { useEffect, useState } from 'react';
import axios from 'axios';
function AdminFulfillments() {
const [fulfillments, setFulfillments] = useState([]);
useEffect(() => {
const token = localStorage.getItem('token');
axios
.get('http://localhost:3000/admin/fulfillments', {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => setFulfillments(response.data))
.catch((err) => console.error(err));
}, []);
return (
<div className="bg-white p-6 rounded-lg shadow-lg">
<h2 className="text-2xl font-bold text-gray-800 mb-6">Manage Fulfillments</h2>
<div className="overflow-x-auto">
<table className="min-w-full bg-white border border-gray-200">
<thead>
<tr className="bg-gray-50">
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Fulfillment ID</th>
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Order ID</th>
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Status</th>
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Actions</th>
</tr>
</thead>
<tbody>
{fulfillments.map((fulfillment) => (
<tr key={fulfillment.id} className="border-t border-gray-200 hover:bg-gray-50">
<td className="px-6 py-4 text-gray-800">#{fulfillment.id}</td>
<td className="px-6 py-4 text-gray-600">#{fulfillment.order_id}</td>
<td className="px-6 py-4 text-gray-600">{fulfillment.status}</td>
<td className="px-6 py-4">
<button className="text-blue-600 hover:underline mr-4">Edit</button>
<button className="text-red-600 hover:underline">Delete</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
}
export default AdminFulfillments;

84
src/pages/AdminMedia.jsx Normal file
View File

@ -0,0 +1,84 @@
import { useState, useEffect } from 'react';
import axios from 'axios';
import { toast } from 'react-toastify';
function AdminMedia() {
const [media, setMedia] = useState([]);
useEffect(() => {
const token = localStorage.getItem('token');
axios
.get('http://localhost:3000/admin/media', {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => setMedia(response.data))
.catch(() => toast.error('Failed to load media'));
}, []);
const handleUpload = async (e) => {
const file = e.target.files[0];
if (!file) return;
const formData = new FormData();
formData.append('image', file);
try {
const token = localStorage.getItem('token');
const response = await axios.post('http://localhost:3000/admin/media', formData, {
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'multipart/form-data' },
});
setMedia([...media, response.data]);
toast.success('Image uploaded successfully');
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to upload image');
}
};
const handleDelete = async (id) => {
if (!window.confirm('Are you sure you want to delete this image?')) return;
try {
const token = localStorage.getItem('token');
await axios.delete(`http://localhost:3000/admin/media/${id}`, {
headers: { Authorization: `Bearer ${token}` },
});
setMedia(media.filter((item) => item.id !== id));
toast.success('Image deleted successfully');
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to delete image');
}
};
return (
<div className="bg-white p-6 rounded-lg shadow-lg">
<h2 className="text-2xl font-bold text-gray-800 mb-6">Media Library</h2>
<div className="mb-4">
<input
type="file"
accept="image/*"
onChange={handleUpload}
className="w-full p-3 border border-gray-300 rounded-md"
/>
</div>
<div className="grid grid-cols-4 gap-4">
{media.map((item) => (
<div key={item.id} className="relative">
<img
src={`http://localhost:3000${item.path}`}
alt={item.filename}
className="w-full h-32 object-cover rounded"
/>
<p className="text-xs text-gray-600 truncate">{item.filename}</p>
<p className="text-xs text-gray-500">By: {item.uploaded_by_username || 'Unknown'}</p>
<p className="text-xs text-gray-500">Uploaded: {new Date(item.uploaded_at).toLocaleString()}</p>
<button
onClick={() => handleDelete(item.id)}
className="absolute top-2 right-2 bg-red-600 text-white p-1 rounded-full hover:bg-red-700"
>
X
</button>
</div>
))}
</div>
</div>
);
}
export default AdminMedia;

49
src/pages/AdminOrders.jsx Normal file
View File

@ -0,0 +1,49 @@
import { useEffect, useState } from 'react';
import axios from 'axios';
function AdminOrders() {
const [orders, setOrders] = useState([]);
useEffect(() => {
const token = localStorage.getItem('token');
axios
.get('http://localhost:3000/admin/orders', {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => setOrders(response.data))
.catch((err) => console.error(err));
}, []);
return (
<div className="bg-white p-6 rounded-lg shadow-lg">
<h2 className="text-2xl font-bold text-gray-800 mb-6">Manage Orders</h2>
<div className="overflow-x-auto">
<table className="min-w-full bg-white border border-gray-200">
<thead>
<tr className="bg-gray-50">
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Order ID</th>
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Total</th>
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Status</th>
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Actions</th>
</tr>
</thead>
<tbody>
{orders.map((order) => (
<tr key={order.id} className="border-t border-gray-200 hover:bg-gray-50">
<td className="px-6 py-4 text-gray-800">#{order.id}</td>
<td className="px-6 py-4 text-gray-600">${order.total}</td>
<td className="px-6 py-4 text-gray-600">{order.status}</td>
<td className="px-6 py-4">
<button className="text-blue-600 hover:underline mr-4">Edit</button>
<button className="text-red-600 hover:underline">Delete</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
}
export default AdminOrders;

96
src/pages/AdminPages.jsx Normal file
View File

@ -0,0 +1,96 @@
import { useState, useEffect } from 'react';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
import { toast } from 'react-toastify';
function AdminPages() {
const [pages, setPages] = useState([]);
const navigate = useNavigate();
useEffect(() => {
const token = localStorage.getItem('token');
if (!token) {
navigate('/login');
return;
}
axios
.get('http://localhost:3000/admin/pages', {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => setPages(response.data))
.catch((err) => {
toast.error('Failed to load pages: ' + (err.response?.data?.error || 'Unauthorized'));
if (err.response?.status === 403) {
navigate('/login');
}
});
}, [navigate]);
const handleDelete = async (id) => {
if (!window.confirm('Are you sure you want to delete this page?')) return;
const token = localStorage.getItem('token');
try {
await axios.delete(`http://localhost:3000/admin/pages/${id}`, {
headers: { Authorization: `Bearer ${token}` },
});
toast.success('Page deleted successfully');
setPages(pages.filter((page) => page.id !== id));
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to delete page');
}
};
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white dark:bg-gray-800 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-700">
<div className="flex justify-between items-center mb-6">
<h1 className="text-3xl font-bold text-gray-800 dark:text-gray-100">Pages</h1>
<button
onClick={() => navigate('/admin/create-page')}
className="bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition duration-300"
>
Create New Page
</button>
</div>
<div className="overflow-x-auto">
<table className="min-w-full bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700">
<thead>
<tr>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Title</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Slug</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Created By</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Actions</th>
</tr>
</thead>
<tbody>
{pages.map((page) => (
<tr key={page.id} className="hover:bg-gray-50 dark:hover:bg-gray-700">
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">{page.title}</td>
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">{page.slug}</td>
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">{page.created_by_username || 'Unknown'}</td>
<td className="py-3 px-4 border-b">
<button
onClick={() => navigate(`/admin/edit-page/${page.id}`)}
className="bg-yellow-600 text-white px-3 py-1 rounded-lg hover:bg-yellow-700 mr-2"
>
Edit
</button>
<button
onClick={() => handleDelete(page.id)}
className="bg-red-600 text-white px-3 py-1 rounded-lg hover:bg-red-700"
>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
}
export default AdminPages;

96
src/pages/AdminPosts.jsx Normal file
View File

@ -0,0 +1,96 @@
import { useState, useEffect } from 'react';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
import { toast } from 'react-toastify';
function AdminPosts() {
const [posts, setPosts] = useState([]);
const navigate = useNavigate();
useEffect(() => {
const token = localStorage.getItem('token');
if (!token) {
navigate('/login');
return;
}
axios
.get('http://localhost:3000/admin/posts', {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => setPosts(response.data))
.catch((err) => {
toast.error('Failed to load posts: ' + (err.response?.data?.error || 'Unauthorized'));
if (err.response?.status === 403) {
navigate('/login');
}
});
}, [navigate]);
const handleDelete = async (id) => {
if (!window.confirm('Are you sure you want to delete this post?')) return;
const token = localStorage.getItem('token');
try {
await axios.delete(`http://localhost:3000/admin/posts/${id}`, {
headers: { Authorization: `Bearer ${token}` },
});
toast.success('Post deleted successfully');
setPosts(posts.filter((post) => post.id !== id));
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to delete post');
}
};
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white dark:bg-gray-800 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-700">
<div className="flex justify-between items-center mb-6">
<h1 className="text-3xl font-bold text-gray-800 dark:text-gray-100">Posts</h1>
<button
onClick={() => navigate('/admin/create-post')}
className="bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 dark:hover:bg-blue-600 transition duration-300"
>
Create New Post
</button>
</div>
<div className="overflow-x-auto">
<table className="min-w-full bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700">
<thead>
<tr>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Title</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Created By</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Created At</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Actions</th>
</tr>
</thead>
<tbody>
{posts.map((post) => (
<tr key={post.id} className="hover:bg-gray-50 dark:hover:bg-gray-700">
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">{post.title}</td>
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">{post.created_by_username || 'Unknown'}</td>
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">{new Date(post.created_at).toLocaleString()}</td>
<td className="py-3 px-4 border-b">
<button
onClick={() => navigate(`/admin/edit-post/${post.id}`)} // Assuming edit route exists
className="bg-yellow-600 text-white px-3 py-1 rounded-lg hover:bg-yellow-700 dark:hover:bg-yellow-600 mr-2"
>
Edit
</button>
<button
onClick={() => handleDelete(post.id)}
className="bg-red-600 text-white px-3 py-1 rounded-lg hover:bg-red-700 dark:hover:bg-red-600"
>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
}
export default AdminPosts;

View File

@ -0,0 +1,51 @@
import { useEffect, useState } from 'react';
import axios from 'axios';
function AdminShipping() {
const [shipping, setShipping] = useState([]);
useEffect(() => {
const token = localStorage.getItem('token');
axios
.get('http://localhost:3000/admin/shipping', {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => setShipping(response.data))
.catch((err) => console.error(err));
}, []);
return (
<div className="bg-white p-6 rounded-lg shadow-lg">
<h2 className="text-2xl font-bold text-gray-800 mb-6">Manage Shipping</h2>
<div className="overflow-x-auto">
<table className="min-w-full bg-white border border-gray-200">
<thead>
<tr className="bg-gray-50">
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Shipping ID</th>
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Order ID</th>
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Address</th>
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Status</th>
<th className="px-6 py-3 text-left text-sm font-semibold text-gray-600">Actions</th>
</tr>
</thead>
<tbody>
{shipping.map((ship) => (
<tr key={ship.id} className="border-t border-gray-200 hover:bg-gray-50">
<td className="px-6 py-4 text-gray-800">#{ship.id}</td>
<td className="px-6 py-4 text-gray-600">#{ship.order_id}</td>
<td className="px-6 py-4 text-gray-600">{ship.address}</td>
<td className="px-6 py-4 text-gray-600">{ship.status}</td>
<td className="px-6 py-4">
<button className="text-blue-600 hover:underline mr-4">Edit</button>
<button className="text-red-600 hover:underline">Delete</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
}
export default AdminShipping;

View File

@ -0,0 +1,94 @@
import { useState, useEffect } from 'react';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
import { toast } from 'react-toastify';
function AdminTemplates() {
const [templates, setTemplates] = useState([]);
const navigate = useNavigate();
useEffect(() => {
const token = localStorage.getItem('token');
if (!token) {
navigate('/login');
return;
}
axios
.get('http://localhost:3000/admin/templates', {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => setTemplates(response.data))
.catch((err) => {
toast.error('Failed to load templates: ' + (err.response?.data?.error || 'Unauthorized'));
if (err.response?.status === 403) {
navigate('/login');
}
});
}, [navigate]);
const handleDelete = async (id) => {
if (!window.confirm('Are you sure you want to delete this template?')) return;
const token = localStorage.getItem('token');
try {
await axios.delete(`http://localhost:3000/admin/templates/${id}`, {
headers: { Authorization: `Bearer ${token}` },
});
toast.success('Template deleted successfully');
setTemplates(templates.filter((template) => template.id !== id));
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to delete template');
}
};
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white dark:bg-gray-800 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-700">
<div className="flex justify-between items-center mb-6">
<h1 className="text-3xl font-bold text-gray-800 dark:text-gray-100">Templates</h1>
<button
onClick={() => navigate('/admin/create-template')}
className="bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 dark:hover:bg-blue-600 transition duration-300"
>
Create New Template
</button>
</div>
<div className="overflow-x-auto">
<table className="min-w-full bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700">
<thead>
<tr>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Name</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Component</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Actions</th>
</tr>
</thead>
<tbody>
{templates.map((template) => (
<tr key={template.id} className="hover:bg-gray-50 dark:hover:bg-gray-700">
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">{template.name}</td>
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">{template.component}</td>
<td className="py-3 px-4 border-b">
<button
onClick={() => navigate(`/admin/create-template/${template.id}`)}
className="bg-yellow-600 text-white px-3 py-1 rounded-lg hover:bg-yellow-700 dark:hover:bg-yellow-600 mr-2"
>
Edit
</button>
<button
onClick={() => handleDelete(template.id)}
className="bg-red-600 text-white px-3 py-1 rounded-lg hover:bg-red-700 dark:hover:bg-red-600"
>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
}
export default AdminTemplates;

103
src/pages/Cart.jsx Normal file
View File

@ -0,0 +1,103 @@
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import axios from 'axios';
import { toast } from 'react-toastify';
import { useCart } from '../context/CartContext';
function Cart() {
const navigate = useNavigate();
const { cart, removeFromCart, updateQuantity, getTotal, clearCart } = useCart();
const [shippingAddress, setShippingAddress] = useState('');
const handleCheckout = async () => {
const token = localStorage.getItem('token');
if (!token) {
toast.error('Please log in to checkout');
navigate('/login');
return;
}
if (!shippingAddress.trim()) {
toast.error('Shipping address is required');
return;
}
try {
const response = await axios.post(
'http://localhost:3000/orders',
{ cart, shippingAddress },
{ headers: { Authorization: `Bearer ${token}` } }
);
toast.success(response.data.message);
clearCart();
navigate('/'); // Redirect to home or order confirmation page
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to create order');
}
};
if (Object.keys(cart).length === 0) {
return <div className="container mx-auto px-6 py-8 text-center text-gray-600">Your cart is empty.</div>;
}
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white p-8 rounded-xl shadow-lg border border-gray-200">
<h1 className="text-4xl font-bold text-gray-800 mb-6">Your Cart</h1>
<div className="space-y-4">
{Object.values(cart).map((item) => (
<div key={item.product.id} className="flex items-center justify-between border-b border-gray-200 pb-4">
<div className="flex items-center">
{item.product.image && (
<img
src={`http://localhost:3000${item.product.image}`}
alt={item.product.name}
className="w-16 h-16 object-cover rounded-md mr-4"
/>
)}
<div>
<h3 className="text-lg font-semibold text-gray-800">{item.product.name}</h3>
<p className="text-gray-600">${item.product.price.toFixed(2)} x {item.quantity}</p>
</div>
</div>
<div className="flex items-center">
<input
type="number"
value={item.quantity}
onChange={(e) => updateQuantity(item.product.id, parseInt(e.target.value))}
min="1"
className="w-16 p-2 border border-gray-300 rounded-lg text-center mr-2"
/>
<button
onClick={() => removeFromCart(item.product.id)}
className="text-red-600 hover:text-red-800"
>
Remove
</button>
</div>
</div>
))}
</div>
<div className="mt-6">
<label className="block text-gray-700 text-sm font-semibold mb-2">Shipping Address</label>
<textarea
value={shippingAddress}
onChange={(e) => setShippingAddress(e.target.value)}
placeholder="Enter your shipping address"
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 h-24 transition duration-200"
required
/>
</div>
<div className="mt-6 text-right">
<p className="text-xl font-bold text-gray-800">Total: ${getTotal()}</p>
<button
onClick={handleCheckout}
className="bg-blue-600 text-white px-6 py-3 rounded-lg hover:bg-blue-700 transition duration-300 mt-4"
>
Checkout
</button>
</div>
</div>
</div>
);
}
export default Cart;

196
src/pages/ContactUs.jsx Normal file
View File

@ -0,0 +1,196 @@
import { useState, useEffect } from 'react';
import FormField from '../components/FormField';
import { toTitleCase } from '../utils/caseUtils';
function ContactUs({ page }) {
const [formData, setFormData] = useState({});
const [errors, setErrors] = useState({});
useEffect(() => {
console.log('ContactUs page data:', page);
console.log('Form fields:', page.form_fields);
if (!page.form_fields) {
console.warn('No form fields available. Check template form_id and forms table.');
}
}, [page]);
const handleFormChange = (field, value) => {
setFormData((prev) => ({ ...prev, [field]: value }));
validateField(field, value);
};
const validateField = (field, value) => {
const config = page.form_fields?.[field];
if (!config) return;
let err = '';
if (config.required && !value) {
err = 'This field is required';
} else if (config.minlength && value.length < config.minlength) {
err = `Minimum length is ${config.minlength} characters`;
} else if (config.maxlength && value.length > config.maxlength) {
err = `Maximum length is ${config.maxlength} characters`;
} else if (config.inputType === 'email' && value && !/^[^@]+@[^@]+\.[^@]+$/.test(value)) {
err = 'Invalid email format';
}
setErrors((prev) => ({ ...prev, [field]: err }));
return err;
};
const validateForm = () => {
let isValid = true;
const newErrors = {};
for (const field in page.form_fields) {
const error = validateField(field, formData[field] || '');
if (error) {
newErrors[field] = error;
isValid = false;
}
}
setErrors(newErrors);
return isValid;
};
const handleFormSubmit = (e) => {
e.preventDefault();
if (validateForm()) {
console.log('Form submitted:', formData);
// Future: Send to backend endpoint
} else {
console.log('Form validation failed:', errors);
}
};
const renderField = (field, value, config) => {
const styleClass = page.styles?.[field]?.class || 'text-gray-600';
switch (config.type) {
case 'text':
case 'number':
case 'date':
case 'select':
case 'enum':
return <p className={styleClass}>{value}</p>;
case 'textarea':
return <div className={`whitespace-pre-wrap ${styleClass}`}>{value}</div>;
case 'image':
return <img src={`http://localhost:3000${value}`} alt={field} className={`w-full rounded-md ${styleClass}`} />;
case 'richtext':
return <div className={styleClass} dangerouslySetInnerHTML={{ __html: value }} />;
case 'repeater':
return (
<div className="space-y-4">
{value?.map((item, idx) => (
<div key={idx} className="border border-gray-300 rounded-lg p-4">
{Object.entries(config.subTemplate).map(([subField, subConfig]) => (
<div key={subField} className="mb-2">
<h3 className="text-sm font-semibold text-gray-700">{toTitleCase(subField)}</h3>
{renderField(subField, item[subField], subConfig)}
</div>
))}
</div>
))}
</div>
);
case 'grid':
return (
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{config.columns.map((column, colIndex) => (
<div key={colIndex}>
<h3 className="text-sm font-semibold text-gray-700">{toTitleCase(column)}</h3>
<p className={styleClass}>{value?.[column]}</p>
</div>
))}
</div>
);
default:
console.warn(`Unsupported field type: ${config.type}`);
return null;
}
};
const renderEnumField = (field, config) => {
const value = formData[field] || '';
const error = errors[field] || (config.required && !value ? 'This field is required' : '');
return (
<div className="mb-4">
<label className="block text-gray-700 text-sm font-semibold mb-2">{toTitleCase(field)}</label>
<div className="flex flex-wrap gap-2">
{(config.values || []).map((val, idx) => (
<label key={idx} className="flex items-center">
<input
type="radio"
name={field}
value={val}
checked={value === val}
onChange={(e) => handleFormChange(field, e.target.value)}
required={config.required}
className="mr-2"
/>
{val}
</label>
))}
</div>
{error && <p className="text-red-500 text-sm mt-1">{error}</p>}
</div>
);
};
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white p-8 rounded-xl shadow-lg border border-gray-200">
<h1 className="text-4xl font-bold text-gray-800 mb-6 text-center">{page.title}</h1>
<div className="grid grid-cols-1 md:grid-cols-2 gap-8">
<div>
{page.structure && Object.entries(page.structure).map(([field, config]) => (
<div key={field} className="mb-6">
<h2 className="text-xl font-semibold text-gray-700 mb-2">{toTitleCase(field)}</h2>
{renderField(field, page.data[field], config)}
</div>
))}
</div>
<div>
<h2 className="text-2xl font-semibold text-gray-700 mb-4">Contact Information</h2>
<p className="text-gray-600 mb-2">Email: contact@decompress.com</p>
<p className="text-gray-600 mb-2">Phone: (123) 456-7890</p>
<p className="text-gray-600">Address: 123 DeComPress St, Web City, WC 12345</p>
{page.form_fields && Object.keys(page.form_fields).length > 0 ? (
<form onSubmit={handleFormSubmit} className="mt-6">
{Object.entries(page.form_fields).map(([field, config]) => (
config.type === 'enum' ? (
renderEnumField(field, config)
) : (
<FormField
key={field}
field={field}
config={config}
value={formData[field] || ''}
onChange={handleFormChange}
errorMessage={errors[field]}
/>
)
))}
<button
type="submit"
className="bg-blue-600 text-white p-3 rounded-lg hover:bg-blue-700 transition duration-300 shadow-md w-full"
>
Submit
</button>
</form>
) : (
<p className="text-gray-600 mt-4">
No contact form available. Please ensure a form is linked to this pages template in the admin panel.
</p>
)}
</div>
</div>
<div className="mt-8 text-sm text-gray-500 border-t border-gray-200 pt-4 text-center">
<p>Created by: {page.created_by_username || 'Unknown'}</p>
<p>Created at: {new Date(page.created_at).toLocaleString()}</p>
<p>Modified at: {new Date(page.modified_at).toLocaleString()}</p>
</div>
</div>
</div>
);
}
export default ContactUs;

315
src/pages/CreateForm.jsx Normal file
View File

@ -0,0 +1,315 @@
import { useState, useEffect } from 'react';
import axios from 'axios';
import { useNavigate, useParams } from 'react-router-dom';
import { toast } from 'react-toastify';
function CreateForm() {
const { id } = useParams();
const navigate = useNavigate();
const [name, setName] = useState('');
const [fields, setFields] = useState([{
name: '',
type: 'input',
inputType: 'text',
required: false,
maxlength: '',
minlength: '',
options: [],
values: [],
}]);
const [isEdit, setIsEdit] = useState(!!id);
useEffect(() => {
if (id) {
const token = localStorage.getItem('token');
axios
.get(`http://localhost:3000/admin/forms`, {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => {
const form = response.data.find((f) => f.id === parseInt(id));
if (form) {
setName(form.name);
const formFields = Object.entries(form.fields).map(([fieldName, config]) => ({
name: fieldName,
type: config.type,
inputType: config.inputType || 'text',
required: config.required || false,
maxlength: config.maxlength || '',
minlength: config.minlength || '',
options: config.options || [],
values: config.values || [],
}));
setFields(formFields);
} else {
toast.error('Form not found');
navigate('/admin/forms');
}
})
.catch(() => toast.error('Failed to load form'));
}
}, [id, navigate]);
const addField = () => {
setFields([...fields, {
name: '',
type: 'input',
inputType: 'text',
required: false,
maxlength: '',
minlength: '',
options: [],
values: [],
}]);
};
const updateField = (index, key, value) => {
const newFields = [...fields];
newFields[index][key] = value;
setFields(newFields);
};
const updateOption = (fieldIndex, optionIndex, value) => {
const newFields = [...fields];
newFields[fieldIndex].options[optionIndex] = value;
setFields(newFields);
};
const addOption = (fieldIndex) => {
const newFields = [...fields];
newFields[fieldIndex].options.push('');
setFields(newFields);
};
const removeOption = (fieldIndex, optionIndex) => {
const newFields = [...fields];
newFields[fieldIndex].options.splice(optionIndex, 1);
setFields(newFields);
};
const updateValue = (fieldIndex, valueIndex, value) => {
const newFields = [...fields];
newFields[fieldIndex].values[valueIndex] = value;
setFields(newFields);
};
const addValue = (fieldIndex) => {
const newFields = [...fields];
newFields[fieldIndex].values.push('');
setFields(newFields);
};
const removeValue = (fieldIndex, valueIndex) => {
const newFields = [...fields];
newFields[fieldIndex].values.splice(valueIndex, 1);
setFields(newFields);
};
const removeField = (index) => {
setFields(fields.filter((_, i) => i !== index));
};
const handleSubmit = async (e) => {
e.preventDefault();
const token = localStorage.getItem('token');
const fieldsObj = fields.reduce((acc, field) => {
const fieldConfig = { type: field.type };
if (field.type === 'input') {
fieldConfig.inputType = field.inputType;
fieldConfig.required = field.required;
if (field.maxlength) fieldConfig.maxlength = parseInt(field.maxlength);
if (field.minlength) fieldConfig.minlength = parseInt(field.minlength);
}
if (field.type === 'textarea') {
fieldConfig.required = field.required;
if (field.maxlength) fieldConfig.maxlength = parseInt(field.maxlength);
}
if (field.type === 'select') {
fieldConfig.options = field.options;
fieldConfig.required = field.required;
}
if (field.type === 'enum') {
fieldConfig.values = field.values;
fieldConfig.required = field.required;
}
return { ...acc, [field.name]: fieldConfig };
}, {});
try {
if (isEdit) {
await axios.put(
`http://localhost:3000/admin/forms/${id}`,
{ name, fields: fieldsObj },
{ headers: { Authorization: `Bearer ${token}` } }
);
toast.success('Form updated successfully');
} else {
await axios.post(
'http://localhost:3000/admin/forms',
{ name, fields: fieldsObj },
{ headers: { Authorization: `Bearer ${token}` } }
);
toast.success('Form created successfully');
}
navigate('/admin/forms');
} catch (err) {
toast.error(err.response?.data?.error || `Failed to ${isEdit ? 'update' : 'create'} form`);
}
};
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white p-8 rounded-xl shadow-lg border border-gray-200 max-w-4xl mx-auto">
<h1 className="text-3xl font-bold text-gray-800 mb-8">{isEdit ? 'Edit Form' : 'Create a New Form'}</h1>
<form onSubmit={handleSubmit}>
<div className="mb-6">
<label className="block text-gray-700 text-sm font-semibold mb-2">Form Name</label>
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Form Name"
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
required
/>
</div>
<div className="mb-6">
<h2 className="text-lg font-semibold text-gray-700 mb-4">Fields</h2>
{fields.map((field, index) => (
<div key={index} className="flex flex-wrap items-center mb-4 p-4 bg-gray-50 rounded-lg border border-gray-200">
<div className="w-full md:w-1/6 pr-2 mb-2 md:mb-0">
<label className="block text-gray-700 text-sm font-semibold mb-1">Field Name</label>
<input
type="text"
value={field.name}
onChange={(e) => updateField(index, 'name', e.target.value)}
placeholder="Field Name"
className="w-full p-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
required
/>
</div>
<div className="w-full md:w-1/6 pr-2 mb-2 md:mb-0">
<label className="block text-gray-700 text-sm font-semibold mb-1">Field Type</label>
<select
value={field.type}
onChange={(e) => updateField(index, 'type', e.target.value)}
className="w-full p-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
>
<option value="input">Input</option>
<option value="textarea">Textarea</option>
<option value="select">Select</option>
<option value="enum">Enum</option>
</select>
</div>
{field.type === 'input' && (
<div className="w-full md:w-1/6 pr-2 mb-2 md:mb-0">
<label className="block text-gray-700 text-sm font-semibold mb-1">Input Type</label>
<select
value={field.inputType}
onChange={(e) => updateField(index, 'inputType', e.target.value)}
className="w-full p-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
>
<option value="text">Text</option>
<option value="password">Password</option>
<option value="email">Email</option>
</select>
</div>
)}
<div className="w-full md:w-1/6 pr-2 mb-2 md:mb-0">
<label className="block text-gray-700 text-sm font-semibold mb-1">Required</label>
<input
type="checkbox"
checked={field.required}
onChange={(e) => updateField(index, 'required', e.target.checked)}
className="w-4 h-4"
/>
</div>
{(field.type === 'input' || field.type === 'textarea') && (
<>
<div className="w-full md:w-1/6 pr-2 mb-2 md:mb-0">
<label className="block text-gray-700 text-sm font-semibold mb-1">Max Length</label>
<input
type="number"
value={field.maxlength}
onChange={(e) => updateField(index, 'maxlength', e.target.value)}
placeholder="Max Length"
className="w-full p-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
{field.type === 'input' && (
<div className="w-full md:w-1/6 pr-2 mb-2 md:mb-0">
<label className="block text-gray-700 text-sm font-semibold mb-1">Min Length</label>
<input
type="number"
value={field.minlength}
onChange={(e) => updateField(index, 'minlength', e.target.value)}
placeholder="Min Length"
className="w-full p-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
)}
</>
)}
<div className="w-full md:w-1/6 flex items-end">
<button
type="button"
onClick={() => removeField(index)}
className="bg-red-600 text-white p-2 rounded-lg hover:bg-red-700 transition duration-200 w-full"
>
Remove Field
</button>
</div>
{(field.type === 'select' || field.type === 'enum') && (
<div className="w-full mt-4">
<label className="block text-gray-700 text-sm font-semibold mb-2">{field.type === 'select' ? 'Options' : 'Values'}</label>
{(field.type === 'select' ? field.options : field.values).map((item, itemIndex) => (
<div key={itemIndex} className="flex mb-2">
<input
type="text"
value={item}
onChange={(e) => (field.type === 'select' ? updateOption : updateValue)(index, itemIndex, e.target.value)}
placeholder={field.type === 'select' ? 'Option' : 'Value'}
className="w-3/4 p-2 border border-gray-300 rounded-lg mr-2 focus:outline-none focus:ring-2 focus:ring-blue-500"
required
/>
<button
type="button"
onClick={() => (field.type === 'select' ? removeOption : removeValue)(index, itemIndex)}
className="bg-red-600 text-white p-2 rounded-lg hover:bg-red-700"
>
Remove
</button>
</div>
))}
<button
type="button"
onClick={() => (field.type === 'select' ? addOption : addValue)(index)}
className="bg-gray-600 text-white p-2 rounded-lg hover:bg-gray-700 transition duration-200"
>
Add {field.type === 'select' ? 'Option' : 'Value'}
</button>
</div>
)}
</div>
))}
<button
type="button"
onClick={addField}
className="bg-gray-600 text-white p-3 rounded-lg hover:bg-gray-700 transition duration-200 mb-6"
>
Add Field
</button>
</div>
<button
type="submit"
className="bg-blue-600 text-white p-3 rounded-lg hover:bg-blue-700 transition duration-300 shadow-md w-full"
>
{isEdit ? 'Update Form' : 'Create Form'}
</button>
</form>
</div>
</div>
);
}
export default CreateForm;

332
src/pages/CreatePage.jsx Normal file
View File

@ -0,0 +1,332 @@
import { useState, useEffect } from 'react';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
import { toast } from 'react-toastify';
import slugify from 'slugify';
import CustomWYSIWYG from '../components/CustomWYSIWYG';
import MediaPickerModal from '../components/MediaPickerModal';
import { toTitleCase } from '../utils/caseUtils';
function CreatePage() {
const [title, setTitle] = useState('');
const [slug, setSlug] = useState('');
const [templateId, setTemplateId] = useState('');
const [data, setData] = useState({});
const [templates, setTemplates] = useState([]);
const [isMediaPickerOpen, setIsMediaPickerOpen] = useState(false);
const [currentField, setCurrentField] = useState(null);
const navigate = useNavigate();
useEffect(() => {
const token = localStorage.getItem('token');
if (!token) {
navigate('/login');
return;
}
axios
.get('http://localhost:3000/admin/templates', {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => setTemplates(response.data))
.catch((err) => {
toast.error('Failed to load templates: ' + (err.response?.data?.error || 'Unauthorized'));
if (err.response?.status === 403) {
navigate('/login');
}
});
}, [navigate]);
const handleTitleChange = (e) => {
const newTitle = e.target.value;
setTitle(newTitle);
setSlug(slugify(newTitle, { lower: true, strict: true }));
};
const handleDataChange = (field, value) => {
setData((prev) => ({ ...prev, [field]: value }));
};
const openMediaPicker = (field) => {
setCurrentField(field);
setIsMediaPickerOpen(true);
};
const addRepeaterItem = (field) => {
setData((prev) => ({
...prev,
[field]: [...(prev[field] || []), {}],
}));
};
const updateRepeaterItem = (field, itemIndex, subField, value) => {
setData((prev) => {
const newItems = [...(prev[field] || [])];
newItems[itemIndex] = { ...newItems[itemIndex], [subField]: value };
return { ...prev, [field]: newItems };
});
};
const removeRepeaterItem = (field, itemIndex) => {
setData((prev) => {
const newItems = [...(prev[field] || [])];
newItems.splice(itemIndex, 1);
return { ...prev, [field]: newItems };
});
};
const handleCreatePage = async (e) => {
e.preventDefault();
const token = localStorage.getItem('token');
if (!token) {
navigate('/login');
return;
}
if (!title.trim()) {
toast.error('Title is required');
return;
}
if (!slug.trim()) {
toast.error('Slug is required');
return;
}
if (!templateId) {
toast.error('Please select a template');
return;
}
if (Object.keys(data).length === 0) {
toast.error('At least one data field is required');
return;
}
try {
const response = await axios.post(
'http://localhost:3000/pages',
{ title, slug, data, template_id: parseInt(templateId) },
{ headers: { Authorization: `Bearer ${token}` } }
);
toast.success(response.data.message);
navigate(`/page/${slug}`);
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to create page');
}
};
const selectedTemplate = templates.find((t) => t.id === parseInt(templateId));
const renderFieldInput = (field, config, itemIndex = null, parentField = null) => {
const fieldPath = itemIndex !== null ? `${parentField}[${itemIndex}].${field}` : field;
const value = itemIndex !== null ? data[parentField]?.[itemIndex]?.[field] : data[field];
switch (config.type) {
case 'text':
return (
<input
type="text"
value={value || ''}
onChange={(e) => handleDataChange(fieldPath, e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
/>
);
case 'textarea':
return (
<textarea
value={value || ''}
onChange={(e) => handleDataChange(fieldPath, e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 h-24 transition duration-200"
/>
);
case 'select':
return (
<select
value={value || ''}
onChange={(e) => handleDataChange(fieldPath, e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
>
<option value="">Select an option</option>
{config.options.map((option, idx) => (
<option key={idx} value={option}>{option}</option>
))}
</select>
);
case 'enum':
return (
<div className="flex flex-wrap gap-2">
{config.values.map((val, idx) => (
<label key={idx} className="flex items-center">
<input
type="radio"
name={fieldPath}
value={val}
checked={value === val}
onChange={(e) => handleDataChange(fieldPath, e.target.value)}
className="mr-2"
/>
{val}
</label>
))}
</div>
);
case 'image':
return (
<div>
<button
type="button"
onClick={() => openMediaPicker(fieldPath)}
className="bg-blue-600 text-white p-3 rounded-lg hover:bg-blue-700 transition duration-200"
>
Select Image
</button>
{value && (
<img src={`http://localhost:3000${value}`} alt="Preview" className="mt-2 max-w-xs rounded-md" />
)}
</div>
);
case 'richtext':
return (
<CustomWYSIWYG
value={value || ''}
onChange={(val) => handleDataChange(fieldPath, val)}
/>
);
case 'number':
return (
<input
type="number"
value={value || ''}
onChange={(e) => handleDataChange(fieldPath, e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
/>
);
case 'date':
return (
<input
type="date"
value={value || ''}
onChange={(e) => handleDataChange(fieldPath, e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
/>
);
default:
return null;
}
};
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white p-8 rounded-xl shadow-lg border border-gray-200 max-w-2xl mx-auto">
<h1 className="text-3xl font-bold text-gray-800 mb-8">Create a New Page</h1>
<form onSubmit={handleCreatePage}>
<div className="mb-6">
<label className="block text-gray-700 text-sm font-semibold mb-2">Page Title</label>
<input
type="text"
value={title}
onChange={handleTitleChange}
placeholder="Page Title"
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
required
/>
</div>
<div className="mb-6">
<label className="block text-gray-700 text-sm font-semibold mb-2">Slug</label>
<input
type="text"
value={slug}
onChange={(e) => setSlug(e.target.value)}
placeholder="page-slug"
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
required
/>
<p className="text-gray-500 text-sm mt-1">Use letters, numbers, and hyphens only (e.g., about-us)</p>
</div>
<div className="mb-6">
<label className="block text-gray-700 text-sm font-semibold mb-2">Template</label>
<select
value={templateId}
onChange={(e) => setTemplateId(e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
>
<option value="">Select a Template</option>
{templates.map((template) => (
<option key={template.id} value={template.id}>
{template.name} ({template.component})
</option>
))}
</select>
</div>
{selectedTemplate && (
<div className="mb-6">
{Object.entries(selectedTemplate.structure).map(([field, config]) => (
<div key={field} className="mb-4">
<label className="block text-gray-700 text-sm font-semibold mb-2">{toTitleCase(field)}</label>
{config.type === 'repeater' ? (
<div className="border border-gray-300 rounded-lg p-4">
{(data[field] || []).map((item, itemIndex) => (
<div key={itemIndex} className="mb-4 p-4 bg-gray-50 rounded-lg">
{Object.entries(config.subTemplate).map(([subField, subConfig]) => (
<div key={subField} className="mb-2">
<label className="block text-gray-600 text-sm mb-1">{toTitleCase(subField)}</label>
{renderFieldInput(subField, subConfig, itemIndex, field)}
</div>
))}
<button
type="button"
onClick={() => removeRepeaterItem(field, itemIndex)}
className="bg-red-600 text-white p-2 rounded-lg hover:bg-red-700"
>
Remove Item
</button>
</div>
))}
<button
type="button"
onClick={() => addRepeaterItem(field)}
className="bg-gray-600 text-white p-2 rounded-lg hover:bg-gray-700"
>
Add Item
</button>
</div>
) : config.type === 'grid' ? (
<div className="border border-gray-300 rounded-lg p-4">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{config.columns.map((column, colIndex) => (
<div key={colIndex}>
<label className="block text-gray-600 text-sm mb-1">{toTitleCase(column)}</label>
<input
type="text"
value={data[field]?.[column] || ''}
onChange={(e) => handleDataChange(`${field}.${column}`, e.target.value)}
className="w-full p-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
))}
</div>
</div>
) : (
renderFieldInput(field, config)
)}
</div>
))}
</div>
)}
<button
type="submit"
className="bg-blue-600 text-white p-3 rounded-lg hover:bg-blue-700 transition duration-300 shadow-md w-full"
>
Create Page
</button>
</form>
<MediaPickerModal
isOpen={isMediaPickerOpen}
onClose={() => setIsMediaPickerOpen(false)}
onSelect={(path) => handleDataChange(currentField, path)}
/>
</div>
</div>
);
}
export default CreatePage;

75
src/pages/CreatePost.jsx Normal file
View File

@ -0,0 +1,75 @@
import { useState } from 'react';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
import { toast } from 'react-toastify';
function CreatePost() {
const [title, setTitle] = useState('');
const [body, setBody] = useState('');
const navigate = useNavigate();
const handleCreatePost = async (e) => {
e.preventDefault();
const token = localStorage.getItem('token');
if (!token) {
navigate('/login');
return;
}
if (!title.trim()) {
toast.error('Title is required');
return;
}
if (!body.trim()) {
toast.error('Body is required');
return;
}
try {
const response = await axios.post(
'http://localhost:3000/posts',
{ title, body },
{ headers: { Authorization: `Bearer ${token}` } }
);
toast.success(response.data.message);
navigate('/admin/posts');
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to create post');
}
};
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white p-6 rounded-lg shadow-lg max-w-2xl mx-auto">
<h1 className="text-2xl font-bold text-gray-800 mb-6">Create a New Post</h1>
<form onSubmit={handleCreatePost}>
<div className="mb-4">
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Post Title"
className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div className="mb-4">
<textarea
value={body}
onChange={(e) => setBody(e.target.value)}
placeholder="Post Body"
className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 h-40"
></textarea>
</div>
<button
type="submit"
className="bg-blue-600 text-white p-3 rounded-md hover:bg-blue-700 transition duration-200"
>
Create Post
</button>
</form>
</div>
</div>
);
}
export default CreatePost;

View File

@ -0,0 +1,430 @@
import { useState, useEffect } from 'react';
import axios from 'axios';
import { useNavigate, useParams } from 'react-router-dom';
import { toast } from 'react-toastify';
import { toCamelCase, toTitleCase } from '../utils/caseUtils';
function CreateTemplate() {
const { id } = useParams();
const navigate = useNavigate();
const [name, setName] = useState('');
const [fields, setFields] = useState([{ displayName: '', name: '', type: 'text', options: [], values: [], subTemplate: {}, columns: [], styleClass: '' }]);
const [component, setComponent] = useState('PageViewer');
const [formId, setFormId] = useState('');
const [forms, setForms] = useState([]);
const [isEdit, setIsEdit] = useState(!!id);
useEffect(() => {
const token = localStorage.getItem('token');
axios
.get('http://localhost:3000/admin/forms', {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => {
setForms(response.data);
console.log('Fetched forms:', response.data);
})
.catch((err) => {
toast.error('Failed to load forms');
console.error('Form fetch error:', err);
});
if (id) {
axios
.get(`http://localhost:3000/admin/templates`, {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => {
const template = response.data.find((t) => t.id === parseInt(id));
if (template) {
setName(template.name);
setComponent(template.component || 'PageViewer');
setFormId(template.form_id ? String(template.form_id) : '');
const structureFields = Object.entries(template.structure).map(([fieldName, config]) => ({
displayName: toTitleCase(fieldName),
name: fieldName,
type: config.type,
options: config.options || [],
values: config.values || [],
subTemplate: config.subTemplate || {},
columns: config.columns || [],
styleClass: template.styles?.[fieldName]?.class || '',
}));
setFields(structureFields);
console.log('Loaded template:', template);
} else {
toast.error('Template not found');
navigate('/admin/templates');
}
})
.catch((err) => {
toast.error('Failed to load template');
console.error('Template fetch error:', err);
});
}
}, [id, navigate]);
const addField = () => {
setFields([...fields, { displayName: '', name: '', type: 'text', options: [], values: [], subTemplate: {}, columns: [], styleClass: '' }]);
};
const updateField = (index, key, value) => {
const newFields = [...fields];
newFields[index][key] = value;
if (key === 'displayName') {
newFields[index].name = toCamelCase(value);
}
setFields(newFields);
};
const updateOption = (fieldIndex, optionIndex, value) => {
const newFields = [...fields];
newFields[fieldIndex].options[optionIndex] = value;
setFields(newFields);
};
const addOption = (fieldIndex) => {
const newFields = [...fields];
newFields[fieldIndex].options.push('');
setFields(newFields);
};
const removeOption = (fieldIndex, optionIndex) => {
const newFields = [...fields];
newFields[fieldIndex].options.splice(optionIndex, 1);
setFields(newFields);
};
const updateValue = (fieldIndex, valueIndex, value) => {
const newFields = [...fields];
newFields[fieldIndex].values[valueIndex] = value;
setFields(newFields);
};
const addValue = (fieldIndex) => {
const newFields = [...fields];
newFields[fieldIndex].values.push('');
setFields(newFields);
};
const removeValue = (fieldIndex, valueIndex) => {
const newFields = [...fields];
newFields[fieldIndex].values.splice(valueIndex, 1);
setFields(newFields);
};
const updateSubField = (fieldIndex, subFieldName, key, value) => {
const newFields = [...fields];
newFields[fieldIndex].subTemplate[subFieldName] = { ...newFields[fieldIndex].subTemplate[subFieldName], [key]: value };
setFields(newFields);
};
const addSubField = (fieldIndex) => {
const newFields = [...fields];
const subFieldName = toCamelCase(`subField${Object.keys(newFields[fieldIndex].subTemplate).length + 1}`);
newFields[fieldIndex].subTemplate[subFieldName] = { type: 'text' };
setFields(newFields);
};
const removeSubField = (fieldIndex, subFieldName) => {
const newFields = [...fields];
delete newFields[fieldIndex].subTemplate[subFieldName];
setFields(newFields);
};
const updateColumn = (fieldIndex, columnIndex, value) => {
const newFields = [...fields];
newFields[fieldIndex].columns[columnIndex] = value;
setFields(newFields);
};
const addColumn = (fieldIndex) => {
const newFields = [...fields];
newFields[fieldIndex].columns.push('');
setFields(newFields);
};
const removeColumn = (fieldIndex, columnIndex) => {
const newFields = [...fields];
newFields[fieldIndex].columns.splice(columnIndex, 1);
setFields(newFields);
};
const removeField = (index) => {
setFields(fields.filter((_, i) => i !== index));
};
const handleSubmit = async (e) => {
e.preventDefault();
const token = localStorage.getItem('token');
const structure = fields.reduce((acc, field) => {
const fieldConfig = { type: field.type };
if (field.type === 'select') fieldConfig.options = field.options;
if (field.type === 'enum') fieldConfig.values = field.values;
if (field.type === 'repeater') fieldConfig.subTemplate = field.subTemplate;
if (field.type === 'grid') fieldConfig.columns = field.columns;
return { ...acc, [field.name]: fieldConfig };
}, {});
const styles = fields.reduce((acc, field) => ({
...acc,
[field.name]: { class: field.styleClass },
}), {});
const payload = {
name,
structure,
styles,
component,
form_id: formId ? parseInt(formId) : null,
};
console.log('Submitting template payload:', payload);
try {
if (isEdit) {
await axios.put(
`http://localhost:3000/admin/templates/${id}`,
payload,
{ headers: { Authorization: `Bearer ${token}` } }
);
toast.success('Template updated successfully');
} else {
await axios.post(
'http://localhost:3000/admin/templates',
payload,
{ headers: { Authorization: `Bearer ${token}` } }
);
toast.success('Template created successfully');
}
navigate('/admin/templates');
} catch (err) {
toast.error(err.response?.data?.error || `Failed to ${isEdit ? 'update' : 'create'} template`);
console.error('Template submission error:', err.response?.data || err);
}
};
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white dark:bg-gray-800 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-700 max-w-4xl mx-auto">
<h1 className="text-3xl font-bold text-gray-800 dark:text-gray-100 mb-8">{isEdit ? 'Edit Template' : 'Create a New Template'}</h1>
<form onSubmit={handleSubmit}>
<div className="mb-6">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-2">Template Name</label>
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Template Name"
className="w-full p-3 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:text-gray-200 transition duration-200"
required
/>
</div>
<div className="mb-6">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-2">Rendering Component</label>
<select
value={component}
onChange={(e) => setComponent(e.target.value)}
className="w-full p-3 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:text-gray-200 transition duration-200"
>
<option value="PageViewer">Default (Page Viewer)</option>
<option value="LandingPage">Landing Page</option>
<option value="ContactUs">Contact Us</option>
<option value="ProductsPage">Products Page</option>
</select>
</div>
<div className="mb-6">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-2">Form</label>
<select
value={formId}
onChange={(e) => {
setFormId(e.target.value);
console.log('Selected formId:', e.target.value);
}}
className="w-full p-3 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:text-gray-200 transition duration-200"
>
<option value="">No Form</option>
{forms.map((form) => (
<option key={form.id} value={form.id}>{form.name}</option>
))}
</select>
</div>
<div className="mb-6">
<h2 className="text-lg font-semibold text-gray-700 dark:text-gray-200 mb-4">Fields</h2>
{fields.map((field, index) => (
<div key={index} className="flex flex-wrap items-center mb-4 p-4 bg-gray-50 dark:bg-gray-700 rounded-lg border border-gray-200 dark:border-gray-600">
<div className="w-full md:w-1/5 pr-2 mb-2 md:mb-0">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-1">Field Name</label>
<input
type="text"
value={field.displayName}
onChange={(e) => updateField(index, 'displayName', e.target.value)}
placeholder="Field Name"
className="w-full p-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:text-gray-200"
required
/>
<p className="text-gray-500 dark:text-gray-400 text-sm mt-1">Key: {field.name || 'Enter name to generate'}</p>
</div>
<div className="w-full md:w-1/5 pr-2 mb-2 md:mb-0">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-1">Field Type</label>
<select
value={field.type}
onChange={(e) => updateField(index, 'type', e.target.value)}
className="w-full p-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:text-gray-200"
>
<option value="text">Text</option>
<option value="textarea">Textarea</option>
<option value="select">Select</option>
<option value="enum">Enum</option>
<option value="repeater">Repeater</option>
<option value="grid">Grid</option>
<option value="image">Image</option>
<option value="richtext">Rich Text</option>
<option value="number">Number</option>
<option value="date">Date</option>
</select>
</div>
<div className="w-full md:w-1/5 pr-2 mb-2 md:mb-0">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-1">Style Class</label>
<input
type="text"
value={field.styleClass}
onChange={(e) => updateField(index, 'styleClass', e.target.value)}
placeholder="Tailwind classes (e.g., text-lg text-blue-600)"
className="w-full p-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:text-gray-200"
/>
</div>
<div className="w-full md:w-1/5 flex items-end">
<button
type="button"
onClick={() => removeField(index)}
className="bg-red-600 text-white p-2 rounded-lg hover:bg-red-700 dark:hover:bg-red-600 transition duration-200 w-full"
>
Remove Field
</button>
</div>
{(field.type === 'select' || field.type === 'enum') && (
<div className="w-full mt-4">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-2">{field.type === 'select' ? 'Options' : 'Values'}</label>
{(field.type === 'select' ? field.options : field.values).map((item, itemIndex) => (
<div key={itemIndex} className="flex mb-2">
<input
type="text"
value={item}
onChange={(e) => (field.type === 'select' ? updateOption : updateValue)(index, itemIndex, e.target.value)}
placeholder={field.type === 'select' ? 'Option' : 'Value'}
className="w-3/4 p-2 border border-gray-300 dark:border-gray-600 rounded-lg mr-2 focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:text-gray-200"
required
/>
<button
type="button"
onClick={() => (field.type === 'select' ? removeOption : removeValue)(index, itemIndex)}
className="bg-red-600 text-white p-2 rounded-lg hover:bg-red-700 dark:hover:bg-red-600"
>
Remove
</button>
</div>
))}
<button
type="button"
onClick={() => (field.type === 'select' ? addOption : addValue)(index)}
className="bg-gray-600 text-white p-2 rounded-lg hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Add {field.type === 'select' ? 'Option' : 'Value'}
</button>
</div>
)}
{field.type === 'repeater' && (
<div className="w-full mt-4">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-2">Sub-Template Fields</label>
{Object.entries(field.subTemplate).map(([subFieldName, subConfig]) => (
<div key={subFieldName} className="flex mb-2">
<input
type="text"
value={toTitleCase(subFieldName)}
disabled
className="w-1/3 p-2 border border-gray-300 dark:border-gray-600 rounded-lg mr-2 dark:bg-gray-800 dark:text-gray-200"
/>
<select
value={subConfig.type}
onChange={(e) => updateSubField(index, subFieldName, 'type', e.target.value)}
className="w-1/3 p-2 border border-gray-300 dark:border-gray-600 rounded-lg mr-2 dark:bg-gray-800 dark:text-gray-200"
>
<option value="text">Text</option>
<option value="textarea">Textarea</option>
<option value="select">Select</option>
<option value="enum">Enum</option>
<option value="image">Image</option>
<option value="richtext">Rich Text</option>
<option value="number">Number</option>
<option value="date">Date</option>
</select>
<button
type="button"
onClick={() => removeSubField(index, subFieldName)}
className="bg-red-600 text-white p-2 rounded-lg hover:bg-red-700 dark:hover:bg-red-600"
>
Remove
</button>
</div>
))}
<button
type="button"
onClick={() => addSubField(index)}
className="bg-gray-600 text-white p-2 rounded-lg hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Add Sub-Field
</button>
</div>
)}
{field.type === 'grid' && (
<div className="w-full mt-4">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-2">Grid Columns</label>
{field.columns.map((column, colIndex) => (
<div key={colIndex} className="flex mb-2">
<input
type="text"
value={column}
onChange={(e) => updateColumn(index, colIndex, e.target.value)}
placeholder="Column Name"
className="w-3/4 p-2 border border-gray-300 dark:border-gray-600 rounded-lg mr-2 dark:bg-gray-800 dark:text-gray-200"
required
/>
<button
type="button"
onClick={() => removeColumn(index, colIndex)}
className="bg-red-600 text-white p-2 rounded-lg hover:bg-red-700 dark:hover:bg-red-600"
>
Remove
</button>
</div>
))}
<button
type="button"
onClick={() => addColumn(index)}
className="bg-gray-600 text-white p-2 rounded-lg hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200"
>
Add Column
</button>
</div>
)}
</div>
))}
<button
type="button"
onClick={addField}
className="bg-gray-600 text-white p-3 rounded-lg hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-200 mb-6"
>
Add Field
</button>
</div>
<button
type="submit"
className="bg-blue-600 text-white p-3 rounded-lg hover:bg-blue-700 dark:hover:bg-blue-600 transition duration-300 shadow-md w-full"
>
{isEdit ? 'Update Template' : 'Create Template'}
</button>
</form>
</div>
</div>
);
}
export default CreateTemplate;

363
src/pages/EditPage.jsx Normal file
View File

@ -0,0 +1,363 @@
import { useState, useEffect } from 'react';
import axios from 'axios';
import { useNavigate, useParams } from 'react-router-dom';
import { toast } from 'react-toastify';
import slugify from 'slugify';
import CustomWYSIWYG from '../components/CustomWYSIWYG';
import MediaPickerModal from '../components/MediaPickerModal';
import { toTitleCase } from '../utils/caseUtils';
function EditPage() {
const { id } = useParams();
const navigate = useNavigate();
const [title, setTitle] = useState('');
const [slug, setSlug] = useState('');
const [templateId, setTemplateId] = useState('');
const [data, setData] = useState({});
const [templates, setTemplates] = useState([]);
const [isMediaPickerOpen, setIsMediaPickerOpen] = useState(false);
const [currentField, setCurrentField] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
const token = localStorage.getItem('token');
if (!token) {
navigate('/login');
return;
}
// Fetch templates
axios
.get('http://localhost:3000/admin/templates', {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => setTemplates(response.data))
.catch((err) => {
toast.error('Failed to load templates: ' + (err.response?.data?.error || 'Unauthorized'));
if (err.response?.status === 403) {
navigate('/login');
}
});
// Fetch page data
axios
.get(`http://localhost:3000/admin/pages`, {
headers: { Authorization: `Bearer ${token}` },
})
.then((response) => {
const page = response.data.find((p) => p.id === parseInt(id));
if (page) {
setTitle(page.title);
setSlug(page.slug || '');
setTemplateId(page.template_id ? String(page.template_id) : '');
setData(page.data);
setLoading(false);
} else {
toast.error('Page not found');
navigate('/admin/pages');
}
})
.catch((err) => {
toast.error('Failed to load page: ' + (err.response?.data?.error || 'Unauthorized'));
if (err.response?.status === 403) {
navigate('/login');
}
});
}, [id, navigate]);
const handleTitleChange = (e) => {
const newTitle = e.target.value;
setTitle(newTitle);
if (!slug || slug === slugify(title, { lower: true, strict: true })) {
setSlug(slugify(newTitle, { lower: true, strict: true }));
}
};
const handleDataChange = (field, value) => {
setData((prev) => ({ ...prev, [field]: value }));
};
const openMediaPicker = (field) => {
setCurrentField(field);
setIsMediaPickerOpen(true);
};
const addRepeaterItem = (field) => {
setData((prev) => ({
...prev,
[field]: [...(prev[field] || []), {}],
}));
};
const updateRepeaterItem = (field, itemIndex, subField, value) => {
setData((prev) => {
const newItems = [...(prev[field] || [])];
newItems[itemIndex] = { ...newItems[itemIndex], [subField]: value };
return { ...prev, [field]: newItems };
});
};
const removeRepeaterItem = (field, itemIndex) => {
setData((prev) => {
const newItems = [...(prev[field] || [])];
newItems.splice(itemIndex, 1);
return { ...prev, [field]: newItems };
});
};
const handleUpdatePage = async (e) => {
e.preventDefault();
const token = localStorage.getItem('token');
if (!token) {
navigate('/login');
return;
}
if (!title.trim()) {
toast.error('Title is required');
return;
}
if (!slug.trim()) {
toast.error('Slug is required');
return;
}
if (!templateId) {
toast.error('Please select a template');
return;
}
if (Object.keys(data).length === 0) {
toast.error('At least one data field is required');
return;
}
try {
await axios.put(
`http://localhost:3000/admin/pages/${id}`,
{ title, slug, data, template_id: parseInt(templateId) },
{ headers: { Authorization: `Bearer ${token}` } }
);
toast.success('Page updated successfully');
navigate(`/page/${slug}`);
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to update page');
}
};
const selectedTemplate = templates.find((t) => t.id === parseInt(templateId));
const renderFieldInput = (field, config, itemIndex = null, parentField = null) => {
const fieldPath = itemIndex !== null ? `${parentField}[${itemIndex}].${field}` : field;
const value = itemIndex !== null ? data[parentField]?.[itemIndex]?.[field] : data[field];
switch (config.type) {
case 'text':
return (
<input
type="text"
value={value || ''}
onChange={(e) => handleDataChange(fieldPath, e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
/>
);
case 'textarea':
return (
<textarea
value={value || ''}
onChange={(e) => handleDataChange(fieldPath, e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 h-24 transition duration-200"
/>
);
case 'select':
return (
<select
value={value || ''}
onChange={(e) => handleDataChange(fieldPath, e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
>
<option value="">Select an option</option>
{config.options.map((option, idx) => (
<option key={idx} value={option}>{option}</option>
))}
</select>
);
case 'enum':
return (
<div className="flex flex-wrap gap-2">
{config.values.map((val, idx) => (
<label key={idx} className="flex items-center">
<input
type="radio"
name={fieldPath}
value={val}
checked={value === val}
onChange={(e) => handleDataChange(fieldPath, e.target.value)}
className="mr-2"
/>
{val}
</label>
))}
</div>
);
case 'image':
return (
<div>
<button
type="button"
onClick={() => openMediaPicker(fieldPath)}
className="bg-blue-600 text-white p-3 rounded-lg hover:bg-blue-700 transition duration-200"
>
Select Image
</button>
{value && (
<img src={`http://localhost:3000${value}`} alt="Preview" className="mt-2 max-w-xs rounded-md" />
)}
</div>
);
case 'richtext':
return (
<CustomWYSIWYG
value={value || ''}
onChange={(val) => handleDataChange(fieldPath, val)}
/>
);
case 'number':
return (
<input
type="number"
value={value || ''}
onChange={(e) => handleDataChange(fieldPath, e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
/>
);
case 'date':
return (
<input
type="date"
value={value || ''}
onChange={(e) => handleDataChange(fieldPath, e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
/>
);
default:
return null;
}
};
if (loading) return <div className="container mx-auto px-6 py-8">Loading...</div>;
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white p-8 rounded-xl shadow-lg border border-gray-200 max-w-2xl mx-auto">
<h1 className="text-3xl font-bold text-gray-800 mb-8">Edit Page</h1>
<form onSubmit={handleUpdatePage}>
<div className="mb-6">
<label className="block text-gray-700 text-sm font-semibold mb-2">Page Title</label>
<input
type="text"
value={title}
onChange={handleTitleChange}
placeholder="Page Title"
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
required
/>
</div>
<div className="mb-6">
<label className="block text-gray-700 text-sm font-semibold mb-2">Slug</label>
<input
type="text"
value={slug}
onChange={(e) => setSlug(e.target.value)}
placeholder="page-slug"
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
required
/>
<p className="text-gray-500 text-sm mt-1">Use letters, numbers, and hyphens only (e.g., about-us)</p>
</div>
<div className="mb-6">
<label className="block text-gray-700 text-sm font-semibold mb-2">Template</label>
<select
value={templateId}
onChange={(e) => setTemplateId(e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-200"
>
<option value="">Select a Template</option>
{templates.map((template) => (
<option key={template.id} value={template.id}>
{template.name} ({template.component})
</option>
))}
</select>
</div>
{selectedTemplate && (
<div className="mb-6">
{Object.entries(selectedTemplate.structure).map(([field, config]) => (
<div key={field} className="mb-4">
<label className="block text-gray-700 text-sm font-semibold mb-2">{toTitleCase(field)}</label>
{config.type === 'repeater' ? (
<div className="border border-gray-300 rounded-lg p-4">
{(data[field] || []).map((item, itemIndex) => (
<div key={itemIndex} className="mb-4 p-4 bg-gray-50 rounded-lg">
{Object.entries(config.subTemplate).map(([subField, subConfig]) => (
<div key={subField} className="mb-2">
<label className="block text-gray-600 text-sm mb-1">{toTitleCase(subField)}</label>
{renderFieldInput(subField, subConfig, itemIndex, field)}
</div>
))}
<button
type="button"
onClick={() => removeRepeaterItem(field, itemIndex)}
className="bg-red-600 text-white p-2 rounded-lg hover:bg-red-700"
>
Remove Item
</button>
</div>
))}
<button
type="button"
onClick={() => addRepeaterItem(field)}
className="bg-gray-600 text-white p-2 rounded-lg hover:bg-gray-700"
>
Add Item
</button>
</div>
) : config.type === 'grid' ? (
<div className="border border-gray-300 rounded-lg p-4">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{config.columns.map((column, colIndex) => (
<div key={colIndex}>
<label className="block text-gray-600 text-sm mb-1">{toTitleCase(column)}</label>
<input
type="text"
value={data[field]?.[column] || ''}
onChange={(e) => handleDataChange(`${field}.${column}`, e.target.value)}
className="w-full p-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
))}
</div>
</div>
) : (
renderFieldInput(field, config)
)}
</div>
))}
</div>
)}
<button
type="submit"
className="bg-blue-600 text-white p-3 rounded-lg hover:bg-blue-700 transition duration-300 shadow-md w-full"
>
Update Page
</button>
</form>
<MediaPickerModal
isOpen={isMediaPickerOpen}
onClose={() => setIsMediaPickerOpen(false)}
onSelect={(path) => handleDataChange(currentField, path)}
/>
</div>
</div>
);
}
export default EditPage;

10
src/pages/Home.jsx Normal file
View File

@ -0,0 +1,10 @@
function Home() {
return (
<div className="container mx-auto px-6 py-8">
<h1 className="text-4xl font-bold text-gray-800 mb-4">Welcome to Your WordPress Clone</h1>
<p className="text-lg text-gray-600">A modern take on WordPress with e-commerce capabilities.</p>
</div>
);
}
export default Home;

220
src/pages/LandingPage.jsx Normal file
View File

@ -0,0 +1,220 @@
import { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import FormField from '../components/FormField';
import { toTitleCase } from '../utils/caseUtils';
function LandingPage({ page }) {
const [formData, setFormData] = useState({});
const [errors, setErrors] = useState({});
useEffect(() => {
console.log('LandingPage page data:', page);
console.log('Form fields:', page.form_fields);
}, [page]);
const handleFormChange = (field, value) => {
setFormData((prev) => ({ ...prev, [field]: value }));
validateField(field, value);
};
const validateField = (field, value) => {
const config = page.form_fields?.[field];
if (!config) return;
let err = '';
if (config.required && !value) {
err = 'This field is required';
} else if (config.minlength && value.length < config.minlength) {
err = `Minimum length is ${config.minlength} characters`;
} else if (config.maxlength && value.length > config.maxlength) {
err = `Maximum length is ${config.maxlength} characters`;
} else if (config.inputType === 'email' && value && !/^[^@]+@[^@]+\.[^@]+$/.test(value)) {
err = 'Invalid email format';
}
setErrors((prev) => ({ ...prev, [field]: err }));
return err;
};
const validateForm = () => {
let isValid = true;
const newErrors = {};
for (const field in page.form_fields) {
const error = validateField(field, formData[field] || '');
if (error) {
newErrors[field] = error;
isValid = false;
}
}
setErrors(newErrors);
return isValid;
};
const handleFormSubmit = (e) => {
e.preventDefault();
if (validateForm()) {
console.log('Form submitted:', formData);
// Future: Send to backend endpoint
} else {
console.log('Form validation failed:', errors);
}
};
// Find hero image and subtitle fields
const heroImageField = Object.entries(page.structure || {}).find(
([, config]) => config.type === 'image'
)?.[0] || 'heroImage';
const subtitleField = Object.entries(page.structure || {}).find(
([, config]) => config.type === 'richtext' || config.type === 'text'
)?.[0] || 'heroText';
const heroImage = page.data?.[heroImageField];
const subtitle = page.data?.[subtitleField];
const renderField = (field, value, config) => {
if (field === heroImageField || field === subtitleField) return null;
const styleClass = page.styles?.[field]?.class || 'text-gray-600';
switch (config.type) {
case 'text':
case 'number':
case 'date':
case 'select':
case 'enum':
return <p className={styleClass}>{value}</p>;
case 'textarea':
return <div className={`whitespace-pre-wrap ${styleClass}`}>{value}</div>;
case 'image':
return <img src={`http://localhost:3000${value}`} alt={field} className={`w-full rounded-md ${styleClass}`} />;
case 'richtext':
return <div className={styleClass} dangerouslySetInnerHTML={{ __html: value }} />;
case 'repeater':
return (
<div className="space-y-4">
{value?.map((item, idx) => (
<div key={idx} className="border border-gray-300 rounded-lg p-4">
{Object.entries(config.subTemplate).map(([subField, subConfig]) => (
<div key={subField} className="mb-2">
<h3 className="text-sm font-semibold text-gray-700">{toTitleCase(subField)}</h3>
{renderField(subField, item[subField], subConfig)}
</div>
))}
</div>
))}
</div>
);
case 'grid':
return (
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{config.columns.map((column, colIndex) => (
<div key={colIndex}>
<h3 className="text-sm font-semibold text-gray-700">{toTitleCase(column)}</h3>
<p className={styleClass}>{value?.[column]}</p>
</div>
))}
</div>
);
default:
return null;
}
};
const renderEnumField = (field, config) => {
const value = formData[field] || '';
const error = errors[field] || (config.required && !value ? 'This field is required' : '');
return (
<div className="mb-4">
<label className="block text-gray-700 text-sm font-semibold mb-2">{toTitleCase(field)}</label>
<div className="flex flex-wrap gap-2">
{(config.values || []).map((val, idx) => (
<label key={idx} className="flex items-center">
<input
type="radio"
name={field}
value={val}
checked={value === val}
onChange={(e) => handleFormChange(field, e.target.value)}
required={config.required}
className="mr-2"
/>
{val}
</label>
))}
</div>
{error && <p className="text-red-500 text-sm mt-1">{error}</p>}
</div>
);
};
return (
<div className="min-h-screen">
<header
className="relative bg-cover bg-center flex items-center justify-center min-h-screen text-white"
style={{ backgroundImage: heroImage ? `url(http://localhost:3000${heroImage})` : 'none' }}
>
<div className="absolute inset-0 bg-black bg-opacity-50"></div>
<div className="relative z-10 text-center px-6">
<h1 className="text-5xl md:text-6xl font-bold mb-4">{page.title}</h1>
{subtitle && (
<div
className={`text-xl md:text-2xl font-light ${page.styles?.[subtitleField]?.class || 'text-gray-200'}`}
dangerouslySetInnerHTML={{ __html: subtitle }}
/>
)}
</div>
</header>
<main className="container mx-auto px-6 py-12">
<div className="bg-white p-8 rounded-xl shadow-lg border border-gray-200">
{page.form_fields && Object.keys(page.form_fields).length > 0 && (
<form onSubmit={handleFormSubmit} className="mb-8">
<h2 className="text-2xl font-semibold text-gray-700 mb-4">Contact Us</h2>
{Object.entries(page.form_fields).map(([field, config]) => (
config.type === 'enum' ? (
renderEnumField(field, config)
) : (
<FormField
key={field}
field={field}
config={config}
value={formData[field] || ''}
onChange={handleFormChange}
errorMessage={errors[field]}
/>
)
))}
<button
type="submit"
className="bg-blue-600 text-white p-3 rounded-lg hover:bg-blue-700 transition duration-300 shadow-md w-full"
>
Submit
</button>
</form>
)}
{page.structure && Object.entries(page.structure).map(([field, config]) => {
const rendered = renderField(field, page.data[field], config);
return rendered ? (
<section key={field} className="mb-8">
<h2 className="text-xl font-semibold text-gray-700 mb-2">{toTitleCase(field)}</h2>
{rendered}
</section>
) : null;
})}
<div className="mt-8 text-sm text-gray-500 border-t border-gray-200 pt-4 text-center">
<p>Created by: {page.created_by_username || 'Unknown'}</p>
<p>Created at: {new Date(page.created_at).toLocaleString()}</p>
<p>Modified at: {new Date(page.modified_at).toLocaleString()}</p>
</div>
<div className="mt-6 text-center">
<Link
to="/contact"
className="bg-blue-600 text-white px-6 py-3 rounded-lg hover:bg-blue-700 transition duration-300"
>
Contact Us
</Link>
</div>
</div>
</main>
</div>
);
}
export default LandingPage;

58
src/pages/Login.jsx Normal file
View File

@ -0,0 +1,58 @@
import { useState } from 'react';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
function Login() {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [message, setMessage] = useState('');
const navigate = useNavigate();
const handleLogin = async (e) => {
e.preventDefault();
try {
const response = await axios.post('http://localhost:3000/login', { username, password });
localStorage.setItem('token', response.data.token);
navigate('/admin');
} catch (err) {
setMessage(err.response?.data?.error || 'Login failed');
}
};
return (
<div className="flex items-center justify-center min-h-screen bg-gray-100">
<div className="bg-white p-8 rounded-lg shadow-lg w-full max-w-md">
<h1 className="text-2xl font-bold text-gray-800 mb-6 text-center">Login</h1>
<form onSubmit={handleLogin}>
<div className="mb-4">
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="Username"
className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div className="mb-6">
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Password"
className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<button
type="submit"
className="w-full bg-blue-600 text-white p-3 rounded-md hover:bg-blue-700 transition duration-200"
>
Login
</button>
{message && <p className="mt-4 text-red-500 text-center">{message}</p>}
</form>
</div>
</div>
);
}
export default Login;

View File

@ -0,0 +1,20 @@
import { Link } from 'react-router-dom';
function OrderSuccess() {
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white p-8 rounded-xl shadow-lg border border-gray-200 text-center">
<h1 className="text-4xl font-bold text-gray-800 mb-6">Order Placed Successfully!</h1>
<p className="text-gray-600 mb-6">Thank you for your purchase. Your order has been received and is being processed.</p>
<Link
to="/"
className="bg-blue-600 text-white px-6 py-3 rounded-lg hover:bg-blue-700 transition duration-300"
>
Continue Shopping
</Link>
</div>
</div>
);
}
export default OrderSuccess;

198
src/pages/PageViewer.jsx Normal file
View File

@ -0,0 +1,198 @@
import { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import axios from 'axios';
import FormField from '../components/FormField';
import { toTitleCase } from '../utils/caseUtils';
function PageViewer({ page }) {
const { slug } = useParams();
const [pageData, setPageData] = useState(page);
const [error, setError] = useState('');
const [formData, setFormData] = useState({});
const [errors, setErrors] = useState({});
useEffect(() => {
if (!page) {
axios
.get(`http://localhost:3000/pages/${slug}`)
.then((response) => {
setPageData(response.data);
console.log('PageViewer page data:', response.data);
console.log('Form fields:', response.data.form_fields);
})
.catch(() => setError('Failed to load page'));
} else {
console.log('PageViewer page data:', page);
console.log('Form fields:', page.form_fields);
}
}, [slug, page]);
const handleFormChange = (field, value) => {
setFormData((prev) => ({ ...prev, [field]: value }));
validateField(field, value);
};
const validateField = (field, value) => {
const config = pageData?.form_fields?.[field];
if (!config) return;
let err = '';
if (config.required && !value) {
err = 'This field is required';
} else if (config.minlength && value.length < config.minlength) {
err = `Minimum length is ${config.minlength} characters`;
} else if (config.maxlength && value.length > config.maxlength) {
err = `Maximum length is ${config.maxlength} characters`;
} else if (config.inputType === 'email' && value && !/^[^@]+@[^@]+\.[^@]+$/.test(value)) {
err = 'Invalid email format';
}
setErrors((prev) => ({ ...prev, [field]: err }));
return err;
};
const validateForm = () => {
let isValid = true;
const newErrors = {};
for (const field in pageData?.form_fields) {
const error = validateField(field, formData[field] || '');
if (error) {
newErrors[field] = error;
isValid = false;
}
}
setErrors(newErrors);
return isValid;
};
const handleFormSubmit = (e) => {
e.preventDefault();
if (validateForm()) {
console.log('Form submitted:', formData);
// Future: Send to backend endpoint
} else {
console.log('Form validation failed:', errors);
}
};
const renderField = (field, value, config) => {
const styleClass = pageData?.styles?.[field]?.class || 'text-gray-600';
switch (config.type) {
case 'text':
case 'number':
case 'date':
case 'select':
case 'enum':
return <p className={styleClass}>{value}</p>;
case 'textarea':
return <div className={`whitespace-pre-wrap ${styleClass}`}>{value}</div>;
case 'image':
return <img src={`http://localhost:3000${value}`} alt={field} className={`max-w-full rounded-md ${styleClass}`} />;
case 'richtext':
return <div className={styleClass} dangerouslySetInnerHTML={{ __html: value }} />;
case 'repeater':
return (
<div className="space-y-4">
{value?.map((item, idx) => (
<div key={idx} className="border border-gray-300 rounded-lg p-4">
{Object.entries(config.subTemplate).map(([subField, subConfig]) => (
<div key={subField} className="mb-2">
<h3 className="text-sm font-semibold text-gray-700">{toTitleCase(subField)}</h3>
{renderField(subField, item[subField], subConfig)}
</div>
))}
</div>
))}
</div>
);
case 'grid':
return (
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{config.columns.map((column, colIndex) => (
<div key={colIndex}>
<h3 className="text-sm font-semibold text-gray-700">{toTitleCase(column)}</h3>
<p className={styleClass}>{value?.[column]}</p>
</div>
))}
</div>
);
default:
return null;
}
};
const renderEnumField = (field, config) => {
const value = formData[field] || '';
const error = errors[field] || (config.required && !value ? 'This field is required' : '');
return (
<div className="mb-4">
<label className="block text-gray-700 text-sm font-semibold mb-2">{toTitleCase(field)}</label>
<div className="flex flex-wrap gap-2">
{(config.values || []).map((val, idx) => (
<label key={idx} className="flex items-center">
<input
type="radio"
name={field}
value={val}
checked={value === val}
onChange={(e) => handleFormChange(field, e.target.value)}
required={config.required}
className="mr-2"
/>
{val}
</label>
))}
</div>
{error && <p className="text-red-500 text-sm mt-1">{error}</p>}
</div>
);
};
if (error) return <div className="container mx-auto px-6 py-8 text-red-500">{error}</div>;
if (!pageData) return <div className="container mx-auto px-6 py-8">Loading...</div>;
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white p-8 rounded-xl shadow-lg border border-gray-200">
<h1 className="text-4xl font-bold text-gray-800 mb-6">{pageData.title}</h1>
{pageData.form_fields && Object.keys(pageData.form_fields).length > 0 && (
<form onSubmit={handleFormSubmit} className="mb-8">
<h2 className="text-2xl font-semibold text-gray-700 mb-4">Contact Us</h2>
{Object.entries(pageData.form_fields).map(([field, config]) => (
config.type === 'enum' ? (
renderEnumField(field, config)
) : (
<FormField
key={field}
field={field}
config={config}
value={formData[field] || ''}
onChange={handleFormChange}
errorMessage={errors[field]}
/>
)
))}
<button
type="submit"
className="bg-blue-600 text-white p-3 rounded-lg hover:bg-blue-700 transition duration-300 shadow-md w-full"
>
Submit
</button>
</form>
)}
{pageData.structure && Object.entries(pageData.structure).map(([field, config]) => (
<div key={field} className="mb-6">
<h2 className="text-xl font-semibold text-gray-700 mb-2">{toTitleCase(field)}</h2>
{renderField(field, pageData.data[field], config)}
</div>
))}
<div className="mt-8 text-sm text-gray-500 border-t border-gray-200 pt-4">
<p>Created by: {pageData.created_by_username || 'Unknown'}</p>
<p>Created at: {new Date(pageData.created_at).toLocaleString()}</p>
<p>Modified at: {new Date(pageData.modified_at).toLocaleString()}</p>
</div>
</div>
</div>
);
}
export default PageViewer;

299
src/pages/ProductsAdmin.jsx Normal file
View File

@ -0,0 +1,299 @@
import { useState, useEffect } from 'react';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
import { toast } from 'react-toastify';
function ProductsAdmin() {
const navigate = useNavigate();
const [products, setProducts] = useState([]);
const [formData, setFormData] = useState({ name: '', description: '', price: '', image: null });
const [editingId, setEditingId] = useState(null);
const [isFormVisible, setIsFormVisible] = useState(false);
useEffect(() => {
const token = localStorage.getItem('token');
if (!token) {
navigate('/login');
return;
}
fetchProducts();
}, [navigate]);
const fetchProducts = async () => {
const token = localStorage.getItem('token');
try {
const response = await axios.get('http://localhost:3000/products', {
headers: { Authorization: `Bearer ${token}` },
});
setProducts(response.data);
} catch (err) {
toast.error('Failed to load products: ' + (err.response?.data?.error || 'Unknown error'));
if (err.response?.status === 403) {
navigate('/login');
}
}
};
const handleInputChange = (e) => {
const { name, value, files } = e.target;
setFormData((prev) => ({
...prev,
[name]: files ? files[0] : value,
}));
};
const handleSubmit = async (e) => {
e.preventDefault();
const token = localStorage.getItem('token');
const form = new FormData();
form.append('name', formData.name);
form.append('description', formData.description);
form.append('price', formData.price);
if (formData.image) {
form.append('image', formData.image);
}
try {
if (editingId) {
await axios.put(`http://localhost:3000/admin/products/${editingId}`, form, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'multipart/form-data',
},
});
toast.success('Product updated successfully');
} else {
await axios.post('http://localhost:3000/admin/products', form, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'multipart/form-data',
},
});
toast.success('Product created successfully');
}
setFormData({ name: '', description: '', price: '', image: null });
setEditingId(null);
setIsFormVisible(false);
fetchProducts();
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to save product');
}
};
const handleEdit = (product) => {
setFormData({
name: product.name,
description: product.description,
price: product.price.toString(),
image: null,
});
setEditingId(product.id);
setIsFormVisible(true);
};
const handleDelete = async (id) => {
if (!window.confirm('Are you sure you want to delete this product?')) return;
const token = localStorage.getItem('token');
try {
await axios.delete(`http://localhost:3000/admin/products/${id}`, {
headers: { Authorization: `Bearer ${token}` },
});
toast.success('Product deleted successfully');
fetchProducts();
} catch (err) {
toast.error(err.response?.data?.error || 'Failed to delete product');
}
};
const handleImport = async (e) => {
const file = e.target.files[0];
if (!file) return;
try {
const reader = new FileReader();
reader.onload = async (event) => {
const json = JSON.parse(event.target.result);
const token = localStorage.getItem('token');
const response = await axios.post('http://localhost:3000/admin/products/import', { products: json }, {
headers: { Authorization: `Bearer ${token}` },
});
toast.success(response.data.message);
fetchProducts();
};
reader.readAsText(file);
} catch (err) {
toast.error('Failed to import products: ' + (err.message || 'Invalid JSON'));
}
};
const handleExport = () => {
const exportData = products.map(({ id, name, description, price, image }) => ({
id,
name,
description,
price,
image,
}));
const json = JSON.stringify(exportData, null, 2);
const blob = new Blob([json], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'products_export.json';
a.click();
URL.revokeObjectURL(url);
};
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white dark:bg-gray-800 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-700">
<div className="flex justify-between items-center mb-6">
<h1 className="text-3xl font-bold text-gray-800 dark:text-gray-100">Products</h1>
<div className="space-x-2">
<button
onClick={() => {
setIsFormVisible(true);
setFormData({ name: '', description: '', price: '', image: null });
setEditingId(null);
}}
className="bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 dark:hover:bg-blue-600 transition duration-300"
>
Add Product
</button>
<button
onClick={handleExport}
className="bg-green-600 text-white px-4 py-2 rounded-lg hover:bg-green-700 dark:hover:bg-green-600 transition duration-300"
>
Export JSON
</button>
<label className="bg-yellow-600 text-white px-4 py-2 rounded-lg hover:bg-yellow-700 dark:hover:bg-yellow-600 transition duration-300 cursor-pointer">
Import JSON
<input type="file" accept=".json" onChange={handleImport} className="hidden" />
</label>
</div>
</div>
{isFormVisible && (
<form onSubmit={handleSubmit} className="mb-8 p-6 bg-gray-50 dark:bg-gray-700 rounded-lg border border-gray-200 dark:border-gray-600">
<h2 className="text-2xl font-semibold text-gray-700 dark:text-gray-200 mb-4">{editingId ? 'Edit Product' : 'Add Product'}</h2>
<div className="mb-4">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-2">Name</label>
<input
type="text"
name="name"
value={formData.name}
onChange={handleInputChange}
placeholder="Product Name"
className="w-full p-3 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:text-gray-200 transition duration-200"
required
/>
</div>
<div className="mb-4">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-2">Description</label>
<textarea
name="description"
value={formData.description}
onChange={handleInputChange}
placeholder="Product Description"
className="w-full p-3 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:text-gray-200 h-24 transition duration-200"
required
/>
</div>
<div className="mb-4">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-2">Price</label>
<input
type="number"
name="price"
value={formData.price}
onChange={handleInputChange}
placeholder="Product Price"
step="0.01"
className="w-full p-3 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:text-gray-200 transition duration-200"
required
/>
</div>
<div className="mb-4">
<label className="block text-gray-700 dark:text-gray-300 text-sm font-semibold mb-2">Image</label>
<input
type="file"
name="image"
accept="image/*"
onChange={handleInputChange}
className="w-full p-3 border border-gray-300 dark:border-gray-600 rounded-lg dark:bg-gray-800 dark:text-gray-200"
/>
</div>
<div className="flex space-x-2">
<button
type="submit"
className="bg-blue-600 text-white p-3 rounded-lg hover:bg-blue-700 dark:hover:bg-blue-600 transition duration-300 flex-1"
>
{editingId ? 'Update Product' : 'Create Product'}
</button>
<button
type="button"
onClick={() => {
setIsFormVisible(false);
setFormData({ name: '', description: '', price: '', image: null });
setEditingId(null);
}}
className="bg-gray-600 text-white p-3 rounded-lg hover:bg-gray-700 dark:hover:bg-gray-600 transition duration-300 flex-1"
>
Cancel
</button>
</div>
</form>
)}
<div className="overflow-x-auto">
<table className="min-w-full bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700">
<thead>
<tr>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Name</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Description</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Price</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Image</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Created By</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Created At</th>
<th className="py-3 px-4 border-b text-left text-gray-700 dark:text-gray-300 font-semibold">Actions</th>
</tr>
</thead>
<tbody>
{products.map((product) => (
<tr key={product.id} className="hover:bg-gray-50 dark:hover:bg-gray-700">
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">{product.name}</td>
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">{product.description}</td>
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">${product.price.toFixed(2)}</td>
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">
{product.image ? (
<img src={`http://localhost:3000${product.image}`} alt={product.name} className="w-16 h-16 object-cover rounded-md" />
) : (
'No Image'
)}
</td>
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">{product.created_by_username || 'Unknown'}</td>
<td className="py-3 px-4 border-b text-gray-800 dark:text-gray-200">{new Date(product.created_at).toLocaleString()}</td>
<td className="py-3 px-4 border-b">
<button
onClick={() => handleEdit(product)}
className="bg-yellow-600 text-white px-3 py-1 rounded-lg hover:bg-yellow-700 dark:hover:bg-yellow-600 mr-2"
>
Edit
</button>
<button
onClick={() => handleDelete(product.id)}
className="bg-red-600 text-white px-3 py-1 rounded-lg hover:bg-red-700 dark:hover:bg-red-600"
>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
}
export default ProductsAdmin;

View File

@ -0,0 +1,75 @@
import Products from '../components/Products';
import { toTitleCase } from '../utils/caseUtils';
function ProductsPage({ page }) {
const renderField = (field, value, config) => {
const styleClass = page.styles?.[field]?.class || 'text-gray-600';
switch (config.type) {
case 'text':
case 'number':
case 'date':
case 'select':
case 'enum':
return <p className={styleClass}>{value}</p>;
case 'textarea':
return <div className={`whitespace-pre-wrap ${styleClass}`}>{value}</div>;
case 'image':
return <img src={`http://localhost:3000${value}`} alt={field} className={`w-full rounded-md ${styleClass}`} />;
case 'richtext':
return <div className={styleClass} dangerouslySetInnerHTML={{ __html: value }} />;
case 'repeater':
return (
<div className="space-y-4">
{value?.map((item, idx) => (
<div key={idx} className="border border-gray-300 rounded-lg p-4">
{Object.entries(config.subTemplate).map(([subField, subConfig]) => (
<div key={subField} className="mb-2">
<h3 className="text-sm font-semibold text-gray-700">{toTitleCase(subField)}</h3>
{renderField(subField, item[subField], subConfig)}
</div>
))}
</div>
))}
</div>
);
case 'grid':
return (
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{config.columns.map((column, colIndex) => (
<div key={colIndex}>
<h3 className="text-sm font-semibold text-gray-700">{toTitleCase(column)}</h3>
<p className={styleClass}>{value?.[column]}</p>
</div>
))}
</div>
);
default:
return null;
}
};
return (
<div className="container mx-auto px-6 py-8">
<div className="bg-white p-8 rounded-xl shadow-lg border border-gray-200">
<h1 className="text-4xl font-bold text-gray-800 mb-6">{page.title}</h1>
{page.structure && Object.entries(page.structure).map(([field, config]) => {
const rendered = renderField(field, page.data[field], config);
return rendered ? (
<section key={field} className="mb-8">
<h2 className="text-xl font-semibold text-gray-700 mb-2">{toTitleCase(field)}</h2>
{rendered}
</section>
) : null;
})}
<Products />
<div className="mt-8 text-sm text-gray-500 border-t border-gray-200 pt-4 text-center">
<p>Created by: {page.created_by_username || 'Unknown'}</p>
<p>Created at: {new Date(page.created_at).toLocaleString()}</p>
<p>Modified at: {new Date(page.modified_at).toLocaleString()}</p>
</div>
</div>
</div>
);
}
export default ProductsPage;

65
src/pages/Register.jsx Normal file
View File

@ -0,0 +1,65 @@
import { useState } from 'react';
import axios from 'axios';
import { toast } from 'react-toastify';
function Register() {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [role, setRole] = useState('user');
const handleRegister = async (e) => {
e.preventDefault();
try {
const response = await axios.post('http://localhost:3000/users', { username, password, role });
toast.success(response.data.message);
} catch (err) {
toast.error(err.response?.data?.error || 'Registration failed');
}
};
return (
<div className="flex items-center justify-center min-h-screen bg-gray-100">
<div className="bg-white p-8 rounded-lg shadow-lg w-full max-w-md">
<h1 className="text-2xl font-bold text-gray-800 mb-6 text-center">Register</h1>
<form onSubmit={handleRegister}>
<div className="mb-4">
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="Username"
className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div className="mb-4">
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Password"
className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div className="mb-6">
<select
value={role}
onChange={(e) => setRole(e.target.value)}
className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
>
<option value="user">User</option>
<option value="admin">Admin</option>
</select>
</div>
<button
type="submit"
className="w-full bg-blue-600 text-white p-3 rounded-md hover:bg-blue-700 transition duration-200"
>
Register
</button>
</form>
</div>
</div>
);
}
export default Register;

22
src/utils/caseUtils.js Normal file
View File

@ -0,0 +1,22 @@
export function toCamelCase(str) {
if (!str) return '';
return str
.toLowerCase()
.replace(/[^a-z0-9]+/g, ' ')
.trim()
.split(' ')
.map((word, index) =>
index === 0 ? word : word.charAt(0).toUpperCase() + word.slice(1)
)
.join('');
}
export function toTitleCase(str) {
if (!str) return '';
return str
.replace(/([A-Z])/g, ' $1')
.trim()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(' ');
}

11
tailwind.config.js Normal file
View File

@ -0,0 +1,11 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./src/**/*.{js,jsx,ts,tsx}',
],
darkMode: 'class', // Ensure dark mode uses class strategy
theme: {
extend: {},
},
plugins: [],
}

11
vite.config.js Normal file
View File

@ -0,0 +1,11 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
// https://vite.dev/config/
export default defineConfig({
plugins: [
react(),
tailwindcss(),
],
})