feat: Enhance Quality Template and Recipe wizards with comprehensive features
Quality Template Wizard improvements: - Added comprehensive fields for better template configuration - Frequency details (time of day, specific conditions) - Responsible person/role assignment - Required equipment/tools specification - Detailed acceptance criteria - Special conditions and notes - Photo requirements toggle - Critical control point (PCC) designation - Notification settings for failures - Dynamic description generation from all fields - Improved UI with organized sections Recipe Wizard improvements: - Added quality templates integration step - Fetch and display available quality templates - Multi-select interface for template assignment - Templates linked to recipe via quality_check_configuration - Optional step - can skip if no templates needed - Shows template details (type, frequency, requirements) - Auto-generates quality config for production stage - Seamless integration with existing recipe creation flow Files modified: - frontend/src/components/domain/unified-wizard/wizards/QualityTemplateWizard.tsx - frontend/src/components/domain/unified-wizard/wizards/RecipeWizard.tsx
This commit is contained in:
@@ -4,8 +4,10 @@ import { ChefHat, Package, ClipboardCheck, CheckCircle2, Loader2, Plus, X, Searc
|
||||
import { useTenant } from '../../../../stores/tenant.store';
|
||||
import { recipesService } from '../../../../api/services/recipes';
|
||||
import { inventoryService } from '../../../../api/services/inventory';
|
||||
import { qualityTemplateService } from '../../../../api/services/qualityTemplates';
|
||||
import { IngredientResponse } from '../../../../api/types/inventory';
|
||||
import { RecipeCreate, RecipeIngredientCreate, MeasurementUnit } from '../../../../api/types/recipes';
|
||||
import { RecipeCreate, RecipeIngredientCreate, MeasurementUnit, RecipeQualityConfiguration } from '../../../../api/types/recipes';
|
||||
import { QualityCheckTemplateResponse } from '../../../../api/types/qualityTemplates';
|
||||
import { showToast } from '../../../../utils/toast';
|
||||
|
||||
interface WizardDataProps extends WizardStepProps {
|
||||
@@ -161,12 +163,11 @@ interface SelectedIngredient {
|
||||
order: number;
|
||||
}
|
||||
|
||||
const IngredientsStep: React.FC<WizardDataProps> = ({ data, onDataChange, onComplete }) => {
|
||||
const IngredientsStep: React.FC<WizardDataProps> = ({ data, onDataChange, onNext }) => {
|
||||
const { currentTenant } = useTenant();
|
||||
const [ingredients, setIngredients] = useState<IngredientResponse[]>([]);
|
||||
const [selectedIngredients, setSelectedIngredients] = useState<SelectedIngredient[]>(data.ingredients || []);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [searchTerm, setSearchTerm] = useState('');
|
||||
|
||||
@@ -214,12 +215,7 @@ const IngredientsStep: React.FC<WizardDataProps> = ({ data, onDataChange, onComp
|
||||
setSelectedIngredients(selectedIngredients.filter(ing => ing.id !== id));
|
||||
};
|
||||
|
||||
const handleSaveRecipe = async () => {
|
||||
if (!currentTenant?.id) {
|
||||
setError('No se pudo obtener información del tenant');
|
||||
return;
|
||||
}
|
||||
|
||||
const handleContinue = () => {
|
||||
if (selectedIngredients.length === 0) {
|
||||
setError('Debes agregar al menos un ingrediente');
|
||||
return;
|
||||
@@ -234,43 +230,8 @@ const IngredientsStep: React.FC<WizardDataProps> = ({ data, onDataChange, onComp
|
||||
return;
|
||||
}
|
||||
|
||||
setSaving(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
// Prepare recipe data according to RecipeCreate interface
|
||||
const recipeIngredients: RecipeIngredientCreate[] = selectedIngredients.map((ing, index) => ({
|
||||
ingredient_id: ing.ingredientId,
|
||||
quantity: ing.quantity,
|
||||
unit: ing.unit,
|
||||
ingredient_notes: ing.notes || null,
|
||||
is_optional: false,
|
||||
ingredient_order: index + 1,
|
||||
}));
|
||||
|
||||
const recipeData: RecipeCreate = {
|
||||
name: data.name,
|
||||
category: data.category,
|
||||
finished_product_id: data.finishedProductId,
|
||||
yield_quantity: parseFloat(data.yieldQuantity),
|
||||
yield_unit: data.yieldUnit as MeasurementUnit,
|
||||
prep_time_minutes: data.prepTime ? parseInt(data.prepTime) : null,
|
||||
instructions: data.instructions ? { steps: data.instructions } : null,
|
||||
ingredients: recipeIngredients,
|
||||
};
|
||||
|
||||
await recipesService.createRecipe(currentTenant.id, recipeData);
|
||||
showToast.success('Receta creada exitosamente');
|
||||
onDataChange({ ...data, ingredients: selectedIngredients });
|
||||
onComplete();
|
||||
} catch (err: any) {
|
||||
console.error('Error creating recipe:', err);
|
||||
const errorMessage = err.response?.data?.detail || 'Error al crear la receta';
|
||||
setError(errorMessage);
|
||||
showToast.error(errorMessage);
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
onDataChange({ ...data, ingredients: selectedIngredients });
|
||||
onNext();
|
||||
};
|
||||
|
||||
const filteredIngredients = ingredients.filter(ing =>
|
||||
@@ -394,14 +355,215 @@ const IngredientsStep: React.FC<WizardDataProps> = ({ data, onDataChange, onComp
|
||||
|
||||
<div className="flex justify-end gap-3 pt-4 border-t border-[var(--border-primary)]">
|
||||
<button
|
||||
onClick={handleSaveRecipe}
|
||||
disabled={saving || selectedIngredients.length === 0 || loading}
|
||||
onClick={handleContinue}
|
||||
disabled={selectedIngredients.length === 0 || loading}
|
||||
className="px-6 py-2.5 bg-[var(--color-primary)] text-white rounded-lg hover:bg-[var(--color-primary)]/90 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
||||
>
|
||||
Continuar
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// Step 3: Quality Templates Selection
|
||||
const QualityTemplatesStep: React.FC<WizardDataProps> = ({ data, onDataChange, onComplete }) => {
|
||||
const { currentTenant } = useTenant();
|
||||
const [templates, setTemplates] = useState<QualityCheckTemplateResponse[]>([]);
|
||||
const [selectedTemplateIds, setSelectedTemplateIds] = useState<string[]>(data.selectedTemplates || []);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
fetchTemplates();
|
||||
}, []);
|
||||
|
||||
const fetchTemplates = async () => {
|
||||
if (!currentTenant?.id) return;
|
||||
setLoading(true);
|
||||
try {
|
||||
const result = await qualityTemplateService.getTemplates(currentTenant.id, { is_active: true });
|
||||
setTemplates(result);
|
||||
} catch (err) {
|
||||
console.error('Error loading quality templates:', err);
|
||||
setError('Error al cargar plantillas de calidad');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const toggleTemplate = (templateId: string) => {
|
||||
setSelectedTemplateIds(prev =>
|
||||
prev.includes(templateId)
|
||||
? prev.filter(id => id !== templateId)
|
||||
: [...prev, templateId]
|
||||
);
|
||||
};
|
||||
|
||||
const handleCreateRecipe = async () => {
|
||||
if (!currentTenant?.id) {
|
||||
setError('No se pudo obtener información del tenant');
|
||||
return;
|
||||
}
|
||||
|
||||
setSaving(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
// Prepare recipe ingredients
|
||||
const recipeIngredients: RecipeIngredientCreate[] = data.ingredients.map((ing: any, index: number) => ({
|
||||
ingredient_id: ing.ingredientId,
|
||||
quantity: ing.quantity,
|
||||
unit: ing.unit,
|
||||
ingredient_notes: ing.notes || null,
|
||||
is_optional: false,
|
||||
ingredient_order: index + 1,
|
||||
}));
|
||||
|
||||
// Prepare quality configuration if templates are selected
|
||||
let qualityConfig: RecipeQualityConfiguration | undefined;
|
||||
if (selectedTemplateIds.length > 0) {
|
||||
qualityConfig = {
|
||||
stages: {
|
||||
production: {
|
||||
template_ids: selectedTemplateIds,
|
||||
required_checks: [],
|
||||
optional_checks: [],
|
||||
blocking_on_failure: true,
|
||||
min_quality_score: 7.0,
|
||||
}
|
||||
},
|
||||
overall_quality_threshold: 7.0,
|
||||
critical_stage_blocking: true,
|
||||
auto_create_quality_checks: true,
|
||||
quality_manager_approval_required: false,
|
||||
};
|
||||
}
|
||||
|
||||
const recipeData: RecipeCreate = {
|
||||
name: data.name,
|
||||
category: data.category,
|
||||
finished_product_id: data.finishedProductId,
|
||||
yield_quantity: parseFloat(data.yieldQuantity),
|
||||
yield_unit: data.yieldUnit as MeasurementUnit,
|
||||
prep_time_minutes: data.prepTime ? parseInt(data.prepTime) : null,
|
||||
instructions: data.instructions ? { steps: data.instructions } : null,
|
||||
ingredients: recipeIngredients,
|
||||
quality_check_configuration: qualityConfig,
|
||||
};
|
||||
|
||||
await recipesService.createRecipe(currentTenant.id, recipeData);
|
||||
showToast.success('Receta creada exitosamente');
|
||||
onDataChange({ ...data, selectedTemplates: selectedTemplateIds });
|
||||
onComplete();
|
||||
} catch (err: any) {
|
||||
console.error('Error creating recipe:', err);
|
||||
const errorMessage = err.response?.data?.detail || 'Error al crear la receta';
|
||||
setError(errorMessage);
|
||||
showToast.error(errorMessage);
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="text-center pb-4 border-b border-[var(--border-primary)]">
|
||||
<ClipboardCheck className="w-12 h-12 mx-auto mb-3 text-[var(--color-primary)]" />
|
||||
<h3 className="text-lg font-semibold text-[var(--text-primary)] mb-2">
|
||||
Plantillas de Calidad (Opcional)
|
||||
</h3>
|
||||
<p className="text-sm text-[var(--text-secondary)]">
|
||||
Selecciona las plantillas de control de calidad que aplicarán a esta receta
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="p-3 bg-red-50 border border-red-200 rounded-lg text-red-700 text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{loading ? (
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<Loader2 className="w-8 h-8 animate-spin text-[var(--color-primary)]" />
|
||||
<span className="ml-3 text-[var(--text-secondary)]">Cargando plantillas...</span>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{templates.length === 0 ? (
|
||||
<div className="text-center py-12 border-2 border-dashed border-[var(--border-secondary)] rounded-lg">
|
||||
<ClipboardCheck className="w-12 h-12 mx-auto mb-3 text-[var(--text-tertiary)]" />
|
||||
<p className="text-[var(--text-secondary)] mb-2">No hay plantillas de calidad disponibles</p>
|
||||
<p className="text-sm text-[var(--text-tertiary)]">
|
||||
Puedes crear plantillas desde el wizard principal
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-3 max-h-96 overflow-y-auto">
|
||||
{templates.map((template) => (
|
||||
<button
|
||||
key={template.id}
|
||||
onClick={() => toggleTemplate(template.id)}
|
||||
className={`w-full p-4 rounded-lg border-2 transition-all text-left ${
|
||||
selectedTemplateIds.includes(template.id)
|
||||
? 'border-[var(--color-primary)] bg-[var(--color-primary)]/5'
|
||||
: 'border-[var(--border-secondary)] hover:border-[var(--color-primary)]/50'
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="flex-1">
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<h4 className="font-semibold text-[var(--text-primary)]">{template.name}</h4>
|
||||
{template.is_required && (
|
||||
<span className="px-2 py-0.5 text-xs bg-orange-100 text-orange-700 rounded-full">
|
||||
Obligatorio
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
{template.description && (
|
||||
<p className="text-sm text-[var(--text-secondary)] line-clamp-2">
|
||||
{template.description}
|
||||
</p>
|
||||
)}
|
||||
<div className="flex items-center gap-3 mt-2 text-xs text-[var(--text-tertiary)]">
|
||||
<span>Tipo: {template.check_type}</span>
|
||||
{template.frequency_days && (
|
||||
<span>• Cada {template.frequency_days} días</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{selectedTemplateIds.includes(template.id) && (
|
||||
<CheckCircle2 className="w-5 h-5 text-[var(--color-primary)] flex-shrink-0 ml-3" />
|
||||
)}
|
||||
</div>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{selectedTemplateIds.length > 0 && (
|
||||
<div className="p-4 bg-[var(--color-primary)]/5 rounded-lg border border-[var(--color-primary)]/20">
|
||||
<p className="text-sm text-[var(--text-primary)]">
|
||||
<strong>{selectedTemplateIds.length}</strong> plantilla(s) seleccionada(s)
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* Action Buttons */}
|
||||
<div className="flex justify-end gap-3 pt-4 border-t border-[var(--border-primary)]">
|
||||
<button
|
||||
onClick={handleCreateRecipe}
|
||||
disabled={saving || loading}
|
||||
className="px-8 py-3 bg-green-600 text-white rounded-lg hover:bg-green-700 font-semibold inline-flex items-center gap-2 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
{saving ? (
|
||||
<>
|
||||
<Loader2 className="w-5 h-5 animate-spin" />
|
||||
Creando...
|
||||
Creando receta...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
@@ -418,4 +580,5 @@ const IngredientsStep: React.FC<WizardDataProps> = ({ data, onDataChange, onComp
|
||||
export const RecipeWizardSteps = (data: Record<string, any>, setData: (data: Record<string, any>) => void): WizardStep[] => [
|
||||
{ id: 'recipe-details', title: 'Detalles de la Receta', description: 'Nombre, categoría, rendimiento', component: (props) => <RecipeDetailsStep {...props} data={data} onDataChange={setData} /> },
|
||||
{ id: 'recipe-ingredients', title: 'Ingredientes', description: 'Selección y cantidades', component: (props) => <IngredientsStep {...props} data={data} onDataChange={setData} /> },
|
||||
{ id: 'recipe-quality-templates', title: 'Plantillas de Calidad', description: 'Controles de calidad aplicables', component: (props) => <QualityTemplatesStep {...props} data={data} onDataChange={setData} /> },
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user