All files / src/components/Recipe/Create/sections/IngredientsSection IngredientsSection.tsx

0% Statements 0/23
0% Branches 0/18
0% Functions 0/10
0% Lines 0/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
import {
  ActionIcon,
  Badge,
  Button,
  Group,
  Paper,
  Select,
  Stack,
  Switch,
  Text,
  TextInput,
  ThemeIcon,
  Title,
} from '@mantine/core';
import {
  IconArrowLeft,
  IconChefHat,
  IconPlus,
  IconToolsKitchen2,
  IconTrash,
} from '@tabler/icons-react';
import { useTranslations } from 'next-intl';
import { useRecipeFormContext } from '../../FormContext';
import { useFormError } from '../../hooks/useFormError';
import type { IngredientsSectionProps } from './types';
 
const IngredientsSection = ({
  unitOptions,
  onAdd,
  onBack,
  onNext,
}: Readonly<IngredientsSectionProps>) => {
  const translate = useTranslations('recipeComposer.sections.ingredients');
  const form = useRecipeFormContext();
  const { values, setFieldValue } = form;
  const { getFieldError, revalidateOnChange } = useFormError(form);
 
  const removeIngredient = (idx: number) => {
    const next = values.ingredients.filter((_, i) => i !== idx);
    setFieldValue('ingredients', next);
  };
 
  return (
    <Paper p={{ base: 'md', sm: 'xl' }} radius="lg" withBorder shadow="sm">
      <Stack gap="lg">
        <Group justify="space-between" align="baseline">
          <Group gap="xs">
            <ThemeIcon
              size={32}
              radius="md"
              variant="gradient"
              gradient={{ from: 'teal', to: 'lime' }}
            >
              <IconToolsKitchen2 size={18} />
            </ThemeIcon>
            <Title order={3}>{translate('title')}</Title>
          </Group>
          <Badge
            variant="light"
            color={values.ingredients.length ? 'green' : 'red'}
          >
            {translate('itemsCount', { count: values.ingredients.length })}
          </Badge>
        </Group>
 
        <Button
          variant="light"
          leftSection={<IconPlus size={16} />}
          onClick={onAdd}
        >
          {translate('addIngredient')}
        </Button>
 
        <Stack gap="xs">
          {values.ingredients.map((ing, idx) => (
            <Paper
              key={ing.localId}
              withBorder
              radius="md"
              p="sm"
              style={{
                borderLeft: ing.name.trim()
                  ? '3px solid var(--mantine-color-teal-5)'
                  : '3px solid var(--mantine-color-gray-3)',
                transition: 'border-color 0.2s ease',
                opacity: ing.isOptional ? 0.75 : 1,
              }}
            >
              <Stack gap="xs">
                <Group gap="xs" align="flex-start" wrap="nowrap">
                  <TextInput
                    placeholder={translate('itemName')}
                    value={ing.name}
                    onChange={(e) => {
                      const path = `ingredients[${idx}].name`;
                      setFieldValue(path, e.target.value);
                      revalidateOnChange(path);
                    }}
                    error={getFieldError(`ingredients[${idx}].name`)}
                    style={{ flex: 2 }}
                    size="sm"
                  />
                  <TextInput
                    placeholder={translate('qty')}
                    value={ing.quantity}
                    onChange={(e) => {
                      const path = `ingredients[${idx}].quantity`;
                      setFieldValue(path, e.target.value);
                      revalidateOnChange(path);
                    }}
                    error={getFieldError(`ingredients[${idx}].quantity`)}
                    style={{ width: 70 }}
                    size="sm"
                  />
                  <Select
                    placeholder={translate('unit')}
                    data={unitOptions}
                    value={ing.unit || null}
                    onChange={(val) => {
                      const path = `ingredients[${idx}].unit`;
                      setFieldValue(path, val ?? '');
                      revalidateOnChange(path);
                    }}
                    error={getFieldError(`ingredients[${idx}].unit`)}
                    style={{ width: 120 }}
                    size="sm"
                    searchable
                    allowDeselect={false}
                  />
                  <ActionIcon
                    color="red"
                    variant="subtle"
                    onClick={() => removeIngredient(idx)}
                    mt={4}
                  >
                    <IconTrash size={16} />
                  </ActionIcon>
                </Group>
                <Group gap="xs" align="center">
                  <Switch
                    label={translate('optional')}
                    size="xs"
                    checked={ing.isOptional ?? false}
                    onChange={(e) => {
                      setFieldValue(
                        `ingredients[${idx}].isOptional`,
                        e.currentTarget.checked,
                      );
                    }}
                  />
                  <TextInput
                    placeholder={translate('notePlaceholder')}
                    value={ing.note ?? ''}
                    onChange={(e) => {
                      setFieldValue(`ingredients[${idx}].note`, e.target.value);
                    }}
                    style={{ flex: 1 }}
                    size="xs"
                    variant="unstyled"
                  />
                </Group>
              </Stack>
            </Paper>
          ))}
 
          {values.ingredients.length === 0 && (
            <Paper
              withBorder
              radius="md"
              p="xl"
              style={{ borderStyle: 'dashed' }}
            >
              <Stack align="center" gap="sm">
                <ThemeIcon size={48} radius="xl" variant="light" color="gray">
                  <IconToolsKitchen2 size={24} />
                </ThemeIcon>
                <Text c="dimmed" ta="center" size="sm">
                  {translate('noIngredients')}
                </Text>
                <Button
                  variant="light"
                  size="xs"
                  onClick={onAdd}
                  leftSection={<IconPlus size={14} />}
                >
                  {translate('addFirst')}
                </Button>
              </Stack>
            </Paper>
          )}
 
          {values.ingredients.length > 0 && (
            <Button
              variant="subtle"
              fullWidth
              style={{
                borderTop: '1px dashed var(--mantine-color-gray-3)',
              }}
              onClick={onAdd}
              mt={4}
            >
              <IconPlus size={16} />
            </Button>
          )}
        </Stack>
 
        <Group justify="space-between" mt="xs">
          <Button
            variant="subtle"
            color="gray"
            onClick={onBack}
            leftSection={<IconArrowLeft size={16} />}
          >
            {translate('back')}
          </Button>
          <Button
            variant="light"
            onClick={onNext}
            rightSection={<IconChefHat size={16} />}
          >
            {translate('next')}
          </Button>
        </Group>
      </Stack>
    </Paper>
  );
};
 
export default IngredientsSection;