implementation of basic image local storage

This commit is contained in:
Henry Feng 2022-11-17 16:17:51 -08:00
parent 540d6563d8
commit 416d26658d
2 changed files with 49 additions and 4 deletions

View File

@ -44,6 +44,44 @@ function initFormHandler() {
let tagContainer = document.getElementById("tag-container-form"); let tagContainer = document.getElementById("tag-container-form");
let form = document.querySelector("form"); let form = document.querySelector("form");
/*
* change the input source of the image between local file and URL
* depending on user's selection
*/
let select = document.getElementById("select");
select.addEventListener("change", function() {
const input = document.getElementById('source');
if (select.value == "file") {
input.innerHTML = `
Source:
<input type="file" id="mealImg" name="mealImg">
`
}
else {
input.innerHTML = `
Source:
<input type="text" id="mealImg" name="mealImg">
`
}
});
//addressing sourcing image from local file
let imgDataURL = "";
document.getElementById("mealImg").addEventListener("change", function() {
const reader = new FileReader();
//store image data URL after successful image load
reader.addEventListener("load", ()=>{
imgDataURL = reader.result;
}, false);
//convert image file into data URL for local storage
reader.readAsDataURL(document.getElementById("mealImg").files[0]);
})
form.addEventListener("submit", function(){ form.addEventListener("submit", function(){
/* /*
* User submits the form for their review. * User submits the form for their review.
@ -51,12 +89,16 @@ function initFormHandler() {
*/ */
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) {
console.log(`${key}`); console.log(`${key}`);
console.log(`${value}`); console.log(`${value}`);
if (`${key}` !== "tag-form") { if (`${key}` !== "tag-form") {
reviewObject[`${key}`] = `${value}`; reviewObject[`${key}`] = `${value}`;
} }
if (`${key}` === "mealImg" && select.value == "file") {
reviewObject["mealImg"] = imgDataURL;
}
} }
reviewObject["tags"] = []; reviewObject["tags"] = [];

View File

@ -10,7 +10,6 @@
<!-- Recipe Card Custom Element --> <!-- Recipe Card Custom Element -->
<script src="assets/scripts/ReviewCard.js" type="module"></script> <script src="assets/scripts/ReviewCard.js" type="module"></script>
<!-- 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" /> -->
@ -26,9 +25,14 @@
<!----> <form id="new-food-entry"> <!----> <form id="new-food-entry">
<fieldset> <fieldset>
<legend>Pic:</legend> <legend>Pic:</legend>
<label for="mealImage"> Choose Input type:
<select id="select" name="select">
<option value="file">File Upload</option>
<option value="url">From an URL</option>
</select>
<label for="mealImage" id="source">
Source: Source:
<input type="text" id="mealImg" name="mealImg"> <input type="file" id="mealImg" name="mealImg">
</label> </label>
<label for="image-alt"> <label for="image-alt">
Alt Text: Alt Text:
@ -36,7 +40,6 @@
</label> </label>
</fieldset> </fieldset>
<fieldset> <fieldset>
<legend> Meal: </legend> <legend> Meal: </legend>
<label for="Meal: ">Meal: <label for="Meal: ">Meal:
<input type="text" id="mealName" name="mealName" required> <input type="text" id="mealName" name="mealName" required>