-
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.
- Loading branch information
Showing
7 changed files
with
219 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<script setup lang="ts"> | ||
import { TarButton, TarModal } from "logitar-vue3-ui"; | ||
import { computed, ref, watchEffect } from "vue"; | ||
import { nanoid } from "nanoid"; | ||
import { useForm } from "vee-validate"; | ||
import { useI18n } from "vue-i18n"; | ||
import DisplayNameInput from "@/components/shared/DisplayNameInput.vue"; | ||
const { t } = useI18n(); | ||
const props = withDefaults( | ||
defineProps<{ | ||
category?: string; | ||
id?: string; | ||
}>(), | ||
{ | ||
id: () => nanoid(), | ||
}, | ||
); | ||
const displayName = ref<string>(""); | ||
const modalRef = ref<InstanceType<typeof TarModal> | null>(null); | ||
const hasChanges = computed<boolean>(() => displayName.value.trim() !== (props.category?.trim() ?? "")); | ||
function hide(): void { | ||
modalRef.value?.hide(); | ||
} | ||
const emit = defineEmits<{ | ||
(e: "saved", category: string): void; | ||
}>(); | ||
const { handleSubmit, isSubmitting } = useForm(); | ||
const onSubmit = handleSubmit(async () => { | ||
emit("saved", displayName.value); | ||
hide(); | ||
}); | ||
watchEffect(() => (displayName.value = props.category ?? "")); | ||
</script> | ||
|
||
<template> | ||
<span> | ||
<TarButton | ||
:icon="category ? 'fas fa-edit' : 'fas fa-plus'" | ||
:text="t(category ? 'actions.edit' : 'actions.create')" | ||
:variant="category ? 'primary' : 'success'" | ||
data-bs-toggle="modal" | ||
:data-bs-target="`#${id}`" | ||
/> | ||
<TarModal | ||
:close="t('actions.close')" | ||
fade | ||
:id="id" | ||
ref="modalRef" | ||
:title="t(category ? 'receipts.categories.title.edit' : 'receipts.categories.title.new')" | ||
> | ||
<form @submit.prevent="onSubmit"> | ||
<DisplayNameInput required v-model="displayName" /> | ||
</form> | ||
<template #footer> | ||
<TarButton icon="fas fa-ban" :text="t('actions.cancel')" variant="secondary" @click="hide" /> | ||
<TarButton | ||
:disabled="isSubmitting || !hasChanges" | ||
:icon="category ? 'fas fa-save' : 'fas fa-plus'" | ||
:loading="isSubmitting" | ||
:status="t('loading')" | ||
:text="t(category ? 'actions.save' : 'actions.create')" | ||
:variant="category ? 'primary' : 'success'" | ||
@click="onSubmit" | ||
/> | ||
</template> | ||
</TarModal> | ||
</span> | ||
</template> |
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,61 @@ | ||
<script setup lang="ts"> | ||
import { useI18n } from "vue-i18n"; | ||
import AppDelete from "@/components/shared/AppDelete.vue"; | ||
import CategoryEdit from "./CategoryEdit.vue"; | ||
import type { CategorySavedEvent } from "@/types/receipts"; | ||
import { useCategoryStore } from "@/stores/categories"; | ||
const categories = useCategoryStore(); | ||
const { t } = useI18n(); | ||
const emit = defineEmits<{ | ||
(e: "deleted", value: string): void; | ||
(e: "saved", value: CategorySavedEvent): void; | ||
}>(); | ||
function onDeleted(category: string): void { | ||
if (categories.remove(category)) { | ||
emit("deleted", category); | ||
} | ||
} | ||
function onSaved(newCategory: string, oldCategory?: string): void { | ||
if (categories.save(newCategory, oldCategory)) { | ||
emit("saved", { newCategory, oldCategory }); | ||
} else { | ||
// TODO(fpion): new name is already used | ||
} | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div class="mb-3"> | ||
<CategoryEdit @saved="onSaved" /> | ||
</div> | ||
<p v-if="categories.categories.length === 0">{{ t("receipts.empty") }}</p> | ||
<table v-else class="table table-striped"> | ||
<thead> | ||
<tr> | ||
<th scope="col">{{ t("displayName") }}</th> | ||
<th scope="col">{{ t("actions.title") }}</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr v-for="category in categories.categories" :key="category"> | ||
<td>{{ category }}</td> | ||
<td> | ||
<CategoryEdit class="me-1" :category="category" @saved="onSaved($event, category)" /> | ||
<AppDelete | ||
class="ms-1" | ||
confirm="receipts.categories.delete.confirm" | ||
:display-name="category" | ||
title="receipts.categories.delete.title" | ||
@confirmed="onDeleted(category)" | ||
/> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
</template> |
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,34 @@ | ||
import { defineStore } from "pinia"; | ||
import { ref } from "vue"; | ||
|
||
export const useCategoryStore = defineStore( | ||
"category", | ||
() => { | ||
const categories = ref<string[]>([]); | ||
|
||
function remove(category: string): boolean { | ||
const index = categories.value.findIndex((c) => c === category); | ||
if (index < 0) { | ||
return false; | ||
} | ||
categories.value.splice(index, 1); | ||
return true; | ||
} | ||
|
||
function save(newCategory: string, oldCategory?: string): boolean { | ||
if (newCategory === oldCategory || categories.value.includes(newCategory)) { | ||
return false; | ||
} | ||
const index = categories.value.findIndex((c) => c === oldCategory); | ||
if (index < 0) { | ||
categories.value.push(newCategory); | ||
} else { | ||
categories.value.splice(index, 1, newCategory); | ||
} | ||
return true; | ||
} | ||
|
||
return { categories, remove, save }; | ||
}, | ||
{ persist: true }, | ||
); // TODO(fpion): tests |
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