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

[WIP] Sort category via buttons #266

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion src/components/Category.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
<h2>Categories for {{ listId }}</h2>

<ul>
<ListCategory v-for="category in categories" :key="category.id" :category="category" />
{{ sortUp() }}
<ListCategory v-for="category in categories.sort((a, b) => a.order > b.order)"
:key="category.id"
:category="category"
@sortUp="sortUp()"
@sortDown="sortDown()"
/>
</ul>

<div class="new-category__wrapper">
Expand Down Expand Up @@ -74,6 +80,12 @@ export default {
this.newCategoryId = category.id
this.category = category.name
},
sortUp() {
console.error("sortup ")
},
sortDown() {
console.error("sort down")
},
async onSaveCategory() {
if (this.newCategoryId === -1) {
this.addCategory()
Expand Down
34 changes: 25 additions & 9 deletions src/components/ListCategory.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
<template>
<li class="list-category">
<NcButton type="tertiary"
:aria-label="isEditing ? t('grocerylist', 'Save changed category') : t('grocerylist', 'Edit category')"
@click="toggleEditMode">
<template #icon>
<IconCheck v-if="isEditing" :size="20" />
<IconPencil v-else :size="20" />
</template>
</NcButton>
<NcTextField v-if="isEditing"
ref="input"
class="list-category__input"
:label="t('grocerylist', 'Category name')"
:loading="loading"
:value.sync="newCategoryName" />
<span v-else class="list-category__name">
<span v-else class="list-category__name" @click="$emit('sortUp')">
{{ category.name }}
</span>
<NcButton type="tertiary"
:aria-label="isEditing ? t('grocerylist', 'Save changed category') : t('grocerylist', 'Edit category')"
@click="toggleEditMode">
<template #icon>
<IconCheck v-if="isEditing" :size="20" />
<IconPencil v-else :size="20" />
</template>
</NcButton>
<NcButton type="tertiary"
@click="$emit('sortUp')">
<template #icon>
<IconArrowUp :size="20" />
</template>
</NcButton>
<NcButton type="tertiary"
@click="$emit('sortDown')">
<template #icon>
<IconArrowDown :size="20" />
</template>
</NcButton>
</li>
</template>

Expand All @@ -28,13 +40,17 @@ import { useCategoryStore } from '../store/categoryStore.ts'

import IconCheck from 'vue-material-design-icons/Check.vue'
import IconPencil from 'vue-material-design-icons/Pencil.vue'
import IconArrowUp from 'vue-material-design-icons/ArrowUp.vue'
import IconArrowDown from 'vue-material-design-icons/ArrowDown.vue'

export default {
name: 'ListCategory',

components: {
IconCheck,
IconPencil,
IconArrowUp,
IconArrowDown,
NcButton,
NcTextField,
},
Expand Down
13 changes: 13 additions & 0 deletions src/store/categoryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,24 @@ export const useCategoryStore = defineStore('category', {
{
id: category.id,
newName: category.name,
order: category.order
},
)
this.categories = { ...this.categories, [category.grocery_list]: data }
},

async updateCategory(category: ICategory) {
const { data } = await axios.post<ICategory[]>(
generateUrl('/apps/grocerylist/api/category/update'),
{
id: category.id,
newName: category.name,
order: category.order
},
)
// this.categories = { ...this.categories, [category.grocery_list]: data }
},

async loadCategories(listId: number) {
const response = await axios.get<ICategory[]>(
generateUrl(`/apps/grocerylist/api/all_categories/${listId}`),
Expand Down
20 changes: 19 additions & 1 deletion src/views/ListSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
</template>
</NcEmptyContent>
<ul v-else class="category-list">
<ListCategory v-for="category in categories" :key="category.name" :category="category" />
<ListCategory
v-for="category in categories.sort((a, b) => a.order > b.order)"
:key="category.name"
:category="category"
@sortUp="() => sortUp(category)"
@sortDown="sortDown()"/>
</ul>

<ListCategoryNew :list-id="listId" />
Expand Down Expand Up @@ -80,6 +85,19 @@ export default {
},

methods: {
async sortUp(category) {
category.order = category.order + 1

try {
this.categoryStore.updateCategory({ ...this.category, id: category.id, name: category.name, order: category.order })
} catch (e) {
console.error(e)
showError(t('grocerylist', 'Could not update category order'))
}
},
sortDown() {
console.error("sort down ")
},
/**
* Handle loading categories, sets the loading state and triggers store updates
*/
Expand Down