kitchen-boy / portfolio-generator

Module 9 - Node.js
1 stars 0 forks source link

Title: Prompt user for more input #3

Closed kitchen-boy closed 2 years ago

kitchen-boy commented 2 years ago

Description

Profile questions

kitchen-boy commented 2 years ago

9.3: Node Package Manager: Lesson Goals

kitchen-boy commented 2 years ago

STEPS:

Create new feature branch:

IMPORTANT

Create the .gitignore file during the initial setup of the project to avoid accidentally tracking the folder with the git add -A command. If the node_modules folder was accidentally tracked by git, use the following command to remove the tracking: $ git rm -r --cached node_modules.

kitchen-boy commented 2 years ago

Import Inquirer into app.js

const inquirer = require('inquirer'); // Verify that inquirer is available by adding console.log(inquirer) in the app.js file. console.log(inquirer)

kitchen-boy commented 2 years ago

Is the order of your require statements important?

Not for the functionality of the application, but remember you also need to code for humans to read. Try to group by source: npm packages, personal modules, and core library modules often get grouped together.

kitchen-boy commented 2 years ago

Use Inquirer

inquirer .prompt([ { type: 'input', name: 'name', message: 'What is your name?' } ]) .then(answers => console.log(answers));

promptUser().then(answers => console.log(answers));

kitchen-boy commented 2 years ago

Add profile & project questions:

const promptUser = () => { return inquirer.prompt([ { type: 'input', name: 'name', message: 'What is your name?' }, { type: 'input', name: 'github', message: 'Enter your GitHub Username' }, { type: 'input', name: 'about', message: 'Provide some information about yourself:' } ]); };

const promptProject = () => { console.log( Add a New Project ); return inquirer.prompt([ { type: 'input', name: 'name', message: 'What is the name of your project?' }, { type: 'input', name: 'description', message: 'Provide a description of the project (Required)' }, { type: 'checkbox', name: 'languages', message: 'What did you build this project with? (Check all that apply)', choices: ['JavaScript', 'HTML', 'CSS', 'ES6', 'jQuery', 'Bootstrap', 'Node'] }, { type: 'input', name: 'link', message: 'Enter the GitHub link to your project. (Required)' }, { type: 'confirm', name: 'feature', message: 'Would you like to feature this project?', default: false }, { type: 'confirm', name: 'confirmAddProject', message: 'Would you like to enter another project?', default: false } ]); };

promptUser() .then(answers => console.log(answers)) .then(promptProject) .then(projectAnswers => console.log(projectAnswers));

kitchen-boy commented 2 years ago

Add Multiple Projects

2 ways for object storage: We pick 2nd option.

  1. Create a global variable that can store the project data but Global variables, however, can be read and altered by other parts of your program, making them susceptible to changes. A local variable wouldn't work to store multiple projects, because the variable would reset at every function call (even tho' Local variables work better because they're limited in scope, so they can't be affected by other parts of the application. )
  2. Have the promptProject() function accept an argument. With the argument option, we could add project data to the argument variable and then call the function with the modified data.

const promptProject = portfolioData => {

portfolioData.projects = [];

// If there's no 'projects' array property, create one. if (!portfolioData.projects) { portfolioData.projects = []; }

.then(projectData => { portfolioData.projects.push(projectData); });

.then(projectData => { portfolioData.projects.push(projectData); if (projectData.confirmAddProject) { return promptProject(portfolioData); } else { return portfolioData; } });

else { return portfolioData; }

promptUser() .then(promptProject) .then(portfolioData => { console.log(portfolioData); });

kitchen-boy commented 2 years ago

9.3.6 Validate Answers:

{ type: 'input', name: 'name', message: 'What is your name? (Required)', validate: nameInput => { if (nameInput) { return true; } else { console.log('Please enter your name!'); return false; } } },

{ type: 'confirm', name: 'confirmAbout', message: 'Would you like to enter some information about yourself for an "About" section?', default: true },

Using when method to conditionally prompt a question based on the user's input:

{ type: 'confirm', name: 'confirmAbout', message: 'Would you like to enter some information about yourself for an "About" section?', default: true }, { type: 'input', name: 'about', message: 'Provide some information about yourself:', when: ({ confirmAbout }) => { if (confirmAbout) { return true; } else { return false; } } }

{ "name": "lernantino", "github": "lernantino", "confirmAbout": true }

kitchen-boy commented 2 years ago

9.3 Node Package Manager (npm):

Learned the following things:

Validate user answers with <inquirer> and conditionally prompt users based on answers.

End of Lesson 3: Code status:

Beginning of Lesson 4: Code status: