Skip to content

Commit a10d614

Browse files
authored
Merge pull request #148 from fsi-tue/feat/better-order-management
Feat/better order management
2 parents 3c17a64 + 46c3651 commit a10d614

49 files changed

Lines changed: 1113 additions & 1087 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bun.lock

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

cache-handler.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class CacheHandler {
2424
tags = [tags].flat()
2525
// Iterate over all entries in the cache
2626
for (let [key, value] of cache) {
27-
// If the value's tags include the specified tag, delete this entry
27+
// If the value's tags include the specified tag, reset this entry
2828
if (value.tags.some((tag) => tags.includes(tag))) {
2929
cache.delete(key)
3030
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"dependencies": {
2727
"@date-fns/utc": "^2.1.0",
2828
"@tailwindcss/postcss": "^4.1.8",
29+
"@tanstack/react-query": "^5.80.7",
2930
"@types/js-yaml": "^4.0.9",
3031
"@yudiel/react-qr-scanner": "^2.3.1",
3132
"accept-language": "^3.0.20",

src/app/WithSystemCheck.tsx

Lines changed: 20 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,39 @@
11
'use client'
22

33
import { Construction } from "lucide-react";
4-
import { ComponentType, useCallback, useEffect, useMemo, useState } from "react";
4+
import React, { ComponentType } from "react";
55
import { Loading } from "@/app/components/Loading";
66
import { useTranslations } from 'next-intl';
7-
import { SystemStatus } from "@/model/system";
7+
import ErrorMessage from "./components/ErrorMessage";
8+
import { useSystem } from "@/lib/fetch/system";
89

9-
let globalSystemStatus: SystemStatus | null = null;
10-
let lastFetchTime = 0;
11-
const CACHE_DURATION = 30000; // 30 seconds
12-
13-
type ExtendedSystemStatus = SystemStatus | 'checking';
1410
const WithSystemCheck = <P extends object>(
1511
WrappedComponent: ComponentType<P>
1612
): ComponentType<P> => {
1713

1814
return function WithSystemCheckComponent(props: P) {
1915
const t = useTranslations();
20-
const [systemStatus, setSystemStatus] = useState<ExtendedSystemStatus>(() => {
21-
const now = Date.now();
22-
if (globalSystemStatus && (now - lastFetchTime) < CACHE_DURATION) {
23-
return globalSystemStatus;
24-
}
25-
return 'checking';
26-
});
27-
const [loading, setLoading] = useState<boolean>(() => {
28-
const now = Date.now();
29-
return !(globalSystemStatus && (now - lastFetchTime) < CACHE_DURATION);
30-
});
31-
32-
const checkSystemStatus = useCallback(async (): Promise<void> => {
33-
const now = Date.now();
34-
if (globalSystemStatus && (now - lastFetchTime) < CACHE_DURATION) {
35-
setSystemStatus(globalSystemStatus);
36-
setLoading(false);
37-
return;
38-
}
3916

40-
setLoading(true);
4117

42-
try {
43-
const response = await fetch('/api/manage/system/status', {
44-
method: 'GET',
45-
credentials: 'include',
46-
// Add cache control for browser caching
47-
cache: 'no-store'
48-
});
18+
const { data, error, isFetching } = useSystem()
4919

50-
if (!response.ok) {
51-
throw new Error(`HTTP error! status: ${response.status}`);
52-
}
53-
54-
const data: { status: SystemStatus } = await response.json();
20+
if (isFetching) {
21+
return <Loading message={t('withsystemcheck.check_system_status')}/>
22+
}
5523

56-
// Update global cache
57-
globalSystemStatus = data.status;
58-
lastFetchTime = Date.now();
24+
if (error) {
25+
return <ErrorMessage error={error.message}/>;
26+
}
5927

60-
setSystemStatus(data.status);
61-
} catch (error) {
62-
console.error('Error fetching system status:', error);
63-
// Keep previous status if available, otherwise set to inactive
64-
if (!globalSystemStatus) {
65-
setSystemStatus('inactive');
66-
}
67-
} finally {
68-
setLoading(false);
69-
}
70-
}, []);
28+
if (!data) {
29+
return null;
30+
}
7131

72-
useEffect(() => {
73-
checkSystemStatus();
74-
}, [checkSystemStatus]);
32+
if (data.status.active) {
33+
return <WrappedComponent {...props} />;
34+
}
7535

76-
// Memoize the inactive system UI to prevent unnecessary re-renders
77-
const inactiveSystemUI = useMemo(() => (
36+
return (
7837
<div className="bg-white rounded-2xl p-6 shadow-sm border border-gray-100">
7938
<div className="flex items-center gap-3 mb-2">
8039
<div className="w-8 h-8 bg-red-100 rounded-full flex items-center justify-center">
@@ -84,26 +43,11 @@ const WithSystemCheck = <P extends object>(
8443
{t('withsystemcheck.system_inactive')}
8544
</h1>
8645
</div>
87-
<p className="text-gray-500 text-sm">
88-
{t('withsystemcheck.system_unavailable_message')}
46+
<p className="text-gray-500 text-md">
47+
{data.status.message ?? t('withsystemcheck.system_unavailable_message')}
8948
</p>
9049
</div>
91-
), [t]);
92-
93-
// Memoize loading component
94-
const loadingUI = useMemo(() => (
95-
<Loading message={t('withsystemcheck.check_system_status')}/>
96-
), [t]);
97-
98-
if (loading) {
99-
return loadingUI;
100-
}
101-
102-
if (systemStatus === 'active') {
103-
return <WrappedComponent {...props} />;
104-
}
105-
106-
return inactiveSystemUI;
50+
)
10751
};
10852
};
10953

0 commit comments

Comments
 (0)