Skip to content

Commit

Permalink
implement filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
grottohub committed Mar 13, 2021
1 parent 44e1ef0 commit 66a4d83
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<div class="col-4"></div>
<div class="col-4">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search Contacts">
<input id="searchBar" type="text" class="form-control" placeholder="Search Contacts">
</div>
</div>
<div class="col-4"></div>
Expand Down
2 changes: 2 additions & 0 deletions public/javascripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ _ui.loaded(() => {
_ui.click({class: "closeModal"}, eventHandlers.close);
_ui.click({class: "onsubmit"}, eventHandlers.onSubmit);
_ui.get({id: 'filterTags'}).addEventListener("tagsUpdated", eventHandlers.updateTagsDOM);
_ui.click({class: 'badge'}, eventHandlers.toggleTag);
_ui.get({id: 'searchBar'}).oninput = eventHandlers.filterContacts;
});
22 changes: 19 additions & 3 deletions public/javascripts/lib/domCache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ContactData } from './contactData.js';
import { partition } from './helpers.js';
import { tagBp, contactBp, contactRowBp } from '../blueprints/bundle.js';
import { tagBp, contactBp, contactRowBp, alert } from '../blueprints/bundle.js';
import { tagsUpdated } from './events.js';

export let domCache;
Expand Down Expand Up @@ -139,6 +139,10 @@ export let domCache;

if (taggedContacts) {
return buildRows(taggedContacts);
} else {
let noContactAlert = _ui.make(alert, ['No contacts found!']);
_ui.state(noContactAlert.firstElementChild, 'danger');
return noContactAlert;
}
},

Expand Down Expand Up @@ -194,6 +198,7 @@ export let domCache;

pushTag(tag) {
if (!includes("tags", tag)) {
tag.blueprint = 'tag';
allTags.push(tag);
_ui.get({id: 'filterTags'}).dispatchEvent(tagsUpdated);
}
Expand All @@ -210,13 +215,24 @@ export let domCache;
}
},

toggleTag(tagValue) {
let tagIdx = activeTags.indexOf(tagValue);
toggleTag(tag) {
let tagValue = tag.innerText,
tagIdx = activeTags.indexOf(tagValue);

if (tagIdx === -1) {
activeTags.push(tagValue);
_ui.get({class: 'badge'}).forEach(uniqTag => {
if (activeTags.includes(uniqTag.innerText)) {
_ui.state(uniqTag, 'active');
}
});
} else {
activeTags.splice(tagIdx, 1);
_ui.get({class: 'badge'}).forEach(uniqTag => {
if (!activeTags.includes(uniqTag.innerText)) {
_ui.state(uniqTag, 'default');
}
});
}
},

Expand Down
10 changes: 9 additions & 1 deletion public/javascripts/lib/eventHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ export const logger = function logger(event) {

// TAGS
export const updateTagsDOM = function updateTagsDOMCallback(event) {
console.log(event);
event.target.replaceChildren(event.detail.tags());
};

export const toggleTag = function toggleTagCallback(event) {
domCache.toggleTag(event.target);
_ui.get({id: 'cardContainer'}).replaceChildren(domCache.activeContacts());
}

// CONTACT HANDLING
export const filterContacts = function filterContactsCallback(event) {
_ui.get({id: 'cardContainer'}).replaceChildren(domCache.filterRows(event.target.value));
};

export const loadContacts = function loadAllContacts(event) {
if (api.method() === "get") {
let allContacts = event.target.response;
Expand Down

0 comments on commit 66a4d83

Please sign in to comment.