Skip to content

Commit

Permalink
Merge pull request #476 from Shelf-nu/fix-uncategorized-bug
Browse files Browse the repository at this point in the history
Fix uncategorized bug
  • Loading branch information
DonKoko authored Oct 23, 2023
2 parents c1bb501 + 2eea049 commit 78b66cb
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/components/assets/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const AssetForm = ({
className="border-b-0 pb-[10px]"
required={zodFieldIsRequired(FormSchema.shape.category)}
>
<CategorySelect defaultValue={category || undefined} />
<CategorySelect defaultValue={category || "uncategorized"} />
</FormRow>

<FormRow
Expand Down
5 changes: 5 additions & 0 deletions app/components/category/category-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export const CategorySelect = ({ defaultValue }: { defaultValue?: string }) => {
</div>

<div className="border-b border-b-gray-300 py-2 ">
<SelectItem value={"uncategorized"} key={"uncategorized"}>
<Badge color={"#808080"} noBg withDot={false}>
Uncategorized
</Badge>
</SelectItem>
{refinedCategories.map((c) => (
<SelectItem value={c.id} key={c.id}>
<Badge color={c.color} noBg withDot={false}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export const CategoryCheckboxDropdown = () => {
const [, setInitialSelect] = useAtom(addInitialSelectedCategoriesAtom);
const [, clearFilters] = useAtom(clearCategoryFiltersAtom);

const uncategorizedItemObj: any = {
id: "uncategorized",
name: "uncategorized",
color: "#808080",
};

const hasCategories = useMemo(
() => refinedCategories.length > 0,
[refinedCategories]
Expand Down Expand Up @@ -121,6 +127,11 @@ export const CategoryCheckboxDropdown = () => {
)}
</div>
<div className="">
<CheckboxItem
key={uncategorizedItemObj.id}
category={uncategorizedItemObj}
selected={items}
/>
{refinedCategories.map((c) => (
<CheckboxItem key={c.id} category={c} selected={items} />
))}
Expand Down
37 changes: 30 additions & 7 deletions app/modules/asset/service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,22 @@ export async function getAssets({
}

if (categoriesIds && categoriesIds.length > 0 && where.asset) {
where.asset.categoryId = {
in: categoriesIds,
};
if (categoriesIds.includes("uncategorized")) {
where.asset.OR = [
{
categoryId: {
in: categoriesIds,
},
},
{
categoryId: null,
},
];
} else {
where.asset.categoryId = {
in: categoriesIds,
};
}
}

if (tagsIds && tagsIds.length > 0 && where.asset) {
Expand Down Expand Up @@ -235,7 +248,7 @@ export async function createAsset({
};

/** If a categoryId is passed, link the category to the asset. */
if (categoryId) {
if (categoryId !== "uncategorized") {
Object.assign(data, {
category: {
connect: {
Expand Down Expand Up @@ -313,6 +326,7 @@ interface UpdateAssetPayload {
id: Asset["id"];
title?: Asset["title"];
description?: Asset["description"];
/** Pass 'uncategorized' to clear the category */
categoryId?: Asset["categoryId"];
newLocationId?: Asset["locationId"];
currentLocationId?: Asset["locationId"];
Expand Down Expand Up @@ -347,8 +361,17 @@ export async function updateAsset(payload: UpdateAssetPayload) {
mainImageExpiration,
};

/** Delete the category id from the payload so we can use connect syntax from prisma */
if (categoryId) {
/** If uncategorized is passed, disconnect the category */
if (categoryId === "uncategorized") {
Object.assign(data, {
category: {
disconnect: true,
},
});
}

// If category id is passed and is differenent than uncategorized, connect the category
if (categoryId && categoryId !== "uncategorized") {
Object.assign(data, {
category: {
connect: {
Expand All @@ -358,7 +381,7 @@ export async function updateAsset(payload: UpdateAssetPayload) {
});
}

/** Delete the category id from the payload so we can use connect syntax from prisma */
/** Connect the new location id */
if (newLocationId) {
Object.assign(data, {
location: {
Expand Down
13 changes: 12 additions & 1 deletion app/routes/_layout+/assets.$assetId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,18 @@ export default function AssetDetailsPage() {
</Badge>
</div>
</li>
) : null}
) : (
<li className="mb-4 flex justify-between">
<span className="text-[12px] font-medium text-gray-600">
Category
</span>
<div className="max-w-[250px]">
<Badge color={"#808080"} withDot={false}>
Uncategorized
</Badge>
</div>
</li>
)}
{location ? (
<li className="mb-2 flex justify-between">
<span className="text-[12px] font-medium text-gray-600">
Expand Down
6 changes: 5 additions & 1 deletion app/routes/_layout+/assets._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ const ListAssetContent = ({
<Badge color={category.color} withDot={false}>
{category.name}
</Badge>
) : null}
) : (
<Badge color={"#808080"} withDot={false}>
{"Uncategorized"}
</Badge>
)}
</Td>

{/* Tags */}
Expand Down

0 comments on commit 78b66cb

Please sign in to comment.