Skip to content

Commit

Permalink
fix CORS stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
cbh123 committed Oct 17, 2023
1 parent fe3b5a9 commit 939aed5
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,62 @@
//

// Include phoenix_html to handle method=PUT/DELETE in forms and buttons.
import "phoenix_html"
import "phoenix_html";
// Establish Phoenix Socket and LiveView configuration.
import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
import topbar from "../vendor/topbar"
import { Socket } from "phoenix";
import { LiveSocket } from "phoenix_live_view";
import topbar from "../vendor/topbar";
import FileSaver from "../vendor/file-saver";

let Hooks = {};

function genId() {
return Date.now().toString(36) + Math.random().toString(36).substr(2);
return Date.now().toString(36) + Math.random().toString(36).substr(2);
}

Hooks.AssignUserId = {
mounted() {
let userId = localStorage.getItem('userId');
if (!userId) {
userId = genId(); // replace this with your user id generation logic
localStorage.setItem('userId', userId);
}
this.pushEvent('assign-user-id', {userId: userId});
mounted() {
let userId = localStorage.getItem("userId");
if (!userId) {
userId = genId(); // replace this with your user id generation logic
localStorage.setItem("userId", userId);
}
};
this.pushEvent("assign-user-id", { userId: userId });
},
};

Hooks.DownloadImage = {
mounted() {
this.el.addEventListener("click", (e) => {
const link = this.el.getAttribute("phx-value-image");
const name = this.el.getAttribute("phx-value-name")
FileSaver.saveAs(link, `${name}.png`);
});
},
};
mounted() {
this.el.addEventListener("click", (e) => {
const link = this.el.getAttribute("phx-value-image");
const name = this.el.getAttribute("phx-value-name");

let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}, hooks: Hooks})
fetch(link)
.then((response) => response.blob())
.then((blob) => {
FileSaver.saveAs(blob, `${name}.png`);
});
});
},
};

// Show progress bar on live navigation and form submits
topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
window.addEventListener("phx:page-loading-start", _info => topbar.show(300))
window.addEventListener("phx:page-loading-stop", _info => topbar.hide())
let csrfToken = document
.querySelector("meta[name='csrf-token']")
.getAttribute("content");
let liveSocket = new LiveSocket("/live", Socket, {
params: { _csrf_token: csrfToken },
hooks: Hooks,
});

// Show progress bar on live navigation and form submits
topbar.config({ barColors: { 0: "#29d" }, shadowColor: "rgba(0, 0, 0, .3)" });
window.addEventListener("phx:page-loading-start", (_info) => topbar.show(300));
window.addEventListener("phx:page-loading-stop", (_info) => topbar.hide());
// connect if there are any LiveViews on the page
liveSocket.connect()
liveSocket.connect();

// expose liveSocket on window for web console debug logs and latency simulation:
// >> liveSocket.enableDebug()
// >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session
// >> liveSocket.disableLatencySim()
window.liveSocket = liveSocket
window.liveSocket = liveSocket;

0 comments on commit 939aed5

Please sign in to comment.