Skip to content

Commit 8b83103

Browse files
Merge pull request #36 from tinybirdco/stock_widget
replace retool with stock widget
2 parents 7688b47 + 5174ec9 commit 8b83103

38 files changed

+5889
-8578
lines changed

.github/workflows/tinybird_cd.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

.github/workflows/tinybird_ci.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.tinyb
22
.DS_Store
33
.diff_tmp
4+
.e

data-project/.tinyenv

Lines changed: 0 additions & 1 deletion
This file was deleted.

demo-ecomm-svelte/package-lock.json

Lines changed: 5742 additions & 8509 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo-ecomm-svelte/src/components/Footer.svelte

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
<footer class="mt-10">
22
<div class="py-10 text-gray-400 bg-backgroundDark">
33
<div class="container mx-auto px-10">
4-
<div class="mt-2 lg:text-2xl">
5-
Visit the
6-
<a
7-
href="/retool"
8-
class="mt-4 font-bold no-underline text-white hover:underline lg:text-2xl"
9-
target="_blank"
10-
>
11-
Procurement App
12-
</a>
13-
to rebalance stock.
14-
</div>
154
<div class="mt-2 lg:text-2xl">
165
Visit the
176
<a

demo-ecomm-svelte/src/components/Header.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
22
import { RefreshCw } from 'lucide-svelte';
33
import MockingbirdWidget from './MockingbirdWidget.svelte';
4+
import StockWidget from './StockWidget.svelte';
45
import { createTooltip } from '@melt-ui/svelte';
56
import { invalidateAll } from '$app/navigation';
67
@@ -64,6 +65,7 @@
6465
<p class="px-4 py-1 text-white text-sm">Refresh</p>
6566
</div>
6667
{/if}
68+
<StockWidget />
6769
<MockingbirdWidget />
6870
</div>
6971
</div>

demo-ecomm-svelte/src/components/MockingbirdWidget.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { createTooltip } from '@melt-ui/svelte';
33
import { createWorker, startWorker, stopWorker } from '../lib/workerBuilder';
44
import { Pause, Play } from 'lucide-svelte';
5-
import { fade } from 'svelte/transition';
65
76
/** @type {import('@tinybirdco/mockingbird').Schema} */
87
const schema = {
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<script>
2+
import { createTooltip } from '@melt-ui/svelte';
3+
import { Truck } from 'lucide-svelte';
4+
5+
const tbAppendToken = import.meta.env.VITE_TB_APPEND_TOKEN;
6+
const host = import.meta.env.VITE_TB_HOST;
7+
8+
const {
9+
elements: { trigger, content },
10+
states: { open }
11+
} = createTooltip({
12+
positioning: {
13+
placement: 'bottom'
14+
},
15+
openDelay: 0,
16+
closeDelay: 0,
17+
closeOnPointerDown: false,
18+
forceVisible: true
19+
});
20+
21+
const resetStock = async () => {
22+
try {
23+
console.log('Starting truncation...');
24+
await truncateLatestAvailability(); // Wait for truncation to complete
25+
console.log('Truncation complete, now appending stock...');
26+
appendStock(); // Call appendStock only if truncation is successful
27+
} catch (error) {
28+
console.error('Failed to reset stock:', error); // Log any errors
29+
}
30+
};
31+
32+
const truncateLatestAvailability = async () => {
33+
try {
34+
const response = await fetch(`https://${host}/v0/datasources/mat_stock_latest/truncate`, {
35+
method: 'POST',
36+
headers: { Authorization: `Bearer ${tbAppendToken}` }
37+
});
38+
39+
// Check if the response is okay
40+
if (!response.ok) {
41+
const errorText = await response.text(); // Get the error text if available
42+
throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);
43+
}
44+
45+
// Handle 205 Reset Content response
46+
if (response.status === 205) {
47+
console.log('Truncation successful, no content to return.');
48+
return; // No content to return, just exit the function
49+
}
50+
51+
// Attempt to parse the response as JSON for other status codes
52+
const res = await response.json();
53+
console.log(res);
54+
return res; // Ensure this returns a value
55+
} catch (error) {
56+
console.error('Error truncating availability:', error);
57+
throw error; // Rethrow the error to be caught in resetStock
58+
}
59+
};
60+
61+
const appendStock = () => {
62+
const data = [
63+
{
64+
timestamp: new Date().toISOString(),
65+
product: '5d0cgAl5BTk',
66+
store: 'warehouse_1',
67+
amount: 10000
68+
},
69+
{
70+
timestamp: new Date().toISOString(),
71+
product: 'YY4YaHKh2jQ',
72+
store: 'warehouse_1',
73+
amount: 10000
74+
},
75+
{
76+
timestamp: new Date().toISOString(),
77+
product: 'p8Drpg_duLw',
78+
store: 'warehouse_1',
79+
amount: 10000
80+
},
81+
{
82+
timestamp: new Date().toISOString(),
83+
product: 'sZzx0cUDX98',
84+
store: 'warehouse_1',
85+
amount: 50000
86+
},
87+
{
88+
timestamp: new Date().toISOString(),
89+
product: 'xFmXLq_KJxg',
90+
store: 'warehouse_1',
91+
amount: 1000
92+
},
93+
{
94+
timestamp: new Date().toISOString(),
95+
product: '6cHumpSxTvs',
96+
store: 'warehouse_1',
97+
amount: 100
98+
},
99+
{
100+
timestamp: new Date().toISOString(),
101+
product: 'Fg15LdqpWrs',
102+
store: 'warehouse_1',
103+
amount: 85
104+
},
105+
{
106+
timestamp: new Date().toISOString(),
107+
product: 'Zu7A1GCSjZE',
108+
store: 'warehouse_1',
109+
amount: 0
110+
},
111+
{
112+
timestamp: new Date().toISOString(),
113+
product: 'fSdBxY0NxVI',
114+
store: 'warehouse_1',
115+
amount: 0
116+
}
117+
];
118+
const ndjson = data.map((row) => JSON.stringify(row)).join('\n');
119+
fetch(`https://${host}/v0/events?name=warehouse_stock`, {
120+
method: 'POST',
121+
body: ndjson,
122+
headers: { Authorization: `Bearer ${tbAppendToken}` }
123+
})
124+
.then((res) => res.json())
125+
.then((data) => console.log(data))
126+
.catch((error) => console.error('Error appending stock:', error)); // Handle errors in appending
127+
};
128+
129+
</script>
130+
131+
<button
132+
class="relative flex h-8 w-8 items-center justify-center rounded-md border border-neutral-200 text-black transition-colors dark:border-neutral-700 dark:text-white"
133+
on:click={resetStock}
134+
use:melt={$trigger}
135+
>
136+
<Truck class="h-4 w-4" color="#fff" />
137+
</button>
138+
139+
{#if $open}
140+
<div use:melt={$content} class="z-50 rounded-lg bg-secondary shadow">
141+
<p class="px-4 py-1 text-white text-sm">Reset stock</p>
142+
</div>
143+
{/if}

demo-ecomm-svelte/src/routes/+page.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ export async function load({ fetch, url }) {
2020
products: res.data
2121
};
2222
} else {
23-
throw new Error(res);
23+
throw new Error(`Error fetching data: ${res.message || 'Unknown error'}`);
2424
}
2525
}

0 commit comments

Comments
 (0)