mirror of
https://github.com/cse110-fa22-group29/cse110-fa22-group29.git
synced 2025-01-30 15:25:55 +00:00
updated crud features to work better
Co-authored-by: look-its-ashton <look-its-ashton@users.noreply.github.com> Co-authored-by: Kara Hoagland <KH-Cl@users.noreply.github.com> Co-authored-by: Gavyn Ezell <ezellgavyn@gmail.com>
This commit is contained in:
parent
4eaa1f38bb
commit
35f44049a2
@ -14,12 +14,13 @@
|
|||||||
<!-- Main Stylesheets & Scripts -->
|
<!-- Main Stylesheets & Scripts -->
|
||||||
<!-- Temporarily commented out reset.css due to furthur discussion needed on the values of the default config-->
|
<!-- Temporarily commented out reset.css due to furthur discussion needed on the values of the default config-->
|
||||||
<!-- <link rel="stylesheet" href="/static/reset.css" /> -->
|
<!-- <link rel="stylesheet" href="/static/reset.css" /> -->
|
||||||
<link rel="stylesheet" href="./static/CreatePage.css" />
|
<link rel="stylesheet" href="./static/ReviewCard.css" />
|
||||||
<script src="assets/scripts/main.js" type="module"></script>
|
<script src="./assets/scripts/CreatePage.js" type="module"></script>
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
<input type="button" value="Home" id="home-btn" onclick="window.location.assign('./index.html')">
|
||||||
<form id="new-food-entry">
|
<form id="new-food-entry">
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<legend>Pic:</legend>
|
<legend>Pic:</legend>
|
||||||
@ -69,8 +70,7 @@
|
|||||||
</label>
|
</label>
|
||||||
|
|
||||||
</fieldset>
|
</fieldset>
|
||||||
<button type="submit" value="Submit">Add Review</button>
|
<button type="submit" id="save-btn" value="Submit">Save Review</button>
|
||||||
<button type="button" class="danger">Clear Review Journal</button>
|
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main>
|
<main>
|
||||||
|
<input type="button" value="Home" id="home-btn" onclick="window.location.assign('./index.html')">
|
||||||
<button type="button" id="update-btn">Update</button>
|
<button type="button" id="update-btn">Update</button>
|
||||||
<button type="button" id="delete-btn" class="danger">Delete</button>
|
<button type="button" id="delete-btn" class="danger">Delete</button>
|
||||||
</main>
|
</main>
|
||||||
|
78
source/assets/scripts/CreatePage.js
Normal file
78
source/assets/scripts/CreatePage.js
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
window.addEventListener("DOMContentLoaded", init);
|
||||||
|
|
||||||
|
function init() {
|
||||||
|
// get next id
|
||||||
|
|
||||||
|
// creates the key
|
||||||
|
initFormHandler();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function initFormHandler() {
|
||||||
|
|
||||||
|
//accessing form components
|
||||||
|
let tagContainer = document.getElementById("tag-container-form");
|
||||||
|
let form = document.querySelector("form");
|
||||||
|
|
||||||
|
form.addEventListener("submit", function(e){
|
||||||
|
/*
|
||||||
|
* User submits the form for their review.
|
||||||
|
* We create reviewCard and put in storage
|
||||||
|
*/
|
||||||
|
e.preventDefault();
|
||||||
|
let formData = new FormData(form);
|
||||||
|
let reviewObject = {};
|
||||||
|
for (let [key, value] of formData) {
|
||||||
|
console.log(`${key}`);
|
||||||
|
console.log(`${value}`);
|
||||||
|
if (`${key}` !== "tag-form") {
|
||||||
|
reviewObject[`${key}`] = `${value}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
reviewObject["tags"] = [];
|
||||||
|
|
||||||
|
let tags = document.querySelectorAll(".tag");
|
||||||
|
for(let i = 0; i < tags.length; i ++) {
|
||||||
|
reviewObject["tags"].push(tags[i].innerHTML);
|
||||||
|
tagContainer.removeChild(tags[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//grabbing the nextID, and putting our review object in storage associated with the ID
|
||||||
|
let nextReviewId = JSON.parse(localStorage.getItem("nextID"));
|
||||||
|
reviewObject["reviewID"] = nextReviewId;
|
||||||
|
|
||||||
|
localStorage.setItem("review"+nextReviewId, JSON.stringify(reviewObject));
|
||||||
|
sessionStorage.setItem("currID", JSON.stringify(nextReviewId));
|
||||||
|
|
||||||
|
//updating our activeIDS list
|
||||||
|
let tempIdArr = JSON.parse(localStorage.getItem("activeIDS"));
|
||||||
|
tempIdArr.push(nextReviewId);
|
||||||
|
localStorage.setItem("activeIDS", JSON.stringify(tempIdArr));
|
||||||
|
|
||||||
|
|
||||||
|
//increment nextID for next review creation
|
||||||
|
nextReviewId++;
|
||||||
|
localStorage.setItem("nextID", JSON.stringify(nextReviewId));
|
||||||
|
|
||||||
|
window.location.assign('./ReviewDetails.html');
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
let tagAddBtn = document.getElementById("tagAdd");
|
||||||
|
tagAddBtn.addEventListener("click", ()=> {
|
||||||
|
let tagField = document.getElementById("tag-form");
|
||||||
|
if (tagField.value.length > 0) {
|
||||||
|
let tagLabel = document.createElement("label");
|
||||||
|
tagLabel.innerHTML = tagField.value;
|
||||||
|
tagLabel.setAttribute("class","tag");
|
||||||
|
tagLabel.addEventListener("click",()=> {
|
||||||
|
tagContainer.removeChild(tagLabel);
|
||||||
|
});
|
||||||
|
|
||||||
|
tagContainer.append(tagLabel);
|
||||||
|
tagField.value = "";
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
@ -6,7 +6,6 @@ class ReviewCard extends HTMLElement {
|
|||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
|
||||||
let shadowEl = this.attachShadow({mode:"open"});
|
let shadowEl = this.attachShadow({mode:"open"});
|
||||||
|
|
||||||
let articleEl = document.createElement("article");
|
let articleEl = document.createElement("article");
|
||||||
@ -88,9 +87,9 @@ class ReviewCard extends HTMLElement {
|
|||||||
//attach event listener to each recipe-card
|
//attach event listener to each recipe-card
|
||||||
this.addEventListener("click", (event) => {
|
this.addEventListener("click", (event) => {
|
||||||
console.log(event.target);
|
console.log(event.target);
|
||||||
console.log(event.target.data);
|
console.log(event.target.reviewId);
|
||||||
//Option 1: sending current data to second html page using localStorage (could also just store index)
|
//Option 1: sending current data to second html page using localStorage (could also just store index)
|
||||||
sessionStorage.setItem("current", JSON.stringify(event.target.data));
|
sessionStorage.setItem("currID", JSON.stringify(event.target.data.reviewID));
|
||||||
window.location.assign("./ReviewDetails.html");
|
window.location.assign("./ReviewDetails.html");
|
||||||
/*
|
/*
|
||||||
//Option 2: sending current data to second html page using string query w/ url (currently not storing value)
|
//Option 2: sending current data to second html page using string query w/ url (currently not storing value)
|
||||||
@ -133,12 +132,18 @@ class ReviewCard extends HTMLElement {
|
|||||||
let articleEl = this.shadowEl.querySelector("article");
|
let articleEl = this.shadowEl.querySelector("article");
|
||||||
|
|
||||||
// setting the article elements for the review card
|
// setting the article elements for the review card
|
||||||
|
this.reviewID = data["reviewID"];
|
||||||
|
|
||||||
//image setup
|
//image setup
|
||||||
let mealImg = document.createElement("img");
|
let mealImg = document.createElement("img");
|
||||||
mealImg.setAttribute("id", "a-mealImg");
|
mealImg.setAttribute("id", "a-mealImg");
|
||||||
mealImg.setAttribute("src",data["mealImg"]);
|
|
||||||
mealImg.setAttribute("alt",data["imgAlt"]);
|
mealImg.setAttribute("alt",data["imgAlt"]);
|
||||||
|
if(data["mealImg"] != ""){
|
||||||
|
mealImg.setAttribute("src",data["mealImg"]);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
mealImg.setAttribute("src", "./assets/images/icons/plate_with_cutlery.png");
|
||||||
|
}
|
||||||
|
|
||||||
//meal name setup
|
//meal name setup
|
||||||
let mealLabel = document.createElement("label");
|
let mealLabel = document.createElement("label");
|
||||||
@ -147,26 +152,6 @@ class ReviewCard extends HTMLElement {
|
|||||||
mealLabel.innerHTML = data["mealName"];
|
mealLabel.innerHTML = data["mealName"];
|
||||||
|
|
||||||
//restaurant name setup
|
//restaurant name setup
|
||||||
/*
|
|
||||||
//review page link
|
|
||||||
//giving it functionality to save the review card's info to session storage for loading the review page
|
|
||||||
let reviewLink = document.createElement('a');
|
|
||||||
reviewLink.setAttribute('href','./review.html')
|
|
||||||
reviewLink.innerHTML = 'review page'
|
|
||||||
reviewLink.addEventListener('click', () => {
|
|
||||||
sessionStorage.clear();
|
|
||||||
let currReview = {
|
|
||||||
"imgSrc": data['imgSrc'],
|
|
||||||
"imgAlt": data['imgAlt'],
|
|
||||||
"mealName": data['mealName'],
|
|
||||||
"restaurant": data['restaurant'],
|
|
||||||
"comments": data['comments'],
|
|
||||||
"rating": data['rating'],
|
|
||||||
"tags": data['tags']
|
|
||||||
}
|
|
||||||
sessionStorage.setItem('currReview', JSON.stringify(currReview));
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
let restaurantLabel = document.createElement("label");
|
let restaurantLabel = document.createElement("label");
|
||||||
restaurantLabel.setAttribute("id", "a-restaurant");
|
restaurantLabel.setAttribute("id", "a-restaurant");
|
||||||
restaurantLabel.setAttribute("class","restaurant-name");
|
restaurantLabel.setAttribute("class","restaurant-name");
|
||||||
@ -197,14 +182,15 @@ class ReviewCard extends HTMLElement {
|
|||||||
for (let i = 0; i < data["tags"].length; i++) {
|
for (let i = 0; i < data["tags"].length; i++) {
|
||||||
let newTag = document.createElement("label");
|
let newTag = document.createElement("label");
|
||||||
newTag.setAttribute("class","tag");
|
newTag.setAttribute("class","tag");
|
||||||
newTag.innerHTML = data["tags"][i] + " ";
|
newTag.innerHTML = data["tags"][i];
|
||||||
tagContainer.append(newTag);
|
tagContainer.append(newTag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//adding final ID to data!
|
||||||
|
|
||||||
articleEl.append(mealImg);
|
articleEl.append(mealImg);
|
||||||
articleEl.append(mealLabel);
|
articleEl.append(mealLabel);
|
||||||
//articleEl.append(reviewLink)
|
|
||||||
articleEl.append(restaurantLabel);
|
articleEl.append(restaurantLabel);
|
||||||
articleEl.append(ratingDiv);
|
articleEl.append(ratingDiv);
|
||||||
articleEl.append(tagContainer);
|
articleEl.append(tagContainer);
|
||||||
@ -237,6 +223,7 @@ class ReviewCard extends HTMLElement {
|
|||||||
let dataContainer = {};
|
let dataContainer = {};
|
||||||
|
|
||||||
// getting the article elements for the review card
|
// getting the article elements for the review card
|
||||||
|
dataContainer["reviewID"] = this.reviewID;
|
||||||
|
|
||||||
//get image
|
//get image
|
||||||
let mealImg = this.shadowEl.getElementById("a-mealImg");
|
let mealImg = this.shadowEl.getElementById("a-mealImg");
|
||||||
|
@ -11,23 +11,18 @@ function init(){
|
|||||||
|
|
||||||
function setupDelete(){
|
function setupDelete(){
|
||||||
let deleteBtn = document.getElementById("delete-btn");
|
let deleteBtn = document.getElementById("delete-btn");
|
||||||
let reviews = getReviewsFromStorage();
|
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
||||||
let current = JSON.parse(sessionStorage.getItem("current"));
|
let activeIDS = JSON.parse(localStorage.getItem("activeIDS"));
|
||||||
deleteBtn.addEventListener("click", function(){
|
deleteBtn.addEventListener("click", function(){
|
||||||
if(window.confirm("Are you sure you want to delete this entry?")){
|
if(window.confirm("Are you sure you want to delete this entry?")){
|
||||||
//delete function
|
for (let i in activeIDS) {
|
||||||
if(current){
|
if (activeIDS[i] == currID) {
|
||||||
console.log(current);
|
activeIDS.splice(i,1);
|
||||||
for(let i = 0; i < reviews.length; i++){
|
localStorage.setItem('activeIDS', JSON.stringify(activeIDS));
|
||||||
console.log(reviews[i]);
|
sessionStorage.removeItem('currID');
|
||||||
if(reviews[i]["mealName"] == current["mealName"] && reviews[i]["restaurant"] == current["restaurant"]){
|
localStorage.removeItem(`review${currID}`);
|
||||||
console.log("match found");
|
window.location.assign("./index.html");
|
||||||
reviews.splice(i,1);
|
break;
|
||||||
saveReviewsToStorage(reviews);
|
|
||||||
sessionStorage.removeItem("current");
|
|
||||||
window.location.assign("./index.html");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -36,30 +31,31 @@ function setupDelete(){
|
|||||||
|
|
||||||
function setupUpdate(){
|
function setupUpdate(){
|
||||||
let updateBtn = document.getElementById("update-btn");
|
let updateBtn = document.getElementById("update-btn");
|
||||||
let reviews = getReviewsFromStorage();
|
let currID = JSON.parse(sessionStorage.getItem("currID"));
|
||||||
let current = JSON.parse(sessionStorage.getItem("current"));
|
let currReview = JSON.parse(localStorage.getItem(`review${currID}`));
|
||||||
let form = document.getElementById("update-food-entry");
|
let form = document.getElementById("update-food-entry");
|
||||||
updateBtn.addEventListener("click", function(){
|
updateBtn.addEventListener("click", function(){
|
||||||
//update function
|
//update function
|
||||||
if(current){
|
if(currReview){
|
||||||
console.log(current);
|
|
||||||
form.style.display = "block";
|
form.style.display = "block";
|
||||||
let tagContainer = document.getElementById("tag-container-form");
|
let tagContainer = document.getElementById("tag-container-form");
|
||||||
console.log(document.querySelectorAll("#update-food-entry input"));
|
|
||||||
|
|
||||||
//Set value of each input element to current's values
|
//Set value of each input element to current's values
|
||||||
document.getElementById("mealImg").defaultValue = current["mealImg"];
|
document.getElementById("mealImg").defaultValue = currReview["mealImg"];
|
||||||
document.getElementById("imgAlt").defaultValue = current["imgAlt"];
|
document.getElementById("imgAlt").defaultValue = currReview["imgAlt"];
|
||||||
document.getElementById("mealName").defaultValue = current["mealName"];
|
document.getElementById("mealName").defaultValue = currReview["mealName"];
|
||||||
document.getElementById("comments").textContent = current["comments"];
|
document.getElementById("comments").textContent = currReview["comments"];
|
||||||
document.getElementById("rating-" + `${current["rating"]}`).checked = true;
|
document.getElementById("s" + `${currReview["rating"]}`).checked = true;
|
||||||
document.getElementById("restaurant").defaultValue = current["restaurant"];
|
document.getElementById("restaurant").defaultValue = currReview["restaurant"];
|
||||||
|
|
||||||
if(current["tags"]){
|
if(currReview["tags"]){
|
||||||
for (let i = 0; i < current["tags"].length; i++) {
|
while (tagContainer.firstChild) {
|
||||||
|
tagContainer.removeChild(tagContainer.firstChild);
|
||||||
|
}
|
||||||
|
for (let i = 0; i < currReview["tags"].length; i++) {
|
||||||
let newTag = document.createElement("label");
|
let newTag = document.createElement("label");
|
||||||
newTag.setAttribute("class","tag");
|
newTag.setAttribute("class","tag");
|
||||||
newTag.innerHTML = current["tags"][i] + " ";
|
newTag.innerHTML = currReview["tags"][i];
|
||||||
newTag.addEventListener("click",()=> {
|
newTag.addEventListener("click",()=> {
|
||||||
tagContainer.removeChild(newTag);
|
tagContainer.removeChild(newTag);
|
||||||
});
|
});
|
||||||
@ -89,16 +85,10 @@ function setupUpdate(){
|
|||||||
tagContainer.removeChild(tags[i]);
|
tagContainer.removeChild(tags[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(let i = 0; i < reviews.length; i++){
|
newData["reviewID"] = currID;
|
||||||
console.log(reviews[i]);
|
|
||||||
if(reviews[i]["mealName"] == current["mealName"] && reviews[i]["restaurant"] == current["restaurant"]){
|
|
||||||
console.log("match found");
|
localStorage.setItem("review"+currID, JSON.stringify(newData));
|
||||||
reviews.splice(i,1,newData);
|
|
||||||
saveReviewsToStorage(reviews);
|
|
||||||
sessionStorage.setItem("current", JSON.stringify(newData));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
form.style.display = "none";
|
form.style.display = "none";
|
||||||
|
|
||||||
|
@ -2,11 +2,19 @@
|
|||||||
* @returns {Array<Object>} An array of reviews found in localStorage
|
* @returns {Array<Object>} An array of reviews found in localStorage
|
||||||
*/
|
*/
|
||||||
export function getReviewsFromStorage() {
|
export function getReviewsFromStorage() {
|
||||||
let result = JSON.parse(localStorage.getItem("reviews"));
|
if (!(localStorage.getItem("activeIDS"))) {
|
||||||
if (result) {
|
// we wanna init the active ID array and start the nextID count
|
||||||
return result;
|
localStorage.setItem("activeIDS", JSON.stringify([]));
|
||||||
|
localStorage.setItem("nextID", JSON.stringify(0));
|
||||||
}
|
}
|
||||||
return new Array(0);
|
//iterate thru activeIDS
|
||||||
|
let activeIDS = JSON.parse(localStorage.getItem("activeIDS"));
|
||||||
|
let reviews = []
|
||||||
|
for (let i = 0; i < activeIDS.length; i++) {
|
||||||
|
let currReview = JSON.parse(localStorage.getItem('review'+activeIDS[i]));
|
||||||
|
reviews.push(currReview);
|
||||||
|
}
|
||||||
|
return reviews;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -15,5 +23,5 @@ export function getReviewsFromStorage() {
|
|||||||
* @param {Array<Object>} reviews An array of reviews
|
* @param {Array<Object>} reviews An array of reviews
|
||||||
*/
|
*/
|
||||||
export function saveReviewsToStorage(reviews) {
|
export function saveReviewsToStorage(reviews) {
|
||||||
localStorage.setItem("reviews", JSON.stringify(reviews));
|
localStorage.setItem(`review${reviewId}`, JSON.stringify(reviews));
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ function init() {
|
|||||||
initFormHandler();
|
initFormHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Array<Object>} reviews An array of reviews
|
* @param {Array<Object>} reviews An array of reviews
|
||||||
*/
|
*/
|
||||||
@ -33,14 +34,14 @@ function addReviewsToDocument(reviews) {
|
|||||||
*/
|
*/
|
||||||
function initFormHandler() {
|
function initFormHandler() {
|
||||||
|
|
||||||
/*
|
|
||||||
//btn to create form (could be its own function?)
|
//btn to create form (could be its own function?)
|
||||||
let createBtn = document.getElementById("create");
|
let createBtn = document.getElementById("create-btn");
|
||||||
createBtn.addEventListener("click", function(){
|
createBtn.addEventListener("click", function(){
|
||||||
window.location.assign("./CreatePage.html");
|
window.location.assign("./CreatePage.html");
|
||||||
});*/
|
});
|
||||||
|
|
||||||
//accessing form components
|
//accessing form components
|
||||||
|
/*
|
||||||
let tagContainer = document.getElementById("tag-container-form");
|
let tagContainer = document.getElementById("tag-container-form");
|
||||||
let form = document.querySelector("form");
|
let form = document.querySelector("form");
|
||||||
|
|
||||||
@ -48,7 +49,6 @@ function initFormHandler() {
|
|||||||
/*
|
/*
|
||||||
* User submits the form for their review.
|
* User submits the form for their review.
|
||||||
* We create reviewCard and put in storage
|
* We create reviewCard and put in storage
|
||||||
*/
|
|
||||||
let formData = new FormData(form);
|
let formData = new FormData(form);
|
||||||
let reviewObject = {};
|
let reviewObject = {};
|
||||||
for (let [key, value] of formData) {
|
for (let [key, value] of formData) {
|
||||||
@ -74,6 +74,9 @@ function initFormHandler() {
|
|||||||
let mainEl = document.querySelector("main");
|
let mainEl = document.querySelector("main");
|
||||||
mainEl.append(newReview);
|
mainEl.append(newReview);
|
||||||
|
|
||||||
|
// TODO: assign an ID to be used for referencing this object form the activeIDs array and the tag arrays
|
||||||
|
let ID = localStorage.nextID;
|
||||||
|
|
||||||
let storedReviews = getReviewsFromStorage();
|
let storedReviews = getReviewsFromStorage();
|
||||||
storedReviews.push(reviewObject);
|
storedReviews.push(reviewObject);
|
||||||
saveReviewsToStorage(storedReviews);
|
saveReviewsToStorage(storedReviews);
|
||||||
@ -115,5 +118,6 @@ function initFormHandler() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -22,8 +22,8 @@
|
|||||||
<main>
|
<main>
|
||||||
<!-- Add Food Entries Here -->
|
<!-- Add Food Entries Here -->
|
||||||
</main>
|
</main>
|
||||||
<!--<button type="button" id="create">CREATE</button>-->
|
<button type="button" id="create-btn"><a href='./CreatePage.html'></a>CREATE</button>
|
||||||
<!----> <form id="new-food-entry">
|
<!-- <form id="new-food-entry">
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<legend>Pic:</legend>
|
<legend>Pic:</legend>
|
||||||
<label for="mealImage">
|
<label for="mealImage">
|
||||||
@ -74,6 +74,6 @@
|
|||||||
</fieldset>
|
</fieldset>
|
||||||
<button type="submit" value="Submit">Add Review</button>
|
<button type="submit" value="Submit">Add Review</button>
|
||||||
<button type="button" class="danger">Clear Review Journal</button>
|
<button type="button" class="danger">Clear Review Journal</button>
|
||||||
</form> <!---->
|
</form> -->
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in New Issue
Block a user