All files / js addnumberplateform.js

29.41% Statements 5/17
0% Branches 0/6
57.14% Functions 4/7
29.41% Lines 5/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49    1x                       1x                   2x             1x   1x                              
import { validateNumberPlate } from './helpers/validatemethods.js';
 
$(function () {
  $.ajaxSetup({ cache: false });
  // Script for add button
  $('#add-button').on('click', function (e) {
    addNumberPlateHandler();
    e.preventDefault();
    return false;
  });
});
 
export function getFormData() {
  // Create JSON key value pairs
  return {
    regNumber: $('#reg-text').val(),
    available: $('#booleanDropdown').val(),
    price: $('#price-numberbox').val(),
  };
}
 
// function for adding a number plate to the database
export function postData(data) {
  // POST request to numberplate endpoint
  $.ajax({
    type: 'POST',
    url: 'http://localhost:8080/numberplate',
    data: JSON.stringify(data),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: (json, status, res) =>
      window.alert(json.message + ': ' + json.regNumber),
    error: (req, status, error) =>
      window.alert('Failed to add number plate: ' + error),
  });
}
 
export function addNumberPlateHandler() {
  const data = getFormData();
  if (data.regNumber && data.regNumber.length > 0) {
    const validation = validateNumberPlate(data.regNumber);
    if (!validation.valid) {
      window.alert(validation.message);
      return;
    }
  }
  postData(data);
}