PHP Classes

File: front-pessoas/js/components/UserForm.js

Recommend this page to a friend!
  Classes of Rodrigo Faustino   App MFE   front-pessoas/js/components/UserForm.js   Download  
File: front-pessoas/js/components/UserForm.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: App MFE
Single-page application with micro-frontends
Author: By
Last change:
Date: 1 month ago
Size: 1,497 bytes
 

Contents

Class file image Download
class UserForm { constructor(fetchService, renderCallback) { this.fetchService = fetchService; this.refreshUsersList = renderCallback; } render() { return ` <form id="addForm"> Nome: <input type="text" id="nome"><br> E-mail: <input type="text" id="email"><br> Senha: <input type="password" id="senha"><br> <button type="submit" class="btn waves-effect waves-light">Adicionar Usuário</button> </form> `; } afterRender() { document.getElementById('addForm').addEventListener('submit', (e) => this.addItem(e)); } async addItem(event) { event.preventDefault(); const nome = document.getElementById('nome').value; const email = document.getElementById('email').value; const senha = document.getElementById('senha').value; if(!nome || !email || !senha){ alert('Campos vazios'); return } await this.fetchService.fetch('/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ nome, email, senha }), }); alert('usuário adicionado com sucesso.'); document.getElementById('nome').value = ''; document.getElementById('email').value = ''; document.getElementById('senha').value = ''; this.refreshUsersList(); } } export default UserForm;