Added reading and using of spool orders in locations
This commit is contained in:
@@ -21,6 +21,8 @@ export function Location({
|
|||||||
onDelete,
|
onDelete,
|
||||||
moveLocation,
|
moveLocation,
|
||||||
onEditTitle,
|
onEditTitle,
|
||||||
|
locationSpoolOrder,
|
||||||
|
setLocationSpoolOrder,
|
||||||
}: {
|
}: {
|
||||||
index: number;
|
index: number;
|
||||||
title: string;
|
title: string;
|
||||||
@@ -29,6 +31,8 @@ export function Location({
|
|||||||
onDelete?: () => void;
|
onDelete?: () => void;
|
||||||
moveLocation: (dragIndex: number, hoverIndex: number) => void;
|
moveLocation: (dragIndex: number, hoverIndex: number) => void;
|
||||||
onEditTitle: (newTitle: string) => void;
|
onEditTitle: (newTitle: string) => void;
|
||||||
|
locationSpoolOrder: number[];
|
||||||
|
setLocationSpoolOrder: (spoolOrder: number[]) => void;
|
||||||
}) {
|
}) {
|
||||||
const [editTitle, setEditTitle] = useState(false);
|
const [editTitle, setEditTitle] = useState(false);
|
||||||
const [newTitle, setNewTitle] = useState(title);
|
const [newTitle, setNewTitle] = useState(title);
|
||||||
@@ -123,7 +127,12 @@ export function Location({
|
|||||||
)}
|
)}
|
||||||
{showDelete && <Button icon={<DeleteOutlined />} size="small" type="text" onClick={onDelete} />}
|
{showDelete && <Button icon={<DeleteOutlined />} size="small" type="text" onClick={onDelete} />}
|
||||||
</h3>
|
</h3>
|
||||||
<SpoolList location={title} spools={spools} />
|
<SpoolList
|
||||||
|
location={title}
|
||||||
|
spools={spools}
|
||||||
|
spoolOrder={locationSpoolOrder}
|
||||||
|
setSpoolOrder={setLocationSpoolOrder}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Button } from "antd";
|
|||||||
import { useEffect, useMemo } from "react";
|
import { useEffect, useMemo } from "react";
|
||||||
import { useSetSetting } from "../../../utils/querySettings";
|
import { useSetSetting } from "../../../utils/querySettings";
|
||||||
import { ISpool } from "../../spools/model";
|
import { ISpool } from "../../spools/model";
|
||||||
import { renameSpoolLocation, useLocations } from "../functions";
|
import { renameSpoolLocation, useLocations, useLocationsSpoolOrders } from "../functions";
|
||||||
import { Location } from "./location";
|
import { Location } from "./location";
|
||||||
|
|
||||||
export function LocationContainer() {
|
export function LocationContainer() {
|
||||||
@@ -14,6 +14,10 @@ export function LocationContainer() {
|
|||||||
const settingsLocations = useLocations();
|
const settingsLocations = useLocations();
|
||||||
const setLocationsSetting = useSetSetting<string[]>("locations");
|
const setLocationsSetting = useSetSetting<string[]>("locations");
|
||||||
|
|
||||||
|
const locationsSpoolOrders = useLocationsSpoolOrders();
|
||||||
|
console.log("locationsSpoolOrders", locationsSpoolOrders);
|
||||||
|
const setLocationsSpoolOrders = useSetSetting<Record<string, number[]>>("locations_spoolorders");
|
||||||
|
|
||||||
const { data, isLoading, isError } = useList<ISpool>({
|
const { data, isLoading, isError } = useList<ISpool>({
|
||||||
resource: "spool",
|
resource: "spool",
|
||||||
meta: {
|
meta: {
|
||||||
@@ -83,9 +87,18 @@ export function LocationContainer() {
|
|||||||
setLocationsSetting.mutate(newLocs);
|
setLocationsSetting.mutate(newLocs);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const setLocationSpoolOrder = (location: string, spoolOrder: number[]) => {
|
||||||
|
setLocationsSpoolOrders.mutate({
|
||||||
|
...locationsSpoolOrders,
|
||||||
|
[location]: spoolOrder,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// Create containers
|
// Create containers
|
||||||
const containers = locationsList.map((loc, idx) => {
|
const containers = locationsList.map((loc, idx) => {
|
||||||
const spools = spoolLocations[loc] ?? [];
|
const spools = spoolLocations[loc] ?? [];
|
||||||
|
const spoolOrder = locationsSpoolOrders[loc] ?? [];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Location
|
<Location
|
||||||
key={loc}
|
key={loc}
|
||||||
@@ -98,6 +111,8 @@ export function LocationContainer() {
|
|||||||
}}
|
}}
|
||||||
moveLocation={moveLocation}
|
moveLocation={moveLocation}
|
||||||
onEditTitle={(newTitle: string) => onEditTitle(loc, newTitle)}
|
onEditTitle={(newTitle: string) => onEditTitle(loc, newTitle)}
|
||||||
|
locationSpoolOrder={spoolOrder}
|
||||||
|
setLocationSpoolOrder={(spoolOrder: number[]) => setLocationSpoolOrder(loc, spoolOrder)}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,22 +9,41 @@ import { SpoolCard } from "./spoolCard";
|
|||||||
|
|
||||||
const { useToken } = theme;
|
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 { token } = useToken();
|
||||||
const invalidate = useInvalidate();
|
const invalidate = useInvalidate();
|
||||||
const [, spoolDrop] = useDrop(() => ({
|
const [, spoolDrop] = useDrop(() => ({
|
||||||
accept: ItemTypes.SPOOL,
|
accept: ItemTypes.SPOOL,
|
||||||
drop: (item: ISpool) => {
|
drop: async (item: ISpool) => {
|
||||||
setSpoolLocation(item.id, location).then(() => {
|
if (item.location === location) return;
|
||||||
invalidate({
|
await setSpoolLocation(item.id, location);
|
||||||
resource: "spool",
|
await invalidate({
|
||||||
id: item.id,
|
resource: "spool",
|
||||||
invalidates: ["list", "detail"],
|
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 = {
|
const style = {
|
||||||
backgroundColor: token.colorBgContainer,
|
backgroundColor: token.colorBgContainer,
|
||||||
borderRadius: token.borderRadiusLG,
|
borderRadius: token.borderRadiusLG,
|
||||||
@@ -32,7 +51,7 @@ export function SpoolList({ location, spools }: { location: string; spools: ISpo
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="loc-spools" ref={spoolDrop} style={style}>
|
<div className="loc-spools" ref={spoolDrop} style={style}>
|
||||||
{spools.map((spool) => (
|
{reorderedSpools.map((spool) => (
|
||||||
<SpoolCard key={spool.id} spool={spool} />
|
<SpoolCard key={spool.id} spool={spool} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -19,21 +19,6 @@ export async function setSpoolLocation(spool_id: number, location: string | null
|
|||||||
return response.json();
|
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<string> {
|
export async function renameSpoolLocation(location: string, newName: string): Promise<string> {
|
||||||
const response = await fetch(getAPIURL() + "/location/" + location, {
|
const response = await fetch(getAPIURL() + "/location/" + location, {
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
@@ -49,3 +34,33 @@ export async function renameSpoolLocation(location: string, newName: string): Pr
|
|||||||
}
|
}
|
||||||
return await response.text();
|
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<string, number[]> {
|
||||||
|
const query = useGetSetting("locations_spoolorders");
|
||||||
|
|
||||||
|
return useMemo(() => {
|
||||||
|
if (!query.data) return {};
|
||||||
|
|
||||||
|
try {
|
||||||
|
return JSON.parse(query.data.value) as Record<string, number[]>;
|
||||||
|
} catch {
|
||||||
|
console.warn("Failed to parse locations spool orders", query.data.value);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}, [query.data]);
|
||||||
|
}
|
||||||
|
|||||||
@@ -70,3 +70,4 @@ register_setting("extra_fields_spool", SettingType.ARRAY, json.dumps([]))
|
|||||||
register_setting("base_url", SettingType.STRING, json.dumps(""))
|
register_setting("base_url", SettingType.STRING, json.dumps(""))
|
||||||
|
|
||||||
register_setting("locations", SettingType.ARRAY, json.dumps([]))
|
register_setting("locations", SettingType.ARRAY, json.dumps([]))
|
||||||
|
register_setting("locations_spoolorders", SettingType.OBJECT, json.dumps({}))
|
||||||
|
|||||||
Reference in New Issue
Block a user