-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ex
95 lines (75 loc) · 2.44 KB
/
index.ex
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
91
92
93
94
95
defmodule MenasQuizWeb.QuestionLive.Index do
use MenasQuizWeb, :live_view
alias MenasQuiz.Quiz
@impl true
def mount(_params, _session, socket) do
result = Quiz.get_questions([])
socket =
socket
|> assign(:quiz_state, "idle")
|> assign(:selected_answer_id, nil)
|> assign(:current_question_index, 0)
|> assign(:asked_questions, [])
case result do
%{questions: questions} ->
socket =
socket
|> assign(:asked_questions, Enum.map(questions, & &1.question))
|> assign(:questions, questions)
|> assign(:current_question, hd(questions))
{:ok, socket}
%{error: error_message} ->
{:ok, put_flash(socket, :error, error_message)}
end
end
@impl true
def handle_event("new_question", _params, socket) do
result = Quiz.get_questions(socket.assigns.asked_questions)
socket =
socket
|> assign(:quiz_state, "idle")
|> assign(:selected_answer_id, nil)
|> assign(:current_question_index, 0)
case result do
%{questions: questions} ->
appended_asked_questions =
socket.assigns.asked_questions ++ Enum.map(questions, & &1.question)
socket =
socket
|> assign(:asked_questions, appended_asked_questions)
|> assign(:questions, questions)
|> assign(:current_question, hd(questions))
{:noreply, socket}
%{error: error_message} ->
{:noreply, put_flash(socket, :error, error_message)}
end
end
@impl true
def handle_event("next_question", _params, socket) do
next_index = socket.assigns.current_question_index + 1
if next_index < length(socket.assigns.questions) do
next_question = Enum.at(socket.assigns.questions, next_index)
socket =
socket
|> assign(:current_question_index, next_index)
|> assign(:current_question, next_question)
|> assign(:quiz_state, "idle")
|> assign(:selected_answer_id, nil)
{:noreply, socket}
else
socket =
socket
|> assign(:quiz_state, "finished")
{:noreply, socket}
end
end
def handle_event("submit_answer", %{"answer" => answer}, socket) do
quiz_state =
if answer == socket.assigns.current_question.solution_id, do: "correct", else: "wrong"
socket =
socket
|> assign(:selected_answer_id, answer)
|> assign(:quiz_state, quiz_state)
{:noreply, socket}
end
end