Skip to content

Commit d3a9fba

Browse files
committed
feat(components/admin): add clear cache button to email images library
- Introduced a new "Clear Cache" button in the AdminEmailAssets component to allow users to clear the email images cache. - Enhanced the layout by wrapping the header and button in a flex container for improved UI. - Integrated cache clearing functionality using a custom hook.
1 parent a6bdd25 commit d3a9fba

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

src/components/admin/AdminEmailAssets.vue

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
<!-- EmailImageLibrary.vue -->
22
<template>
33
<div class="email-images-library">
4-
<h3>{{ $t('Email Images') }}</h3>
4+
<div class="header-section">
5+
<h3>{{ $t('Email Images') }}</h3>
6+
<base-button
7+
:text="$t('Clear Cache')"
8+
:alt="$t('Clear Cache')"
9+
data-testid="testClearEmailImagesCacheButton"
10+
variant="outline"
11+
size="small"
12+
:disabled="isClearing"
13+
:show-spinner="isClearing"
14+
:action="clearEmailImagesCache"
15+
>
16+
{{ $t('Clear Cache') }}
17+
</base-button>
18+
</div>
519
<div class="images-grid">
620
<div
721
v-for="image in emailImages"
@@ -26,9 +40,11 @@ import axios from 'axios';
2640
import { useI18n } from 'vue-i18n';
2741
import { useToast } from 'vue-toastification';
2842
import BaseButton from '@/components/BaseButton.vue';
43+
import { useEmailImagesClearCache } from '@/hooks/useEmailImagesClearCache';
2944
3045
const { t } = useI18n();
3146
const $toasted = useToast();
47+
const { clearEmailImagesCache, isClearing } = useEmailImagesClearCache();
3248
3349
const emailImages = ref<any[]>([]);
3450
@@ -60,9 +76,15 @@ onMounted(async () => {
6076
max-height: 80vh;
6177
overflow-y: auto;
6278
}
79+
.header-section {
80+
display: flex;
81+
justify-content: space-between;
82+
align-items: center;
83+
margin-bottom: 1rem;
84+
}
6385
.email-images-library h3 {
6486
font-size: 1.25rem;
65-
margin-bottom: 0.5rem;
87+
margin: 0;
6688
}
6789
.images-grid {
6890
display: grid;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { ref, readonly } from 'vue';
2+
import { whenever } from '@vueuse/core';
3+
import { useToast } from 'vue-toastification';
4+
import { useApi } from '@/hooks/useApi';
5+
import { getErrorMessage } from '@/utils/errors';
6+
7+
interface ClearCacheResponse {
8+
cleared_count: number;
9+
message?: string;
10+
}
11+
12+
export function useEmailImagesClearCache() {
13+
const $toast = useToast();
14+
const api = useApi();
15+
const isClearing = ref(false);
16+
17+
const clearEmailImagesCache =
18+
async (): Promise<ClearCacheResponse | null> => {
19+
if (isClearing.value) {
20+
return null;
21+
}
22+
23+
isClearing.value = true;
24+
25+
try {
26+
const { data, error, isFinished } = api<ClearCacheResponse>(
27+
'/admins/email-images',
28+
{
29+
method: 'DELETE',
30+
headers: {
31+
'Content-Type': 'application/json',
32+
},
33+
},
34+
);
35+
36+
// Wait for the request to complete
37+
await new Promise<void>((resolve) => {
38+
const unwatch = whenever(isFinished, () => {
39+
unwatch();
40+
resolve();
41+
});
42+
});
43+
44+
if (error.value) {
45+
const errorMessage = getErrorMessage(error.value);
46+
$toast.error(`Failed to clear email images cache: ${errorMessage}`);
47+
return null;
48+
}
49+
50+
if (data.value) {
51+
const clearedCount = data.value.cleared_count;
52+
$toast.success(
53+
`Email images cache cleared successfully! ${clearedCount} entries removed.`,
54+
);
55+
return data.value;
56+
}
57+
58+
return null;
59+
} catch (error) {
60+
const errorMessage = getErrorMessage(error);
61+
$toast.error(`Error clearing email images cache: ${errorMessage}`);
62+
return null;
63+
} finally {
64+
isClearing.value = false;
65+
}
66+
};
67+
68+
return {
69+
clearEmailImagesCache,
70+
isClearing: readonly(isClearing),
71+
};
72+
}

0 commit comments

Comments
 (0)