All files / src/components/MenuFeedback index.tsx

99.04% Statements 104/105
66.66% Branches 4/6
100% Functions 1/1
99.04% Lines 104/105

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  1x     1x     1x     1x 1x 1x 1x 1x     1x 1x     1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                         1x 1x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x   58x 58x 58x 58x   58x 58x 58x 58x 58x 58x   58x 58x     58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x   58x 58x 58x 58x 58x 58x 58x   58x 58x 116x 116x   116x 96x 96x 116x 58x 58x 58x 58x 58x   58x 58x 58x 58x 58x 58x 58x 58x   58x 1x   1x  
// Libs
import { forwardRef, useRef } from 'react';
 
// Types
import { BaseVariant } from '@/types';
 
// Utils
import { combineClasses } from '@shared/utils';
 
// Components
import { ImageFallback } from '@/components/common';
import { CloseIcon } from '@/components/icons/reacts';
import { FeedbackHeader } from '@/components/FeedbackHeader';
import { FeedbackSection } from '@/components/FeedbackSection';
import { FeedbackSubmit } from '@/components/FeedbackSubmit';
 
// Constants
import { FALLBACK_SRC } from '@shared/constants';
import { MENU_FEEDBACK_LABELS } from '@/constants/dailyFeedback';
 
// Styles
import './styles/menu-feedback.css';
 
const variantTheme = {
  [BaseVariant.PRIMARY]: {
    root: 'menu-feedback-root',
    close: 'menu-feedback-close',
    mealPanel: 'menu-feedback-meal-panel',
    imageWrapper: 'menu-feedback-image-wrapper',
    image: 'menu-feedback-image',
    details: 'menu-feedback-details',
    rank: 'menu-feedback-rank',
    mealList: 'menu-feedback-meal-list',
    content: 'menu-feedback-content',
  },
};
 
export interface MenuFeedbackProps {
  name: string;
  avatar: string;
  date: string;
  mealImage: string;
  mealItems: string;
  comment: string;
  onCommentChange: (value: string) => void;
  isAnonymous?: boolean;
  onToggleAnonymous?: () => void;
  onEmojiClick?: (emoji: string) => void;
  onSubmit?: () => void;
  onClose?: () => void;
  isSubmitDisabled?: boolean;
  hideCloseButton?: boolean;
  className?: string;
  variant?: BaseVariant;
}
 
export const MenuFeedback = forwardRef<HTMLTextAreaElement, MenuFeedbackProps>(
  (
    {
      name,
      avatar,
      date,
      mealImage,
      mealItems,
      comment,
      onCommentChange,
      isAnonymous = false,
      onToggleAnonymous,
      onEmojiClick,
      onSubmit,
      onClose,
      isSubmitDisabled = false,
      hideCloseButton = false,
      className = '',
      variant = BaseVariant.PRIMARY,
    },
    ref,
  ) => {
    const theme = variantTheme[variant];
    const textareaRef = useRef<HTMLTextAreaElement>(null);
    const isSendDisabled = isSubmitDisabled || !comment.trim();
 
    return (
      <div
        data-variant={variant}
        className={combineClasses(theme.root, className)}
      >
        {!hideCloseButton && (
          <button
            type="button"
            aria-label={MENU_FEEDBACK_LABELS.close}
            onClick={onClose}
            className={theme.close}
          >
            <CloseIcon width={20} height={20} />
          </button>
        )}
 
        <div className={theme.mealPanel}>
          <div className={theme.imageWrapper}>
            <ImageFallback
              src={mealImage}
              alt={mealItems ?? name}
              width={220}
              height={330}
              className={theme.image}
              fallbackSrc={FALLBACK_SRC.DEFAULT}
            />
          </div>
        </div>
 
        <div className={theme.content}>
          <FeedbackHeader
            name={name}
            avatar={avatar}
            date={date}
            isAnonymous={isAnonymous}
          />
 
          <FeedbackSection
            ref={(el) => {
              textareaRef.current = el;
              if (typeof ref === 'function') {
                ref(el);
              } else if (ref) {
                ref.current = el;
              }
            }}
            comment={comment}
            onCommentChange={onCommentChange}
            isAnonymous={isAnonymous}
            onToggleAnonymous={onToggleAnonymous}
          />
 
          <FeedbackSubmit
            textareaRef={textareaRef as React.RefObject<HTMLTextAreaElement>}
            onEmojiSelect={onEmojiClick}
            onSubmit={onSubmit}
            isSendDisabled={isSendDisabled}
          />
        </div>
      </div>
    );
  },
);
 
MenuFeedback.displayName = 'MenuFeedback';