Skip to content

Commit

Permalink
[bin] Switch wokwi creation from POST to GET
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwofford committed May 14, 2024
1 parent 753f53f commit 741ae7b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
7 changes: 7 additions & 0 deletions pages/api/bin/wokwi/new/[parts].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { findOrCreateProject } from "."

export default async function handler(req, res) {
const parts = req.query.parts.split('|')
const shareLink = await findOrCreateProject(parts)
res.redirect(shareLink)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AirtablePlus from "airtable-plus"

const findOrCreateProject = async (partsList = []) => {
export const findOrCreateProject = async (partsList = []) => {
const airtable = new AirtablePlus({
apiKey: process.env.AIRTABLE_API_KEY,
baseID: 'appKjALSnOoA0EmPk',
Expand Down
28 changes: 6 additions & 22 deletions public/bin/landing-new/gambling.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,33 +174,17 @@ async function generateBuildLink(e) {
}
e.classList.add("disabled")
e.classList.add("loading")
const payload = {
parts: selectedParts
};

try {
const response = await fetch('/api/bin/wokwi/new/', {
mode: 'cors',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
})
if (!response.ok) {
throw new Error('Network response was not ok');
}
const json = await response.json()
const shareLink = json.shareLink
const parts = encodeURI(selectedParts.join('|'))
const origin = window.location.origin
const url = new URL(`${origin}/api/bin/wokwi/new/${parts}`)

window.open(url, '_blank').focus()

window.open(shareLink, '_blank').focus()
} catch (error) {
console.error('Error:', error)
// e.classList.add("error")
}
e.classList.remove("disabled")
e.classList.remove("loading")
}

window.addEventListener("load", async (e) => {
fetchedParts = await partsData()
document.querySelector(".gambling-roll").classList.remove("disabled")
Expand Down

0 comments on commit 741ae7b

Please sign in to comment.