Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doroudi/issue25 #28

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ declare module 'vue' {
NPageHeader: typeof import('naive-ui')['NPageHeader']
NPopselect: typeof import('naive-ui')['NPopselect']
NSelect: typeof import('naive-ui')['NSelect']
NTreeSelect: typeof import('naive-ui')['NTreeSelect']
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
Sidebar: typeof import('./components/Sidebar.vue')['default']
Expand Down
2 changes: 1 addition & 1 deletion src/components/Category/CategoryManagement.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@
return () => h(NIcon, null, { default: () => h(icon) })
}

function edit(row: RowData) {

Check warning on line 63 in src/components/Category/CategoryManagement.vue

View workflow job for this annotation

GitHub Actions / lint

'row' is defined but never used. Allowed unused args must match /^_/u

}
function deleteItem(row: RowData) {

Check warning on line 66 in src/components/Category/CategoryManagement.vue

View workflow job for this annotation

GitHub Actions / lint

'row' is defined but never used. Allowed unused args must match /^_/u

}
function rowKey(row: RowData) {
return row.index
return row.id
}
function getItems() {
store.getCategories(options.value)
Expand Down
18 changes: 11 additions & 7 deletions src/components/Category/CreateCategory.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang='ts'>
import type { FormInst, FormRules } from 'naive-ui/es/form'
import type { SelectMixedOption } from 'naive-ui/es/select/src/interface'
import type { TreeSelectOption } from 'naive-ui/es/tree-select/src/interface'
import { storeToRefs } from 'pinia'
import type { Category, CategoryCreateModel } from '~/models/Category'

Expand All @@ -18,13 +18,17 @@ async function create() {
}
})
}
const categoriesOptions = categories.value.map((x: Category) => {

const categoriesOptions = categories.value.map(mapCategoryToOptions)

function mapCategoryToOptions(item: Category): TreeSelectOption {
return {
value: x.id,
label: x.name,
key: item.id,
label: item.name,
children: item.children?.map(mapCategoryToOptions),
}
})
const parents: SelectMixedOption[] = [{ value: 0, label: 'Root' }, ...categoriesOptions]
}
const parents: TreeSelectOption[] = [{ key: 0, label: 'Root' }, ...categoriesOptions]
const nameInput = ref()
onMounted(() => {
nameInput.value?.focus()
Expand Down Expand Up @@ -69,7 +73,7 @@ const rules: FormRules = {
</div>
<div class="form-control">
<n-form-item class="mb-5" :label="t('categories.create.parent')">
<n-select id="parentId" v-model:value="categoryItem.parentId" :options="parents" :placeholder="t('categories.create.parent')" />
<n-tree-select v-model="categoryItem.parentId" key-field="key" :options="parents" :placeholder="t('categories.create.parent')" default-value="Root" />
</n-form-item>
</div>
<n-button attr-type="submit" size="large" :block="true" type="primary" :loading="isLoading">
Expand Down
21 changes: 20 additions & 1 deletion src/mocks/handlers/category.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const handlers = [
id: faker.datatype.number({ max: 2000 }),
name: newItem.name,
productsCount: 0,
subItems: [],
children: [],
}
categories.push(category)
return res(
Expand All @@ -37,6 +37,25 @@ function createFakeCategory(): Category {
id: faker.datatype.number(),
name: faker.commerce.productAdjective(),
productsCount: faker.datatype.number({ min: 1, max: 130 }),
children: [
{
id: faker.datatype.number(),
name: faker.commerce.productAdjective(),
productsCount: faker.datatype.number({ min: 1, max: 130 }),
},
{
id: faker.datatype.number(),
name: faker.commerce.productAdjective(),
productsCount: faker.datatype.number({ min: 1, max: 130 }),
children: [
{
id: faker.datatype.number(),
name: faker.commerce.productAdjective(),
productsCount: faker.datatype.number({ min: 1, max: 130 }),
},
],
},
],
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/models/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface Category {
id: number
name: string
productsCount: number
subItems?: Category[]
children?: Category[]
}

export interface CategoryCreateModel {
Expand Down
3 changes: 1 addition & 2 deletions src/store/category.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ export const useCategoryStore = defineStore('Category', () => {
async function createCategory(categoryItem: CategoryCreateModel) {
isLoading.value = true
try {
const response = await categoryService.createCategory(categoryItem)
await categoryService.createCategory(categoryItem)
getCategories(options.value)
// categories.value.push(response)
}
finally {
isLoading.value = false
Expand Down
Loading