Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated App doesn't work in Safari/Firefox #1506

Merged
merged 7 commits into from
Sep 19, 2022
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,54 @@
rel="stylesheet"
href="https://unpkg.com/@shopify/polaris@4.25.0/styles.min.css"
/>
<% if embedded_app? %> <script>
document.addEventListener("DOMContentLoaded", async function() {
<% if ShopifyApp.use_importmap? %>
await import("lib/shopify_app")
<% end %>

var SessionToken = window["app-bridge"].actions.SessionToken
var app = window.app;
<% if embedded_app? %>
<script type="module">
async function displayProducts() {
var SessionToken = window["app-bridge"].actions.SessionToken
var app = window.app;

app.dispatch(
SessionToken.request(),
);

// Save a session token for future requests
window.sessionToken = await new Promise((resolve) => {
app.subscribe(SessionToken.Action.RESPOND, (data) => {
resolve(data.sessionToken || "");
app.dispatch(
SessionToken.request(),
);
// Save a session token for future requests
window.sessionToken = await new Promise((resolve) => {
app.subscribe(SessionToken.Action.RESPOND, (data) => {
resolve(data.sessionToken || "");
});
});
});

var fetchProducts = function() {
var headers = new Headers({ "Authorization": "Bearer " + window.sessionToken });
return fetch("/products", { headers })
.then(response => response.json())
.then(data => {
var products = data.products;
var fetchProducts = function() {
var headers = new Headers({ "Authorization": "Bearer " + window.sessionToken });
return fetch("/products", { headers })
.then(response => response.json())
.then(data => {
var products = data.products;

if (products === undefined || products.length == 0) {
document.getElementById("products").innerHTML = "<br>No products to display.";
} else {
var list = "";
products.forEach((product) => {
var link = `<a target="_top" href="https://<%%= @shop_origin %>/admin/products/${product.id}">`
list += "<li>" + link + product.title + "</a></li>";
});
document.getElementById("products").innerHTML = "<ul>" + list + "</ul>";
}
});
}();
});
</script>
<% end %> </head>
if (products === undefined || products.length == 0) {
document.getElementById("products").innerHTML = "<br>No products to display.";
} else {
var list = "";
products.forEach((product) => {
var link = `<a target="_top" href="https://<%%= @shop_origin %>/admin/products/${product.id}">`
list += "<li>" + link + product.title + "</a></li>";
});
document.getElementById("products").innerHTML = "<ul>" + list + "</ul>";
}
});
}();
};

<% if ShopifyApp.use_importmap? %>
import "lib/shopify_app"
displayProducts();
<% else %>
document.addEventListener("DOMContentLoaded", async function() {
displayProducts();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify this?

Suggested change
document.addEventListener("DOMContentLoaded", async function() {
displayProducts();
});
document.addEventListener("DOMContentLoaded", displayProducts);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! I was trying to do this but I was using displayProducts() instead of displayProducts lesson learn lol. Thanks for this!

<% end %>
nelsonwittwer marked this conversation as resolved.
Show resolved Hide resolved
</script>
<% end %>
</head>
<body>
<h2>Products</h2>
<% if embedded_app? %> <div id="products"><br>Loading...</div><% else %>
Expand Down