breakout into monorepo

This commit is contained in:
Dominic Ferrando
2026-03-02 13:39:47 -05:00
parent 6421f6527f
commit 45acec2cfd
90 changed files with 12179 additions and 2987 deletions
+13
View File
@@ -0,0 +1,13 @@
// See https://svelte.dev/docs/kit/types#app.d.ts
// for information about these interfaces
declare global {
namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface PageState {}
// interface Platform {}
}
}
export {};
+11
View File
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="107" height="128" viewBox="0 0 107 128"><title>svelte-logo</title><path d="M94.157 22.819c-10.4-14.885-30.94-19.297-45.792-9.835L22.282 29.608A29.92 29.92 0 0 0 8.764 49.65a31.5 31.5 0 0 0 3.108 20.231 30 30 0 0 0-4.477 11.183 31.9 31.9 0 0 0 5.448 24.116c10.402 14.887 30.942 19.297 45.791 9.835l26.083-16.624A29.92 29.92 0 0 0 98.235 78.35a31.53 31.53 0 0 0-3.105-20.232 30 30 0 0 0 4.474-11.182 31.88 31.88 0 0 0-5.447-24.116" style="fill:#ff3e00"/><path d="M45.817 106.582a20.72 20.72 0 0 1-22.237-8.243 19.17 19.17 0 0 1-3.277-14.503 18 18 0 0 1 .624-2.435l.49-1.498 1.337.981a33.6 33.6 0 0 0 10.203 5.098l.97.294-.09.968a5.85 5.85 0 0 0 1.052 3.878 6.24 6.24 0 0 0 6.695 2.485 5.8 5.8 0 0 0 1.603-.704L69.27 76.28a5.43 5.43 0 0 0 2.45-3.631 5.8 5.8 0 0 0-.987-4.371 6.24 6.24 0 0 0-6.698-2.487 5.7 5.7 0 0 0-1.6.704l-9.953 6.345a19 19 0 0 1-5.296 2.326 20.72 20.72 0 0 1-22.237-8.243 19.17 19.17 0 0 1-3.277-14.502 17.99 17.99 0 0 1 8.13-12.052l26.081-16.623a19 19 0 0 1 5.3-2.329 20.72 20.72 0 0 1 22.237 8.243 19.17 19.17 0 0 1 3.277 14.503 18 18 0 0 1-.624 2.435l-.49 1.498-1.337-.98a33.6 33.6 0 0 0-10.203-5.1l-.97-.294.09-.968a5.86 5.86 0 0 0-1.052-3.878 6.24 6.24 0 0 0-6.696-2.485 5.8 5.8 0 0 0-1.602.704L37.73 51.72a5.42 5.42 0 0 0-2.449 3.63 5.79 5.79 0 0 0 .986 4.372 6.24 6.24 0 0 0 6.698 2.486 5.8 5.8 0 0 0 1.602-.704l9.952-6.342a19 19 0 0 1 5.295-2.328 20.72 20.72 0 0 1 22.237 8.242 19.17 19.17 0 0 1 3.277 14.503 18 18 0 0 1-8.13 12.053l-26.081 16.622a19 19 0 0 1-5.3 2.328" style="fill:#fff"/></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

@@ -0,0 +1,295 @@
<script lang="ts">
import {
LevelCalculator,
Activity,
Attribute,
Gender,
lbToKg,
range,
type LevelCalculatorOutput,
type Standards,
type ActivityPerformance,
type Player,
} from "@blade-and-brawn/calculator";
interface CalcData {
levels?: LevelCalculatorOutput;
player: Player;
activityPerformances: ActivityPerformance[];
}
interface Props {
allStandards: Standards;
}
const { allStandards }: Props = $props();
const levelCalculator = $derived(new LevelCalculator(allStandards));
let calculations = $state([] as CalcData[]);
$effect(() => {
for (const calculation of calculations) {
calculation.levels = levelCalculator.calculate(
calculation.player,
calculation.activityPerformances,
);
}
});
$effect(() => {
const savedCalcs = localStorage.getItem("calculations");
if (savedCalcs) calculations = JSON.parse(savedCalcs);
});
$effect(() => {
localStorage.setItem("calculations", JSON.stringify(calculations));
});
$inspect(calculations[0]);
const createCalculation = function (name: string) {
return {
player: {
name: name,
metrics: {
age: 18,
weight: lbToKg(180),
gender: Gender.Male,
},
},
activityPerformances: Object.values(Activity).map((a) => ({
activity: a,
performance: 0,
})),
};
};
function downloadObject(obj: unknown, filename = "data.json") {
const json = JSON.stringify(obj, null, 2);
const blob = new Blob([json], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
}
const formatMs = (ms: number) => {
const total = Math.floor(ms / 1000);
const m = Math.floor(total / 60);
const s = total % 60;
return `${m}m ${s}s`;
};
const inchesToFeetInches = (inches: number) => {
const ft = Math.floor(inches / 12);
const rem = inches - ft * 12;
const whole = Math.floor(rem);
const isHalf = Math.abs(rem - whole - 0.5) < 1e-9;
const inchLabel = isHalf ? `${whole}½` : `${whole}`;
return `${ft}' ${inchLabel}"`;
};
const formatSeconds = (ms: number) => (ms / 1000).toFixed(2) + " seconds";
const performanceOptionsFromActivity = function (activity: Activity) {
const options: { name: string; value: string }[] = [];
switch (activity) {
case Activity.BackSquat:
case Activity.Deadlift:
case Activity.BenchPress:
for (let i = 0; i < 600; ++i) {
options.push({
name: String(i) + " lb",
value: String(lbToKg(i)),
});
}
break;
case Activity.Run: {
const MAX_MIN = 30;
for (let ms = 0; ms <= MAX_MIN * 60_000; ms += 1_000) {
options.push({ name: formatMs(ms), value: String(ms) });
}
break;
}
case Activity.BroadJump: {
const MAX_INCHES = 15 * 12 + 5;
for (let halfStep = 0; halfStep <= MAX_INCHES * 2; halfStep++) {
const inches = halfStep / 2;
options.push({
name: inchesToFeetInches(inches),
value: (inches * 2.54).toFixed(1),
});
}
break;
}
case Activity.ConeDrill: {
const MIN_MS = 5_000;
const MAX_MS = 20_000;
for (let ms = MIN_MS; ms <= MAX_MS; ms += 10) {
options.push({
name: formatSeconds(ms),
value: String(ms),
});
}
break;
}
}
return options;
};
</script>
<div class="mt-5 mb-5 flex w-full">
<div class="ml-auto">
<button
onclick={() =>
calculations.push(
createCalculation(String(calculations.length)),
)}
class="btn btn-primary">New</button
>
<button
onclick={() => downloadObject(calculations)}
class="btn btn-secondary">Export</button
>
</div>
</div>
<section
class="w-full px-3 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4"
>
{#each calculations as calculation, index}
<div
class="card card-compact bg-base-200 p-4 shadow-lg max-w-sm w-full"
>
<div class="card-body gap-4 p-4">
<input
class="input input-bordered input-sm w-full"
type="text"
bind:value={calculation.player.name}
placeholder="Player name"
/>
<ul
class="bg-base-100 rounded-box shadow-xs divide-y divide-base-300"
>
<li class="p-3">
<div class="flex items-center justify-between">
<div class="font-semibold text-sm">OVERALL</div>
<div class="badge badge-neutral badge-xs">
{calculation?.levels?.player || "N/A"}
</div>
</div>
</li>
{#each Object.values(Attribute) as attribute}
<li class="p-3">
<div class="flex items-center justify-between">
<div class="opacity-80 text-sm">
{attribute}
</div>
<div class="badge badge-ghost badge-xs">
{calculation?.levels?.attributes?.[
attribute
] || "N/A"}
</div>
</div>
</li>
{/each}
</ul>
<fieldset
class="fieldset bg-base-200/60 rounded-lg"
onchange={() =>
(calculation.levels = levelCalculator.calculate(
calculation.player,
calculation.activityPerformances,
))}
>
<div class="grid grid-cols-1 md:grid-cols-3 gap-3">
<label class="form-control">
<span class="label mb-1 text-xs">Gender</span>
<select
class="select select-bordered select-sm w-full"
bind:value={calculation.player.metrics.gender}
>
{#each Object.values(Gender) as gender}
<option value={gender}>{gender}</option>
{/each}
</select>
</label>
<label class="form-control">
<span class="label mb-1 text-xs">Age</span>
<input
class="input input-bordered input-sm w-full"
type="number"
min="1"
max="100"
step="1"
bind:value={calculation.player.metrics.age}
/>
</label>
<label class="form-control">
<span class="label mb-1 text-xs">Weight</span>
<select
class="select select-bordered select-sm w-full"
bind:value={calculation.player.metrics.weight}
>
{#each range(400) as weight}
<option value={String(lbToKg(weight))}
>{weight}</option
>
{/each}
</select>
</label>
</div>
</fieldset>
<fieldset
class="fieldset bg-base-200/60 rounded-lg"
onchange={() =>
(calculation.levels = levelCalculator.calculate(
calculation.player,
calculation.activityPerformances,
))}
>
<div class="grid grid-cols-2 gap-3">
{#each calculation.activityPerformances as activityPerformance}
<label class="form-control space-y-1">
<span class="label mb-1 text-xs">
{activityPerformance.activity}
</span>
<select
class="select select-bordered select-sm w-full"
bind:value={activityPerformance.performance}
>
{#each performanceOptionsFromActivity(activityPerformance.activity) as option}
<option value={option.value}
>{option.name}</option
>
{/each}
</select>
</label>
{/each}
</div>
</fieldset>
</div>
<button
onclick={() =>
confirm(`Delete ${calculation.player.name}?`) &&
calculations.splice(index, 1)}
class="btn btn-error btn-sm w-1/4">Delete</button
>
</div>
{/each}
</section>
@@ -0,0 +1,128 @@
<script lang="ts">
import {
Standards,
Activity,
cmToIn,
kgToLb,
range,
type Metrics,
type Standard,
type StandardsConfig,
} from "@blade-and-brawn/calculator";
interface Props {
allStandards: Standards;
selected: {
activity: Activity;
metrics: Metrics;
cfg: StandardsConfig;
};
}
const { selected, allStandards }: Props = $props();
const getLvlValue = (
activity: Activity,
standard: Standard,
lvl: number,
): string => {
const rawValue = standard.levels[lvl];
const unit = allStandards.byActivity(activity).getMetadata().unit;
switch (unit) {
case "ms":
const seconds = (rawValue / 1000).toFixed(1);
return `${seconds}`;
case "cm": {
const totalInches = cmToIn(rawValue);
const feet = Math.floor(totalInches / 12);
const inches = Math.round(totalInches % 12);
return `${feet}'${inches}"`;
}
case "kg":
return String(Math.round(kgToLb(rawValue)));
default:
return "";
}
};
</script>
<section class="w-full">
<div>
<div class="flex items-center">
<p class="text-xs label">
{#if selected.cfg.activity[selected.activity].enableGeneration && allStandards
.byActivity(selected.activity)
.getMetadata().generators.length}
(Generated data: {allStandards
.byActivity(selected.activity)
.getMetadata()
.generators.map((g) => g.metric)
.join(", ")})
{:else}
(Generated data: NONE)
{/if}
</p>
</div>
</div>
{#if !selected.metrics.age}
{#each allStandards.agesFor(selected.activity, selected.metrics.gender) as age}
{@render standardsTable(selected.activity, {
...selected.metrics,
age,
})}
{/each}
{:else}
{@render standardsTable(selected.activity, selected.metrics)}
{/if}
</section>
{#snippet standardsTable(activity: Activity, metrics: Metrics)}
{#snippet standardsTableRow(standard: Standard)}
<tr class="hover:bg-base-300">
<th>
{standard.metrics.weight
? parseFloat(kgToLb(standard.metrics.weight).toFixed(2))
: "None"}</th
>
{#each range(selected.cfg.global.maxLevel).map((i) => ++i) as lvl}
<td>{getLvlValue(activity, standard, lvl)}</td>
{/each}
</tr>
{/snippet}
<div class="mt-5">
<h3 class="text-lg italic font-semibold">{metrics.age}</h3>
<div class="overflow-x-auto">
<table class="table table-zebra table-pin-cols">
<thead>
<tr>
<th>Body weight</th>
{#each range(selected.cfg.global.maxLevel).map((i) => ++i) as lvl}
<td>{lvl}</td>
{/each}
</tr>
</thead>
<tbody>
{#if metrics.weight}
{@const standard = allStandards
.byActivity(activity)
.byGender(metrics.gender)
.byAge(metrics.age)
.byWeight(metrics.weight)
.getOneInterpolated()}
{@render standardsTableRow(standard)}
{:else}
{@const standards = allStandards
.byActivity(activity)
.byGender(metrics.gender)
.byAge(metrics.age)
.getAllInterpolated({ normalizeForLb: true })}
{#each standards as standard}
{@render standardsTableRow(standard)}
{/each}
{/if}
</tbody>
</table>
</div>
</div>
{/snippet}
+44
View File
@@ -0,0 +1,44 @@
<script lang="ts">
import { page } from "$app/state";
import favicon from "$lib/assets/favicon.svg";
import "./app.css";
const links = [
{ name: "Calculator", path: "/calculator" },
{ name: "Commerce", path: "/commerce" },
];
let { children } = $props();
</script>
<svelte:head>
<link rel="icon" href={favicon} />
</svelte:head>
<div class="drawer lg:drawer-open">
<input id="sidebar" type="checkbox" class="drawer-toggle" />
<div class="drawer-content p-10 flex flex-col items-center">
{@render children?.()}
<!-- <label for="sidebar" class="btn btn-primary drawer-button lg:hidden"> -->
<!-- Open drawer -->
<!-- </label> -->
</div>
<div class="drawer-side">
<label for="sidebar" aria-label="close sidebar" class="drawer-overlay"
></label>
<ul
class="menu menu-lg bg-base-200 text-base-content min-h-full w-72 p-4"
>
{#each links as link}
<li>
<a
class={{
"menu-active": page.url.pathname === link.path,
}}
href={link.path}>{link.name}</a
>
</li>
{/each}
</ul>
</div>
</div>
+6
View File
@@ -0,0 +1,6 @@
import { redirect } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
export const load: PageServerLoad = ({}) => {
redirect(308, "/calculator");
};
+19
View File
@@ -0,0 +1,19 @@
@import "tailwindcss";
@plugin "daisyui";
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
font:
14px/1.45 system-ui,
-apple-system,
Segoe UI,
Roboto,
Inter,
sans-serif;
}
@@ -0,0 +1,373 @@
<script lang="ts">
import {
Activity,
DEFAULT_STANDARDS_DATA,
Gender,
lbToKg,
Standards,
type Metrics,
type StandardsConfig,
} from "@blade-and-brawn/calculator";
import StandardsTable from "$lib/components/StandardsTable.svelte";
import PlayersTable from "$lib/components/PlayersTable.svelte";
let activeTab = $state("standards");
const defaultStandards = new Standards(DEFAULT_STANDARDS_DATA);
let input = $state({
activity: Activity.BenchPress,
metrics: {
gender: Gender.Male,
age: "",
weight: "",
},
cfg: {
global: {
maxLevel: String(defaultStandards.cfg.global.maxLevel),
},
activity: Object.fromEntries(
Object.values(Activity).map((activity) => {
return [
activity,
{
enableGeneration:
defaultStandards.cfg.activity[activity]
.enableGeneration,
weightAdvantage: String(
defaultStandards.cfg.activity[activity]
.weightModifier,
),
ageModifier: String(
defaultStandards.cfg.activity[activity]
.ageModifier,
),
difficultyModifier: String(
defaultStandards.cfg.activity[activity]
.difficultyModifier,
),
peakAge: String(
defaultStandards.cfg.activity[activity].peakAge,
),
stretch: {
upper: String(
defaultStandards.cfg.activity[activity]
.stretch.upper,
),
lower: String(
defaultStandards.cfg.activity[activity]
.stretch.lower,
),
},
},
];
}),
),
},
});
const selected = $derived({
activity: input.activity,
metrics: {
gender: input.metrics.gender,
age: +input.metrics.age,
weight: lbToKg(+input.metrics.weight),
} as Metrics,
cfg: {
global: {
maxLevel: +input.cfg.global.maxLevel,
},
activity: Object.fromEntries(
Object.values(Activity).map((activity) => [
activity,
{
weightModifier:
+input.cfg.activity[activity].weightAdvantage,
ageModifier: +input.cfg.activity[activity].ageModifier,
enableGeneration:
input.cfg.activity[activity].enableGeneration,
difficultyModifier:
+input.cfg.activity[activity].difficultyModifier,
peakAge: +input.cfg.activity[activity].peakAge,
stretch: {
lower: +input.cfg.activity[activity].stretch.lower,
upper: +input.cfg.activity[activity].stretch.upper,
},
},
]),
),
} as StandardsConfig,
});
const allStandards = $derived(
new Standards(DEFAULT_STANDARDS_DATA, selected.cfg),
);
$effect(() => {
const savedParameters = localStorage.getItem("parameters");
if (savedParameters) input = JSON.parse(savedParameters);
});
$effect(() => {
localStorage.setItem("parameters", JSON.stringify(input));
});
</script>
<section class="w-full flex justify-between gap-2 pt-10 items-center">
<fieldset class="fieldset bg-base-200 p-6 max-w-lg">
<legend class="fieldset-legend text-lg font-semibold"> Data </legend>
<div class="flex gap-4">
<fieldset class="fieldset bg-base-300 p-3 max-w-xl">
<legend class="fieldset-legend text-sm font-semibold">
General
</legend>
<div class="grid grid-cols-1 gap-4">
<label>
<span class="label mb-1">Activity</span>
<select
class="select w-full"
name="activities"
bind:value={input.activity}
>
{#each Object.values(Activity) as activity}
<option value={activity}>
{allStandards
.byActivity(activity)
.getMetadata().name}
</option>
{/each}
</select>
</label>
<label>
<span class="label mb-1">Max level</span>
<input
class="input w-full"
type="number"
min="1"
max="100"
placeholder="5"
bind:value={input.cfg.global.maxLevel}
/>
</label>
</div>
</fieldset>
<fieldset class="fieldset bg-base-300 p-3 max-w-3xs">
<legend class="fieldset-legend text-sm font-semibold">
Data Generation
</legend>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<label class="label">
<input
type="checkbox"
bind:checked={
input.cfg.activity[selected.activity]
.enableGeneration
}
class="toggle toggle-lg m-auto"
/>
</label>
<label>
<span class="label mb-1">Weight significance</span>
<input
class="input w-full"
type="number"
min="-1"
max="1"
step=".05"
placeholder=".1"
bind:value={
input.cfg.activity[selected.activity]
.weightAdvantage
}
disabled={!selected.cfg.activity[selected.activity]
.enableGeneration}
/>
</label>
<!-- <label> -->
<!-- <span class="label mb-1">Weight skew</span> -->
<!-- <input -->
<!-- class="input w-full" -->
<!-- type="number" -->
<!-- min="-1" -->
<!-- max="1" -->
<!-- step=".05" -->
<!-- bind:value={ -->
<!-- inputSelected.cfg.activity[selected.activity] -->
<!-- .weightSkew -->
<!-- } -->
<!-- disabled={!selected.cfg.activity[selected.activity] -->
<!-- .enableGeneration} -->
<!-- /> -->
<!-- </label> -->
<label>
<span class="label mb-1">Difficulty modifier</span>
<input
class="input w-full"
type="number"
min="-1"
max="1"
step=".05"
bind:value={
input.cfg.activity[selected.activity]
.difficultyModifier
}
disabled={!selected.cfg.activity[selected.activity]
.enableGeneration}
/>
</label>
<label>
<span class="label mb-1">Age significance</span>
<input
class="input w-full"
type="number"
min="-1"
max="1"
step=".05"
bind:value={
input.cfg.activity[selected.activity]
.ageModifier
}
disabled={!selected.cfg.activity[selected.activity]
.enableGeneration}
/>
</label>
<label>
<span class="label mb-1">Peak age</span>
<input
class="input w-full"
type="number"
min="1"
max="100"
step="1"
bind:value={
input.cfg.activity[selected.activity].peakAge
}
disabled={!selected.cfg.activity[selected.activity]
.enableGeneration}
/>
</label>
<fieldset class="fieldset bg-base-200 p-3 max-w-xl">
<legend class="fieldset-legend text-sm font-semibold">
Stretch
</legend>
<label>
<span class="label mb-1">Lower</span>
<input
class="input w-full"
type="number"
min="0"
max="10"
step="1"
bind:value={
input.cfg.activity[selected.activity]
.stretch.lower
}
disabled={!selected.cfg.activity[
selected.activity
].enableGeneration}
/>
</label>
<label>
<span class="label mb-1">Upper</span>
<input
class="input w-full"
type="number"
min="0"
max="10"
step="1"
bind:value={
input.cfg.activity[selected.activity]
.stretch.upper
}
disabled={!selected.cfg.activity[
selected.activity
].enableGeneration}
/>
</label>
</fieldset>
</div>
</fieldset>
</div>
</fieldset>
{#if activeTab === "standards"}
<fieldset class="fieldset bg-base-200 p-6 max-w-lg">
<legend class="fieldset-legend text-lg font-semibold">
Metrics
</legend>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<label>
<span class="label mb-1">Gender</span>
<select
class="select w-full"
name="genders"
bind:value={input.metrics.gender}
>
{#each Object.values(Gender) as gender}
<option value={gender}>{gender}</option>
{/each}
</select>
</label>
<label>
<span class="label mb-1">Age</span>
<input
class="input w-full"
type="number"
min="0"
max="100"
placeholder="18"
bind:value={input.metrics.age}
/>
</label>
<label>
<span class="label mb-1">Weight (lb)</span>
<input
class="input w-full"
type="number"
min="0"
max="600"
placeholder="170"
bind:value={input.metrics.weight}
/>
</label>
</div>
</fieldset>
{/if}
</section>
<div class="divider"></div>
<div role="tablist" class="tabs tabs-border">
<button
role="tab"
class="tab"
class:tab-active={activeTab === "standards"}
onclick={() => (activeTab = "standards")}
>
Standards
</button>
<button
role="tab"
class="tab"
class:tab-active={activeTab === "players"}
onclick={() => (activeTab = "players")}
>
Players
</button>
</div>
{#if activeTab === "standards"}
<StandardsTable {selected} {allStandards} />
{/if}
{#if activeTab === "players"}
<PlayersTable {allStandards} />
{/if}
@@ -0,0 +1,11 @@
import { PrintfulService, WebflowService } from "@blade-and-brawn/commerce";
import type { PageServerLoad } from "./$types";
export const load = async ({ }: Parameters<PageServerLoad>[0]) => {
return {
products: {
printful: PrintfulService.Products.getAll(),
webflow: WebflowService.Products.getAll(),
},
};
};
@@ -0,0 +1,131 @@
<script lang="ts">
import type { Printful } from "@blade-and-brawn/commerce";
import { onMount } from "svelte";
const { data } = $props();
const apiUrl = "http://localhost:3000";
const synchronizer = $state({
isSyncing: false,
syncingIds: [] as number[],
poll: async function () {
const res = (await (
await fetch(`${apiUrl}/products/sync`)
).json()) as any;
if (res.data) {
synchronizer.isSyncing = res.data.isSyncing;
synchronizer.syncingIds = res.data.syncingIds;
}
},
startPolling: () => {
const intervalId = setInterval(async () => {
await synchronizer.poll();
if (!synchronizer.isSyncing) clearInterval(intervalId);
}, 100);
},
syncAll: async () => {
synchronizer.startPolling();
await fetch(`${apiUrl}/products/sync`, { method: "POST" });
},
sync: async (printfulProductId: number) => {
synchronizer.startPolling();
await fetch(`${apiUrl}/products/sync/${printfulProductId}`, {
method: "POST",
});
},
});
onMount(async () => {
await synchronizer.poll();
if (synchronizer.isSyncing) {
synchronizer.startPolling();
}
});
const isProductSynced = async (
printfulProduct: Printful.Products.SyncProduct,
) => {
const webflowProducts = await data.products.webflow;
return webflowProducts.some(
(p) => p.product.id === printfulProduct.external_id.split("-")[0],
);
};
</script>
{#await data.products.printful}
<p>Loading...</p>
{:then printfulProducts}
<button
disabled={synchronizer.isSyncing}
onclick={() => synchronizer.syncAll()}
class="btn btn-xl mb-5"
>
{#if synchronizer.isSyncing}
Syncing...
{:else}
Sync all
{/if}
</button>
<div class="w-full overflow-x-auto">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Sync</th>
</tr>
</thead>
<tbody>
{#each printfulProducts as printfulProduct}
<tr class="hover:bg-base-300">
<td
><button
onclick={async () => {
const productData = (await (
await fetch(
`${apiUrl}/products/${printfulProduct.id}`,
)
).json()) as any;
console.log(productData);
}}
class="cursor-pointer underline"
>
{printfulProduct.name}
</button></td
>
<td>
{#await isProductSynced(printfulProduct) then isSynced}
{#if synchronizer.syncingIds.includes(printfulProduct.id)}
<div class="badge badge-warning">
Syncing...
</div>
{:else if isSynced}
<div class="badge badge-success">
Synced
</div>
{:else}
<div class="badge badge-error">
Desynced
</div>
{/if}
{/await}
</td>
<td>
<button
disabled={synchronizer.isSyncing}
onclick={() =>
synchronizer.sync(printfulProduct.id)}
class="btn"
>
Sync
</button>
</td>
</tr>
{/each}
</tbody>
</table>
</div>
{:catch error}
<p>Something went wrong: {error.message}</p>
{/await}