import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { Router } from '@angular/router'
-import { Notifier } from '@app/core/notification'
+import { Notifier } from '@app/core/notification/notifier.service'
import { OAuthClientLocal, User as UserServerModel, UserRefreshToken } from '../../../../../shared'
import { User } from '../../../../../shared/models/users'
import { UserLogin } from '../../../../../shared/models/users/user-login.model'
import { environment } from '../../../environments/environment'
-import { RestExtractor } from '../../shared/rest'
+import { RestExtractor } from '../../shared/rest/rest-extractor.service'
import { AuthStatus } from './auth-status.model'
import { AuthUser } from './auth-user.model'
import { objectToUrlEncoded } from '@app/shared/misc/utils'
import { ToastModule } from 'primeng/toast'
import { Notifier } from './notification'
import { MessageService } from 'primeng/api'
+import { UserNotificationSocket } from '@app/core/notification/user-notification-socket.service'
@NgModule({
imports: [
UserRightGuard,
RedirectService,
Notifier,
- MessageService
+ MessageService,
+ UserNotificationSocket
]
})
export class CoreModule {
export * from './notifier.service'
+export * from './user-notification-socket.service'
--- /dev/null
+import { Injectable } from '@angular/core'
+import { environment } from '../../../environments/environment'
+import { UserNotification as UserNotificationServer } from '../../../../../shared'
+import { Subject } from 'rxjs'
+import * as io from 'socket.io-client'
+import { AuthService } from '../auth'
+
+export type NotificationEvent = 'new' | 'read' | 'read-all'
+
+@Injectable()
+export class UserNotificationSocket {
+ private notificationSubject = new Subject<{ type: NotificationEvent, notification?: UserNotificationServer }>()
+
+ private socket: SocketIOClient.Socket
+
+ constructor (
+ private auth: AuthService
+ ) {}
+
+ dispatch (type: NotificationEvent, notification?: UserNotificationServer) {
+ this.notificationSubject.next({ type, notification })
+ }
+
+ getMyNotificationsSocket () {
+ const socket = this.getSocket()
+
+ socket.on('new-notification', (n: UserNotificationServer) => this.dispatch('new', n))
+
+ return this.notificationSubject.asObservable()
+ }
+
+ private getSocket () {
+ if (this.socket) return this.socket
+
+ this.socket = io(environment.apiUrl + '/user-notifications', {
+ query: { accessToken: this.auth.getAccessToken() }
+ })
+
+ return this.socket
+ }
+}
></a>
</div>
- <my-user-notifications [ignoreLoadingBar]="true" [infiniteScroll]="false"></my-user-notifications>
+ <my-user-notifications [ignoreLoadingBar]="true" [infiniteScroll]="false" itemsPerPage="10"></my-user-notifications>
<a class="all-notifications" routerLink="/my-account/notifications" i18n>See all your notifications</a>
</ng-template>
import { User } from '../shared/users/user.model'
import { UserNotificationService } from '@app/shared/users/user-notification.service'
import { Subscription } from 'rxjs'
-import { Notifier } from '@app/core'
+import { Notifier, UserNotificationSocket } from '@app/core'
import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
import { NavigationEnd, Router } from '@angular/router'
import { filter } from 'rxjs/operators'
constructor (
private userNotificationService: UserNotificationService,
+ private userNotificationSocket: UserNotificationSocket,
private notifier: Notifier,
private router: Router
) {}
}
private subscribeToNotifications () {
- this.notificationSub = this.userNotificationService.getMyNotificationsSocket()
+ this.notificationSub = this.userNotificationSocket.getMyNotificationsSocket()
.subscribe(data => {
if (data.type === 'new') return this.unreadNotifications++
if (data.type === 'read') return this.unreadNotifications--
import { Injectable } from '@angular/core'
import { HttpClient, HttpParams } from '@angular/common/http'
-import { RestExtractor, RestService } from '@app/shared/rest'
+import { RestExtractor, RestService } from '../rest'
import { catchError, map, tap } from 'rxjs/operators'
import { environment } from '../../../environments/environment'
import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '../../../../../shared'
-import { UserNotification } from '@app/shared/users/user-notification.model'
-import { Subject } from 'rxjs'
-import * as io from 'socket.io-client'
-import { AuthService } from '@app/core'
-import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
-import { User } from '@app/shared'
+import { UserNotification } from './user-notification.model'
+import { AuthService } from '../../core'
+import { ComponentPagination } from '../rest/component-pagination.model'
+import { User } from '..'
+import { UserNotificationSocket } from '@app/core/notification/user-notification-socket.service'
@Injectable()
export class UserNotificationService {
static BASE_NOTIFICATIONS_URL = environment.apiUrl + '/api/v1/users/me/notifications'
static BASE_NOTIFICATION_SETTINGS = environment.apiUrl + '/api/v1/users/me/notification-settings'
- private notificationSubject = new Subject<{ type: 'new' | 'read' | 'read-all', notification?: UserNotification }>()
-
private socket: SocketIOClient.Socket
constructor (
private auth: AuthService,
private authHttp: HttpClient,
private restExtractor: RestExtractor,
- private restService: RestService
+ private restService: RestService,
+ private userNotificationSocket: UserNotificationSocket
) {}
listMyNotifications (pagination: ComponentPagination, unread?: boolean, ignoreLoadingBar = false) {
.pipe(map(n => n.total))
}
- getMyNotificationsSocket () {
- const socket = this.getSocket()
-
- socket.on('new-notification', (n: UserNotificationServer) => {
- this.notificationSubject.next({ type: 'new', notification: new UserNotification(n) })
- })
-
- return this.notificationSubject.asObservable()
- }
-
markAsRead (notification: UserNotification) {
const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read'
return this.authHttp.post(url, body, { headers })
.pipe(
map(this.restExtractor.extractDataBool),
- tap(() => this.notificationSubject.next({ type: 'read' })),
+ tap(() => this.userNotificationSocket.dispatch('read')),
catchError(res => this.restExtractor.handleError(res))
)
}
return this.authHttp.post(url, {}, { headers })
.pipe(
map(this.restExtractor.extractDataBool),
- tap(() => this.notificationSubject.next({ type: 'read-all' })),
+ tap(() => this.userNotificationSocket.dispatch('read-all')),
catchError(res => this.restExtractor.handleError(res))
)
}
)
}
- private getSocket () {
- if (this.socket) return this.socket
-
- this.socket = io(environment.apiUrl + '/user-notifications', {
- query: { accessToken: this.auth.getAccessToken() }
- })
-
- return this.socket
- }
-
private formatNotification (notification: UserNotificationServer) {
return new UserNotification(notification)
}
export class UserNotificationsComponent implements OnInit {
@Input() ignoreLoadingBar = false
@Input() infiniteScroll = true
+ @Input() itemsPerPage = 20
notifications: UserNotification[] = []
componentPagination: ComponentPagination = {
currentPage: 1,
- itemsPerPage: 20,
+ itemsPerPage: this.itemsPerPage,
totalItems: null
}