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

As admin I want to edit and delete categories #29

Merged
merged 1 commit 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: 0 additions & 1 deletion src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ declare module 'vue' {
NNotificationProvider: typeof import('naive-ui')['NNotificationProvider']
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']
Expand Down
18 changes: 16 additions & 2 deletions src/components/Category/CategoryManagement.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
AddCircle20Regular as PlusIcon,
} from '@vicons/fluent'
import { storeToRefs } from 'pinia'
import { useDialog, useMessage } from 'naive-ui'

const { t } = useI18n()
const collapsed = ref(false)
const store = useCategoryStore()
const { categories, isLoading } = storeToRefs(store)
const dialog = useDialog()
const message = useMessage()
onMounted(getItems)
const columns: DataTableColumns<RowData> = [
{
Expand Down Expand Up @@ -46,7 +49,7 @@
type: 'error',
ghost: true,
renderIcon: renderIcon(DeleteIcon),
onClick: () => deleteItem(row),
onClick: () => handleDeleteItem(row),
},
{ default: () => 'Delete' },
),
Expand All @@ -60,12 +63,23 @@
return () => h(NIcon, null, { default: () => h(icon) })
}

function edit(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 deleteItem(row: RowData) {

function handleDeleteItem(row: RowData) {
dialog.error({
title: 'Confirm',
content: 'Are you sure?',
positiveText: 'Yes, Delete',
negativeText: 'Cancel',
onPositiveClick: () => {
store.deleteCategory(row.id)
message.success('Category was deleted!')
},
})
}

function rowKey(row: RowData) {
return row.id
}
Expand Down
10 changes: 10 additions & 0 deletions src/mocks/handlers/category.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ const handlers = [
ctx.json(category),
)
}),
rest.delete('/api/Category/:id', (req, res, ctx) => {
const id = req.params.id.toString()
const itemIndex = categories.findIndex(x => x.id === Number.parseInt(id))
categories.splice(itemIndex, 1)
return res(
ctx.delay(1000),
ctx.status(200),
ctx.json(true),
)
}),

]

Expand Down
4 changes: 4 additions & 0 deletions src/services/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ class CategoryService {
async createCategory(categoryItem: CategoryCreateModel): Promise<Category> {
return apiService.post<Category>('', categoryItem)
}

async deleteCategory(id: string | number): Promise<boolean> {
return apiService.delete<boolean>(id)
}
}
export default new CategoryService()
5 changes: 3 additions & 2 deletions src/store/category.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export const useCategoryStore = defineStore('Category', () => {
}
}

function deleteCategory() {

async function deleteCategory(id: string | number) {
await categoryService.deleteCategory(id)
getCategories(options.value)
}

function editCategory() {
Expand Down
Loading