training icon indicating copy to clipboard operation
training copied to clipboard

Issue on page /react/external_data.html [16.2. Writing Middleware]

Open avimishra18 opened this issue 3 years ago • 8 comments

Issue

ESLint Warning: import/no-anonymous-default-export in the code snippet of the topic 16.2. Writing Middleware

Link to Community Discussion. Assigning this issue to myself.

Incorrect Code Snippet

export default store => next => action => {
  const { request, type, ...rest } = action;

  if (!request) {
    return next(action);
  }

  next({ ...rest, type: `${type}_PENDING` });

  const actionPromise = fetch(`http://localhost:3001${request.path}`, {
    method: request.op,
    body: request.data && JSON.stringify(request.data)
  });

  actionPromise.then(response => {
    response.json().then(data => next({ data, type: `${type}_SUCCESS` }));
  });

  return actionPromise;
};

Corrected Code Snippet

const api = store => next => action => {
  const { request, type, ...rest } = action;

  if (!request) {
    return next(action);
  }

  next({ ...rest, type: `${type}_PENDING` });

  const actionPromise = fetch(`http://localhost:3001${request.path}`, {
    method: request.op,
    body: request.data && JSON.stringify(request.data)
  });

  actionPromise.then(response => {
    response.json().then(data => next({ data, type: `${type}_SUCCESS` }));
  });

  return actionPromise;
};

export default api;

avimishra18 avatar Mar 12 '22 07:03 avimishra18

Please submit a PR for this. Thank you!

stevepiercy avatar Mar 12 '22 07:03 stevepiercy

@stevepiercy, considering this & #590. Should I make two different PR for 2 different issues or a single one? (Like, which approach will be better)

avimishra18 avatar Mar 12 '22 07:03 avimishra18

Thanks for asking. For the Training repo, we like to make it easy to contribute, and not make busy work. I think for a few number of small changes, combining them into a single pull request is fine. Also because this is for a single training, it makes sense to get it all done in a single swoop.

stevepiercy avatar Mar 12 '22 07:03 stevepiercy

@stevepiercy LineFeed is LF right?

avimishra18 avatar Mar 12 '22 08:03 avimishra18

I don't understand the question. Do you mean what are the line endings for files in the repo?

stevepiercy avatar Mar 12 '22 09:03 stevepiercy

Yes, exactly. Sorry, my bad couldn't frame my question right.

avimishra18 avatar Mar 12 '22 09:03 avimishra18

Honestly, I don't know. It "just works" for me. On my local machine, files use LF. I assume the repo uses LF as well.

stevepiercy avatar Mar 12 '22 10:03 stevepiercy

Great, I use Mac as well. So shouldn't be a problem for me.

avimishra18 avatar Mar 12 '22 10:03 avimishra18