enable primary button (#673)

* feat: enable primary button

* feat: better attachments

* fix: unneeded param
This commit is contained in:
Tonya
2025-05-05 01:43:38 +00:00
committed by GitHub
parent 8fb0ae64d7
commit 232dc08fcd
3 changed files with 33 additions and 36 deletions

View File

@@ -91,9 +91,26 @@ 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 {
if typ == attachment.TypePhoto && primary {
bldr = bldr.SetPrimary(true)
err := r.db.Attachment.Update().
Where(
attachment.HasItemWith(item.ID(itemID)),
attachment.IDNEQ(bldrId),
).
SetPrimary(false).
Exec(ctx)
if err != nil {
log.Err(err).Msg("failed to remove primary from other attachments")
err := tx.Rollback()
if err != nil {
return nil, err
}
return nil, err
}
} 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)),
@@ -112,23 +129,6 @@ func (r *AttachmentRepo) Create(ctx context.Context, itemID uuid.UUID, doc ItemC
if cnt == 0 {
bldr = bldr.SetPrimary(true)
}
} else if typ == attachment.TypePhoto && primary {
bldr = bldr.SetPrimary(true)
err := r.db.Attachment.Update().
Where(
attachment.HasItemWith(item.ID(itemID)),
attachment.IDNEQ(bldrId),
).
SetPrimary(false).
Exec(ctx)
if err != nil {
log.Err(err).Msg("failed to remove primary from other attachments")
err := tx.Rollback()
if err != nil {
return nil, err
}
return nil, err
}
}
// Get the group ID for the item the attachment is being created for

View File

@@ -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) {

View File

@@ -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`),