Skip to content
Snippets Groups Projects
Commit 048f8b72 authored by Nawfel Senoussi's avatar Nawfel Senoussi
Browse files

refacto askQuestion

parent 10b940ba
Branches master
No related tags found
No related merge requests found
......@@ -6,7 +6,7 @@ import { TezosToolkit } from '@taquito/taquito'
import { InMemorySigner } from '@taquito/signer'
// IMPORT UTILS
import { askQuestion } from './src/utils/askQuestion.js'
import { askMultipleQuestion } from './src/utils/askQuestion.js'
import { getAsciiArtText } from './src/utils/getAsciiArtText.js'
import { getRole } from './src/utils/getRole.js'
......@@ -36,26 +36,29 @@ program.command('main')
// DISPLAY HEADER
await getAsciiArtText()
console.log('\n')
console.log(chalk.bgBlue(`Vous êtes connecté en tant que ${getRole(tezos)}\n`))
console.log(chalk.bgBlue(`Vous êtes connecté en tant que ${getRole(tezos)}`))
console.log('\n')
console.log(chalk.yellow('Que souhaitez-vous faire ?'))
console.log('\n')
// START MAIN APPLICATION
while (true) {
let choice: string
switch (getRole(tezos)) {
case 'ADMIN_FA_TOKEN':
choice = await askQuestion(`${chalk.yellow('Que souhaitez-vous faire ?')} \n\n1: Créer une association\n2: Créer un token\n3: Bruler mes tokens\n4: Voir mon portefeuille\n0: Quitter\n`)
choice = await askMultipleQuestion(['Créer une association', 'Créer un token', 'Bruler mes tokens', 'Voir mon portefeuille', 'Quitter'])
await handleAdminFATokenChoice(choice, tezos)
break
case 'ADMIN_ASSOCIATION':
choice = await askQuestion(`${chalk.yellow('Que souhaitez-vous faire ?')} \n\n1: Créer un token\n2: Bruler mes tokens\n3: Voir mon portefeuille\n0: Quitter\n`)
choice = await askMultipleQuestion(['Créer un token', 'Bruler mes tokens', 'Voir mon portefeuille', 'Quitter'])
await handleAdminAssociationChoice(choice, tezos)
break
case 'ADHERENT':
choice = await askQuestion(`${chalk.yellow('Que souhaitez-vous faire ?')} \n\n1: Faire une proposition\n2: Créer un token FA\n3: Voir mon portefeuille\n0: Quitter\n`)
choice = await askMultipleQuestion(['Faire une proposition', 'Créer un token FA', 'Voir mon portefeuille', 'Quitter'])
await handleAdherentChoice(choice, tezos)
break
case 'CONNECTED':
choice = await askQuestion(`${chalk.yellow('Que souhaitez-vous faire ?')} \n\n1: Rejoindre une association\n2: Créer un token FA\n3: Voir les associations\n4: Voir mon portefeuille\n0: Quitter\n`)
choice = await askMultipleQuestion(['Rejoindre une association', 'Créer un token FA', 'Voir les associations', 'Voir mon portefeuille', 'Quitter'])
await handleConnectedChoice(choice, tezos)
break
}
......
......@@ -3,7 +3,7 @@ import chalk from 'chalk'
import { type TezosToolkit } from '@taquito/taquito'
// IMPORT UTILS
import { askQuestion } from '../utils/askQuestion.js'
import { askOneQuestion } from '../utils/askQuestion.js'
// IMPORT TYPES
import { type Association } from '../types/Association.js'
......@@ -22,8 +22,8 @@ import { burnToken } from '../services/token.service.js'
* @returns {Promise<void>} A promise resolved once the association creation process is complete.
*/
async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> {
const name = await askQuestion(chalk.yellow('Nom: '))
const description = await askQuestion(chalk.yellow('Description: '))
const name = await askOneQuestion('Nom: ')
const description = await askOneQuestion('Description: ')
const association: Association = {
name,
description
......@@ -45,7 +45,7 @@ async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> {
* @returns {Promise<void>} A promise resolved once the token creation process is complete.
*/
async function handleCreateToken (tezos: TezosToolkit): Promise<void> {
const nbTokenFungible: string = await askQuestion(chalk.yellow('Nombre de token fongible: '))
const nbTokenFungible: string = await askOneQuestion('Nombre de token fongible: ')
try {
const op = await createFAToken(nbTokenFungible, tezos)
......@@ -76,8 +76,8 @@ async function handleGetBalance (tezos: TezosToolkit): Promise<void> {
* @returns {Promise<void>} A promise resolved once the proposal creation process is complete.
*/
async function handleCreateProposal (tezos: TezosToolkit): Promise<void> {
const title = await askQuestion(chalk.yellow('Titre: '))
const description = await askQuestion(chalk.yellow('Description: '))
const title = await askOneQuestion('Titre: ')
const description = await askOneQuestion('Description: ')
const proposal: Proposal = {
title,
description
......@@ -99,7 +99,7 @@ async function handleCreateProposal (tezos: TezosToolkit): Promise<void> {
* @returns {Promise<void>} A promise resolved once the joining process is complete.
*/
async function handleJoinAssociation (tezos: TezosToolkit): Promise<void> {
const name = await askQuestion(chalk.yellow("Nom de l'association: "))
const name = await askOneQuestion("Nom de l'association: ")
try {
console.log("Inscription à l'association en cours...")
......@@ -136,7 +136,7 @@ async function handleGetAssociations (tezos: TezosToolkit): Promise<void> {
* @returns {Promise<void>} A promise resolved once the joining process is complete.
*/
async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void> {
const name = await askQuestion(chalk.yellow("Nom de l'association: "))
const name = await askOneQuestion("Nom de l'association: ")
try {
console.log("Récupération des détails de l'association en cours...")
......@@ -154,7 +154,7 @@ async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void>
* @returns {Promise<void>} A promise resolved once the proposal creation process is complete.
*/
async function handleBurnToken (tezos: TezosToolkit): Promise<void> {
const nbTokenToBurn = await askQuestion(chalk.yellow('Nombre de token à bruler: '))
const nbTokenToBurn = await askOneQuestion('Nombre de token à bruler: ')
try {
console.log('Brulure des tokens en cours...')
......
import { type TezosToolkit } from '@taquito/taquito'
import { handleBurnToken, handleCreateAssociation, handleCreateProposal, handleCreateToken, handleGetAssociations, handleGetBalance, handleJoinAssociation } from './handlers'
import { handleBurnToken, handleCreateAssociation, handleCreateProposal, handleCreateToken, handleGetAssociations, handleGetBalance, handleJoinAssociation } from './handlers.js'
import chalk from 'chalk'
/**
......@@ -10,8 +10,6 @@ import chalk from 'chalk'
*/
async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): Promise<void> {
switch (choice) {
case '0':
break
case '1':
await handleCreateAssociation(tezos)
break
......@@ -24,6 +22,8 @@ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): P
case '4':
await handleGetBalance(tezos)
break
case '5':
break
default:
console.log(chalk.bgRedBright('\nChoix invalide\n'))
break
......@@ -38,8 +38,6 @@ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): P
*/
async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit): Promise<void> {
switch (choice) {
case '0':
break
case '1':
await handleCreateToken(tezos)
break
......@@ -49,6 +47,8 @@ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit
case '3':
await handleGetBalance(tezos)
break
case '5':
break
default:
console.log(chalk.bgRedBright('\nChoix invalide\n'))
break
......@@ -63,8 +63,6 @@ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit
*/
async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promise<void> {
switch (choice) {
case '0':
break
case '1':
await handleCreateProposal(tezos)
break
......@@ -74,6 +72,8 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi
case '3':
await handleGetBalance(tezos)
break
case '5':
break
default:
console.log(chalk.bgRedBright('\nChoix invalide\n'))
break
......@@ -88,8 +88,6 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi
*/
async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Promise<void> {
switch (choice) {
case '0':
break
case '1':
await handleJoinAssociation(tezos)
break
......@@ -102,6 +100,8 @@ async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Prom
case '4':
await handleGetBalance(tezos)
break
case '5':
break
default:
console.log(chalk.bgRedBright('\nChoix invalide\n'))
break
......
import chalk from 'chalk'
import { createInterface } from 'readline'
async function askQuestion (question: string): Promise<string> {
async function askMultipleQuestion (options: string[]): Promise<string> {
const readline = createInterface({
input: process.stdin,
output: process.stdout
})
return await new Promise((resolve) => {
options.forEach((option, index) => {
console.log(`${index + 1}: ${option}`)
})
readline.question('\nVotre choix: ', (answer: string) => {
readline.close()
resolve(answer.trim())
})
})
}
async function askOneQuestion (question: string): Promise<string> {
const readline = createInterface({
input: process.stdin,
output: process.stdout
})
return await new Promise(resolve => {
readline.question(question, (answer: string) => {
readline.question(chalk.yellow(question), (answer: string) => {
resolve(answer.trim())
readline.close()
})
})
}
export { askQuestion }
export { askMultipleQuestion, askOneQuestion }
......@@ -22,9 +22,9 @@ describe('roleHandlers', () => {
})
describe('handleAdminFATokenChoice', () => {
describe('when choice is "0"', () => {
describe('when choice is "5"', () => {
it('should not call any handler', async () => {
await handleAdminFATokenChoice('0', mockedTezosToolkit)
await handleAdminFATokenChoice('5', mockedTezosToolkit)
expect(handleCreateAssociation).not.toHaveBeenCalled()
expect(handleCreateToken).not.toHaveBeenCalled()
......@@ -76,9 +76,9 @@ describe('roleHandlers', () => {
})
describe('handleAdminAssociationChoice', () => {
describe('when choice is "0"', () => {
describe('when choice is "5"', () => {
it('should not call any handler', async () => {
await handleAdminAssociationChoice('0', mockedTezosToolkit)
await handleAdminAssociationChoice('5', mockedTezosToolkit)
expect(handleCreateToken).not.toHaveBeenCalled()
expect(handleBurnToken).not.toHaveBeenCalled()
......@@ -121,9 +121,9 @@ describe('roleHandlers', () => {
})
describe('handleAdherentChoice', () => {
describe('when choice is "0"', () => {
describe('when choice is "5"', () => {
it('should not call any handler', async () => {
await handleAdherentChoice('0', mockedTezosToolkit)
await handleAdherentChoice('5', mockedTezosToolkit)
expect(handleCreateProposal).not.toHaveBeenCalled()
expect(handleCreateToken).not.toHaveBeenCalled()
......@@ -166,9 +166,9 @@ describe('roleHandlers', () => {
})
describe('handleConnectedChoice', () => {
describe('when choice is "0"', () => {
describe('when choice is "5"', () => {
it('should not call any handler', async () => {
await handleConnectedChoice('0', mockedTezosToolkit)
await handleConnectedChoice('5', mockedTezosToolkit)
expect(handleJoinAssociation).not.toHaveBeenCalled()
expect(handleCreateToken).not.toHaveBeenCalled()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment