-3

I'm refactoring some code in a react file, and I have two functions which almost does the same thing... but one return a function and the other one execute some code.

I'm not really good for now with ES6 and arrow function. And I don't understand how to refactor this.

 switchEventSelectedSchedule = cb => option => {
    this.setState(
      // Mutate the state
      () => ({
        eventSelected: option.id,
        isLoading: true
      }),
      // Callback to fire when the state has been mutated with the new event id
      async () => {
        await this.longPollingAllMatches();
        this.setState(() => ({
          isLoading: false
        }));
        const { currentRoundId, rounds } = this.state;
        cb(rounds, currentRoundId);
      }
    );
  };

  switchEventSelectedRoundTable = option => {
    this.setState(
      // Mutate the state
      () => ({
        eventSelected: option.id,
        isLoading: true
      }),
      // Callback to fire when the state has been mutated with the new event id
      async () => {
        await this.longPollingAllMatches();
        this.setState(() => ({
          isLoading: false
        }));
      }
    );
  };

in one case (imagine if(schedule)) I need to return the cb function else I must just execute the rest of the code.

Sorry seems dumb but I think I misunderstood something in the ES6 syntax to achieve this....

Many thx !

VaX
  • 115
  • 2
  • 10
  • You can try using Babel to convert your ES6 to ES5 here: https://babeljs.io/repl – Elder Sep 05 '19 at 21:12
  • I don't think people can help you without making major guesses because you don't explain what the current code is supposed to accomplish or show the calling context or explain what your goal is for a refactor. I personally despise code structures such as `switchEventSelectedSchedule = cb => option => {}` as it's just plain hard to read and we need to see the calling context to see how it's being used. – jfriend00 Sep 05 '19 at 21:12
  • @jfriend00 Did I miss anything in my answer? Not sure why the question is getting "unclear" close votes. – Bergi Sep 05 '19 at 21:20
  • @Bergi - Well, the question is not clear to several of us and I still don't see how the question leads to your answer. So, even when someone who thinks they understand the question (you) shows me an answer, I still don't understand the question. To me, that's an unclear question. "unclear" is in the eye of the beholder so kudos to you for understanding it I guess. Still unclear to me. – jfriend00 Sep 05 '19 at 21:25
  • @jfriend00 I understood that the OP wants to refactor this working code, did recognise the code duplication, but doesn't know how to resolve it. But sure, clarity is a subjective measure :-) – Bergi Sep 05 '19 at 21:30
  • @jfriend00 I undestand it can be unclear :) sorry for that and for my english by the way !!! :D but Bergi understood it well as he says this code worked but with a lot of duplication ;) one point for both guys :D ! peace !! and thx for you time helping guys like me ! – VaX Sep 05 '19 at 21:34

1 Answers1

1

Simply have switchEventSelectedRoundTable call the other function with a callback that does nothing. Since switchEventSelectedSchedule is curried, this is simple:

switchEventSelectedRoundTable = switchEventSelectedSchedule((rounds, currentRoundId) => {});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375