@@ -19,17 +19,25 @@ const options = require('./boxen-options')
1919 * Check specifically if the user decided to quit the program or wanted to make another search
2020 * @param {String } input The picked choice after prompt
2121 */
22- function _checkUserAnswers ( input ) {
22+ function _checkUserAnswers ( input , lang ) {
2323 if ( input . userPick . includes ( '(Try another search)' ) ) {
2424 process . stdout . write ( '\u001Bc' ) // Clear the console
2525 header . logAppName ( )
2626 prompt . topicInteractive . menu = [ ]
2727 return _search ( )
2828 }
2929
30+ if ( input . userPick . includes ( '(Try another random)' ) ) {
31+ process . stdout . write ( '\u001Bc' ) // Clear the console
32+ prompt . randomInteractive . menu = [ ]
33+ return displayRandomArticlesList ( )
34+ }
35+
3036 if ( input . userPick . includes ( '(Quit)' ) ) {
3137 process . exit ( 1 )
3238 }
39+
40+ config . storeSearches ( input . userPick , lang )
3341}
3442
3543/**
@@ -41,7 +49,9 @@ function _fillInteractiveTopicsName(topics, promptName) {
4149 topics . forEach ( item => {
4250 prompt [ promptName ] . menu . push ( item . title )
4351 } )
44- prompt [ promptName ] . menu . push ( yellow ( '(Try another search)' ) )
52+ promptName === 'randomInteractive' ?
53+ prompt [ promptName ] . menu . push ( yellow ( '(Try another random)' ) ) :
54+ prompt [ promptName ] . menu . push ( yellow ( '(Try another search)' ) )
4555 prompt [ promptName ] . menu . push ( red ( '(Quit)' ) )
4656}
4757
@@ -117,17 +127,16 @@ async function _askForATopic() {
117127 */
118128async function _refineTopics ( ) {
119129 const input = await qoa . interactive ( prompt . topicInteractive )
120- await _checkUserAnswers ( input )
121130 const lang = await config . checkLang ( )
131+ await _checkUserAnswers ( input , lang )
122132 const response = await fetch . getArticle ( input . userPick , lang )
123133
124- config . storeSearches ( input . userPick , lang )
125134 _displayArticle ( response )
126135 prompt . topicInteractive . menu = [ ]
127136}
128137
129138/**
130- * Just calling other functions
139+ * Just calling other functions ¯\_(ツ)_/¯
131140 */
132141async function _search ( ) {
133142 await _askForATopic ( )
@@ -199,3 +208,21 @@ exports.displayPreviousSearches = async () => {
199208 _displayArticle ( response )
200209 prompt . historyInteractive . menu = [ ]
201210}
211+
212+ /**
213+ * Allow the user to display a list of random articles
214+ * Pick one of them, trigger an api call and display the response
215+ */
216+ const displayRandomArticlesList = async ( ) => {
217+ await header . logAppName ( )
218+ const lang = await config . checkLang ( )
219+ const suggestedTopics = await fetch . getRandomSuggestions ( lang )
220+ _fillInteractiveTopicsName ( suggestedTopics , 'randomInteractive' )
221+ const input = await qoa . interactive ( prompt . randomInteractive )
222+ await _checkUserAnswers ( input , lang )
223+ const response = await fetch . getArticle ( input . userPick , lang )
224+ _displayArticle ( response )
225+ prompt . historyInteractive . menu = [ ]
226+ }
227+
228+ exports . displayRandomArticlesList = displayRandomArticlesList
0 commit comments