1import StatusIcon from '../Global/StatusIcon';
2import i18n from '@/i18n';
3
4const BVToastMixin = {
5  components: {
6    StatusIcon,
7  },
8  methods: {
9    $_BVToastMixin_createTitle(title, status) {
10      const statusIcon = this.$createElement('StatusIcon', {
11        props: { status },
12      });
13      const titleWithIcon = this.$createElement(
14        'strong',
15        { class: 'toast-icon' },
16        [statusIcon, title],
17      );
18      return titleWithIcon;
19    },
20    $_BVToastMixin_createBody(messageBody) {
21      if (Array.isArray(messageBody)) {
22        return messageBody.map((message) =>
23          this.$createElement('p', { class: 'mb-0' }, message),
24        );
25      } else {
26        return [this.$createElement('p', { class: 'mb-0' }, messageBody)];
27      }
28    },
29    $_BVToastMixin_createTimestamp() {
30      const timestamp = this.$filters.formatTime(new Date());
31      return this.$createElement('p', { class: 'mt-3 mb-0' }, timestamp);
32    },
33    $_BVToastMixin_createRefreshAction() {
34      return this.$createElement(
35        'BLink',
36        {
37          class: 'd-inline-block mt-3',
38          on: {
39            click: () => {
40              this.$root.$emit('refresh-application');
41            },
42          },
43        },
44        i18n.global.t('global.action.refresh'),
45      );
46    },
47    $_BVToastMixin_initToast(body, title, variant) {
48      this.$root.$bvToast.toast(body, {
49        title,
50        variant,
51        autoHideDelay: 10000, //auto hide in milliseconds
52        noAutoHide: variant !== 'success',
53        isStatus: true,
54        solid: true,
55      });
56    },
57    successToast(
58      message,
59      {
60        title: t = i18n.global.t('global.status.success'),
61        timestamp,
62        refreshAction,
63      } = {},
64    ) {
65      const body = this.$_BVToastMixin_createBody(message);
66      const title = this.$_BVToastMixin_createTitle(t, 'success');
67      if (refreshAction) body.push(this.$_BVToastMixin_createRefreshAction());
68      if (timestamp) body.push(this.$_BVToastMixin_createTimestamp());
69      this.$_BVToastMixin_initToast(body, title, 'success');
70    },
71    errorToast(
72      message,
73      {
74        title: t = i18n.global.t('global.status.error'),
75        timestamp,
76        refreshAction,
77      } = {},
78    ) {
79      const body = this.$_BVToastMixin_createBody(message);
80      const title = this.$_BVToastMixin_createTitle(t, 'danger');
81      if (refreshAction) body.push(this.$_BVToastMixin_createRefreshAction());
82      if (timestamp) body.push(this.$_BVToastMixin_createTimestamp());
83      this.$_BVToastMixin_initToast(body, title, 'danger');
84    },
85    warningToast(
86      message,
87      {
88        title: t = i18n.global.t('global.status.warning'),
89        timestamp,
90        refreshAction,
91      } = {},
92    ) {
93      const body = this.$_BVToastMixin_createBody(message);
94      const title = this.$_BVToastMixin_createTitle(t, 'warning');
95      if (refreshAction) body.push(this.$_BVToastMixin_createRefreshAction());
96      if (timestamp) body.push(this.$_BVToastMixin_createTimestamp());
97      this.$_BVToastMixin_initToast(body, title, 'warning');
98    },
99    infoToast(
100      message,
101      {
102        title: t = i18n.global.t('global.status.informational'),
103        timestamp,
104        refreshAction,
105      } = {},
106    ) {
107      const body = this.$_BVToastMixin_createBody(message);
108      const title = this.$_BVToastMixin_createTitle(t, 'info');
109      if (refreshAction) body.push(this.$_BVToastMixin_createRefreshAction());
110      if (timestamp) body.push(this.$_BVToastMixin_createTimestamp());
111      this.$_BVToastMixin_initToast(body, title, 'info');
112    },
113  },
114};
115
116export default BVToastMixin;
117