Skip to content

Instantly share code, notes, and snippets.

@cklanac
Created April 18, 2018 13:28
Show Gist options
  • Save cklanac/cc49a81bd14dd4c6c2d9b95ab805a782 to your computer and use it in GitHub Desktop.
Save cklanac/cc49a81bd14dd4c6c2d9b95ab805a782 to your computer and use it in GitHub Desktop.
Feature / Noteful App V1 POST and DELETE Client update
/* global $ */
'use strict';
const api = {
search: function (query, callback) {
$.ajax({
type: 'GET',
url: '/api/notes/',
dataType: 'json',
data: query,
success: callback
});
},
details: function (id, callback) {
$.ajax({
type: 'GET',
dataType: 'json',
url: `/api/notes/${id}`,
success: callback
});
},
update: function (id, obj, callback) {
$.ajax({
type: 'PUT',
url: `/api/notes/${id}`,
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(obj),
success: callback
});
},
create: function (obj, callback) {
$.ajax({
type: 'POST',
url: '/api/notes',
contentType: 'application/json',
dataType: 'json',
processData: false,
data: JSON.stringify(obj),
success: callback
});
},
remove: function (id, callback) {
return $.ajax({
type: 'DELETE',
url: `/api/notes/${id}`,
dataType: 'json',
success: callback
});
}
};
/* global $ store api */
'use strict';
const noteful = (function () {
function render() {
const notesList = generateNotesList(store.notes, store.currentNote);
$('.js-notes-list').html(notesList);
const editForm = $('.js-note-edit-form');
editForm.find('.js-note-title-entry').val(store.currentNote.title);
editForm.find('.js-note-content-entry').val(store.currentNote.content);
}
/**
* GENERATE HTML FUNCTIONS
*/
function generateNotesList(list, currentNote) {
const listItems = list.map(item => `
<li data-id="${item.id}" class="js-note-element ${currentNote.id === item.id ? 'active' : ''}">
<a href="#" class="name js-note-show-link">${item.title}</a>
<button class="removeBtn js-note-delete-button">X</button>
</li>`);
return listItems.join('');
}
/**
* HELPERS
*/
function getNoteIdFromElement(item) {
const id = $(item).closest('.js-note-element').data('id');
return id;
}
/**
* EVENT LISTENERS AND HANDLERS
*/
function handleNoteItemClick() {
$('.js-notes-list').on('click', '.js-note-show-link', event => {
event.preventDefault();
const noteId = getNoteIdFromElement(event.currentTarget);
api.details(noteId, detailsResponse => {
store.currentNote = detailsResponse;
render();
});
});
}
function handleNoteSearchSubmit() {
$('.js-notes-search-form').on('submit', event => {
event.preventDefault();
const searchTerm = $('.js-note-search-entry').val();
store.currentSearchTerm = searchTerm ? { searchTerm } : {};
api.search(store.currentSearchTerm, searchResponse => {
store.notes = searchResponse;
render();
});
});
}
function handleNoteFormSubmit() {
$('.js-note-edit-form').on('submit', function (event) {
event.preventDefault();
const editForm = $(event.currentTarget);
const noteObj = {
id: store.currentNote.id,
title: editForm.find('.js-note-title-entry').val(),
content: editForm.find('.js-note-content-entry').val()
};
if (noteObj.id) {
api.update(store.currentNote.id, noteObj, updateResponse => {
store.currentNote = updateResponse;
api.search(store.currentSearchTerm, searchResponse => {
store.notes = searchResponse;
render();
});
});
} else {
api.create(noteObj, createResponse => {
store.currentNote = createResponse;
api.search(store.currentSearchTerm, searchResponse => {
store.notes = searchResponse;
render();
});
});
}
});
}
function handleNoteStartNewSubmit() {
$('.js-start-new-note-form').on('submit', event => {
event.preventDefault();
store.currentNote = {};
render();
});
}
function handleNoteDeleteClick() {
$('.js-notes-list').on('click', '.js-note-delete-button', event => {
event.preventDefault();
const noteId = getNoteIdFromElement(event.currentTarget);
api.remove(noteId, () => {
api.search(store.currentSearchTerm, searchResponse => {
store.notes = searchResponse;
if (noteId === store.currentNote.id) {
store.currentNote = {};
}
render();
});
});
});
}
function bindEventListeners() {
handleNoteItemClick();
handleNoteSearchSubmit();
handleNoteFormSubmit();
handleNoteStartNewSubmit();
handleNoteDeleteClick();
}
// This object contains the only exposed methods from this module:
return {
render: render,
bindEventListeners: bindEventListeners,
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment