Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mock Author API for "Building Applications with React and Flux" on Pluralsight
"use strict";
//This file is mocking a web API by hitting hard coded data.
var authors = require('./authorData').authors;
var _ = require('lodash');
//This would be performed on the server in a real app. Just stubbing in.
var _generateId = function(author) {
return author.firstName.toLowerCase() + '-' + author.lastName.toLowerCase();
};
var _clone = function(item) {
return JSON.parse(JSON.stringify(item)); //return cloned copy so that the item is passed by value instead of by reference
};
var AuthorApi = {
getAllAuthors: function() {
return _clone(authors);
},
getAuthorById: function(id) {
var author = _.find(authors, {id: id});
return _clone(author);
},
saveAuthor: function(author) {
//pretend an ajax call to web api is made here
console.log('Pretend this just saved the author to the DB via AJAX call...');
if (author.id) {
var existingAuthorIndex = _.indexOf(authors, _.find(authors, {id: author.id}));
authors.splice(existingAuthorIndex, 1, author);
} else {
//Just simulating creation here.
//The server would generate ids for new authors in a real app.
//Cloning so copy returned is passed by value rather than by reference.
author.id = _generateId(author);
authors.push(author);
}
return _clone(author);
},
deleteAuthor: function(id) {
console.log('Pretend this just deleted the author from the DB via an AJAX call...');
_.remove(authors, { id: id});
}
};
module.exports = AuthorApi;
@coryhouse

This comment has been minimized.

Copy link
Owner Author

coryhouse commented Jan 31, 2016

This file belongs in /src/api/authorApi.js

@juampick

This comment has been minimized.

Copy link

juampick commented Oct 25, 2016

Thanks @coryhouse

@saikumarp

This comment has been minimized.

Copy link

saikumarp commented Oct 27, 2016

thanks @coryhouse

@jeffreyyong

This comment has been minimized.

Copy link

jeffreyyong commented Jan 3, 2017

thanks @coryhouse

@gameoft

This comment has been minimized.

Copy link

gameoft commented Feb 2, 2017

thanks @coryhouse

@destino92

This comment has been minimized.

Copy link

destino92 commented Feb 6, 2017

hello i am trying to load authors for the dropdown but it seems that they never get loaded and i get this error.
I am stuck at this line and when i console state it doesn't have authors.
const authorsFormattedForDropdown = state.authors.map(author => { return { value: author.id, text: author.firstName + ' ' + author.lastName }; });
screenshot 23

@dmsergeev

This comment has been minimized.

Copy link

dmsergeev commented Mar 30, 2017

@destino92 Did you add authorData.js?

@rtorres90

This comment has been minimized.

Copy link

rtorres90 commented Jul 3, 2017

Thanks!!

@mark-zacharias

This comment has been minimized.

Copy link

mark-zacharias commented Jul 19, 2017

@jankoboz

This comment has been minimized.

Copy link

jankoboz commented Dec 22, 2017

Thank you.

@igomolskyi

This comment has been minimized.

Copy link

igomolskyi commented Apr 23, 2018

Thank you :)

@leonardo-anjos

This comment has been minimized.

Copy link

leonardo-anjos commented Sep 6, 2018

Thanks!

@viviramji

This comment has been minimized.

Copy link

viviramji commented Dec 10, 2018

Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.