Skip to content

Commit

Permalink
Additional choices coming back from api
Browse files Browse the repository at this point in the history
Relates #68
  • Loading branch information
frannyfra committed Sep 17, 2019
1 parent e0aabad commit 6ab9bde
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 29 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"tape": "^4.11.0"
},
"dependencies": {
"cookie-parser": "^1.4.4",
"dotenv": "^8.1.0",
"env2": "^2.2.2",
"express": "^4.17.1",
Expand Down
23 changes: 0 additions & 23 deletions public/js/main.js
Original file line number Diff line number Diff line change
@@ -1,23 +0,0 @@
// document.cookie = 'recipes='
// document.querySelectorAll('.recipe__select').forEach(button => {
// button.addEventListener('click', (e) => {
// if (document.cookie.includes(button.id)) {
// let oldString = document.cookie;
// let newString = oldString.replace(`+${button.id}`, "");
// document.cookie = newString;
// }
// else if (document.cookie.split('+').length > 2) {
// window.alert("Please select two recipes only");
// } else {
// document.cookie = `${document.cookie}+${button.id}`
// }
// console.log('Cookie: ', document.cookie);
// })
// });

// document.querySelector('.newplan__nav_buttons__next').addEventListener('click', (e) => {
// if (document.cookie.split('+').length !== 3) {
// e.preventDefault();
// window.alert("Please select two recipes");
// }
// });
2 changes: 2 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const exphbs = require("express-handlebars");
const helpers = require("./views/helpers/index");
const controllers = require("./controllers/index");
const app = express();
const cookieParser = require("cookie-parser");

app.set("views", path.join(__dirname, "views"));
app.set("view engine", "hbs");
Expand All @@ -19,6 +20,7 @@ app.engine(
})
);

app.use(cookieParser());
app.set("port", process.env.PORT || 3000);
app.use(favicon(path.join(__dirname, "..", "public", "assets", "favicon.ico")));
app.use(express.static(path.join(__dirname, "..", "public")));
Expand Down
16 changes: 10 additions & 6 deletions src/controllers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ exports.getMealPlans = (req, res) => {
};

exports.getAdditionalChoices = (req, res) => {
console.log('hello', 'cookies:' , req.cookies)
api.getIngredientsApi(document.cookie.split('+')[1], document.cookie.split('+')[2])
.then(ingredients => api.getRecipesApi(ingredients))
.then(result => {
res.render("newplanAdditionalChoices", { recipes : result});
})

const recipes = {
one: req.cookies.recipes.split('+')[1],
two:req.cookies.recipes.split('+')[1]
}
api.getIngredientsApi(recipes.one, recipes.two)
.then(ingredients => api.getRecipesApi(ingredients))
.then(result => {
res.render("newplanAdditionalChoices", { recipes : result});
})
.catch(err => {
res.render("error", {
statusCode: 500,
Expand Down

0 comments on commit 6ab9bde

Please sign in to comment.