MySQL Editor in NodeJS ~ Part two
Tags: mysql, node, learning, opensource
Published: 2018-09-13 14:54:11

---
title: MySQL Editor in NodeJS ~ Part two
published: true
description: Learning to use NodeJS
tags: MySQL, node, learning, opensource
cover_image: https://thepracticaldev.s3.amazonaws.com/i/iply2eckh6x45bbw03ml.png
series: Project NodeTree
---

# MySQL Editor in NodeJS

This week I've continued making the interface for MySQL Query Browser. You can now store sessions though there is nothing implemented yet to actually interact with MySQL or even test the connection. If you'd like to follow along with my journey here is my [post index](https://dev.to/link2twenty/adventures-of-a-hobbyist--index-4oj7). 

{% github ignis-pwa/nodetree %}

## How's the interface?

I've gone for a material design inspired look with adaptive design, in the past I've used web components but I decided with this project, at least for now, I'd do everything myself with HTML, CSS and JavaScript.

Here's a quick look at the interface so far.

**Responsive:**

![responsive](https://thepracticaldev.s3.amazonaws.com/i/4ep4arnru0k6acqunhne.gif)

**Add new (mobile):**

![add new](https://thepracticaldev.s3.amazonaws.com/i/wcck9ri44hd413e21q95.gif)

**Delete (desktop):**

![delete](https://thepracticaldev.s3.amazonaws.com/i/pzpgehovwyqdgodd3yl8.gif)

## Did you learn anything?

I came up with a method to make a confirm modal, though I'm sure either someone else has done this already or it's a bad idea for some reason, that I thought was quite interesting.

**Javascript:**

```javascript
class ShowHide {
  constructor(selector) {
    this.element = document.querySelector(selector);
  }
  show() {
    this.element.hasAttribute('hidden') && this.element.removeAttribute('hidden');
  }
  hide() {
    this.element.hasAttribute('hidden') || this.element.setAttribute('hidden', '');
  }
}

class ModalController extends ShowHide {
  constructor(selector, confirmModal = false) {
    super(selector);
    this.confirmModal = confirmModal;
    this.hide();
  }
  confirm() {
    let confirmObj = new CustomEvent('confirm', { detail: true });
    this.element.dispatchEvent(confirmObj);
    this.hide();
  }
  reject() {
    let confirmObj = new CustomEvent('confirm', { detail: false });
    this.element.dispatchEvent(confirmObj);
    this.hide();
  }
  confirmPromise() {
    if (!this.confirmModal) return false;
    this.show();
    return new Promise((res, rej) => {
      this.element.addEventListener('confirm', (e) => {
        if (e.detail) { res() } else { rej() }
      }, { once: true })
    })
  }
}

const confirmationB = new ModalController('.modal.conf', true);
confirmationB.element.querySelector('.backdrop').addEventListener('click', () => { confirmationB.reject() });
confirmationB.element.querySelector('.modal-reject').addEventListener('click', () => { confirmationB.reject() });
confirmationB.element.querySelector('.modal-confirm').addEventListener('click', () => { confirmationB.confirm() });

```

**HTML:**
```html
<div hidden class="modal conf">
  <div class="backdrop"></div>
  <div class="content shadow">
    <div class="header">Confirmation</div>
    <div class="central">Are you sure?</div>
    <div class="footer">
      <button class="modal-reject">No</button>
      <button class="modal-confirm">Yes</button>
    </div>
  </div>
</div>
```

There are some CSS styles too, of course, if you're interested in seeing them they're uploaded to the GitHub page linked above.

With this, we can now run something like this.

```javascript
confirmationB.confirmPromise().then(()=>{
  console.log('The user pressed yes.');
}).catch(()=>{
  console.log('The user pressed no or outside the modal box.');
})
```

Which I've implemented for confirming if the user actually wants to delete a session or not.

![confirm](https://thepracticaldev.s3.amazonaws.com/i/3vflsdhzklc07mklr2f7.png)

## What's next?

Next week I'll be looking at making it functional, I aim to have it show a list of available tables and also display data from a query in a table. 

My next post will have more UI elements I'm sure, I'm yet to make the UI for displaying tables but I've got some ideas, do you like the gifs, or are they a bit annoying?

If you'd like to look through my repository and point out things I can do better that would be amazing and I'd incorporate that into next weeks stuff.

## End of post

As always, thanks so much for reading ❤
This project has been easy so far but rest assured I'm sure we'll hit a slow down and some complications in no time 😀


🦄🦄🦄