refactor: abstract edit page components and functions
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Utility functions for updating wishlist properties
|
||||
*/
|
||||
|
||||
type UpdateField = {
|
||||
[key: string]: string;
|
||||
};
|
||||
|
||||
async function updateWishlist(field: UpdateField): Promise<boolean> {
|
||||
const response = await fetch("?/updateWishlist", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
body: new URLSearchParams(field),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.error(`Failed to update wishlist: ${Object.keys(field)[0]}`);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function updateTitle(title: string): Promise<boolean> {
|
||||
return updateWishlist({ title });
|
||||
}
|
||||
|
||||
export async function updateDescription(description: string | null): Promise<boolean> {
|
||||
return updateWishlist({ description: description || "" });
|
||||
}
|
||||
|
||||
export async function updateColor(color: string | null): Promise<boolean> {
|
||||
return updateWishlist({ color: color || "" });
|
||||
}
|
||||
|
||||
export async function updateEndDate(endDate: string | null): Promise<boolean> {
|
||||
return updateWishlist({ endDate: endDate || "" });
|
||||
}
|
||||
|
||||
export async function reorderItems(items: Array<{ id: string; order: number }>): Promise<boolean> {
|
||||
const response = await fetch("?/reorderItems", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
items: JSON.stringify(items),
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.error("Failed to update item order");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user