|
@@ -0,0 +1,121 @@
|
|
|
|
|
+<template>
|
|
|
|
|
+ <div class="notification-toggle-wrapper">
|
|
|
|
|
+ <q-toggle v-model="is_enabled"
|
|
|
|
|
+ class="large-toggle"
|
|
|
|
|
+ checked-icon="notifications"
|
|
|
|
|
+ unchecked-icon="notifications_off"
|
|
|
|
|
+ color="primary"
|
|
|
|
|
+ :label="`اطلاعرسانی درس سطح یک`"
|
|
|
|
|
+ left-label
|
|
|
|
|
+ :disable="loading"
|
|
|
|
|
+ @update:model-value="onToggle">
|
|
|
|
|
+ <q-tooltip anchor="top middle"
|
|
|
|
|
+ self="bottom middle"
|
|
|
|
|
+ :offset="[10, 10]">
|
|
|
|
|
+ {{ is_enabled ? 'غیرفعال کردن اطلاعرسانی' : 'فعالسازی اطلاعرسانی' }}
|
|
|
|
|
+ </q-tooltip>
|
|
|
|
|
+
|
|
|
|
|
+ <template v-slot:loading>
|
|
|
|
|
+ <q-spinner color="primary" />
|
|
|
|
|
+ </template>
|
|
|
|
|
+ </q-toggle>
|
|
|
|
|
+
|
|
|
|
|
+ <!-- دیالوگ لاگین -->
|
|
|
|
|
+ <q-dialog v-model="showLoginDialog">
|
|
|
|
|
+ <login-component @login-success="onLoginSuccess" />
|
|
|
|
|
+ </q-dialog>
|
|
|
|
|
+ </div>
|
|
|
|
|
+</template>
|
|
|
|
|
+
|
|
|
|
|
+<script>
|
|
|
|
|
+import LoginComponent from 'src/components/Auth/LoginComponent.vue'
|
|
|
|
|
+import { APIGateway } from 'src/api/APIGateway'
|
|
|
|
|
+
|
|
|
|
|
+export default {
|
|
|
|
|
+ name: 'NotificationToggle',
|
|
|
|
|
+ components: { LoginComponent },
|
|
|
|
|
+ props: {
|
|
|
|
|
+ initialState: {
|
|
|
|
|
+ type: Boolean,
|
|
|
|
|
+ default: false
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ data () {
|
|
|
|
|
+ return {
|
|
|
|
|
+ sourceType: 'UNIT',
|
|
|
|
|
+ sourceId: 1,
|
|
|
|
|
+ // is_enabled: this.initialState,
|
|
|
|
|
+ is_enabled: false,
|
|
|
|
|
+ loading: false,
|
|
|
|
|
+ showLoginDialog: false,
|
|
|
|
|
+ isUserLogin: false
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ mounted () {
|
|
|
|
|
+ this.loadAuthData()
|
|
|
|
|
+ if (this.isUserLogin) {
|
|
|
|
|
+ this.fetchNotificationStatus()
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ methods: {
|
|
|
|
|
+ loadAuthData () { // prevent Hydration node mismatch
|
|
|
|
|
+ this.user = this.$store.getters['Auth/user']
|
|
|
|
|
+ this.isUserLogin = this.$store.getters['Auth/isUserLogin']
|
|
|
|
|
+ },
|
|
|
|
|
+ fetchNotificationStatus () {
|
|
|
|
|
+ this.loading = true
|
|
|
|
|
+ APIGateway.alert.getMyCorrespondingAlert(
|
|
|
|
|
+ {
|
|
|
|
|
+ source_id: this.sourceId,
|
|
|
|
|
+ source_type: this.sourceType
|
|
|
|
|
+ }).then((alert) => {
|
|
|
|
|
+ this.is_enabled = alert?.is_enabled
|
|
|
|
|
+ }).catch(() => {
|
|
|
|
|
+ this.is_enabled = false
|
|
|
|
|
+ }).finally(() => {
|
|
|
|
|
+ this.loading = false
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
|
|
+ onToggle (val) {
|
|
|
|
|
+ if (!this.isUserLogin) {
|
|
|
|
|
+ this.showLoginDialog = true
|
|
|
|
|
+ // برگردوندن toggle به حالت قبلی تا لاگین انجام بشه
|
|
|
|
|
+ this.is_enabled = !val
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ this.toggleNotification(val)
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ toggleNotification (val) {
|
|
|
|
|
+ this.loading = true
|
|
|
|
|
+ APIGateway.alert.submit({
|
|
|
|
|
+ source_id: this.sourceId,
|
|
|
|
|
+ source_type: this.sourceType,
|
|
|
|
|
+ is_enabled: val,
|
|
|
|
|
+ owner: this.user.id
|
|
|
|
|
+ }).then((data) => {
|
|
|
|
|
+ this.is_enabled = data.is_enabled
|
|
|
|
|
+ }).finally(() => {
|
|
|
|
|
+ this.loading = false
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
|
|
+ onLoginSuccess () {
|
|
|
|
|
+ this.showLoginDialog = false
|
|
|
|
|
+ this.fetchNotificationStatus()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+</script>
|
|
|
|
|
+
|
|
|
|
|
+<style scoped lang="scss">
|
|
|
|
|
+.notification-toggle-wrapper {
|
|
|
|
|
+ display: flex;
|
|
|
|
|
+ justify-content: center;
|
|
|
|
|
+ align-items: center;
|
|
|
|
|
+ padding: 16px;
|
|
|
|
|
+ font-size: 18px;
|
|
|
|
|
+}
|
|
|
|
|
+:deep(.q-toggle__inner) {
|
|
|
|
|
+ font-size: 64px;
|
|
|
|
|
+}
|
|
|
|
|
+</style>
|