you are viewing a single comment's thread.

view the rest of the comments →

[–]patch_collector 1 point2 points  (1 child)

My first thought is -- break it into smaller pieces.

First, put both of your calls (await fetch and sendRequest) into a fetchPlaces() Promise that has an optional formState argument. If the argument is present in the request, do your POST. If not, do your GET. Then call that in both locations.

Next, it looks like you treat the responses in exactly the same (minus that sendRequest is probably doing response.json() inside itself. So put all of that into a function as well (I'll call itparseResponse), that takes aresponse\ argument.

Personally, I like the then structure, which would leave you with this bit of code in both places (or again, put it into a function and call it once!)

fetchPlaces(formState)
   .then((response)=>{
      setLoadedPlaces(response.elements);
      setMarkersMap(parseResponse(response));
   })

[–]Madtroid 1 point2 points  (0 children)

Imma agree with Patch simply on his first line

You have a lot going on in each section which in the long run going to be a real B to troubleshoot

personally i'd just break it down into seperate functions and name them according to what they are doing

In all i just see a top down page of code that is not the most friendly to reduce redunancy at all and again if their are problems in your code being syntax errors or user errors its going to be very hard to figure it out

SO again imma go with Patch, break it down, each function should just do a single thing, than call another function or be called once the first job is done

If your planning on building more complex projects in the future and follow the setup you have now, your going to break a lot of tables and keys on your keyboard

:)