task-managment / src / lib / utils.ts
utils.ts
Raw
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
import moment from "moment";
import 'moment/locale/es';


export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}


export function formatPricePesos(number:number | string) {
  return number.toLocaleString('es-DO', {
    style: 'currency',
    currency: 'DOP',
    minimumFractionDigits: 2,
  });
}



export function getRangeValue(range: string | undefined): { start: Date, end: Date } | undefined {
  const now = new Date();

  switch (range) {
    case 'today':
      const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate());
      const todayEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999);
      return { start: todayStart, end: todayEnd };
    
    case 'thisMonth':
      const monthStart = new Date(now.getFullYear(), now.getMonth(), 1);
      const monthEnd = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59, 999);
      return { start: monthStart, end: monthEnd };
    
    case 'last3Months':
      const last3MonthsStart = new Date(now.getFullYear(), now.getMonth() - 2, 1);
      const last3MonthsEnd = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59, 999);
      return { start: last3MonthsStart, end: last3MonthsEnd };
    
    case 'thisYear':
      const yearStart = new Date(now.getFullYear(), 0, 1);
      const yearEnd = new Date(now.getFullYear(), 11, 31, 23, 59, 59, 999);
      return { start: yearStart, end: yearEnd };
    
    default:
      return undefined;
  }
}


export const getStatusColor = (statuss:string | undefined) => {
  switch (statuss) {
    case "no iniciada":
      return "bg-yellow-500";
    case "en proceso":
      return "bg-blue-500";
    case "completada":
      return "bg-green-300";
    case "revisada":
      return "bg-green-700";
    case "cancelada":
      return "bg-red-600";
    case "reabierta":
      return "bg-blue-800";
    case "no completada":
      return "bg-purple-500";
    default:
      return "bg-yellow-700";
  }
};


export const getPriorityColor = (priority: string) => {

  switch (priority) {
    case "baja":
      return "bg-blue-700 "; // Color verde para "completada"
    case "media":
      return "bg-yellow-500 "; // Color amarillo para "en proceso"
    case "alta":
      return "bg-red-600 "; // Color rojo para "no iniciada"
    default:
      return "bg-gray-500"; // Color gris por defecto para cualquier otro estado
  }
};



export const formatDate = (date:Date | string | number | undefined,format:string='YYYY-MM-DD') => {
   if(!date) {
    return moment(new Date()).format(format);

   }
    moment.locale("es");
    return moment(date).format(format);

}

export const removeTimeFromDate = (date:Date | string | number | undefined) => {
  if(!date) {
   return moment(new Date()).startOf('day').toDate();
  }
   moment.locale("es");
   return moment(date).startOf('day').toDate()

}

export function formatDateSpecial(date:Date | string) {
  moment.locale("es");
  const now = moment();
  const inputDate = moment(date);

  if (inputDate.isSame(now, 'day')) {
    return 'Hoy';
  } else if (inputDate.isSame(now.add(1, 'day'), 'day')) {
    return 'Mañana';
  } else if (inputDate.isSame(now, 'week')) {
    return inputDate.format('dddd'); // Día de la semana
  } else {
    return inputDate.format('MMM D'); // Fecha en formato "MMM D"
  }
}





export const formatDateToLocal=(date:Date | string )=>{
  const actualDate = new Date(date)
  actualDate.setMinutes(actualDate.getMinutes() + actualDate.getTimezoneOffset()) 
  return moment(actualDate).format('MMMM Do YYYY, h:mm: a')
}

export const removeLocalOffsetFromDate = (date: Date | string): Date => {
  // Convertir la entrada a un objeto Date
  const actualDate = new Date(date);

  // Remover el offset de la zona horaria
  const utcTime = actualDate.getTime() + actualDate.getTimezoneOffset() * 60000;

  // Crear una nueva fecha en UTC
  const utcDate = new Date(utcTime);

  return utcDate;
};

export const addLocalOffsetFromDate = (date: Date | string) => {
  const actualDate = new Date(date);
  // Remover el offset de la zona horaria
  actualDate.setMinutes(actualDate.getMinutes() + actualDate.getTimezoneOffset());

  // Formatear la fecha en el formato deseado
  return moment(actualDate).toDate();
};



type ChangedValues<T> = {
  oldValues: Partial<T>;
  newValues: Partial<T>;
};

export function getChangedValues<T>(oldObj: T, newObj: Partial<T>): ChangedValues<T> {
  const oldValues: Partial<T> = {};
  const newValues: Partial<T> = {};

  for (const key in oldObj) {
    if (newObj.hasOwnProperty(key)) {
      if (oldObj[key] !== newObj[key]) {
        oldValues[key] = oldObj[key];
        newValues[key] = newObj[key]!;
      }
    }
  }

  
  return { oldValues, newValues };
}