JSFiddle - React, Tailwind, and code Playground

by amrendra kumar

JavaScript

const parseJsonSafely = (query) => {

  let parsedData
  try {
    if (query) {
      parsedData = JSON.parse(query)

    }
  } catch (error) {
    console.error("parse json", error);
  }

  return parsedData
}

const getQueryType = (queryString) => {
  let defaultQueryType = 'basicQuery'
  if (!queryString) return defaultQueryType

  const andRegex = /\"\$and\":/g
  const orRegex = /\"\$or\":/g

  let ands = queryString.match(andRegex) || []
  let ors = queryString.match(orRegex) || []

  if (ands.length > 1 || ors.length) {
    return 'advanceQuery'
  }

  return defaultQueryType
}

  
const filterAndQuery = (andQuery=[], typeValue) => {
    try {
        let filteredQuery = andQuery.filter((query) => Object.keys(query)[0] !== typeValue )
        return filteredQuery
    } catch (error) {
        throw error
    }
}



const updateQueryObject = (tableFilterQueryObject, queryObject, typeValue) => {
    try {
        if(typeValue === '_content_type_uid') {
            let {_content_type_uid, ...rest} = queryObject
            const filteredQuery = filterAndQuery(rest.$and, typeValue)
            const updatedQueryObject = {_content_type_uid: tableFilterQueryObject._content_type_uid, $and: filteredQuery }
            return updatedQueryObject
        }
    
        const filteredQuery = filterAndQuery(queryObject.$and, typeValue)
        const updatedQueryObject = { $and: [...filteredQuery, ...tableFilterQueryObject.$and] }
        return updatedQueryObject
    } catch (error) {
        throw error
    }
}

const getFinalQueryString = (tableFilterQueryObject, searchText='') => {
    let searchedData = {}
    searchedData.queryObject = tableFilterQueryObject
    if(searchText) {
        searchedData.searchText = searchText
    }
    console.log('searchdata', searchedData)
    return JSON.stringify(searchedData)
}

let $and= [
    { _content_type_uid: { $eq: "allfieldtest" } },
    { locale: { $eq: "en-us" } }
    //{ _publish_environment: { $in:...