All files / munch-ease-frontend/src/components AlertToast.vue

0% Statements 0/90
0% Branches 0/1
0% Functions 0/1
0% Lines 0/90

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91                                                                                                                                                                                     
<template>
  <div
    v-if="showAlert"
    :class="['alert', type]"
    @click="dismiss"
  >
    <img
      v-if="icon"
      :src="icon"
      alt="Notification icon"
    >
    <div class="message-container">
      <h4 class="heading">
        {{ heading }}
      </h4>
      <p class="message">
        {{ message }}
      </p>
    </div>
  </div>
</template>

<script setup>
import { computed, watch } from 'vue'
import { useAlert } from '@/composables/useAlert'

const alertIcons = {
  error: new URL('@/assets/notification-error.svg', import.meta.url).toString(),
  success: new URL('@/assets/notification-success.svg', import.meta.url).toString(),
  info: new URL('@/assets/notification-info.svg', import.meta.url).toString(),
}

const { current, clear, scheduleAutoDismiss } = useAlert()

const showAlert = computed(() => !!current.value)
const heading = computed(() => current.value?.heading ?? '')
const message = computed(() => current.value?.message ?? '')
const type = computed(() => current.value?.type ?? '')
const icon = computed(() => (type.value && alertIcons[type.value] ? alertIcons[type.value] : null))

function dismiss() {
  clear()
}

watch(
  () => current.value?._ts,
  (ts) => {
    if (ts) scheduleAutoDismiss(5000)
  }
)
</script>

<style scoped>
/* Display as a toast, in the bottom right corner */
/* Place the icon to the left for the full height, then have the heading and message stacked to the right */

.alert {
  position: fixed;
  bottom: 1em;
  right: 1em;
  display: flex;
  align-items: center;
  padding: 1em;
  border-radius: 5px;
  color: white;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  text-align: left;
}

.alert img {
  width: 2em;
  height: 2em;
  margin-right: 1em;

  /* Invert svg colors */
  filter: invert(1);
}

.alert.error {
  background-color: #f44336;
}

.alert.success {
  background-color: #4caf50;
}

.alert.info {
  background-color: #2196f3;
}
</style>