@@ -2,6 +2,7 @@ defmodule AlgoraWeb.JobsLive do
2
2
@ moduledoc false
3
3
use AlgoraWeb , :live_view
4
4
5
+ alias Algora.Accounts
5
6
alias Algora.Jobs
6
7
alias Algora.Jobs.JobPosting
7
8
@@ -16,7 +17,8 @@ defmodule AlgoraWeb.JobsLive do
16
17
socket
17
18
|> assign ( :page_title , "Jobs" )
18
19
|> assign ( :jobs , jobs )
19
- |> assign ( :form , to_form ( changeset ) ) }
20
+ |> assign ( :form , to_form ( changeset ) )
21
+ |> assign_user_applications ( ) }
20
22
end
21
23
22
24
@ impl true
@@ -64,6 +66,15 @@ defmodule AlgoraWeb.JobsLive do
64
66
</ div >
65
67
</ div >
66
68
</ div >
69
+ <%= if MapSet . member? ( @ user_applications , job . id ) do %>
70
+ < . button disabled class = "opacity-50 " >
71
+ < . icon name = "tabler-check " class = "h-4 w-4 mr-2 -ml-1 " /> Applied
72
+ </ . button >
73
+ <% else %>
74
+ < . button phx-click = "apply_job " phx-value-job-id = { job . id } >
75
+ < . icon name = "github " class = "h-4 w-4 mr-2 " /> Apply with GitHub
76
+ </ . button >
77
+ <% end %>
67
78
</ div >
68
79
< div class = "text-sm text-muted-foreground " >
69
80
{ job . description }
@@ -142,4 +153,34 @@ defmodule AlgoraWeb.JobsLive do
142
153
{ :noreply , assign ( socket , :form , to_form ( changeset ) ) }
143
154
end
144
155
end
156
+
157
+ @ impl true
158
+ def handle_event ( "apply_job" , % { "job-id" => job_id } , socket ) do
159
+ if socket . assigns [ :current_user ] do
160
+ if Accounts . has_fresh_token? ( socket . assigns . current_user ) do
161
+ case Jobs . create_application ( job_id , socket . assigns . current_user ) do
162
+ { :ok , _application } ->
163
+ { :noreply , assign_user_applications ( socket ) }
164
+
165
+ { :error , _changeset } ->
166
+ { :noreply , put_flash ( socket , :error , "Failed to submit application. Please try again." ) }
167
+ end
168
+ else
169
+ { :noreply , redirect ( socket , external: Algora.Github . authorize_url ( ) ) }
170
+ end
171
+ else
172
+ { :noreply , redirect ( socket , external: Algora.Github . authorize_url ( ) ) }
173
+ end
174
+ end
175
+
176
+ defp assign_user_applications ( socket ) do
177
+ user_applications =
178
+ if socket . assigns [ :current_user ] do
179
+ Jobs . list_user_applications ( socket . assigns . current_user )
180
+ else
181
+ MapSet . new ( )
182
+ end
183
+
184
+ assign ( socket , :user_applications , user_applications )
185
+ end
145
186
end
0 commit comments