Skip to content

Commit c882f94

Browse files
committed
store user return to in session
1 parent caf8586 commit c882f94

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

assets/js/app.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,4 +766,22 @@ window.addEventListener("phx:open_popup", (e: CustomEvent) => {
766766
}
767767
});
768768

769+
// Add event listener for storing session values
770+
window.addEventListener("phx:store-session", (event) => {
771+
const token = document
772+
.querySelector('meta[name="csrf-token"]')
773+
.getAttribute("content");
774+
775+
console.log(event.detail);
776+
777+
fetch("/store-session", {
778+
method: "POST",
779+
headers: {
780+
"Content-Type": "application/json",
781+
"X-CSRF-Token": token,
782+
},
783+
body: JSON.stringify(event.detail),
784+
});
785+
});
786+
769787
export default Hooks;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule AlgoraWeb.StoreSessionController do
2+
use AlgoraWeb, :controller
3+
4+
def create(conn, params) do
5+
dbg(params)
6+
7+
updated_conn =
8+
Enum.reduce(params, conn, fn {key, value}, acc_conn ->
9+
put_session(acc_conn, String.to_existing_atom(key), value)
10+
end)
11+
12+
send_resp(updated_conn, 200, "")
13+
end
14+
end

lib/algora_web/live/jobs_live.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,16 @@ defmodule AlgoraWeb.JobsLive do
214214
{:noreply, put_flash(socket, :error, "Failed to submit application. Please try again.")}
215215
end
216216
else
217-
{:noreply, redirect(socket, external: Algora.Github.authorize_url())}
217+
{:noreply,
218+
socket
219+
|> push_event("store-session", %{user_return_to: "/jobs"})
220+
|> redirect(external: Algora.Github.authorize_url())}
218221
end
219222
else
220-
{:noreply, redirect(socket, external: Algora.Github.authorize_url())}
223+
{:noreply,
224+
socket
225+
|> push_event("store-session", %{user_return_to: "/jobs"})
226+
|> redirect(external: Algora.Github.authorize_url())}
221227
end
222228
end
223229

lib/algora_web/router.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ defmodule AlgoraWeb.Router do
145145

146146
live "/0/bounties/:id", OG.BountyLive, :show
147147
get "/og/*path", OGImageController, :generate
148+
149+
post "/store-session", StoreSessionController, :create
148150
end
149151

150152
scope "/api", AlgoraWeb.API do

0 commit comments

Comments
 (0)