Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ikeyvin/form-list-petition-db #126

Open
wants to merge 5 commits into
base: works
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 146 additions & 28 deletions src/works/ikeyvin/components/form-petition.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { LitElement, html } from 'lit-element';
import { formPeticion } from '../utils/costum-css.js';
import { dateFormatter } from '../utils/functions';
import { dataCandidato, dataCliente } from '../utils/constants.js';

class FormPetition extends LitElement {
static get styles() {
Expand All @@ -8,99 +10,215 @@ class FormPetition extends LitElement {

static get properties() {
return {
listaPeticion: { type: Array },
message: { type: String, attribute: false },
alertMessage: { type: Boolean },
};
}

constructor() {
super();
this.listaPeticion = [];
this.message = '';
this.id = '';
this.titulo = '';
this.descripcion = '';
this.fecha = new Date();
this.cliente = '';
this.candidato = '';
this.publicado = false;
}

addPeticion() {
let storedPeticion = JSON.parse(window.localStorage.getItem('list-peticion'));
storedPeticion = storedPeticion === null ? [] : storedPeticion;
connectedCallback() {
super.connectedCallback();
window.addEventListener('set-petition', async (e) => {
this.id = e.detail.id;
this.titulo = e.detail.titulo;
this.descripcion = e.detail.descripcion;
this.fecha = e.detail.fecha;
this.cliente = e.detail.cliente;
this.candidato = e.detail.candidato;
this.publicado = e.detail.publicar;
await this.requestUpdate();
});
}

addPetition() {
const inputTitulo = this.shadowRoot.querySelector('#peticionTitulo');
const inputDescripcion = this.shadowRoot.querySelector('#peticionDescripcion');
const inputPublicar = this.shadowRoot.querySelector('#peticionPublicar');
const inputCliente = this.shadowRoot.querySelector('#cliPet');
const inputCandidato = this.shadowRoot.querySelector('#candiPet');
const inputId = this.shadowRoot.querySelector('#petId');

if (inputTitulo.value.length > 0 && inputDescripcion.value.length > 0) {
storedPeticion.push({
id: new Date().valueOf(),
titulo: inputTitulo.value,
descripcion: inputDescripcion.value,
fecha: new Date(),
cliente: inputCliente.value,
candidato: inputCandidato.value,
publicar: inputPublicar.checked,
const event = new CustomEvent('add-petition', {
detail: {
id: new Date().valueOf(),
titulo: inputTitulo.value,
descripcion: inputDescripcion.value,
fecha: new Date(),
cliente: inputCliente.value,
candidato: inputCandidato.value,
publicar: inputPublicar.checked,
},
bubbles: true,
composed: true,
});

localStorage.setItem('list-peticion', JSON.stringify(storedPeticion));
this.listaPeticion = [...[storedPeticion], ...this.listaPeticion];
this.dispatchEvent(event);

inputTitulo.value = '';
inputDescripcion.value = '';
inputPublicar.checked = false;
inputCliente.value = '';
inputCandidato.value = '';
inputId.value = '';
this.message = '';
} else {
this.message = 'TITULO y DESCRIPCIÓN no debe estar vacío!';
this.alertMessage = true;
this.message = '¡Título y Descripción no debe estar vacío!';
}
}

async deletePetition(id) {
if (id === '') {
this.alertMessage = true;
this.message = '¡Seleciona una petición!';
} else {
const event = new CustomEvent('delete-petition', {
detail: {
id: id,
},
bubbles: true,
composed: true,
});
this.dispatchEvent(event);

const inputTitulo = this.shadowRoot.querySelector('#peticionTitulo');
const inputDescripcion = this.shadowRoot.querySelector('#peticionDescripcion');
const inputPublicar = this.shadowRoot.querySelector('#peticionPublicar');
const inputCliente = this.shadowRoot.querySelector('#cliPet');
const inputCandidato = this.shadowRoot.querySelector('#candiPet');
const inputId = this.shadowRoot.querySelector('#petId');

await this.updateComplete;
inputTitulo.value = '';
inputDescripcion.value = '';
inputPublicar.checked = false;
inputCliente.value = '';
inputCandidato.value = '';
inputId.value = '';
this.id = '';
}
}

async updatePetition(id) {
if (id === '') {
this.alertMessage = true;
this.message = '¡Seleciona una petición!';
} else {
const inputTitulo = this.shadowRoot.querySelector('#peticionTitulo');
const inputDescripcion = this.shadowRoot.querySelector('#peticionDescripcion');
const inputPublicar = this.shadowRoot.querySelector('#peticionPublicar');
const inputCliente = this.shadowRoot.querySelector('#cliPet');
const inputCandidato = this.shadowRoot.querySelector('#candiPet');
const inputId = this.shadowRoot.querySelector('#petId');

const event = new CustomEvent('update-petition', {
detail: {
id: id,
titulo: inputTitulo.value,
descripcion: inputDescripcion.value,
fecha: new Date(),
cliente: inputCliente.value,
candidato: inputCandidato.value,
publicar: inputPublicar.checked,
},
bubbles: true,
composed: true,
});
this.dispatchEvent(event);

await this.updateComplete;
inputTitulo.value = '';
inputDescripcion.value = '';
inputPublicar.checked = false;
inputCliente.value = '';
inputCandidato.value = '';
inputId.value = '';
this.id = '';
}
}

closeAlertMessage() {
this.alertMessage = false;
}

render() {
return html`
<div class="form">
<div class="heading">
<h1>Editar la entrada</h1>
<button @click="${() => this.addPeticion()}">AÑADIR NUEVO</button>
<button id="addBtn" class="addBtn" @click="${() => this.addPetition()}">AÑADIR NUEVO</button>
</div>
<div class="petitionForm">
<input type="hidden" id="petId" name="petId" value="${this.id}" />
<div class="column-a1">
<input id="peticionTitulo" name="peticionTitulo" placeholder="TÍTULO DE LA PETICIÓN" autocomplete="off" />
<input
id="peticionTitulo"
name="peticionTitulo"
placeholder="TÍTULO DE LA PETICIÓN"
autocomplete="off"
value="${this.titulo}"
/>
<p type="Descripción de la petición" class="descrip"></p>
<textarea id="peticionDescripcion" name="peticionDescripcion"></textarea>
<textarea id="peticionDescripcion" name="peticionDescripcion">${this.descripcion}</textarea>
<br />
<br />
<p class="alertMessage">${this.message}</p>
<div class="alertMessage ${this.alertMessage ? 'active' : ''}">
<div class="message">
<span class="closebtn" @click="${() => this.closeAlertMessage()}">&times;</span>${this.message}
</div>
</div>
</div>
<div class="column-a2">
<div class="postPet">
<p type="Publicar" class="postHead"></p>
<p class="contentArea">Fecha de publicación:</p>
<p class="contentArea">Fecha de publicación: <b>${dateFormatter(this.fecha).dateHour}</b></p>
<div class="postFoot">
<div class="foot-left">
<a style="color: red">Mover a la papelera</a>
<a id="deleteBtn" class="deleteBtn" style="color: red" @click="${() => this.deletePetition(this.id)}"
>Mover a la papelera</a
>
</div>
<div class="foot-right">
<button id="updateBtn" class="updateBtn" @click="${() => this.updatePetition(this.id)}">
ACTUALIZAR
</button>
</div>
<div class="foot-right"><button class="updateBtn">ACTUALIZAR</button></div>
</div>
</div>
<div class="postPet">
<p type="Cliente de la petición" class="postHead"></p>
<div class="contentArea">
<select name="cliente" id="cliPet">
<option value=""></option>
<option value="Test 1">Test 1</option>
<option value="${this.cliente}">${this.cliente}</option>
${dataCliente.map((data) => html`<option value="${data.nombre}">${data.nombre}</option> `)}
</select>
</div>
</div>
<div class="postPet">
<p type="Asignar candidato" class="postHead"></p>
<div class="contentArea">
<select name="candidato" id="candiPet">
<option value=""></option>
<option value="Test 2">Test 2</option>
<option value="${this.candidato}">${this.candidato}</option>
${dataCandidato.map((data) => html`<option value="${data.nombre}">${data.nombre}</option> `)}
</select>
</div>
</div>
<div class="checkbox">
<input id="peticionPublicar" type="checkbox" />
${this.publicado
? html`<input id="peticionPublicar" type="checkbox" checked />`
: html`<input id="peticionPublicar" type="checkbox" />`}
<label for="c1" style="font-weight: bold">PUBLICAR</label>
</div>
</div>
Expand Down
17 changes: 16 additions & 1 deletion src/works/ikeyvin/components/list-petition.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { nothing } from 'lit-html';
import { dateFormatter } from '../utils/functions';
import './form-petition';
import { tablePeticion, modalPopup } from '../utils/costum-css';
import { getInfoPetitions } from '../utils/api/api-request.js';

class ListPetition extends LitElement {
static get styles() {
Expand All @@ -18,13 +19,27 @@ class ListPetition extends LitElement {

constructor() {
super();
this.listaPeticiones = JSON.parse(window.localStorage.getItem('list-peticion'));
this.listaPeticiones = [];
this.popupOpen = false;
this.popupPetTitulo = '';
this.popupPetDescripcion = '';
this.popupPetFecha = '';
}

async firstUpdated() {
await this.getList();
}

async getList() {
const request = await getInfoPetitions();
if (!request.error) {
this.listaPeticiones = [...request.data];
} else if (request.errorCode === 500) {
// eslint-disable-next-line no-alert
alert(request.error);
}
}

showPetition(id) {
this.listaPeticiones.map((peticion) => {
if (peticion.id === id) {
Expand Down
Loading