Skip to content

Commit 57b2699

Browse files
authored
Merge pull request #446 from devforth/feature/AdminForth/1125/please-rework-all-frontend-api
Feature/admin forth/1125/please rework all frontend api
2 parents e5e629f + 6761962 commit 57b2699

28 files changed

+183
-111
lines changed

adminforth/commands/createCustomComponent/templates/customCrud/afterBreadcrumbs.vue.hbs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
<script setup lang="ts">
1414
import { onMounted } from 'vue';
1515
import { useI18n } from 'vue-i18n';
16-
import adminforth from '@/adminforth';
16+
import { useAdminforth } from '@/adminforth';
1717
import { AdminForthResourceCommon, AdminUser } from "@/types/Common";
1818
1919
const { t } = useI18n();
2020
21+
const { alert } = useAdminforth();
22+
2123
const props = defineProps<{
2224
record: any
2325
resource: AdminForthResourceCommon
@@ -30,7 +32,7 @@ onMounted(() => {
3032
});
3133
3234
function handleClick() {
33-
adminforth.alert({
35+
alert({
3436
message: t('Confirmed'),
3537
variant: 'success',
3638
});

adminforth/commands/createCustomComponent/templates/customCrud/beforeActionButtons.vue.hbs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
<script setup lang="ts">
1414
import { onMounted } from 'vue';
1515
import { useI18n } from 'vue-i18n';
16-
import adminforth from '@/adminforth';
16+
import { useAdminforth } from '@/adminforth';
1717
import { AdminForthResourceCommon, AdminUser } from "@/types/Common";
1818

19+
const { alert } = useAdminforth();
1920
const { t } = useI18n();
2021

2122
const props = defineProps<{
@@ -30,7 +31,7 @@ onMounted(() => {
3031
});
3132

3233
function handleClick() {
33-
adminforth.alert({
34+
alert({
3435
message: t('Confirmed'),
3536
variant: 'success',
3637
});

adminforth/commands/createCustomComponent/templates/customCrud/beforeBreadcrumbs.vue.hbs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
<script setup lang="ts">
1414
import { onMounted } from 'vue';
1515
import { useI18n } from 'vue-i18n';
16-
import adminforth from '@/adminforth';
16+
import { useAdminforth } from '@/adminforth';
1717
import { AdminForthResourceCommon, AdminUser } from "@/types/Common";
1818
1919
const { t } = useI18n();
2020
21+
const { alert } = useAdminforth();
22+
2123
const props = defineProps<{
2224
record: any
2325
resource: AdminForthResourceCommon
@@ -30,7 +32,7 @@ onMounted(() => {
3032
});
3133
3234
function handleClick() {
33-
adminforth.alert({
35+
alert({
3436
message: t('Confirmed'),
3537
variant: 'success',
3638
});

adminforth/commands/createCustomComponent/templates/customCrud/bottom.vue.hbs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
<script setup lang="ts">
1414
import { onMounted } from 'vue';
1515
import { useI18n } from 'vue-i18n';
16-
import adminforth from '@/adminforth';
16+
import { useAdminforth } from '@/adminforth';
1717
import { AdminForthResourceCommon, AdminUser } from "@/types/Common";
1818
1919
const { t } = useI18n();
2020
21+
const { alert } = useAdminforth();
22+
2123
const props = defineProps<{
2224
record: any
2325
resource: AdminForthResourceCommon
@@ -30,7 +32,7 @@ onMounted(() => {
3032
});
3133
3234
function handleClick() {
33-
adminforth.alert({
35+
alert({
3436
message: t('Confirmed'),
3537
variant: 'success',
3638
});

adminforth/commands/createCustomComponent/templates/customCrud/threeDotsDropdownItems.vue.hbs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212

1313
<script setup lang="ts">
1414
import { useI18n } from 'vue-i18n';
15-
import adminforth from '@/adminforth';
15+
import { useAdminforth} from '@/adminforth';
1616
import { onMounted } from 'vue';
1717
import { AdminForthResourceCommon, AdminUser } from "@/types/Common";
1818
1919
const { t } = useI18n();
2020
21+
const { alert } = useAdminforth();
22+
2123
const props = defineProps<{
2224
record: any
2325
resource: AdminForthResourceCommon
@@ -29,7 +31,7 @@ const menuItems = [
2931
{
3032
label: 'Show Success Alert',
3133
action: () => {
32-
adminforth.alert({
34+
alert({
3335
message: t('Success Alert'),
3436
variant: 'success',
3537
});

adminforth/documentation/docs/tutorial/03-Customization/06-customPages.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,16 @@ import { ref, type Ref, onMounted, computed } from 'vue';
182182
import dayjs from 'dayjs';
183183
import { callApi } from '@/utils';
184184
import { useI18n } from 'vue-i18n';
185-
import adminforth from '@/adminforth';
185+
import { useAdminforth } from '@/adminforth';
186186
import { AreaChart, BarChart, PieChart } from '@/afcl';
187187
188188
const data: Ref<{listedVsUnlistedPriceByDays: any, listedVsUnlistedByDays: any,
189189
apartsByDays: any, apartsCountsByRooms: any, topCountries: any, totalAparts: any} | null> = ref(null);
190190
191191
const { t } = useI18n();
192192
193+
const { alert } = useAdminforth();
194+
193195
const COLORS = ["#4E79A7", "#F28E2B", "#E15759", "#76B7B2", "#59A14F"]
194196
195197
const apartsCountsByDaysChart = computed(() => {
@@ -246,7 +248,7 @@ onMounted(async () => {
246248
try {
247249
data.value = await callApi({path: '/api/dashboard/', method: 'GET'});
248250
} catch (error) {
249-
adminforth.alert({
251+
alert({
250252
message: t(`Error fetching data: {message}`, { message: error.message }),
251253
variant: 'danger',
252254
});

adminforth/documentation/docs/tutorial/03-Customization/07-alert.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ AdminForth has very simple [frontend API](/docs/api/FrontendAPI/interfaces/Front
1212
To show an alert use `adminforth.alert` method:
1313

1414
```ts
15-
import adminforth from '@/adminforth';
15+
import { useAdminforth } from '@/adminforth';
1616

17-
adminforth.alert({message: 'Hello world', variant: 'success'})
17+
const { alert } = useAdminforth();
18+
19+
alert({message: 'Hello world', variant: 'success'})
1820
```
1921

2022
Next variants are supported:
@@ -62,9 +64,11 @@ Here's how the alert looks now:
6264
To show a confirmation dialog use `adminforth.confirm` method:
6365

6466
```ts
65-
import adminforth from '@/adminforth';
67+
import { useAdminforth } from '@/adminforth';
68+
69+
const { confirm } = useAdminforth();
6670

67-
const isConfirmed = await adminforth.confirm({message: 'Are you sure?', yes: 'Yes', no: 'No'})
71+
const isConfirmed = await confirm({message: 'Are you sure?', yes: 'Yes', no: 'No'})
6872
```
6973

7074
## Usage example
@@ -88,26 +92,28 @@ Create a Vue component in the custom directory of your project, e.g. `Alerts.vue
8892
</div>
8993
</template>
9094
<script setup>
91-
import adminforth from '@/adminforth';
95+
import { useAdminforth } from '@/adminforth';
9296
import { Button } from '@/afcl'
9397
import { useI18n } from 'vue-i18n';
9498
9599
const { t } = useI18n();
96100
101+
const { alert, confirm } = useAdminforth();
102+
97103
function successAlert(message) {
98-
adminforth.alert({message, variant: 'success'})
104+
alert({message, variant: 'success'})
99105
};
100106
101107
function warningAlert(message) {
102-
adminforth.alert({message, variant: 'warning'})
108+
alert({message, variant: 'warning'})
103109
};
104110
105111
async function doConfirm() {
106-
const isConfirmed = await adminforth.confirm({message: t('Are you sure?'), yes: t('Yes'), no: t('No')})
112+
const isConfirmed = await confirm({message: t('Are you sure?'), yes: t('Yes'), no: t('No')})
107113
if (isConfirmed){
108-
adminforth.alert({message: t('Confirmed'), variant: 'success'})
114+
alert({message: t('Confirmed'), variant: 'success'})
109115
} else {
110-
adminforth.alert({message: t('Not confirmed'), variant: 'warning'})
116+
alert({message: t('Not confirmed'), variant: 'warning'})
111117
}
112118
}
113119
</script>

adminforth/documentation/docs/tutorial/03-Customization/08-pageInjections.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ Now create file `ApartsPie.vue` in the `custom` folder of your project:
7272
import { onMounted, ref, Ref, computed } from 'vue';
7373
import { PieChart } from '@/afcl';
7474
import { callApi } from '@/utils';
75-
import adminforth from '@/adminforth';
75+
import { useAdminforth } from '@/adminforth';
7676
7777
7878
const data: Ref<any[]> = ref([]);
7979
80+
const { alert } = useAdminforth();
8081
8182
const COLORS = ["#4E79A7", "#F28E2B", "#E15759", "#76B7B2", "#59A14F"]
8283
const rooms = computed(() => {
@@ -93,7 +94,7 @@ Now create file `ApartsPie.vue` in the `custom` folder of your project:
9394
try {
9495
data.value = await callApi({ path: '/api/aparts-by-room-percentages', method: 'GET' });
9596
} catch (error) {
96-
adminforth.alert({
97+
alert({
9798
message: `Error fetching data: ${error.message}`,
9899
variant: 'danger',
99100
timeout: 'unlimited'
@@ -296,22 +297,24 @@ Now create file `CheckReadingTime.vue` in the `custom` folder of your project:
296297
297298
<script setup>
298299
import { getReadingTime} from "text-analyzer";
299-
import adminforth from '@/adminforth';
300+
import { useAdminforth } from '@/adminforth';
300301
301302
defineExpose({
302303
click,
303304
});
304305
306+
const { alert, list } = useAdminforth();
307+
305308
function checkReadingTime() {
306309
const text = document.querySelector('[data-af-column="description"]')?.innerText;
307310
if (text) {
308311
const readingTime = getReadingTime(text);
309-
adminforth.alert({
312+
alert({
310313
message: `Reading time: ${readingTime.minutes} minutes`,
311314
variant: 'success',
312315
});
313316
}
314-
adminforth.list.closeThreeDotsDropdown();
317+
list.closeThreeDotsDropdown();
315318
}
316319
317320
function click() {
@@ -330,7 +333,7 @@ npm i text-analyzer
330333
```
331334
332335
333-
> ☝️ Please note that we are using AdminForth [Frontend API](/docs/api/FrontendAPI/interfaces/FrontendAPIInterface/) `adminforth.list.closeThreeDotsDropdown();` to close the dropdown after the item is clicked.
336+
> ☝️ Please note that we are using AdminForth [Frontend API](/docs/api/FrontendAPI/interfaces/FrontendAPIInterface/) `list.closeThreeDotsDropdown();` to close the dropdown after the item is clicked.
334337
335338
>☝️ Please note that the injected component might have an exposed click function as well as a defined click function, which executes the click on component logic.
336339
@@ -541,7 +544,7 @@ You have opportunity to inject custom components to the global layout. For examp
541544
542545
![alt text](<Group 6.png>)
543546
544-
use `adminforth.closeUserMenuDropdown();` to close the dropdown after the item is clicked.
547+
use `closeUserMenuDropdown();` to close the dropdown after the item is clicked.
545548
546549
```ts title="/index.ts"
547550
{
@@ -567,14 +570,16 @@ Now create file `CustomUserMenuItem.vue` in the `custom` folder of your project:
567570
</template>
568571
569572
<script setup>
570-
import adminforth from '@/adminforth';
573+
import { useAdminforth } from '@/adminforth';
574+
575+
const { alert, closeUserMenuDropdown } = useAdminforth()
571576
572577
function openCustomPage() {
573-
adminforth.alert({
578+
alert({
574579
message: 'Custom page is opened',
575580
variant: 'success',
576581
});
577-
adminforth.closeUserMenuDropdown();
582+
closeUserMenuDropdown();
578583
}
579584
</script>
580585
```
@@ -620,9 +625,12 @@ Now create file `AnyPageWelcome.vue` in the `custom` folder of your project:
620625
621626
<script setup>
622627
import { onMounted } from 'vue';
623-
import adminforth from '@/adminforth';
628+
import { useAdminforth } from '@/adminforth';
629+
630+
const { alert } = useAdminforth();
631+
624632
onMounted(() => {
625-
adminforth.alert({
633+
alert({
626634
message: 'Welcome!',
627635
variant: 'success',
628636
});

adminforth/documentation/docs/tutorial/03-Customization/10-menuConfiguration.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,11 @@ Most times you need to refresh the badge from some backend API or hook. To do th
248248
More rare case when you need to refresh menu item from the frontend component. You can achieve this by calling the next method:
249249

250250
```typescript
251-
import adminforth from '@/adminforth';
251+
import { useAdminforth } from '@/adminforth';
252252

253-
adminforth.menu.refreshMenuBadges()
253+
const { menu } = useAdminforth();
254+
255+
menu.refreshMenuBadges()
254256
```
255257

256258
## Avatars

adminforth/documentation/docs/tutorial/07-Plugins/02-TwoFactorsAuth.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ Imagine you have some button which does some API call
445445

446446
<script setup lang="ts">
447447
import { callApi } from '@/utils';
448-
import adminforth from '@/adminforth';
449448

450449
async function callAdminAPI() {
451450
const verificationResult = await window.adminforthTwoFaModal.get2FaConfirmationResult();
@@ -486,7 +485,9 @@ You might want to protect this call with a second factor also. To do it, we need
486485

487486
<script setup lang="ts">
488487
import { callApi } from '@/utils';
489-
import adminforth from '@/adminforth';
488+
import { useAdminforth } from '@/adminforth';
489+
490+
const { alert } = useAdminforth();
490491

491492
async function callAdminAPI() {
492493
// diff-add
@@ -505,7 +506,7 @@ async function callAdminAPI() {
505506
// diff-add
506507
if (!res?.ok) {
507508
// diff-add
508-
adminforth.alert({ message: res.error, variant: 'danger' });
509+
alert({ message: res.error, variant: 'danger' });
509510
// diff-add
510511
}
511512
}

0 commit comments

Comments
 (0)