Skip to content

Commit

Permalink
get/create trello board and person's list
Browse files Browse the repository at this point in the history
  • Loading branch information
justincy committed Aug 10, 2017
1 parent f69cebb commit 63e8d58
Showing 1 changed file with 71 additions and 8 deletions.
79 changes: 71 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const BOARD_NAME = 'FamilySearch Research Tasks';
const BOARD_DESCRIPTION = 'Track your research tasks in Trello. Used by the FamilySearch Custom Trello Tab.';

setup();

Expand Down Expand Up @@ -63,23 +65,84 @@ function trelloRequest(method, url, params = {}) {
});
}

function begin() {
async function begin() {
$('#login').hide();
document.write('<h2>Authenticated</h2>');
chooseBoard();
const boardId = await getBoardId();
const listId = await getListId(boardId, getFSPersonId());

console.log(listId);
}

function getFSPersonId() {
const params = (new URL(document.location.href)).searchParams;
return params.get('pid');
}

/**
* Get the ID of the Trello list for this person, or create a new one
*
* @param {String} boardId Trello board ID
* @param {String} pid FamilySearch person ID
*/
async function getListId(boardId, pid) {

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

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

if(existingList) {
return existingList.id;
}

// Create a new board for this person
else {
const response = await trelloRequest('POST', `/board/${boardId}/lists`, {
// TODO: add person's name to the board name
name: pid,
pos: 'bottom'
});
return response.id;
}
}

/**
* Choose an existing FamilySearch board or create a new one
* Get the ID of the FamilySearch board or create a new board and return its ID
*/
async function chooseBoard() {
async function getBoardId() {

// Get a list of all personal (non-org boards)
const personalBoards = await trelloRequest('GET', '/members/me/boards', {
// Get a list of open boards
const existingFamilySearchBoard = await trelloRequest('GET', '/members/me/boards', {
filter: 'open'
}).then((boards) => {
})

// Filter to just personal boards
.then((boards) => {
return boards.filter(b => b.idOrganization === null);
})

// Choose a FamilySearch board if one exists
.then((boards) => {
return boards.find(b => b.name === BOARD_NAME);
});

// Is there an existing FamilySearch board?
if(existingFamilySearchBoard) {
return existingFamilySearchBoard.id;
}

// Create a board if one doesn't exist
else {
const response = await trelloRequest('POST', '/boards', {
name: BOARD_NAME,
desc: BOARD_DESCRIPTION,
defaultLists: false
});
return response.id;
}

}

0 comments on commit 63e8d58

Please sign in to comment.