Skip to content

Commit

Permalink
Add transaction (DOM only)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradtraversy committed Nov 6, 2023
1 parent 525a9f8 commit 1749a06
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Balance :total="total" />
<IncomeExpenses :income="+income" :expenses="+expenses" />
<TransactionList :transactions="transactions" />
<AddTransaction />
<AddTransaction @transactionSubmitted="handleTransactionSubmitted" />
</div>
</template>

Expand All @@ -17,6 +17,10 @@ import AddTransaction from './components/AddTransaction.vue';
import { ref, computed } from 'vue';
import { useToast } from 'vue-toastification';
const toast = useToast();
const transactions = ref([
{ id: 1, text: 'Flower', amount: -19.99 },
{ id: 2, text: 'Salary', amount: 299.97 },
Expand Down Expand Up @@ -46,4 +50,20 @@ const expenses = computed(() => {
.reduce((acc, transaction) => acc + transaction.amount, 0)
.toFixed(2);
});
// Submit transaction
const handleTransactionSubmitted = (transactionData) => {
transactions.value.push({
id: generateUniqueId(),
text: transactionData.text,
amount: transactionData.amount,
});
toast.success('Transaction added.');
};
// Generate unique ID
const generateUniqueId = () => {
return Math.floor(Math.random() * 1000000);
};
</script>
10 changes: 8 additions & 2 deletions src/components/AddTransaction.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@ const amount = ref('');
// Get toast interface
const toast = useToast();
const emit = defineEmits(['transactionSubmitted']);
const onSubmit = () => {
if (!text.value || !amount.value) {
// Display a toast error message if either field is empty
toast.error('Both fields must be filled.');
return;
}
// Proceed with form submission logic here...
console.log('Form submitted:', text.value, amount.value);
const transactionData = {
text: text.value,
amount: parseFloat(amount.value),
};
emit('transactionSubmitted', transactionData);
// Clear form fields
text.value = '';
Expand Down

0 comments on commit 1749a06

Please sign in to comment.