Skip to content

Commit 8a0ef50

Browse files
authored
Merge pull request #2 from Zenfulcode/fix-category-parentid-update
fix(category): Improve category mappers and API client integration
2 parents 5f0e116 + f1dc5bb commit 8a0ef50

5 files changed

Lines changed: 42 additions & 29 deletions

File tree

src/lib/mappers/category.mapper.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Category } from '$lib/types';
2-
import type { CategoryDTO, ResponseDTO } from 'commercify-api-client';
2+
import type { CategoryDTO, ListResponseDTO, ResponseDTO } from 'commercify-api-client';
33

44
export const categoryResponseMapper = (
55
dto: ResponseDTO<CategoryDTO>
@@ -8,19 +8,11 @@ export const categoryResponseMapper = (
88
success: boolean;
99
error?: string;
1010
} => {
11-
if (!dto.data) {
11+
if (!dto.success || !dto.data) {
1212
return {
1313
data: null,
1414
success: false,
15-
error: 'Category data is missing'
16-
};
17-
}
18-
19-
if (dto.error) {
20-
return {
21-
data: null,
22-
success: false,
23-
error: dto.error
15+
error: dto.error || 'Category not found'
2416
};
2517
}
2618

@@ -31,14 +23,14 @@ export const categoryResponseMapper = (
3123
};
3224
};
3325

34-
export const categoryListMapper = (dto: {
35-
data: CategoryDTO[];
36-
}): { data: Category[]; success: boolean; error?: string } => {
37-
if (!dto.data || !Array.isArray(dto.data)) {
26+
export const categoryListMapper = (
27+
dto: ListResponseDTO<CategoryDTO>
28+
): { data: Category[]; success: boolean; error?: string } => {
29+
if (!dto.success || !dto.data) {
3830
return {
3931
data: [],
4032
success: false,
41-
error: 'Category data is missing or not in the expected format'
33+
error: dto.error || 'No categories found'
4234
};
4335
}
4436

src/lib/server/commercify/api.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import type {
2929
AdminProductListRequest,
3030
UpdateCategoryRequest,
3131
OrderListRequest,
32-
OrderParameters
32+
OrderParameters,
33+
CreateCategoryRequest
3334
} from 'commercify-api-client';
3435
import { EnvironmentConfig } from '../env';
3536
import {
@@ -46,8 +47,13 @@ import {
4647
orderSummaryResponseMapper
4748
} from '$lib/mappers';
4849
import { OrderCache, ProductCache } from '$lib/cache';
49-
import type { CreateProductInput, UpdateCategoryInput, UpdateProductInput } from '$lib/types';
50-
import { categoryResponseMapper } from '$lib/mappers/category.mapper';
50+
import type {
51+
CreateCategoryInput,
52+
CreateProductInput,
53+
UpdateCategoryInput,
54+
UpdateProductInput
55+
} from '$lib/types';
56+
import { categoryListMapper, categoryResponseMapper } from '$lib/mappers/category.mapper';
5157
import { paymentResponseMapper } from '$lib/mappers/payments.mapper';
5258

5359
/**
@@ -373,7 +379,7 @@ export class CachedCommercifyApiClient {
373379
return {
374380
list: CacheHelpers.createSimpleCachedEndpoint(
375381
'categories',
376-
() => this.client.categories.list(),
382+
() => this.client.categories.list(categoryListMapper),
377383
CACHE_TTL.CATEGORIES
378384
),
379385
get: (id: number) => {
@@ -384,8 +390,22 @@ export class CachedCommercifyApiClient {
384390
CACHE_TTL.CATEGORY
385391
);
386392
},
393+
create: async (data: CreateCategoryInput) => {
394+
const requestData: CreateCategoryRequest = {
395+
name: data.name,
396+
description: data.description || '',
397+
parent_id: data.parentId ? parseInt(data.parentId) : undefined
398+
};
399+
400+
const result = await this.client.categories.create(requestData, categoryResponseMapper);
401+
402+
// Invalidate category caches after creation
403+
await CacheInvalidator.invalidateAllCategoryCaches();
404+
405+
return result;
406+
},
387407
update: async (id: number, data: any) => {
388-
const requestData = {
408+
const requestData: UpdateCategoryRequest = {
389409
name: data.name,
390410
description: data.description || '',
391411
parent_id: data.parentId ? parseInt(data.parentId) : undefined

src/routes/admin/products/categories/[id]/edit/+page.server.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const load: PageServerLoad = async ({ params, locals }) => {
1919
}
2020

2121
const categories = categoriesResult.data || [];
22-
const category = categories.find((c: Category) => String(c.id) === String(categoryId));
22+
const category: Category = categories.find((c: Category) => String(c.id) === String(categoryId));
2323

2424
if (!category) {
2525
throw error(404, 'Category not found');
@@ -30,15 +30,16 @@ export const load: PageServerLoad = async ({ params, locals }) => {
3030
{
3131
name: category.name,
3232
description: category.description || '',
33-
parentId: category.parentId?.toString()
33+
parentId: category.parentId ? String(category.parentId) : undefined
3434
},
3535
zod(categorySchema)
3636
);
3737

38+
const parentOptions = categories.filter((c: Category) => String(c.id) !== String(categoryId)); // Exclude self from parent options
3839
return {
3940
form,
4041
category,
41-
categories: categories.filter((c: Category) => String(c.id) !== String(categoryId)) // Exclude self from parent options
42+
categories: parentOptions
4243
};
4344
};
4445

src/routes/admin/products/categories/[id]/edit/+page.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@
8181
<Select.Root type="single" bind:value={$formData.parentId} name={props.name}>
8282
<Select.Trigger {...props}>
8383
{$formData.parentId
84-
? data.categories.find(
85-
(c: Category) => String(c.id) === String($formData.parentId)
86-
)?.name
84+
? data.categories.find((c: Category) => String(c.id) === $formData.parentId)
85+
?.name
8786
: 'Select a parent category (optional)'}
8887
</Select.Trigger>
8988
<Select.Content>

src/routes/admin/products/categories/new/+page.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,14 @@
8181
<Select.Root type="single" bind:value={$formData.parentId} name={props.name}>
8282
<Select.Trigger {...props}>
8383
{$formData.parentId
84-
? data.categories.find((c: Category) => c.id === $formData.parentId)?.name
84+
? data.categories.find((c: Category) => String(c.id) === $formData.parentId)
85+
?.name
8586
: 'Select a parent category (optional)'}
8687
</Select.Trigger>
8788
<Select.Content>
8889
<Select.Item value="">No parent (top-level category)</Select.Item>
8990
{#each data.categories as category}
90-
<Select.Item value={category.id}>
91+
<Select.Item value={String(category.id)}>
9192
{category.name}
9293
</Select.Item>
9394
{/each}

0 commit comments

Comments
 (0)