Skip to content

Commit

Permalink
route building
Browse files Browse the repository at this point in the history
  • Loading branch information
DzikStar committed Nov 28, 2024
1 parent 63ea1f5 commit 166678a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions assets/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ function createElementWithClass(tagName, className = "") {
return element;
}

function buildRoute(routeName, args = [], env){
let prefix;

if(env == "DEV"){
prefix = "https://wumpus-central.github.io";
} else {
const depth = window.location.pathname.split('/').filter(Boolean).length;
prefix = depth === 0 ? "./" : "../".repeat(depth);
}

const routes = {
EXPERIMENTS_COLLECTION: `/experiments-archive/data/experiments.json`,
EXPERIMENT_WITH_KIND: `/experiments-archive/data/${args[0]}/${args[1]}.json`,
EXPERIMENT_NO_KIND: `/experiments-archive/data/${args[0]}.json`
}

return `${prefix}${routes[routeName]}`;
}

/*
CONSTRUCTORS
Expand All @@ -36,7 +54,7 @@ function pageUnderConstruction(){
async function buildList(kind) {
const pageMount = document.getElementById("page-mount");

const response = await fetch('../../experiments-archive/data/experiments.json');
const response = await fetch(buildRoute("EXPERIMENTS_COLLECTION"));
const experiments = (await response.json()).reverse();

const listContainer = createElementWithClass("div", "experimentsListContainer");
Expand All @@ -63,7 +81,7 @@ async function openExperiment(exp_id, exp_kind){
const experimentCardHeader = createElementWithClass("div", "experimentCardHeader");
const experimentCardContent = createElementWithClass("div", "experimentCardContent");

const fixedExpPath = exp_kind ? `../../experiments-archive/data/${exp_kind}/${exp_id}.json` : `../../experiments-archive/data/${exp_id}.json`;
const fixedExpPath = exp_kind ? buildRoute("EXPERIMENT_WITH_KIND", [exp_kind, exp_id]) : buildRoute("EXPERIMENT_NO_KIND", [exp_id]);
const response = await fetch(fixedExpPath);
let experiment = await response.json();

Expand Down

0 comments on commit 166678a

Please sign in to comment.