-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
90 lines (82 loc) · 2.86 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Get the necessary elements from the DOM
const appContainer = document.getElementById("app");
const startBtn = document.getElementById("startBtn");
const initialHTML = appContainer.innerHTML; // Store the initial HTML content
// Define the user flow steps and button choices
const steps = [
{
text: "Find a menu item or button which looks related to what you want to do.",
choices: [
{ text: "Okay, I found one", nextStep: 1 },
{ text: "I can't find one", nextStep: 2 },
],
},
{
text: "Click the menu item or button. Did it work?",
choices: [
{ text: "Yes", nextStep: 4 },
{ text: "No", nextStep: 3 },
],
},
{
text: "Pick one at random.",
choices: [
{ text: "Okay", nextStep: 1 },
{ text: "I've tried them all", nextStep: 5 },
],
},
{
text: `<a href="https://google.com/" target="_blank">Google</a> the name of the program plus a few words related to what you want to do. Follow any instructions. Did it work?`,
choices: [
{ text: "Yes", nextStep: 4 },
{ text: "No", nextStep: 6 },
],
},
{
text: "You're done!",
choices: [{ text: "Start over", nextStep: -1 }],
},
{
text: "Have you been trying this for over half an hour?",
choices: [
{ text: "Yes", nextStep: 6 },
{ text: "No", nextStep: 1 },
],
},
{
text: "Ask someone for help or give up.",
choices: [{ text: "Start over", nextStep: -1 }],
},
];
let currentStep = 0; // Track the current step in the user flow
// Function to handle button clicks and update the user flow
function handleButtonClick(choiceIndex) {
const choice = steps[currentStep].choices[choiceIndex];
if (choice.nextStep === -1) {
// Start over choice, reset to initial state
currentStep = 0;
appContainer.innerHTML = initialHTML;
appContainer.classList.toggle("fadeIn"); // Apply fadeIn class
// Reattach event listener to the start button
document.getElementById("startBtn").addEventListener("click", renderStep);
} else if (choice.nextStep !== undefined) {
// Update the current step
currentStep = choice.nextStep;
appContainer.classList.toggle("fadeIn"); // Apply fadeIn class
renderStep();
}
}
// Function to render the current step in the user flow
function renderStep() {
const step = steps[currentStep];
let buttonsHTML = "";
if (step.choices) {
// Build the HTML for the button choices
buttonsHTML = step.choices.map((choice, index) => `<button onclick="handleButtonClick(${index})">${choice.text}</button>`).join("");
}
// Update the app container with the current step text and button choices
appContainer.innerHTML = `<p>${step.text}</p><section>${buttonsHTML}</section>`;
appContainer.classList.toggle("fadeIn"); // Apply fadeIn class
}
// Add event listener to the start button
document.getElementById("startBtn").addEventListener("click", renderStep);