~mna/hockeysim

2086a9ccfcf89c66019c518b09a46920185cae41 — Martin Angers 7 months ago 487ab3c master
refactor season list to use league selection
3 files changed, 38 insertions(+), 29 deletions(-)

M src/cmds/season_cmds/list.js
M src/cmds/season_cmds/new.js
M src/leagues.js
M src/cmds/season_cmds/list.js => src/cmds/season_cmds/list.js +7 -1
@@ 1,5 1,8 @@
import CFonts from 'cfonts'
import { DB } from 'mymigrate'
import inquirer from 'inquirer'
import { listOrSelectLeagueQuestion } from '../../questions'
import { leaguesWithPrefix } from '../../leagues'
import exec from '../../handlers/season_cmds/list'

export const command = ['list', '$0']


@@ 20,7 23,10 @@ export async function handler (argv) {
  // TODO: select league from list
  const conn = await new DB ().connect ()
  try {
    await exec (conn, argv)
    const res = await inquirer.prompt ([
      listOrSelectLeagueQuestion (conn, 'Select league:', leaguesWithPrefix, argv.league ?? ''),
    ])
    await exec (conn, res)
  } finally {
    await conn.end ()
  }

M src/cmds/season_cmds/new.js => src/cmds/season_cmds/new.js +23 -28
@@ 16,32 16,24 @@ export function builder (yargs) {
    .string ('league')
}

// TODO: (for all commands) display error if nothing can be selected.

function questions (conn, { league = '' }) {
  return () => {
    return inquirer.prompt ([
      listOrSelectLeagueQuestion (conn, 'Select league:', leaguesForNewSeason, league),
      {
        type: 'number',
        name: 'year',
        message: 'Season year:',
        default ({ leagueId }) {
          return nextSeasonForLeague (conn, leagueId)
        },
        validate (input) {
          const year = parseInt (input, 10)
          if (Number.isNaN (year)) {
            return 'invalid number'
          }
          if (year < Constants.minSeasonYear || year > Constants.maxSeasonYear) {
            return `invalid year, must be between ${Constants.minSeasonYear} and ${Constants.maxSeasonYear}`
          }
          return true
        },
      },
      confirmQuestion ('Create this season?'),
    ])
function selectYearQuestion (conn) {
  return {
    type: 'number',
    name: 'year',
    message: 'Season year:',
    default ({ leagueId }) {
      return nextSeasonForLeague (conn, leagueId)
    },
    validate (input) {
      const year = parseInt (input, 10)
      if (Number.isNaN (year)) {
        return 'invalid number'
      }
      if (year < Constants.minSeasonYear || year > Constants.maxSeasonYear) {
        return `invalid year, must be between ${Constants.minSeasonYear} and ${Constants.maxSeasonYear}`
      }
      return true
    },
  }
}



@@ 53,8 45,11 @@ export async function handler (argv) {

  const conn = await new DB ().connect ()
  try {
    const prompt = questions (conn, argv)
    const res = await prompt ()
    const res = await inquirer.prompt ([
      listOrSelectLeagueQuestion (conn, 'Select league:', leaguesForNewSeason, argv.league ?? ''),
      selectYearQuestion (conn),
      confirmQuestion ('Create this season?'),
    ])
    if (res.confirm) {
      await exec (conn, res)
    }

M src/leagues.js => src/leagues.js +8 -0
@@ 96,3 96,11 @@ export async function leaguesForNewSeason (conn, prefix) {
    )`, [LeagueState.active, prefix + '%', SeasonState.completed])
  return res
}

export async function leaguesWithPrefix (conn, prefix) {
  const [res] = await conn.query (`
    select id, name
    from   leagues l
    where  l.name like ?`, prefix + '%')
  return res
}