Skip to main content

Javascript filtering several options at the time

I am using react to create some page that requires a filtering menu. I have a dogs array which contain every dog information, and I am trying to filtering by its temperaments. every dog has at least three temperaments, and I have been trying to check if they have at least one of the temperaments submitted in the filter menu. But nothing seems to work.

This the controling component.

import {useEffect, useState} from "react";
import {useDispatch, useSelector} from "react-redux";
import {filteringDogs, getTemps, onSearchBreed, orderAscendingDogs, orderDescendingDogs} from "../store";

import styles from "./styles/SideBarComp.module.css";
import {useForm} from "../hooks";

export const SideBarComp = () => {
  const dispatch = useDispatch()

  const { dogs } = useSelector( state => state.dogs )

  const { temps } = useSelector( state => state.temps )
  useEffect(() => {
    dispatch(getTemps())
    
  }, [])

  

  const { breed, onInputChange } = useForm({
    breed: '',

  }) 

  const [selectedTemperaments, setSelectedTemperaments] = useState([]);

  const handleTempCheckboxChange = (tempName) => {
    // Update the selectedTemperaments array based on checkbox changes
    if (selectedTemperaments.includes(tempName)) {
      setSelectedTemperaments(
        selectedTemperaments.filter((temp) => temp !== tempName)
      );
    } else {
      setSelectedTemperaments([tempName, ...selectedTemperaments]);
    }

    dispatch(filteringDogs(selectedTemperaments))
  };

  // console.log(selectedTemperaments);

  const onSubmit = (event) => {
    event.preventDefault()
    dispatch(onSearchBreed({ breed }));
  }

  const onAscendingClick = (dogs) => { 
    dispatch(orderAscendingDogs(dogs));
    
  };

  const onDescendingClick = (dogs) => {
    dispatch(orderDescendingDogs(dogs)); 
    
  };


  

  return (
    <main className={styles.sideBar}>
      <form onSubmit={onSubmit} className={styles.searchSection}>
        <input
          className={styles.searchBar}
          type="text"
          placeholder="Golden retriever..."
          name="breed"
          value={breed}
          onChange={onInputChange}
        />
        <button type="submit" className={styles.searchButton}>
          Search
        </button>
        <section className={styles.filterSection}>
          <div className={styles.tempFilter}>
            <h3>Filter by temperaments</h3>
            <div className={styles.inputs}>
              {temps.map((item) => (
                <div key={item.id} className={styles.singleInput}>
                  <input
                    id={item.name}
                    name={item.name}
                    type="checkbox"
                    onChange={() => handleTempCheckboxChange(item.name)}
                  />
                  <label htmlFor={item.name}>{item.name}</label>
                </div>
              ))}
            </div>
          </div>
        </section>
      </form>
      <div className={styles.orderFilter}>
        <h3>Order</h3>
        <div className={styles.orderButtonsArea}>
          <button
            onClick={() => onAscendingClick(dogs)}
            className={styles.orderButtons}
          >
            A - Z
          </button>
          <button
            onClick={() => onDescendingClick(dogs)}
            className={styles.orderButtons}
          >
            Z - A
          </button>
        </div>
      </div>
    </main>
  );
};

Here it is my store and reducer

import { createSlice } from "@reduxjs/toolkit";

export const dogSlice = createSlice({
  name: "dogs",
  initialState: {
    dogs: [],
    loading: false,
    currentPage: 1,
    itemsPerPage: 8,
    totalPages: 0,
  },
  reducers: {
    startLoading: (state /* action */) => {
      state.loading = true;
    },
    setDogs: (state, action) => {
      state.dogs = action.payload;
      state.totalPages = Math.ceil(action.payload.length / 8);
      state.loading = false;
    },
    setCurrentPage: (state, action) => {
      state.currentPage = action.payload;
    },
    findbyDogBreed: (state, action) => {
      state.dogs = action.payload;
      state.loading = false;
    },
  },
});

// Action creators are generated for each case reducer function
export const {
  startLoading,
  setDogs,
  setCurrentPage,
  descendingOrder,
  ascendingOrder,
  findbyDogBreed,
} = dogSlice.actions;

import { setCurrentPage, setDogs, startLoading } from "./dogSlice";
import { dogIns } from "../../../api";

export const getDogs = (page = 1) => {
  return async (dispatch) => {
    dispatch(startLoading());

    const { data } = await dogIns.get("/dogs");

    dispatch(setDogs(data));
    dispatch(setCurrentPage(page));
  };
};

export const handlePages = (page = 1) => {
  return async (dispatch) => {
    dispatch(setCurrentPage(page));
  };
};

export const orderDescendingDogs = (dogs) => {
  return async (dispatch) => {
    dispatch(startLoading());

    const dogsSorted = [...dogs].sort((a, b) => {
      const nameA = a.name;
      const nameB = b.name;
      if (nameA > nameB) {
        return -1;
      }
      if (nameA < nameB) {
        return 1;
      }
      return 0;
    });
    dispatch(setDogs(dogsSorted));
  };
};

export const orderAscendingDogs = (dogs) => {
  return async (dispatch) => {
    dispatch(startLoading());

    const dogsSorted = [...dogs].sort((a, b) => {
      const nameA = a.name;
      const nameB = b.name;
      if (nameA < nameB) {
        return -1;
      }
      if (nameA > nameB) {
        return 1;
      }
      return 0;
    });
    dispatch(setDogs(dogsSorted));
  };
};

export const onSearchBreed = ({ breed }) => {
  return async (dispatch) => {
    const { data } = await dogIns.get("/dogs");

    const dogsMatching = data.filter((item) =>
      item.name.toLowerCase().startsWith(breed.toLowerCase())
    );

    dispatch(setDogs(dogsMatching));
  };
};

export const filteringDogs = (selectedTemperaments) => {
  return async (dispatch) => {
    // console.log(selectedTemperaments);
    const { data } = await dogIns.get("/dogs");

    const dogsMatching = data.filter((dog) =>
      selectedTemperaments.some((temp) => dog.temperaments.includes(temp))
    );
    
    dispatch(setDogs(dogsMatching));
  };
};

I already tried a for loop, maybe I didn't use it well, but didn't work. I have been using AI but the options haven't work either, and I just found options on youtube for filtering a single property at the time.

Via Active questions tagged javascript - Stack Overflow https://ift.tt/kdCn58I

Comments

Popular posts from this blog

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

The number of nodes and edges I calculated manually and by a method provided by networkx library dont match

I am trying to analyse a graph using networkx library in Python. I have implemented a function that returns the nodes and edges of that graph. After that I counted the number of nodes and edges by implementing the len() function for nodes and edges, which gave me 17 for both nodes and edges. However, when I used the method graph.number_of_nodes() of networkx library it gave me 53. My code: import pandas as pd import networkx as nx import matplotlib.pyplot as plt header_list = ["a","b","w"] E =pd.read_csv("aves-barn-swallow-contact-network.edges",sep=" ",header= None,names=header_list) # print(E.head()) # prints first few rows of the csv file G = nx.from_pandas_edgelist(E,"a","b",["w"]) nx.draw(G) plt.show() class GraphAnalyse: #finding nodes and edges def nodeEdge(self,Graph): nodes = Graph.nodes() edges = Graph.edges() ...