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

refacto askQuestion

parent 10b940ba
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ import { TezosToolkit } from '@taquito/taquito' ...@@ -6,7 +6,7 @@ import { TezosToolkit } from '@taquito/taquito'
import { InMemorySigner } from '@taquito/signer' import { InMemorySigner } from '@taquito/signer'
// IMPORT UTILS // IMPORT UTILS
import { askQuestion } from './src/utils/askQuestion.js' import { askMultipleQuestion } from './src/utils/askQuestion.js'
import { getAsciiArtText } from './src/utils/getAsciiArtText.js' import { getAsciiArtText } from './src/utils/getAsciiArtText.js'
import { getRole } from './src/utils/getRole.js' import { getRole } from './src/utils/getRole.js'
...@@ -36,26 +36,29 @@ program.command('main') ...@@ -36,26 +36,29 @@ program.command('main')
// DISPLAY HEADER // DISPLAY HEADER
await getAsciiArtText() await getAsciiArtText()
console.log('\n') 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 // START MAIN APPLICATION
while (true) { while (true) {
let choice: string let choice: string
switch (getRole(tezos)) { switch (getRole(tezos)) {
case 'ADMIN_FA_TOKEN': 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) await handleAdminFATokenChoice(choice, tezos)
break break
case 'ADMIN_ASSOCIATION': 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) await handleAdminAssociationChoice(choice, tezos)
break break
case 'ADHERENT': 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) await handleAdherentChoice(choice, tezos)
break break
case 'CONNECTED': 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) await handleConnectedChoice(choice, tezos)
break break
} }
......
...@@ -3,7 +3,7 @@ import chalk from 'chalk' ...@@ -3,7 +3,7 @@ import chalk from 'chalk'
import { type TezosToolkit } from '@taquito/taquito' import { type TezosToolkit } from '@taquito/taquito'
// IMPORT UTILS // IMPORT UTILS
import { askQuestion } from '../utils/askQuestion.js' import { askOneQuestion } from '../utils/askQuestion.js'
// IMPORT TYPES // IMPORT TYPES
import { type Association } from '../types/Association.js' import { type Association } from '../types/Association.js'
...@@ -22,8 +22,8 @@ import { burnToken } from '../services/token.service.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. * @returns {Promise<void>} A promise resolved once the association creation process is complete.
*/ */
async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> { async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> {
const name = await askQuestion(chalk.yellow('Nom: ')) const name = await askOneQuestion('Nom: ')
const description = await askQuestion(chalk.yellow('Description: ')) const description = await askOneQuestion('Description: ')
const association: Association = { const association: Association = {
name, name,
description description
...@@ -45,7 +45,7 @@ async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> { ...@@ -45,7 +45,7 @@ async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> {
* @returns {Promise<void>} A promise resolved once the token creation process is complete. * @returns {Promise<void>} A promise resolved once the token creation process is complete.
*/ */
async function handleCreateToken (tezos: TezosToolkit): Promise<void> { 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 { try {
const op = await createFAToken(nbTokenFungible, tezos) const op = await createFAToken(nbTokenFungible, tezos)
...@@ -76,8 +76,8 @@ async function handleGetBalance (tezos: TezosToolkit): Promise<void> { ...@@ -76,8 +76,8 @@ async function handleGetBalance (tezos: TezosToolkit): Promise<void> {
* @returns {Promise<void>} A promise resolved once the proposal creation process is complete. * @returns {Promise<void>} A promise resolved once the proposal creation process is complete.
*/ */
async function handleCreateProposal (tezos: TezosToolkit): Promise<void> { async function handleCreateProposal (tezos: TezosToolkit): Promise<void> {
const title = await askQuestion(chalk.yellow('Titre: ')) const title = await askOneQuestion('Titre: ')
const description = await askQuestion(chalk.yellow('Description: ')) const description = await askOneQuestion('Description: ')
const proposal: Proposal = { const proposal: Proposal = {
title, title,
description description
...@@ -99,7 +99,7 @@ async function handleCreateProposal (tezos: TezosToolkit): Promise<void> { ...@@ -99,7 +99,7 @@ async function handleCreateProposal (tezos: TezosToolkit): Promise<void> {
* @returns {Promise<void>} A promise resolved once the joining process is complete. * @returns {Promise<void>} A promise resolved once the joining process is complete.
*/ */
async function handleJoinAssociation (tezos: TezosToolkit): Promise<void> { 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 { try {
console.log("Inscription à l'association en cours...") console.log("Inscription à l'association en cours...")
...@@ -136,7 +136,7 @@ async function handleGetAssociations (tezos: TezosToolkit): Promise<void> { ...@@ -136,7 +136,7 @@ async function handleGetAssociations (tezos: TezosToolkit): Promise<void> {
* @returns {Promise<void>} A promise resolved once the joining process is complete. * @returns {Promise<void>} A promise resolved once the joining process is complete.
*/ */
async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void> { 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 { try {
console.log("Récupération des détails de l'association en cours...") console.log("Récupération des détails de l'association en cours...")
...@@ -154,7 +154,7 @@ async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void> ...@@ -154,7 +154,7 @@ async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void>
* @returns {Promise<void>} A promise resolved once the proposal creation process is complete. * @returns {Promise<void>} A promise resolved once the proposal creation process is complete.
*/ */
async function handleBurnToken (tezos: TezosToolkit): Promise<void> { 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 { try {
console.log('Brulure des tokens en cours...') console.log('Brulure des tokens en cours...')
......
import { type TezosToolkit } from '@taquito/taquito' 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' import chalk from 'chalk'
/** /**
...@@ -10,8 +10,6 @@ import chalk from 'chalk' ...@@ -10,8 +10,6 @@ import chalk from 'chalk'
*/ */
async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): Promise<void> { async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): Promise<void> {
switch (choice) { switch (choice) {
case '0':
break
case '1': case '1':
await handleCreateAssociation(tezos) await handleCreateAssociation(tezos)
break break
...@@ -24,6 +22,8 @@ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): P ...@@ -24,6 +22,8 @@ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): P
case '4': case '4':
await handleGetBalance(tezos) await handleGetBalance(tezos)
break break
case '5':
break
default: default:
console.log(chalk.bgRedBright('\nChoix invalide\n')) console.log(chalk.bgRedBright('\nChoix invalide\n'))
break break
...@@ -38,8 +38,6 @@ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): P ...@@ -38,8 +38,6 @@ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): P
*/ */
async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit): Promise<void> { async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit): Promise<void> {
switch (choice) { switch (choice) {
case '0':
break
case '1': case '1':
await handleCreateToken(tezos) await handleCreateToken(tezos)
break break
...@@ -49,6 +47,8 @@ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit ...@@ -49,6 +47,8 @@ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit
case '3': case '3':
await handleGetBalance(tezos) await handleGetBalance(tezos)
break break
case '5':
break
default: default:
console.log(chalk.bgRedBright('\nChoix invalide\n')) console.log(chalk.bgRedBright('\nChoix invalide\n'))
break break
...@@ -63,8 +63,6 @@ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit ...@@ -63,8 +63,6 @@ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit
*/ */
async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promise<void> { async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promise<void> {
switch (choice) { switch (choice) {
case '0':
break
case '1': case '1':
await handleCreateProposal(tezos) await handleCreateProposal(tezos)
break break
...@@ -74,6 +72,8 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi ...@@ -74,6 +72,8 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi
case '3': case '3':
await handleGetBalance(tezos) await handleGetBalance(tezos)
break break
case '5':
break
default: default:
console.log(chalk.bgRedBright('\nChoix invalide\n')) console.log(chalk.bgRedBright('\nChoix invalide\n'))
break break
...@@ -88,8 +88,6 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi ...@@ -88,8 +88,6 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi
*/ */
async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Promise<void> { async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Promise<void> {
switch (choice) { switch (choice) {
case '0':
break
case '1': case '1':
await handleJoinAssociation(tezos) await handleJoinAssociation(tezos)
break break
...@@ -102,6 +100,8 @@ async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Prom ...@@ -102,6 +100,8 @@ async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Prom
case '4': case '4':
await handleGetBalance(tezos) await handleGetBalance(tezos)
break break
case '5':
break
default: default:
console.log(chalk.bgRedBright('\nChoix invalide\n')) console.log(chalk.bgRedBright('\nChoix invalide\n'))
break break
......
import chalk from 'chalk'
import { createInterface } from 'readline' 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({ const readline = createInterface({
input: process.stdin, input: process.stdin,
output: process.stdout output: process.stdout
}) })
return await new Promise(resolve => { return await new Promise(resolve => {
readline.question(question, (answer: string) => { readline.question(chalk.yellow(question), (answer: string) => {
resolve(answer.trim()) resolve(answer.trim())
readline.close() readline.close()
}) })
}) })
} }
export { askQuestion } export { askMultipleQuestion, askOneQuestion }
...@@ -22,9 +22,9 @@ describe('roleHandlers', () => { ...@@ -22,9 +22,9 @@ describe('roleHandlers', () => {
}) })
describe('handleAdminFATokenChoice', () => { describe('handleAdminFATokenChoice', () => {
describe('when choice is "0"', () => { describe('when choice is "5"', () => {
it('should not call any handler', async () => { it('should not call any handler', async () => {
await handleAdminFATokenChoice('0', mockedTezosToolkit) await handleAdminFATokenChoice('5', mockedTezosToolkit)
expect(handleCreateAssociation).not.toHaveBeenCalled() expect(handleCreateAssociation).not.toHaveBeenCalled()
expect(handleCreateToken).not.toHaveBeenCalled() expect(handleCreateToken).not.toHaveBeenCalled()
...@@ -76,9 +76,9 @@ describe('roleHandlers', () => { ...@@ -76,9 +76,9 @@ describe('roleHandlers', () => {
}) })
describe('handleAdminAssociationChoice', () => { describe('handleAdminAssociationChoice', () => {
describe('when choice is "0"', () => { describe('when choice is "5"', () => {
it('should not call any handler', async () => { it('should not call any handler', async () => {
await handleAdminAssociationChoice('0', mockedTezosToolkit) await handleAdminAssociationChoice('5', mockedTezosToolkit)
expect(handleCreateToken).not.toHaveBeenCalled() expect(handleCreateToken).not.toHaveBeenCalled()
expect(handleBurnToken).not.toHaveBeenCalled() expect(handleBurnToken).not.toHaveBeenCalled()
...@@ -121,9 +121,9 @@ describe('roleHandlers', () => { ...@@ -121,9 +121,9 @@ describe('roleHandlers', () => {
}) })
describe('handleAdherentChoice', () => { describe('handleAdherentChoice', () => {
describe('when choice is "0"', () => { describe('when choice is "5"', () => {
it('should not call any handler', async () => { it('should not call any handler', async () => {
await handleAdherentChoice('0', mockedTezosToolkit) await handleAdherentChoice('5', mockedTezosToolkit)
expect(handleCreateProposal).not.toHaveBeenCalled() expect(handleCreateProposal).not.toHaveBeenCalled()
expect(handleCreateToken).not.toHaveBeenCalled() expect(handleCreateToken).not.toHaveBeenCalled()
...@@ -166,9 +166,9 @@ describe('roleHandlers', () => { ...@@ -166,9 +166,9 @@ describe('roleHandlers', () => {
}) })
describe('handleConnectedChoice', () => { describe('handleConnectedChoice', () => {
describe('when choice is "0"', () => { describe('when choice is "5"', () => {
it('should not call any handler', async () => { it('should not call any handler', async () => {
await handleConnectedChoice('0', mockedTezosToolkit) await handleConnectedChoice('5', mockedTezosToolkit)
expect(handleJoinAssociation).not.toHaveBeenCalled() expect(handleJoinAssociation).not.toHaveBeenCalled()
expect(handleCreateToken).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