update: use better effect pattern for sorted wishlist in edit page
This commit is contained in:
@@ -23,22 +23,24 @@
|
|||||||
let editFormElement = $state<HTMLElement | null>(null);
|
let editFormElement = $state<HTMLElement | null>(null);
|
||||||
let searchQuery = $state("");
|
let searchQuery = $state("");
|
||||||
|
|
||||||
let sortedItems = $state<Item[]>([]);
|
let items = $state<Item[]>([]);
|
||||||
|
|
||||||
|
$effect.pre(() => {
|
||||||
|
const sorted = [...data.wishlist.items].sort(
|
||||||
|
(a, b) => Number(a.order) - Number(b.order),
|
||||||
|
);
|
||||||
|
items = sorted;
|
||||||
|
});
|
||||||
|
|
||||||
let filteredItems = $derived(
|
let filteredItems = $derived(
|
||||||
searchQuery.trim()
|
searchQuery.trim()
|
||||||
? sortedItems.filter(item =>
|
? items.filter(item =>
|
||||||
item.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
item.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
||||||
item.description?.toLowerCase().includes(searchQuery.toLowerCase())
|
item.description?.toLowerCase().includes(searchQuery.toLowerCase())
|
||||||
)
|
)
|
||||||
: sortedItems
|
: items
|
||||||
);
|
);
|
||||||
|
|
||||||
$effect(() => {
|
|
||||||
sortedItems = [...data.wishlist.items].sort(
|
|
||||||
(a, b) => Number(a.order) - Number(b.order),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
function handleItemAdded() {
|
function handleItemAdded() {
|
||||||
showAddForm = false;
|
showAddForm = false;
|
||||||
}
|
}
|
||||||
@@ -59,7 +61,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleColorChange(itemId: string, newColor: string) {
|
function handleColorChange(itemId: string, newColor: string) {
|
||||||
sortedItems = sortedItems.map((item) =>
|
items = items.map((item) =>
|
||||||
item.id === itemId ? { ...item, color: newColor } : item,
|
item.id === itemId ? { ...item, color: newColor } : item,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -91,17 +93,16 @@
|
|||||||
async function handlePositionChange(newPosition: number) {
|
async function handlePositionChange(newPosition: number) {
|
||||||
if (!editingItem) return;
|
if (!editingItem) return;
|
||||||
|
|
||||||
const currentIndex = sortedItems.findIndex(item => item.id === editingItem.id);
|
const currentIndex = items.findIndex(item => item.id === editingItem.id);
|
||||||
if (currentIndex === -1) return;
|
if (currentIndex === -1) return;
|
||||||
|
|
||||||
const newIndex = newPosition - 1; // Convert to 0-based index
|
const newIndex = newPosition - 1; // Convert to 0-based index
|
||||||
|
|
||||||
// Reorder the array
|
const newItems = [...items];
|
||||||
const newItems = [...sortedItems];
|
|
||||||
const [movedItem] = newItems.splice(currentIndex, 1);
|
const [movedItem] = newItems.splice(currentIndex, 1);
|
||||||
newItems.splice(newIndex, 0, movedItem);
|
newItems.splice(newIndex, 0, movedItem);
|
||||||
|
|
||||||
sortedItems = newItems;
|
items = newItems;
|
||||||
await handleReorder(newItems);
|
await handleReorder(newItems);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -150,14 +151,14 @@
|
|||||||
onSuccess={handleItemUpdated}
|
onSuccess={handleItemUpdated}
|
||||||
onCancel={cancelEditing}
|
onCancel={cancelEditing}
|
||||||
onColorChange={handleColorChange}
|
onColorChange={handleColorChange}
|
||||||
currentPosition={sortedItems.findIndex(item => item.id === editingItem.id) + 1}
|
currentPosition={items.findIndex(item => item.id === editingItem.id) + 1}
|
||||||
totalItems={sortedItems.length}
|
totalItems={items.length}
|
||||||
onPositionChange={handlePositionChange}
|
onPositionChange={handlePositionChange}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if sortedItems.length > 5}
|
{#if items.length > 5}
|
||||||
<SearchBar bind:value={searchQuery} />
|
<SearchBar bind:value={searchQuery} />
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user