Skip to content

Commit

Permalink
add link to person in the card description
Browse files Browse the repository at this point in the history
  • Loading branch information
justincy committed Aug 16, 2017
1 parent 80e8d2c commit 3fa4ed0
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,22 @@ async function begin() {
loading();
$('#login').hide();

// Get/create Trello board and list

const boardId = await getBoardId();
const list = await getList(boardId, getFSPersonId());
const pid = getFSPersonId();
const personsName = await getFSPersonsName(pid);
const newCardDesc = `${personsName}'s profile: https://familysearch.org/tree/person/${pid}`;
let list = await getList(boardId, pid);
if(!list) {
list = await createList(boardId, personsName ? `${personsName} - ${pid}` : pid)
}
const listId = list.id;
$('#list-title').text(list.name);
await displayList(listId);

// Setup event listeners

$('#new-card-link').click(function(e){
$(this).hide();
$('#new-card').show();
Expand All @@ -91,17 +101,19 @@ async function begin() {
});

$('#new-card-button').click((e) => {
addNewCard(listId);
addNewCard(listId, newCardDesc);
e.stopPropagation();
});
$('#new-card-title').keypress(function(e) {
if(e.which == 13) {
addNewCard(listId);
addNewCard(listId, newCardDesc);
e.stopPropagation();
e.preventDefault();
}
});
$('#content').show();

// Now we're done loading
notLoading();
}

Expand All @@ -116,8 +128,9 @@ function notLoading() {
* Add a new card to the list
*
* @param {String} listId Trello list ID
* @param {String} desc
*/
function addNewCard(listId) {
function addNewCard(listId, desc) {
const $title = $('#new-card-title');
const title = $title.val().trim();

Expand All @@ -127,7 +140,8 @@ function addNewCard(listId) {

trelloRequest('POST', `/cards`, {
name: title,
idList: listId
idList: listId,
desc
}).then(() => {
$title.val('');
$('#new-card-button').prop('disabled', false);
Expand Down Expand Up @@ -180,28 +194,26 @@ function displayCard(card) {
* @param {String} pid FamilySearch person ID
*/
async function getList(boardId, pid) {

const nameRegex = new RegExp(`${pid}$`);

// Get all lists for this board
const existingList = await trelloRequest('GET', `/board/${boardId}/lists`, {
return await trelloRequest('GET', `/board/${boardId}/lists`, {
filter: 'open'
}).then((lists) => {
return lists.find(l => nameRegex.test(l.name));
});

if(existingList) {
return existingList;
}

// Create a new board for this person
else {
const personsName = await getFSPersonsName(pid);
return await trelloRequest('POST', `/board/${boardId}/lists`, {
name: personsName ? `${personsName} - ${pid}` : pid,
pos: 'bottom'
});
}
}

/**
* Create a Trello list
*
* @param {String} boardId Trello board ID
* @param {String} name Name of the new Trello board
* @return {Object} trello list
*/
async function createList(boardId, name) {
return await trelloRequest('POST', `/board/${boardId}/lists`, {
name,
pos: 'bottom'
});
}

/**
Expand Down

0 comments on commit 3fa4ed0

Please sign in to comment.