Skip to content

Commit

Permalink
fix delete button on update
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Zarazan committed Nov 22, 2024
1 parent feb454a commit c66d580
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/javascript/components/recipes/RecipeEdit.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react';
import { useAppDispatch, useAppSelector } from '../../store/hooks';
import { updateRecipe } from '../../store/recipesSlice';
import { RecipeFormData } from './RecipeNew';
import { RecipeFormData } from './RecipeForm';
import useCsrfToken from '../../hooks/useCsrfToken';
import { useNavigate, useParams } from 'react-router-dom';
import RecipeForm from './RecipeForm';
Expand Down
26 changes: 22 additions & 4 deletions app/javascript/components/recipes/RecipeForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import React from 'react';
import { RecipeFormData } from './RecipeNew';
import { useAppSelector } from '../../store/hooks';
import { Ingredient } from '../../types/types';

export interface RecipeFormData {
name: string;
description: string;
ingredients_attributes: Ingredient[];
}

interface RecipeFormProps {
formData: RecipeFormData;
Expand All @@ -25,13 +31,26 @@ const RecipeForm: React.FC<RecipeFormProps> = ({ formData, setFormData, onSubmit
const deleteIngredient = (indexToDelete: number) => {
setFormData({
...formData,
ingredients_attributes: formData.ingredients_attributes.filter((_, index) => index !== indexToDelete)
ingredients_attributes: formData.ingredients_attributes
.map((ingredient, index) => {
if (index === indexToDelete) {
if (ingredient.id) {
return { ...ingredient, _destroy: true };
}
return null;
}
return ingredient;
})
.filter((ingredient): ingredient is Ingredient =>
ingredient !== null
)
});
};

const handleIngredientChange = (index: number, field: 'food_id' | 'measurement', value: string | number) => {
const newIngredients = [...formData.ingredients_attributes];
newIngredients[index] = {
id: newIngredients[index]?.id || undefined,
food_id: newIngredients[index]?.food_id || 0,
measurement: newIngredients[index]?.measurement || '',
[field]: value
Expand Down Expand Up @@ -66,7 +85,7 @@ const RecipeForm: React.FC<RecipeFormProps> = ({ formData, setFormData, onSubmit
<div className="mb-4">
<h3 className="mb-2">Ingredients</h3>
{formData.ingredients_attributes.map((ingredient, index) => (
<div key={index} className="flex gap-4 mb-2">
<div key={index} className={`flex gap-4 mb-2 ${ingredient._destroy && 'hidden'}`}>
<select
value={ingredient.food_id || ''}
onChange={(e) => handleIngredientChange(index, 'food_id', parseInt(e.target.value))}
Expand All @@ -77,7 +96,6 @@ const RecipeForm: React.FC<RecipeFormProps> = ({ formData, setFormData, onSubmit
<option
key={food.id}
value={food.id}
selected={food.id === ingredient.food_id}
>
{food.name}
</option>
Expand Down
8 changes: 1 addition & 7 deletions app/javascript/components/recipes/RecipeNew.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ import { fetchFoods } from '../../store/foodsSlice';
import useCsrfToken from '../../hooks/useCsrfToken';
import { useNavigate } from 'react-router-dom';
import RecipeForm from './RecipeForm';
import { Ingredient } from '../../types/types';

export interface RecipeFormData {
name: string;
description: string;
ingredients_attributes: Ingredient[];
}
import { RecipeFormData } from './RecipeForm';

const NewRecipeForm: React.FC = () => {
const dispatch = useAppDispatch();
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export interface Ingredient {
id?: number;
food_id?: number;
food_id: number;
food_name?: string;
measurement: string;
_destroy?: boolean;
}

export interface Recipe {
Expand Down
2 changes: 1 addition & 1 deletion app/models/recipe.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Recipe < ApplicationRecord
has_many :ingredients

accepts_nested_attributes_for :ingredients
accepts_nested_attributes_for :ingredients, allow_destroy: true
end

0 comments on commit c66d580

Please sign in to comment.