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

0% Statements 0/29
0% Branches 0/16
0% Functions 0/12
0% Lines 0/25

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                                                                                                                                                                                                                                                                                                                                                                                                                                 
import {
  ActionIcon,
  Badge,
  Button,
  Group,
  Paper,
  Stack,
  Text,
  Textarea,
  ThemeIcon,
  Title,
} from '@mantine/core';
import {
  IconArrowDown,
  IconArrowLeft,
  IconArrowUp,
  IconChefHat,
  IconPlus,
  IconTrash,
  IconWand,
} from '@tabler/icons-react';
import { useTranslations } from 'next-intl';
import { useRecipeFormContext } from '../../FormContext';
import { useFormError } from '../../hooks/useFormError';
import type { StepsSectionProps } from './types';
 
const StepsSection = ({
  onAdd,
  onBack,
  onSubmit,
  isSubmitting,
  submitLabel,
}: Readonly<StepsSectionProps>) => {
  const translate = useTranslations('recipeComposer.sections.steps');
  const form = useRecipeFormContext();
  const { values, setFieldValue } = form;
  const { getFieldError, revalidateOnChange } = useFormError(form);
 
  const removeStep = (idx: number) => {
    const next = values.preparationSteps.filter((_, i) => i !== idx);
    const reordered = next.map((s, i) => ({ ...s, order: i + 1 }));
    setFieldValue('preparationSteps', reordered);
  };
 
  const moveStep = (idx: number, direction: 'up' | 'down') => {
    const next = [...values.preparationSteps];
    const target = direction === 'up' ? idx - 1 : idx + 1;
    if (target < 0 || target >= next.length) return;
    [next[idx], next[target]] = [next[target], next[idx]];
    const reordered = next.map((s, i) => ({ ...s, order: i + 1 }));
    setFieldValue('preparationSteps', reordered);
  };
 
  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: 'orange', to: 'red' }}
            >
              <IconChefHat size={18} />
            </ThemeIcon>
            <Title order={3}>{translate('title')}</Title>
          </Group>
          <Badge
            variant="light"
            color={values.preparationSteps.length ? 'green' : 'red'}
          >
            {translate('stepsCount', { count: values.preparationSteps.length })}
          </Badge>
        </Group>
 
        <Button
          variant="light"
          leftSection={<IconPlus size={16} />}
          onClick={onAdd}
        >
          {translate('addStep')}
        </Button>
 
        <Stack gap="xs">
          {values.preparationSteps.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">
                  <IconChefHat size={24} />
                </ThemeIcon>
                <Text c="dimmed" ta="center" size="sm">
                  {translate('emptyState')}
                </Text>
                <Button
                  variant="light"
                  size="xs"
                  onClick={onAdd}
                  leftSection={<IconPlus size={14} />}
                >
                  {translate('addFirst')}
                </Button>
              </Stack>
            </Paper>
          )}
 
          {values.preparationSteps
            .slice()
            .sort((a, b) => a.order - b.order)
            .map((step, idx) => (
              <Group key={step.localId ?? idx} align="flex-start" wrap="nowrap">
                <ThemeIcon
                  size={24}
                  radius="50%"
                  variant="light"
                  color="gray"
                  mt={4}
                >
                  <Text size="xs" fw={700}>
                    {idx + 1}
                  </Text>
                </ThemeIcon>
                <Textarea
                  placeholder={translate('stepPlaceholder', { index: idx + 1 })}
                  autosize
                  minRows={2}
                  value={step.description}
                  onChange={(e) => {
                    const path = `preparationSteps[${idx}].description`;
                    setFieldValue(path, e.target.value);
                    revalidateOnChange(path);
                  }}
                  error={getFieldError(`preparationSteps[${idx}].description`)}
                  style={{ flex: 1 }}
                />
                <Stack gap={4}>
                  <ActionIcon
                    variant="subtle"
                    size="sm"
                    disabled={idx === 0}
                    onClick={() => moveStep(idx, 'up')}
                  >
                    <IconArrowUp size={14} />
                  </ActionIcon>
                  <ActionIcon
                    variant="subtle"
                    size="sm"
                    disabled={idx === values.preparationSteps.length - 1}
                    onClick={() => moveStep(idx, 'down')}
                  >
                    <IconArrowDown size={14} />
                  </ActionIcon>
                  <ActionIcon
                    variant="subtle"
                    color="red"
                    size="sm"
                    onClick={() => removeStep(idx)}
                  >
                    <IconTrash size={14} />
                  </ActionIcon>
                </Stack>
              </Group>
            ))}
 
          {values.preparationSteps.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
            color="dark"
            loading={isSubmitting}
            onClick={onSubmit}
            leftSection={<IconWand size={16} />}
          >
            {submitLabel ?? translate('publish')}
          </Button>
        </Group>
      </Stack>
    </Paper>
  );
};
 
export default StepsSection;