0

I have an HTML checkmark group that looks like this:

Show:
[ ] New
[ ] Regulars
[ ] Veterans

In my javascript code, I use Axios to connect to a .Net API like this:

const fetchGamerTags = async (showNew, showRegulars, showVeterans) => {

    let apiAddress = "";
    
    if (showNew) {
        apiAddress = "api/tags/GetNew/"
    } else if (showRegulars) {
        apiAddress = "api/tags/GetRegulars/"
    } else if (showVeterans) {
        apiAddress = "api/tags/GetVeterans/"
    } else {
        apiAddress = "api/tags/GetAll/"
    }
    
  const result = await axios(apiAddress, {
    params: {
      isDeleted: false
    }
  });
  

My problem is, I don't know how to handle if a user selects multiple choices...like New and Veterans. Is there a way to handle that in my javascript?

Thanks!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185
  • Does this answer your question? [Getting all selected checkboxes in an array](https://stackoverflow.com/questions/590018/getting-all-selected-checkboxes-in-an-array) – lux Nov 19 '21 at 20:20
  • @lux not really because how would I add that to my `if else` block? – SkyeBoniwell Nov 19 '21 at 20:23

1 Answers1

1

use axios.all to get multiple requests

const fetchGamerTags = async (showNew, showRegulars, showVeterans) => {

    let getNew = axios.get("api/tags/GetNew/", {params: {isDeleted: false}});
    let getRegulars = axios.get("api/tags/GetRegulars/", {params: {isDeleted: false}});
    let getVeterans = axios.get("api/tags/GetVeterans/", {params: {isDeleted: false}});
    
    if(showNew && showRegulars && showVeterans) {
        const result = await axios.get(apiAddress, {
        params: {
          isDeleted: false
        }
      });
    } else {
       
    list = [];
    
    if (showNew) {
        list.push = getNew;
    }
    if (showRegulars) {
        list.push = getRegulars;
    } 
    if (showVeterans) {
        list.push = getVeterans;
    }

  const result = await axios.all(list, {
    params: {
      isDeleted: false
    }
  });
  }
  return result;
  }
Joshua Craven
  • 4,407
  • 1
  • 31
  • 39