Skip to content

Commit

Permalink
feat: delete child table row
Browse files Browse the repository at this point in the history
- fix child row add/update button disabling logic
  • Loading branch information
ruchamahabal committed Aug 21, 2023
1 parent 56dfe73 commit a31a837
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
25 changes: 20 additions & 5 deletions frontend/src/components/ExpenseTaxesTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,20 @@
v-if="!isReadOnly"
class="flex w-full flex-row items-center justify-between gap-3"
>
<Button
v-if="editingIdx !== null"
class="py-3 px-12 border-red-600 text-red-600"
icon-left="trash"
appearance="white"
@click="deleteExpenseTax()"
>
Delete
</Button>
<Button
appearance="primary"
class="w-full py-3 px-12"
@click="closeModal()"
:icon-left="editingIdx === null ? 'plus' : 'check'"
@click="updateExpenseTax()"
:disabled="addButtonDisabled"
>
{{ editingIdx === null ? "Add Tax" : "Update Tax" }}
Expand All @@ -114,7 +124,7 @@
<script setup>
import { IonModal } from "@ionic/vue"
import { FeatherIcon, createResource } from "frappe-ui"
import { computed, ref, watch, inject } from "vue"
import { computed, ref, watch } from "vue"
import FormField from "@/components/FormField.vue"
import EmptyState from "@/components/EmptyState.vue"
Expand All @@ -133,7 +143,7 @@ const props = defineProps({
default: false,
},
})
const emit = defineEmits(["add-expense-tax", "update-expense-tax"])
const emit = defineEmits(["add-expense-tax", "update-expense-tax", "delete-expense-tax"])
const expenseTax = ref({})
const editingIdx = ref(null)
Expand All @@ -146,7 +156,12 @@ const openModal = async (item, idx) => {
isModalOpen.value = true
}
const closeModal = async () => {
const deleteExpenseTax = () => {
emit("delete-expense-tax", editingIdx.value)
resetSelectedItem()
}
const updateExpenseTax = () => {
if (editingIdx.value === null) {
emit("add-expense-tax", expenseTax.value)
} else {
Expand Down Expand Up @@ -204,7 +219,7 @@ const modalTitle = computed(() => {
})
const addButtonDisabled = computed(() => {
return props.fields?.some((field) => {
return taxesTableFields.data?.some((field) => {
if (field.reqd && !expenseTax.value[field.fieldname]) {
return true
}
Expand Down
51 changes: 33 additions & 18 deletions frontend/src/components/ExpensesTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
{{ `Sanctioned: ${currency} ${item.sanctioned_amount || 0}` }}
</span>
<span class="whitespace-pre"> &middot; </span>
<span class="whitespace-nowrap">
<span class="whitespace-nowrap" v-if="item.expense_date">
{{ dayjs(item.expense_date).format("D MMM") }}
</span>
</div>
Expand Down Expand Up @@ -94,10 +94,20 @@
v-if="!isReadOnly"
class="flex w-full flex-row items-center justify-between gap-3"
>
<Button
v-if="editingIdx !== null"
class="py-3 px-12 border-red-600 text-red-600"
icon-left="trash"
appearance="white"
@click="deleteExpenseItem()"
>
Delete
</Button>
<Button
appearance="primary"
class="w-full py-3 px-12"
@click="closeModal()"
:icon-left="editingIdx === null ? 'plus' : 'check'"
@click="updateExpenseItem()"
:disabled="addButtonDisabled"
>
{{ editingIdx === null ? "Add Expense" : "Update Expense" }}
Expand Down Expand Up @@ -132,7 +142,7 @@ const props = defineProps({
default: false,
},
})
const emit = defineEmits(["add-expense-item", "update-expense-item"])
const emit = defineEmits(["add-expense-item", "update-expense-item", "delete-expense-item"])
const dayjs = inject("$dayjs")
const expenseItem = ref({})
const editingIdx = ref(null)
Expand All @@ -147,7 +157,12 @@ const openModal = async (item, idx) => {
isModalOpen.value = true
}
const closeModal = async () => {
const deleteExpenseItem = () => {
emit("delete-expense-item", editingIdx.value)
resetSelectedItem()
}
const updateExpenseItem = () => {
if (editingIdx.value === null) {
emit("add-expense-item", expenseItem.value)
} else {
Expand All @@ -162,20 +177,6 @@ function resetSelectedItem() {
editingIdx.value = null
}
const modalTitle = computed(() => {
if (props.isReadOnly) return "Expense Item"
return editingIdx.value === null ? "New Expense Item" : "Edit Expense Item"
})
const addButtonDisabled = computed(() => {
return props.fields?.some((field) => {
if (field.reqd && !expenseItem.value[field.fieldname]) {
return true
}
})
})
const expensesTableFields = createResource({
url: "hrms.api.get_doctype_fields",
params: { doctype: "Expense Claim Detail" },
Expand All @@ -195,6 +196,20 @@ const expensesTableFields = createResource({
})
expensesTableFields.reload()
const modalTitle = computed(() => {
if (props.isReadOnly) return "Expense Item"
return editingIdx.value === null ? "New Expense Item" : "Edit Expense Item"
})
const addButtonDisabled = computed(() => {
return expensesTableFields.data?.some((field) => {
if (field.reqd && !expenseItem.value[field.fieldname]) {
return true
}
})
})
// child table form scripts
watch(
() => expenseItem.value.expense_type,
Expand Down
21 changes: 16 additions & 5 deletions frontend/src/views/expense_claim/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
:isReadOnly="isReadOnly || isFormReadOnly"
@addExpenseItem="addExpenseItem"
@updateExpenseItem="updateExpenseItem"
@deleteExpenseItem="deleteExpenseItem"
/>
</template>

Expand All @@ -31,6 +32,7 @@
:isReadOnly="isReadOnly || isFormReadOnly"
@addExpenseTax="addExpenseTax"
@updateExpenseTax="updateExpenseTax"
@deleteExpenseTax="deleteExpenseTax"
/>
</template>

Expand All @@ -47,7 +49,7 @@
</template>

<script setup>
import { IonPage, IonContent, modalController } from "@ionic/vue"
import { IonPage, IonContent } from "@ionic/vue"
import { createResource } from "frappe-ui"
import { computed, ref, watch, inject } from "vue"
Expand Down Expand Up @@ -264,30 +266,39 @@ function addExpenseItem(item) {
calculateTotals()
calculateTaxes()
allocateAdvanceAmount()
modalController.dismiss()
}
function updateExpenseItem(item, idx) {
expenseClaim.value.expenses[idx] = item
calculateTotals()
calculateTaxes()
allocateAdvanceAmount()
modalController.dismiss()
}
function deleteExpenseItem(idx) {
expenseClaim.value.expenses.splice(idx, 1)
calculateTotals()
calculateTaxes()
allocateAdvanceAmount()
}
function addExpenseTax(item) {
if (!expenseClaim.value.taxes) expenseClaim.value.taxes = []
expenseClaim.value.taxes.push(item)
calculateTaxes()
allocateAdvanceAmount()
modalController.dismiss()
}
function updateExpenseTax(item, idx) {
expenseClaim.value.taxes[idx] = item
calculateTaxes()
allocateAdvanceAmount()
modalController.dismiss()
}
function deleteExpenseTax(idx) {
expenseClaim.value.taxes.splice(idx, 1)
calculateTaxes()
allocateAdvanceAmount()
}
function calculateTotals() {
Expand Down

0 comments on commit a31a837

Please sign in to comment.