Skip to content

Instantly share code, notes, and snippets.

@EricCote
Last active June 14, 2018 20:32
Show Gist options
  • Save EricCote/dd770ab26cbeec7e3e07e64e4323f88e to your computer and use it in GitHub Desktop.
Save EricCote/dd770ab26cbeec7e3e07e64e4323f88e to your computer and use it in GitHub Desktop.
script pour React Academy
import { authors } from './authorData'
import _ from 'lodash'
//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 });
}
};
export default AuthorApi;
var theData = {
authors:
[
{
id: '1',
firstName: 'Stephen',
lastName: 'King'
},
{
id: '2',
firstName: 'J.K.',
lastName: 'Rowlings'
},
{
id: '3',
firstName: 'John',
lastName: 'Grisham'
}
]
};
export default theData;
export var authors = theData.authors;
"scripts": {
"prebuild-css": "IF NOT EXIST src\\bootstrap XCOPY node_modules\\bootstrap\\scss src\\bootstrap /i /s /y",
"build-css": "node-sass-chokidar src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment