codenuk_frontend_mine/DYNAMIC_TEMPLATES.md

119 lines
4.2 KiB
Markdown

# Dynamic Templates Implementation
## Overview
The frontend now fetches templates dynamically from the database instead of using static templates. This allows for real-time template management and custom template creation.
## Changes Made
### 1. Template Service (`src/lib/template-service.ts`)
- Created service to communicate with the template-manager API
- Handles fetching templates by category, individual templates, and creating new templates
- Includes TypeScript interfaces for type safety
### 2. Custom Hook (`src/hooks/useTemplates.ts`)
- Manages template data fetching and state
- Converts database templates to UI format
- Handles loading states and error handling
- Provides template feature fetching
### 3. Custom Template Form (`src/components/custom-template-form.tsx`)
- Form component for creating new templates
- Includes all required fields: type, title, description, category
- Optional fields: icon, gradient, border, text, subtext
- Validates required fields before submission
### 4. Updated Main Dashboard (`src/components/main-dashboard.tsx`)
- Replaced static templates with dynamic database templates
- Added loading and error states
- Dynamic category generation based on available templates
- Custom template creation functionality
- Fallback to static templates if database is unavailable
### 5. UI Components
- Added `textarea.tsx` component for form inputs
- Enhanced existing components with proper styling
## API Integration
### Template Manager Service
- **Base URL**: `http://localhost:8009`
- **Endpoints**:
- `GET /api/templates` - Get all templates grouped by category
- `GET /api/templates/:id` - Get specific template with features
- `GET /api/templates/type/:type` - Get template by type
- `POST /api/templates` - Create new template
### Database Schema
Templates are stored in PostgreSQL with the following structure:
```sql
CREATE TABLE templates (
id UUID PRIMARY KEY,
type VARCHAR(100) UNIQUE,
title VARCHAR(200),
description TEXT,
category VARCHAR(100),
icon VARCHAR(50),
gradient VARCHAR(100),
border VARCHAR(100),
text VARCHAR(100),
subtext VARCHAR(100),
is_active BOOLEAN,
created_at TIMESTAMP,
updated_at TIMESTAMP
);
```
## Usage
### Viewing Templates
1. Templates are automatically loaded from the database on page load
2. If database is unavailable, fallback static templates are shown
3. Templates are grouped by category dynamically
### Creating Custom Templates
1. Click "Create Custom Template" button
2. Fill in required fields (type, title, description, category)
3. Optionally add styling fields (icon, gradient, border, text, subtext)
4. Submit to create the template in the database
5. Template will appear in the list after creation
### Template Features
- Each template can have associated features stored in `template_features` table
- Features are fetched when a template is selected
- Features include complexity, type, and usage statistics
## Error Handling
- Network errors show retry button
- Loading states with spinner
- Graceful fallback to static templates
- Form validation for required fields
## ✅ Implemented Features
### Template Management
-**Dynamic Template Display** - Templates fetched from database
-**Custom Template Creation** - Create new templates via form
-**Template Editing** - Edit existing templates
-**Template Deletion** - Delete templates with confirmation
-**Real-time Updates** - Changes reflect immediately in UI
### API Endpoints
-`GET /api/templates` - Get all templates grouped by category
-`GET /api/templates/:id` - Get specific template with features
-`POST /api/templates` - Create new template
-`PUT /api/templates/:id` - Update existing template
-`DELETE /api/templates/:id` - Delete template (soft delete)
### Database Operations
-**Soft Delete** - Templates are marked as inactive rather than physically deleted
-**Data Integrity** - All operations maintain referential integrity
-**Error Handling** - Comprehensive error handling for all operations
## Future Enhancements
- Feature management for templates
- Bulk template operations
- Template versioning
- Template sharing between users
- Template import/export functionality
- Template analytics and usage tracking