Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

enable primary button (#673)

Tonya232dc08fcdbe
diff --git a/backend/internal/data/repo/repo_item_attachments.go b/backend/internal/data/repo/repo_item_attachments.go
index c301b60b..9eba2225 100644
--- a/backend/internal/data/repo/repo_item_attachments.go
+++ b/backend/internal/data/repo/repo_item_attachments.go
@@ -91,44 +91,44 @@ func (r *AttachmentRepo) Create(ctx context.Context, itemID uuid.UUID, doc ItemC
SetItemID(itemID).
SetTitle(doc.Title)
- // Autoset primary to true if this is the first attachment
- // that is of type photo
- if typ == attachment.TypePhoto {
- cnt, err := tx.Attachment.Query().
+ if typ == attachment.TypePhoto && primary {
+ bldr = bldr.SetPrimary(true)
+ err := r.db.Attachment.Update().
Where(
attachment.HasItemWith(item.ID(itemID)),
- attachment.TypeEQ(typ),
+ attachment.IDNEQ(bldrId),
).
- Count(ctx)
+ SetPrimary(false).
+ Exec(ctx)
if err != nil {
- log.Err(err).Msg("failed to count attachments")
+ log.Err(err).Msg("failed to remove primary from other attachments")
err := tx.Rollback()
if err != nil {
return nil, err
}
return nil, err
}
-
- if cnt == 0 {
- bldr = bldr.SetPrimary(true)
- }
- } else if typ == attachment.TypePhoto && primary {
- bldr = bldr.SetPrimary(true)
- err := r.db.Attachment.Update().
+ } else if typ == attachment.TypePhoto {
+ // Autoset primary to true if this is the first attachment
+ // that is of type photo
+ cnt, err := tx.Attachment.Query().
Where(
attachment.HasItemWith(item.ID(itemID)),
- attachment.IDNEQ(bldrId),
+ attachment.TypeEQ(typ),
).
- SetPrimary(false).
- Exec(ctx)
+ Count(ctx)
if err != nil {
- log.Err(err).Msg("failed to remove primary from other attachments")
+ log.Err(err).Msg("failed to count attachments")
err := tx.Rollback()
if err != nil {
return nil, err
}
return nil, err
}
+
+ if cnt == 0 {
+ bldr = bldr.SetPrimary(true)
+ }
}
// Get the group ID for the item the attachment is being created for
diff --git a/frontend/components/Item/CreateModal.vue b/frontend/components/Item/CreateModal.vue
index 2c62a531..191e3919 100644
--- a/frontend/components/Item/CreateModal.vue
+++ b/frontend/components/Item/CreateModal.vue
@@ -95,8 +95,7 @@
<p>Rotate photo</p>
</TooltipContent>
</Tooltip>
- <!-- TODO: re-enable when we have a way to set primary photos -->
- <!-- <Tooltip>
+ <Tooltip>
<TooltipTrigger>
<Button
size="icon"
@@ -112,7 +111,7 @@
<TooltipContent>
<p>Set as {{ photo.primary ? "non" : "" }} primary photo</p>
</TooltipContent>
- </Tooltip> -->
+ </Tooltip>
</TooltipProvider>
<p class="mt-1 text-sm" style="overflow-wrap: anywhere">{{ photo.photoName }}</p>
</div>
@@ -135,8 +134,8 @@
import MdiPackageVariantClosed from "~icons/mdi/package-variant-closed";
import MdiDelete from "~icons/mdi/delete";
import MdiRotateClockwise from "~icons/mdi/rotate-clockwise";
- // import MdiStarOutline from "~icons/mdi/star-outline";
- // import MdiStar from "~icons/mdi/star";
+ import MdiStarOutline from "~icons/mdi/star-outline";
+ import MdiStar from "~icons/mdi/star";
import { AttachmentTypes } from "~~/lib/api/types/non-generated";
import { useDialog, useDialogHotkey } from "~/components/ui/dialog-provider";
import LabelSelector from "~/components/Label/Selector.vue";
@@ -196,16 +195,12 @@
form.photos.splice(index, 1);
}
- // TODO: actually set the primary when adding item
-
- // function setPrimary(index: number) {
- // const primary = form.photos.findIndex(p => p.primary);
+ function setPrimary(index: number) {
+ const primary = form.photos.findIndex(p => p.primary);
- // if (primary !== -1) form.photos[primary].primary = false;
- // if (primary !== index) form.photos[index].primary = true;
-
- // toast.error("Currently this does not do anything, the first photo will always be primary");
- // }
+ if (primary !== -1) form.photos[primary].primary = false;
+ if (primary !== index) form.photos[index].primary = true;
+ }
function previewImage(event: Event) {
const input = event.target as HTMLInputElement;
@@ -285,7 +280,8 @@
data.id,
photo.file,
photo.photoName,
- AttachmentTypes.Photo
+ AttachmentTypes.Photo,
+ photo.primary
);
if (attachError) {
diff --git a/frontend/lib/api/classes/items.ts b/frontend/lib/api/classes/items.ts
index a19ebe6b..0778b04f 100644
--- a/frontend/lib/api/classes/items.ts
+++ b/frontend/lib/api/classes/items.ts
@@ -32,13 +32,14 @@ export type ItemsQuery = {
};
export class AttachmentsAPI extends BaseAPI {
- add(id: string, file: File | Blob, filename: string, type: AttachmentTypes | null = null) {
+ add(id: string, file: File | Blob, filename: string, type: AttachmentTypes | null = null, primary: boolean = false) {
const formData = new FormData();
formData.append("file", file);
if (type) {
formData.append("type", type);
}
formData.append("name", filename);
+ formData.append("primary", primary.toString());
return this.http.post<FormData, ItemOut>({
url: route(`/items/${id}/attachments`),