All files / src/components/ScheduleUpdateMeal index.tsx

100% Statements 52/52
100% Branches 15/15
100% Functions 3/3
100% Lines 52/52

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  1x     1x     1x   1x 13x 13x 25x   25x 34x   34x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x   22x 22x 22x 22x 22x 16x 16x 28x 16x 16x 22x 25x 25x   13x 13x 13x 13x   13x 13x 1x 13x 12x 12x 13x   13x 13x 13x   13x 13x  
// Libs
import { useEffect } from 'react';
 
// Constants
import { ENV } from '@/constants';
 
// Utils
import { isExpiredDay } from '@/utils';
 
export const ScheduleUpdateMeal = () => {
  useEffect(() => {
    const updateMealCard = () => {
      const mealCards = document.querySelectorAll<HTMLElement>('.day-card');
 
      mealCards.forEach((card) => {
        const cardDateStr = card.dataset.date || '';
 
        if (isExpiredDay(cardDateStr, ENV.EXPIRE_HOUR, ENV.EXPIRE_MINUTE)) {
          const voteSection = card.querySelector<HTMLElement>(
            '[data-section="vote"]',
          );
          const dateSection = card.querySelector<HTMLElement>(
            '[data-section="date"]',
          );
          const statsSection = card.querySelector<HTMLElement>(
            '[data-section="stats"]',
          );
          const image = card.querySelector<HTMLImageElement>('img');
          const voteWrapper = card.querySelector<HTMLElement>(
            'vote-reaction-wrapper',
          );
 
          if (image) image.style.opacity = '0.4';
          if (voteSection) voteSection.style.opacity = '0.4';
          if (dateSection) dateSection.style.opacity = '0.4';
          if (statsSection) statsSection.style.opacity = '0.4';
          if (voteSection) {
            const buttons = voteSection.querySelectorAll('button');
            buttons.forEach((btn) => {
              btn.disabled = true;
            });
          }
        }
      });
    };
 
    const scheduleUpdates = () => {
      const now = new Date();
      const expire = new Date();
      expire.setHours(ENV.EXPIRE_HOUR, ENV.EXPIRE_MINUTE, 0, 0);
 
      const timeUntilExpire = expire.getTime() - now.getTime();
      if (timeUntilExpire > 0) {
        setTimeout(updateMealCard, timeUntilExpire);
      } else {
        updateMealCard();
      }
    };
 
    updateMealCard();
    scheduleUpdates();
  }, []);
 
  return null;
};