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