Skip to content

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

feat: add ability to convert type of item/location (#1546)

Ellc0bcd3b4682f
diff --git a/frontend/components/Entity/CreateModal.vue b/frontend/components/Entity/CreateModal.vue
index 04cde6b7..2bd813c3 100644
--- a/frontend/components/Entity/CreateModal.vue
+++ b/frontend/components/Entity/CreateModal.vue
@@ -3,28 +3,12 @@
<template #title>
<div class="flex items-center gap-2 text-nowrap">
<span>Create</span>
- <Select :model-value="selectedEntityType?.id">
- <SelectTrigger class="h-7 p-1">
- <SelectValue
- class="text-xl"
- :placeholder="$t('components.entity.create_modal.type_selector_placeholder')"
- />
- </SelectTrigger>
- <SelectContent>
- <SelectItem
- v-for="type in subItemCreate ? entityTypes.filter(t => !t.isLocation) : entityTypes"
- :key="type.id"
- :value="type.id"
- @click="onEntityTypeChanged(type.id)"
- >
- <div class="flex items-center gap-2">
- <MdiMapMarkerOutline v-if="type.isLocation" class="size-4" />
- <MdiPackageVariantClosed v-else class="size-4" />
- <span>{{ t(type.name) }}</span>
- </div>
- </SelectItem>
- </SelectContent>
- </Select>
+ <EntitySelector
+ :selected-entity-type="selectedEntityType?.id"
+ :entity-types="subItemCreate ? entityTypes.filter(t => !t.isLocation) : entityTypes"
+ size="sm"
+ @entity-type-changed="onEntityTypeChanged"
+ />
</div>
</template>
<template #header-actions>
@@ -244,7 +228,6 @@
import { toast } from "@/components/ui/sonner";
import { Button, ButtonGroup } from "~/components/ui/button";
import BaseModal from "@/components/App/CreateModal.vue";
- import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import type {
EntityCreate,
EntityTemplateOut,
@@ -261,7 +244,6 @@
import MdiFileDocumentOutline from "~icons/mdi/file-document-outline";
import MdiChevronDown from "~icons/mdi/chevron-down";
import MdiClose from "~icons/mdi/close";
- import MdiMapMarkerOutline from "~icons/mdi/map-marker-outline";
import { AttachmentTypes } from "~~/lib/api/types/non-generated";
import { useDialog, useDialogHotkey } from "~/components/ui/dialog-provider";
import TagSelector from "~/components/Tag/Selector.vue";
@@ -281,6 +263,7 @@
type PhotoPreview,
} from "~/components/Form/photo-uploader";
import { useEntityTypeStore } from "~~/stores/entityTypes";
+ import EntitySelector from "~/components/Entity/Selector.vue";
const { t } = useI18n();
const { openDialog, closeDialog, registerOpenDialogCallback } = useDialog();
diff --git a/frontend/components/Entity/Selector.vue b/frontend/components/Entity/Selector.vue
new file mode 100644
index 00000000..415a0d14
--- /dev/null
+++ b/frontend/components/Entity/Selector.vue
@@ -0,0 +1,33 @@
+<template>
+ <Select :model-value="selectedEntityType">
+ <SelectTrigger :class="{ 'h-7 p-1': size === 'sm' }">
+ <SelectValue :class="{ 'text-xl': size === 'sm' }" :placeholder="$t('components.entity.selector.placeholder')" />
+ </SelectTrigger>
+ <SelectContent>
+ <SelectItem v-for="type in entityTypes" :key="type.id" :value="type.id" @click="onEntityTypeChanged(type.id)">
+ <div class="flex items-center gap-2">
+ <MdiMapMarkerOutline v-if="type.isLocation" class="size-4" />
+ <MdiPackageVariantClosed v-else class="size-4" />
+ <span>{{ t(type.name) }}</span>
+ </div>
+ </SelectItem>
+ </SelectContent>
+ </Select>
+</template>
+
+<script setup lang="ts">
+ import { useI18n } from "vue-i18n";
+ import MdiMapMarkerOutline from "~icons/mdi/map-marker-outline";
+ import MdiPackageVariantClosed from "~icons/mdi/package-variant-closed";
+ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "~/components/ui/select";
+ import type { EntityTypeSummary } from "~/lib/api/types/data-contracts";
+
+ const { t } = useI18n();
+
+ defineProps<{
+ entityTypes: EntityTypeSummary[];
+ selectedEntityType?: string;
+ onEntityTypeChanged: (id: string) => void;
+ size?: "sm" | "md";
+ }>();
+</script>
diff --git a/frontend/locales/en.json b/frontend/locales/en.json
index c3139952..f8018d6f 100644
--- a/frontend/locales/en.json
+++ b/frontend/locales/en.json
@@ -177,6 +177,9 @@
}
},
"entity": {
+ "selector": {
+ "placeholder": "Select a type…"
+ },
"create_modal": {
"entity_name": "{type} Name",
"entity_quantity": "{type} Quantity",
@@ -205,7 +208,6 @@
"product_tooltip_scan_barcode": "Autofill with a barcode from 📷",
"rotate_photo": "Rotate photo",
"set_as_primary_photo": "Set as { isPrimary, select, true {non-} false {} other {}}primary photo",
- "type_selector_placeholder": "Select a type…",
"item_selector_no_results_text": "Type to search…"
}
},
@@ -489,7 +491,8 @@
"yes": "Yes",
"item": "Item",
"location": "Location",
- "entity": "Entity"
+ "entity": "Entity",
+ "entity_type": "Entity Type"
},
"home": {
"quick_statistics": "Quick Statistics",
@@ -579,7 +582,8 @@
"select_type": "Select a type",
"title": "Attachment Edit"
},
- "view_image": "View Image"
+ "view_image": "View Image",
+ "change_entity_type_confirm": "Changing the entity type may cause loss of data in fields that are not shared between the current and new entity types. Are you sure you want to change the entity type?"
},
"edit_details": "Edit Details",
"field": "Field",
diff --git a/frontend/pages/item/[id]/index/edit.vue b/frontend/pages/item/[id]/index/edit.vue
index 4d1298e4..76a07ecb 100644
--- a/frontend/pages/item/[id]/index/edit.vue
+++ b/frontend/pages/item/[id]/index/edit.vue
@@ -31,6 +31,8 @@
import BaseCard from "@/components/Base/Card.vue";
import { Card } from "~/components/ui/card";
import DropZone from "~/components/global/DropZone.vue";
+ import EntitySelector from "~/components/Entity/Selector.vue";
+ import { useEntityTypeStore } from "~/stores/entityTypes";
const { t } = useI18n();
@@ -44,6 +46,8 @@
const api = useUserApi();
const preferences = useViewPreferences();
+ const entityTypeStore = useEntityTypeStore();
+
const itemId = computed<string>(() => route.params.id as string);
const tagStore = useTagStore();
@@ -92,6 +96,14 @@
}
saving.value = true;
+ const isConvertingToLocation = item.value.entityType?.isLocation;
+ if (isConvertingToLocation) {
+ const { isCanceled } = await confirm.open(t("items.edit.change_entity_type_confirm"));
+ if (isCanceled) {
+ saving.value = false;
+ return;
+ }
+ }
let purchasePrice = 0;
let soldPrice = 0;
@@ -116,6 +128,7 @@
// backend. The form/picker hold strings; sending the spread above is
// sufficient.
syncChildEntityLocations: item.value.syncChildEntityLocations,
+ entityTypeId: item.value.entityType!.id,
};
const { error } = await api.items.update(itemId.value, payload);
@@ -128,7 +141,9 @@
}
toast.success(t("items.toast.item_saved"));
- if (redirect) {
+ if (isConvertingToLocation) {
+ navigateTo("/location/" + itemId.value);
+ } else if (redirect) {
navigateTo("/item/" + itemId.value);
}
}
@@ -665,6 +680,14 @@
<Switch v-model="item.syncChildEntityLocations" @update:model-value="syncChildEntityLocations()" />
</div>
<TagSelector v-model="item.tagIds" :tags="tags" />
+ <div class="flex flex-col gap-1">
+ <Label class="px-1">{{ $t("global.entity_type") }}</Label>
+ <EntitySelector
+ :entity-types="entityTypeStore.allTypes"
+ :selected-entity-type="item.entityType?.id"
+ @entity-type-changed="id => (item.entityType = entityTypeStore.findById(id))"
+ />
+ </div>
</div>
<div class="border-t sm:p-0">
diff --git a/frontend/pages/location/[id]/index/edit.vue b/frontend/pages/location/[id]/index/edit.vue
index 90413904..3d56d381 100644
--- a/frontend/pages/location/[id]/index/edit.vue
+++ b/frontend/pages/location/[id]/index/edit.vue
@@ -28,6 +28,8 @@
import BaseCard from "@/components/Base/Card.vue";
import { Card } from "~/components/ui/card";
import DropZone from "~/components/global/DropZone.vue";
+ import EntitySelector from "~/components/Entity/Selector.vue";
+ import { useEntityTypeStore } from "~/stores/entityTypes";
const { t } = useI18n();
@@ -41,6 +43,8 @@
const api = useUserApi();
const preferences = useViewPreferences();
+ const entityTypeStore = useEntityTypeStore();
+
const locationId = computed<string>(() => route.params.id as string);
const tagStore = useTagStore();
@@ -86,6 +90,8 @@
async function saveLocation(redirect: boolean) {
saving.value = true;
+ const isConvertingToItem = !item.value.entityType?.isLocation;
+
const payload: EntityUpdate = {
...item.value,
parentId: parent.value?.id || null,
@@ -95,6 +101,7 @@
soldPrice: item.value.soldPrice || 0,
// Date-only fields stay as YYYY-MM-DD strings via the spread above.
syncChildEntityLocations: item.value.syncChildEntityLocations,
+ entityTypeId: item.value.entityType!.id,
};
const { error } = await api.items.update(locationId.value, payload);
@@ -107,7 +114,9 @@
}
toast.success(t("locations.toast.location_updated"));
- if (redirect) {
+ if (isConvertingToItem) {
+ navigateTo("/item/" + locationId.value);
+ } else if (redirect) {
navigateTo("/location/" + locationId.value);
}
}
@@ -384,6 +393,14 @@
<div class="mb-6 grid gap-4 border-t px-5 pt-2 md:grid-cols-2">
<LocationSelector v-model="parent" label="Parent Location" :current-location="item" />
<TagSelector v-model="item.tagIds" :tags="tags" />
+ <div class="flex flex-col gap-1">
+ <Label class="px-1">{{ $t("global.entity_type") }}</Label>
+ <EntitySelector
+ :entity-types="entityTypeStore.allTypes"
+ :selected-entity-type="item.entityType?.id"
+ @entity-type-changed="id => (item.entityType = entityTypeStore.findById(id))"
+ />
+ </div>
</div>
<div class="border-t sm:p-0">
diff --git a/frontend/pages/location/[id]/index/index.vue b/frontend/pages/location/[id]/index/index.vue
index 29bcb060..1bce100d 100644
--- a/frontend/pages/location/[id]/index/index.vue
+++ b/frontend/pages/location/[id]/index/index.vue
@@ -230,7 +230,7 @@
<button
v-for="(photo, i) in photos"
:key="i"
- class="group relative overflow-hidden rounded-lg border bg-muted h-32 aspect-1"
+ class="group relative aspect-1 h-32 overflow-hidden rounded-lg border bg-muted"
@click="openImageDialog(photo, location.id)"
>
<img