onEditTitle(loc, newTitle)}
+ locationSpoolOrder={spoolOrder}
+ setLocationSpoolOrder={(spoolOrder: number[]) => setLocationSpoolOrder(loc, spoolOrder)}
/>
);
});
diff --git a/client/src/pages/locations/components/spoolList.tsx b/client/src/pages/locations/components/spoolList.tsx
index 0b68fdc..1a9f7b7 100644
--- a/client/src/pages/locations/components/spoolList.tsx
+++ b/client/src/pages/locations/components/spoolList.tsx
@@ -9,22 +9,41 @@ import { SpoolCard } from "./spoolCard";
const { useToken } = theme;
-export function SpoolList({ location, spools }: { location: string; spools: ISpool[] }) {
+export function SpoolList({
+ location,
+ spools,
+ spoolOrder,
+ setSpoolOrder,
+}: {
+ location: string;
+ spools: ISpool[];
+ spoolOrder: number[];
+ setSpoolOrder: (spoolOrder: number[]) => void;
+}) {
const { token } = useToken();
const invalidate = useInvalidate();
const [, spoolDrop] = useDrop(() => ({
accept: ItemTypes.SPOOL,
- drop: (item: ISpool) => {
- setSpoolLocation(item.id, location).then(() => {
- invalidate({
- resource: "spool",
- id: item.id,
- invalidates: ["list", "detail"],
- });
+ drop: async (item: ISpool) => {
+ if (item.location === location) return;
+ await setSpoolLocation(item.id, location);
+ await invalidate({
+ resource: "spool",
+ id: item.id,
+ invalidates: ["list", "detail"],
});
},
}));
+ // Make sure all spools are in the spoolOrders array
+ const finalSpoolOrder = [...spoolOrder].filter((id) => spools.find((spool) => spool.id === id)); // Remove any spools that are not in the spools array
+ spools.forEach((spool) => {
+ if (!finalSpoolOrder.includes(spool.id)) finalSpoolOrder.push(spool.id);
+ });
+
+ // Reorder the spools based on the spoolOrder array
+ const reorderedSpools = finalSpoolOrder.map((id) => spools.find((spool) => spool.id === id)!);
+
const style = {
backgroundColor: token.colorBgContainer,
borderRadius: token.borderRadiusLG,
@@ -32,7 +51,7 @@ export function SpoolList({ location, spools }: { location: string; spools: ISpo
return (
- {spools.map((spool) => (
+ {reorderedSpools.map((spool) => (
))}
diff --git a/client/src/pages/locations/functions.ts b/client/src/pages/locations/functions.ts
index 0091efc..6d6e6ee 100644
--- a/client/src/pages/locations/functions.ts
+++ b/client/src/pages/locations/functions.ts
@@ -19,21 +19,6 @@ export async function setSpoolLocation(spool_id: number, location: string | null
return response.json();
}
-export function useLocations(): string[] {
- const query = useGetSetting("locations");
-
- return useMemo(() => {
- if (!query.data) return [];
-
- try {
- return JSON.parse(query.data.value) as string[];
- } catch {
- console.warn("Failed to parse locations", query.data.value);
- return [];
- }
- }, [query.data]);
-}
-
export async function renameSpoolLocation(location: string, newName: string): Promise {
const response = await fetch(getAPIURL() + "/location/" + location, {
method: "PATCH",
@@ -49,3 +34,33 @@ export async function renameSpoolLocation(location: string, newName: string): Pr
}
return await response.text();
}
+
+export function useLocations(): string[] {
+ const query = useGetSetting("locations");
+
+ return useMemo(() => {
+ if (!query.data) return [];
+
+ try {
+ return JSON.parse(query.data.value) as string[];
+ } catch {
+ console.warn("Failed to parse locations", query.data.value);
+ return [];
+ }
+ }, [query.data]);
+}
+
+export function useLocationsSpoolOrders(): Record {
+ const query = useGetSetting("locations_spoolorders");
+
+ return useMemo(() => {
+ if (!query.data) return {};
+
+ try {
+ return JSON.parse(query.data.value) as Record;
+ } catch {
+ console.warn("Failed to parse locations spool orders", query.data.value);
+ return {};
+ }
+ }, [query.data]);
+}
diff --git a/spoolman/settings.py b/spoolman/settings.py
index bb7217c..5051031 100644
--- a/spoolman/settings.py
+++ b/spoolman/settings.py
@@ -70,3 +70,4 @@ register_setting("extra_fields_spool", SettingType.ARRAY, json.dumps([]))
register_setting("base_url", SettingType.STRING, json.dumps(""))
register_setting("locations", SettingType.ARRAY, json.dumps([]))
+register_setting("locations_spoolorders", SettingType.OBJECT, json.dumps({}))