-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_live.ex
More file actions
155 lines (127 loc) · 3.94 KB
/
password_live.ex
File metadata and controls
155 lines (127 loc) · 3.94 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
defmodule CorrecthorseWeb.PasswordLive do
@moduledoc false
use CorrecthorseWeb, :live_view
alias Correcthorse.Password
alias Correcthorse.Debounce.Debouncer
@default_min_words 4
@min_min_words 2
@max_min_words 50
def mount(_params, _session, socket) do
{:ok, debouncer} = Debouncer.start_link(self(), 500)
socket =
assign(socket,
min_words: @default_min_words,
min_chars: min_chars_from_min_words(@default_min_words),
separator: "-",
capitalise: :none,
append: [],
password: "",
wordlist: [],
_debouncer: debouncer
)
if connected?(socket) do
{:ok, assign_new_password(socket)}
else
{:ok, assign_password(socket, [])}
end
end
def handle_event("generate-password", _, socket) do
{:noreply, assign_new_password(socket)}
end
def handle_event("password-generation-details-changed", params, socket) do
%{_debouncer: debouncer} = socket.assigns
Debouncer.bounce(debouncer, :generate_new_password)
{min_words, min_chars} = extract_min_words_chars(params)
{:noreply, assign(socket, min_words: min_words, min_chars: min_chars)}
end
def handle_event(
"password-decoration-details-changed",
%{
"separator" => separator,
"capitalise" => capitalise,
"append" => append
},
socket
) do
capitalise = String.to_existing_atom(capitalise)
%{wordlist: wordlist} = socket.assigns
password = generated_password(wordlist, separator, append, capitalise)
{:noreply,
assign(socket,
separator: separator,
capitalise: capitalise,
append: append,
password: password
)}
end
def handle_info(:generate_new_password, socket) do
{:noreply, assign_new_password(socket)}
end
defp extract_min_words_chars(%{
"min-words" => min_words,
"min-chars" => min_chars,
"_target" => target
}) do
min_words = String.to_integer(min_words)
min_chars =
case target do
["min-words"] -> min_chars_from_min_words(min_words)
_ -> String.to_integer(min_chars)
end
{min_words, min_chars}
end
defp generated_password(wordlist, socket) do
%{separator: separator, append: append, capitalise: capitalise} = socket.assigns
generated_password(wordlist, separator, append, capitalise)
end
defp generated_password(wordlist, separator, append, capitalise) do
Password.to_password(wordlist, separator, append: append, capitalise: capitalise)
end
defp assign_new_password(socket) do
%{min_words: min_words, min_chars: min_chars} = socket.assigns
assign_password(
socket,
Password.words(min_words, min_chars)
)
end
defp assign_password(socket, wordlist) do
assign(socket,
wordlist: wordlist,
password: generated_password(wordlist, socket)
)
end
defp separators do
[{"", "none"}, {"-", "dash"}, {"1", "1"}, {" ", "space"}]
end
defp capitalisations do
[{:none, "None"}, {:first, "First"}, {:each_word, "Each"}]
end
defp radio_selection(name, current_value, values) do
assigns = %{}
~L"""
<%= for {value, id} <- values do %>
<input type="radio" id="<%=id %>" name="<%= name%>" value="<%= value %>"
<%= if current_value == value, do: "checked" %>
/>
<label for="%<=name %>"><%= id %></label>
<% end %>
"""
end
defp append_checkbox(append, checked) do
assigns = %{}
~L"""
<input type="checkbox"
id="append<%= append %>"
value = "<%= append %>"
name= "append[]"
<%= if checked, do: "checked" %>
/>
<label for="append<%= append %>"><%= append %></label>
"""
end
defp min_chars_from_min_words(min_words), do: min_words * 5
defp max_min_words, do: @max_min_words
defp min_min_words, do: @min_min_words
defp min_min_chars, do: min_chars_from_min_words(min_min_words())
defp max_min_chars, do: min_chars_from_min_words(max_min_words())
end