{"size": 2183, "ext": "js", "max_stars_repo_path": "public/admin/fields.js", "max_stars_repo_name": "YNascimento/desafio04-foodfy", "max_stars_repo_stars_event_min_datetime": "2020-08-24T20:21:04.000Z", "max_stars_repo_stars_event_max_datetime": "2020-08-24T20:21:04.000Z", "max_issues_repo_path": "public/admin/fields.js", "max_issues_repo_name": "YSNascimento/desafio04-admin-foodfy", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "public/admin/fields.js", "max_forks_repo_name": "YSNascimento/desafio04-admin-foodfy", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 30.7464788732, "max_line_length": 80, "alphanum_fraction": 0.6775080165}
let ingredients = document.getElementById('ingredients')
let preparations = document.getElementById('preparations')


function addInput(inputBox, inputField) {
    let newField

    if(inputField.length > 0){
        newField = inputField[inputField.length - 1].cloneNode(true);
    }
    else{
        newField = document.createElement('div')
        newField.setAttribute('id', "ingredient-0")
        newField.setAttribute('class', "ingredient")

        const inputEl = document.createElement('input')
        inputEl.setAttribute('type','text')
        inputEl.setAttribute('name','ingredients[]')
        inputEl.setAttribute('value','')

        const spanEl = document.createElement('span')
        spanEl.setAttribute('class', 'closeBtn')

        const iEl = document.createElement('i')
        iEl.setAttribute('class', 'fas fa-times')

        spanEl.appendChild(iEl)
        newField.appendChild(inputEl)
        newField.appendChild(spanEl)
    }
        
    // Não adiciona um novo input se o último tem um valor vazio
    if (inputField.length > 0 && newField.children[0].value == "") return false;

    // Deixa o valor do input vazio
    newField.children[0].value = "";
    newField.setAttribute('id','ingredient-'+inputField.length)
    
    inputBox.appendChild(newField);
}

function deleteInput(e){
    // e.target was the clicked element
    if (e.target && (e.target.tagName == "i" || e.target.tagName == "I" ) ){
        const targetField = e.target.parentElement.parentElement
        targetField.remove()
	
}

document.querySelector("#addIngredient").addEventListener("click", function(){
    const inputBox = document.querySelector("#ingredients");
    const inputField = document.querySelectorAll(".ingredient");

    addInput(inputBox, inputField)
})
  
  
document.querySelector("#addPreparation").addEventListener("click", function(){
    const inputBox = document.querySelector("#preparations");
    const inputField = document.querySelectorAll(".preparation");

    addInput(inputBox, inputField)
})

ingredients.addEventListener("click",function(e){
    deleteInput(e)
})

preparations.addEventListener("click", function(e){
    deleteInput(e)
})