Duffer Derek

Current Path : /var/www/api-mk-planner.bitkit.dk/httpdocs/Frontend/src/utils/
Upload File :
Current File : /var/www/api-mk-planner.bitkit.dk/httpdocs/Frontend/src/utils/commonFunctions.js

import { postWithToken } from "../services/apiRequest";

// Clean filter data to remove any non-serializable properties (React/DOM references)
const cleanFilterValue = (value) => {
  if (value === null || value === undefined) {
    return null;
  }
  
  // If it's a Date object, convert to ISO string
  if (value instanceof Date) {
    return value.toISOString();
  }
  
  // If it's a primitive value, return as is
  if (typeof value !== 'object') {
    return value;
  }
  
  // If it's an array, clean each item
  if (Array.isArray(value)) {
    return value.map(cleanFilterValue);
  }
  
  // If it's an object, extract only serializable properties
  const cleaned = {};
  const allowedKeys = ['id', 'value', 'name', 'label', 'color', 'item_id'];
  
  for (const key of allowedKeys) {
    if (key in value && value[key] !== undefined) {
      const val = value[key];
      // Only include primitive values or recursively clean nested objects
      if (val === null || typeof val !== 'object' || Array.isArray(val)) {
        cleaned[key] = val;
      } else if (typeof val === 'object') {
        cleaned[key] = cleanFilterValue(val);
      }
    }
  }
  
  // If we didn't extract any properties and it's not a Date, return null to avoid empty objects
  if (Object.keys(cleaned).length === 0) {
    return null;
  }
  
  return cleaned;
};

// Clean filters object to ensure all values are serializable
const cleanFilters = (filters) => {
  if (!filters || typeof filters !== 'object') {
    return filters;
  }
  
  const cleaned = {};
  for (const key in filters) {
    if (filters.hasOwnProperty(key)) {
      const value = filters[key];
      // Skip functions and non-serializable values
      if (typeof value === 'function') {
        continue;
      }
      
      // Handle Date objects directly for date fields
      if ((key === 'startDate' || key === 'endDate' || key === 'date') && value instanceof Date) {
        cleaned[key] = value.toISOString();
      } else {
        cleaned[key] = cleanFilterValue(value);
      }
    }
  }
  return cleaned;
};

export const getDashboardData = async (token, filters) => {
  try {
    // Clean filters before sending to API to avoid circular reference errors
    const cleanedFilters = cleanFilters(filters);
    
    const req = {
      filters: cleanedFilters,
    };
    const response = await postWithToken("/api/get-dashboard", {
      access_token: token,
      data: req,
    });
    return response;
    // Assuming postWithToken returns the actual data you need
  } catch (error) {
    console.error("Error fetching dashboard data:", error);
  }
};
export const updateOrAddTasks = async (tasksDetails, token) => {
  try {
    const response = await postWithToken("/api/add-or-update-tasks", {
      access_token: token,
      data: tasksDetails,
    });
    return response;
  } catch (error) {
    console.error("Error fetching dashboard data:", error);
  }
};

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists