feat(infra): implement delete functionality for transactions with UI integration
This commit is contained in:
@@ -5,6 +5,10 @@
|
|||||||
transaction: Transaction;
|
transaction: Transaction;
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
delete: [id: string];
|
||||||
|
}>();
|
||||||
|
|
||||||
function formatDate(date: string) {
|
function formatDate(date: string) {
|
||||||
const formatter = new Intl.DateTimeFormat('en-US', {
|
const formatter = new Intl.DateTimeFormat('en-US', {
|
||||||
year: 'numeric',
|
year: 'numeric',
|
||||||
@@ -39,6 +43,14 @@
|
|||||||
<div class="bottom">
|
<div class="bottom">
|
||||||
<p>{{ transaction.description }}</p>
|
<p>{{ transaction.description }}</p>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="actions">
|
||||||
|
<button
|
||||||
|
class="delete-btn"
|
||||||
|
@click="emit('delete', transaction.id)"
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -69,4 +81,26 @@
|
|||||||
font-size: 0.875rem;
|
font-size: 0.875rem;
|
||||||
color: #666666;
|
color: #666666;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.actions {
|
||||||
|
display: flex;
|
||||||
|
justify-content: flex-end;
|
||||||
|
gap: 0.5rem;
|
||||||
|
padding-top: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.delete-btn {
|
||||||
|
background: none;
|
||||||
|
border: 0.0625rem solid var(--state-error);
|
||||||
|
border-radius: 0.25rem;
|
||||||
|
color: var(--state-error);
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 0.75rem;
|
||||||
|
padding: 0.25rem 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.delete-btn:hover {
|
||||||
|
background: var(--state-error);
|
||||||
|
color: var(--text-primary);
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ export class TransactionServiceFactory implements ITransactionServiceFactory {
|
|||||||
|
|
||||||
export interface ITransactionService {
|
export interface ITransactionService {
|
||||||
get: (pageNumber?: number, pageSize?: number) => Promise<Result<Page<Transaction>, Error[]>>;
|
get: (pageNumber?: number, pageSize?: number) => Promise<Result<Page<Transaction>, Error[]>>;
|
||||||
|
deleteById: (id: string) => Promise<Result<boolean, Error[]>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class TransactionService implements ITransactionService {
|
export class TransactionService implements ITransactionService {
|
||||||
@@ -32,6 +33,24 @@ export class TransactionService implements ITransactionService {
|
|||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async deleteById(id: string): Promise<Result<boolean, Error[]>> {
|
||||||
|
const url = this.endpoints.get + '/' + id;
|
||||||
|
const request = new ClientRequest(url);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await this.client.delete(request);
|
||||||
|
|
||||||
|
if (res.ok === false) {
|
||||||
|
return Err([new Error('Failed to delete transaction.')]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(true);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return Err([new Error('Failed to delete transaction.')]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async get(pageNumber: number = 1, pageSize: number = 500) {
|
async get(pageNumber: number = 1, pageSize: number = 500) {
|
||||||
const queryParams = new URLSearchParams({
|
const queryParams = new URLSearchParams({
|
||||||
pageNumber: pageNumber.toString(),
|
pageNumber: pageNumber.toString(),
|
||||||
|
|||||||
@@ -10,6 +10,17 @@
|
|||||||
|
|
||||||
const transactions = ref<Transaction[]>([]);
|
const transactions = ref<Transaction[]>([]);
|
||||||
|
|
||||||
|
async function handleDelete(id: string) {
|
||||||
|
const result = await transactionService.deleteById(id);
|
||||||
|
|
||||||
|
if (result.err) {
|
||||||
|
alert(result.val.map(e => e.message).join('\n'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
transactions.value = transactions.value.filter(t => t.id !== id);
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
const result = await transactionService.get();
|
const result = await transactionService.get();
|
||||||
|
|
||||||
@@ -30,7 +41,10 @@
|
|||||||
v-for="transaction in transactions"
|
v-for="transaction in transactions"
|
||||||
:key="transaction.id"
|
:key="transaction.id"
|
||||||
>
|
>
|
||||||
<TransactionCard :transaction="transaction" />
|
<TransactionCard
|
||||||
|
:transaction="transaction"
|
||||||
|
@delete="handleDelete"
|
||||||
|
/>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user