|
|
|
// Cards module - everything related to cards should go here (database install is automatic for AO server so no feature module)
|
|
|
|
import inquirer from 'inquirer'
|
|
|
|
import { getCardByName, createCard, prioritizeCard } from './api.js'
|
|
|
|
import { headerStyle } from './styles.js'
|
|
|
|
import { prioritiesMenu } from './priority.js'
|
|
|
|
import { aoEnv } from './settings.js'
|
|
|
|
|
|
|
|
// The card menu is complex and so has been split into this separate file
|
|
|
|
export async function cardMenu() {
|
|
|
|
console.log(`\n${headerStyle('My Deck')}`)
|
|
|
|
const cardChoices = [
|
|
|
|
{ name: 'Top priorities', value: 'priorities', short: 'priorities' }, // hand? (7) (add #s in parens)
|
|
|
|
{ name: 'Cards in hand', value: 'subcards', short: 'hand' }, // (current) deck? (60)
|
|
|
|
{ name: 'Browse full deck', value: 'browse', short: 'browse' }, // archive? (10,000)
|
|
|
|
'Back to AO Menu'
|
|
|
|
]
|
|
|
|
try {
|
|
|
|
const answer = await inquirer.prompt({
|
|
|
|
name: 'card_menu',
|
|
|
|
type: 'list',
|
|
|
|
message: 'Please choose:',
|
|
|
|
choices: cardChoices,
|
|
|
|
pageSize: cardChoices.length,
|
|
|
|
})
|
|
|
|
switch(answer.card_menu) {
|
|
|
|
case 'priorities':
|
|
|
|
while(await prioritiesMenu()) {}
|
|
|
|
break
|
|
|
|
case 'subcards':
|
|
|
|
while(await subcardsMenu()) {}
|
|
|
|
break
|
|
|
|
case 'browse':
|
|
|
|
while(await browseMenu()) {}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} catch(error) {
|
|
|
|
if (error === 'EVENT_INTERRUPTED') {
|
|
|
|
console.log('\nESC')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
async function subcardsMenu() {
|
|
|
|
console.log('Not yet implemented')
|
|
|
|
}
|
|
|
|
|
|
|
|
async function browseMenu() {
|
|
|
|
console.log('Not yet implemented')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the user to create a card, checks if it already exists, and then creates it if it doesn't
|
|
|
|
export async function createCardInteractive(prioritized = true) {
|
|
|
|
console.log('createcardinteractive')
|
|
|
|
const memberId = aoEnv('AO_CLI_SESSION_MEMBERID')
|
|
|
|
if(!memberId) {
|
|
|
|
console.log('Not logged in.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const answer = await inquirer.prompt({
|
|
|
|
name: 'new_card_text',
|
|
|
|
type: 'input',
|
|
|
|
message: 'New card or Enter to end:',
|
|
|
|
})
|
|
|
|
if(answer.new_card_text.trim().length <= 0) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Check if the card alerady exists
|
|
|
|
const fetchedCards = await getCardByName(answer.new_card_text, false)
|
|
|
|
if(fetchedCards && fetchedCards.length >= 1) {
|
|
|
|
if(fetchedCards.length >= 2) {
|
|
|
|
console.log('More than one copy of this card was found. This should not happen.')
|
|
|
|
}
|
|
|
|
if(prioritized) {
|
|
|
|
console.log('Card already exists, prioritizing.')
|
|
|
|
const prioritizeResult = await prioritizeCard(fetchedCards[0].taskId, memberId)
|
|
|
|
if(!prioritizeResult.ok) {
|
|
|
|
console.log('May have failed to prioritize card:', prioritizeResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
console.log('card does not exist yet. creating...', answer.new_card_text)
|
|
|
|
const result = await createCard(answer.new_card_text, false, true)
|
|
|
|
return answer.new_card_text
|
|
|
|
}
|