generated from antfu-collective/vitesse
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from doroudi:doroudi/issue17
Implement Add Category
- Loading branch information
Showing
10 changed files
with
234 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<script setup lang='ts'> | ||
import { type DataTableColumns, NButton, NIcon } from 'naive-ui/es/components' | ||
import type { RowData } from 'naive-ui/es/data-table/src/interface' | ||
import { | ||
DismissCircle24Regular as DeleteIcon, | ||
Edit32Regular as EditIcon, | ||
AddCircle20Regular as PlusIcon, | ||
} from '@vicons/fluent' | ||
import { storeToRefs } from 'pinia' | ||
const { t } = useI18n() | ||
const collapsed = ref(false) | ||
const store = useCategoryStore() | ||
const { categories, isLoading } = storeToRefs(store) | ||
onMounted(getItems) | ||
const columns: DataTableColumns<RowData> = [ | ||
{ | ||
title: 'Name', | ||
key: 'name', | ||
}, | ||
{ | ||
title: 'Products Count', | ||
key: 'productsCount', | ||
}, | ||
{ | ||
title: 'Actions', | ||
key: 'actions', | ||
width: 200, | ||
render(row) { | ||
return [ | ||
h( | ||
NButton, | ||
{ | ||
size: 'small', | ||
renderIcon: renderIcon(EditIcon), | ||
ghost: true, | ||
class: 'mr-2', | ||
onClick: () => edit(row), | ||
}, | ||
{ default: () => 'Edit' }, | ||
), | ||
h( | ||
NButton, | ||
{ | ||
size: 'small', | ||
type: 'error', | ||
ghost: true, | ||
renderIcon: renderIcon(DeleteIcon), | ||
onClick: () => deleteItem(row), | ||
}, | ||
{ default: () => 'Delete' }, | ||
), | ||
] | ||
}, | ||
}, | ||
] | ||
const { options } = storeToRefs(store) | ||
const showAddDialog = ref(false) | ||
function renderIcon(icon: any) { | ||
return () => h(NIcon, null, { default: () => h(icon) }) | ||
} | ||
function edit(row: RowData) { | ||
} | ||
function deleteItem(row: RowData) { | ||
} | ||
function rowKey(row: RowData) { | ||
return row.index | ||
} | ||
function getItems() { | ||
store.getCategories(options.value) | ||
} | ||
function handlePageChange(page: number) { | ||
options.value.page = page | ||
getItems() | ||
} | ||
function handleSorterChange() { | ||
getItems() | ||
} | ||
function handleFiltersChange() { | ||
getItems() | ||
} | ||
function createCategory() { | ||
showAddDialog.value = true | ||
} | ||
</script> | ||
|
||
<template> | ||
<n-layout has-sider> | ||
<n-layout-content> | ||
<div> | ||
<div class="flex items-center mb-5"> | ||
<h1 class="page-title mx-2"> | ||
{{ t('categories.title') }} | ||
</h1> | ||
<NButton type="primary" quaternary round @click="createCategory"> | ||
<template #icon> | ||
<NIcon> | ||
<PlusIcon /> | ||
</NIcon> | ||
</template> | ||
{{ t('categories.createButton') }} | ||
</NButton> | ||
</div> | ||
<n-data-table | ||
remote :columns="columns" :data="categories" :loading="isLoading" :pagination="options" | ||
:row-key="rowKey" @update:sorter="handleSorterChange" @update:filters="handleFiltersChange" | ||
@update:page="handlePageChange" | ||
/> | ||
</div> | ||
</n-layout-content> | ||
<n-layout-sider | ||
bordered collapse-mode="width" :collapsed-width="0" :width="300" :collapsed="collapsed" show-trigger | ||
@collapse="collapsed = true" @expand="collapsed = false" | ||
> | ||
<CategoryStatics /> | ||
</n-layout-sider> | ||
|
||
<n-drawer v-model:show="showAddDialog" :width="502" placement="right"> | ||
<n-drawer-content closable title="Create Category"> | ||
<CreateCategory @close="showAddDialog = false" /> | ||
</n-drawer-content> | ||
</n-drawer> | ||
</n-layout> | ||
</template> | ||
|
||
<style scoped lang='scss'> | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,75 @@ | ||
<script setup lang='ts'> | ||
import type { FormInst, FormRules } from 'naive-ui/es/form' | ||
import type { SelectMixedOption } from 'naive-ui/es/select/src/interface' | ||
import { storeToRefs } from 'pinia' | ||
import type { Category } from '~/models/Category' | ||
import type { Category, CategoryCreateModel } from '~/models/Category' | ||
const emits = defineEmits(['close']) | ||
const categoryStore = useCategoryStore() | ||
const { isLoading } = storeToRefs(categoryStore) | ||
const categoryItem = ref<Category>({} as Category) | ||
const { isLoading, categories } = storeToRefs(categoryStore) | ||
const categoryItem = ref<CategoryCreateModel>({ name: '', parentId: 0 }) | ||
const { t } = useI18n() | ||
const formRef = ref<FormInst | null>(null) | ||
async function create() { | ||
// TODO: validate | ||
await categoryStore.createCategory(categoryItem.value) | ||
emits('close') | ||
formRef.value?.validate(async (errors: any) => { | ||
console.log('🚀 ~ file: CreateCategory.vue:16 ~ formRef.value?.validate ~ errors:', errors) | ||
if (!errors) { | ||
await categoryStore.createCategory(categoryItem.value) | ||
emits('close') | ||
} | ||
}) | ||
} | ||
const categoriesOptions = categories.value.map((x: Category) => { | ||
return { | ||
value: x.id, | ||
label: x.name, | ||
} | ||
}) | ||
const parents: SelectMixedOption[] = [{ value: 0, label: 'Root' }, ...categoriesOptions] | ||
const nameInput = ref() | ||
onMounted(() => { | ||
nameInput.value?.focus() | ||
}) | ||
const rules: FormRules = { | ||
name: [ | ||
{ | ||
required: true, | ||
trigger: ['blur', 'change'], | ||
message: t('categories.validations.nameRequired'), | ||
}, | ||
], | ||
parentId: [ | ||
{ | ||
required: true, | ||
trigger: ['blur', 'change'], | ||
message: t('categories.validations.parentRequired'), | ||
}, | ||
], | ||
} | ||
</script> | ||
|
||
<template> | ||
<form @submit.prevent="create()"> | ||
<div class="mb-5"> | ||
<span class="p-float-label"> | ||
<label for="username" class="block font-medium">{{ t('category.create.categoryName') }}</label> | ||
<n-input id="name" :placeholder="t('category.create.categoryName')" /> | ||
</span> | ||
<n-form ref="formRef" :model="categoryItem" :rules="rules" @submit.prevent="create()"> | ||
<div class="form-control"> | ||
<n-form-item class="mb-5" path="name" :label="t('categories.create.categoryName')"> | ||
<n-input | ||
id="name" ref="nameInput" v-model:value="categoryItem.name" autofocus | ||
:placeholder="t('categories.create.categoryName')" | ||
/> | ||
</n-form-item> | ||
</div> | ||
|
||
<div class="mb-8"> | ||
<label for="parent" class="block font-medium">{{ t('category.create.parent') }}</label> | ||
<n-input type="text" :placeholder="t('category.create.parent')" /> | ||
<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-form-item> | ||
</div> | ||
|
||
<n-button attr-type="submit" size="large" :block="true" type="primary" :loading="isLoading"> | ||
{{ t('category.create.buttonTitle') }} | ||
{{ t('categories.create.buttonTitle') }} | ||
</n-button> | ||
</form> | ||
</n-form> | ||
</template> | ||
|
||
<style scoped lang='scss'></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.