JSFiddle - React, Tailwind, and code Playground

by Olayinka Otuniyi

HTML

<button id="addValue" onclick="addRecord()">Add</button>
<span id="currentId"></span>

JavaScript

function addRecord() {
  let myDatabase = window.localStorage;
  let currentId = document.getElementById('currentId');
  let lastId = '';
  let fileId = {
    id: '',
  }
  let addedValues = {
    'fileNumber': 12,
    'volumeNumber': 1,
    'directorate': 'wana',
  }
  for (let i = 0; i < myDatabase.length; i++) {
    lastId = JSON.parse(myDatabase.key(i)).id;
  }
  try {
    fileId.id = ++lastId;
    currentId.innerHTML = fileId.id;
    myDatabase.setItem(JSON.stringify(fileId), JSON.stringify(addedValues));
  } catch (err) {
    alert(`There is some problem, the record cannot be saved! 
${err}`);
  }
}