<commit_before>import {takeEvery, all, call, put} from 'redux-saga/effects'

import rest from '../../../rest'
import {notificationTransform} from '../../api'
import * as actions from './actions'

export const notificationCenterSelector = state => state.notification.center
export const notificationToasterSelector = state => state.notification.toaster

const LIMIT = 10

export default function* sagas(accept) {
  if (!accept) {
    yield all([
      call(loadInitialUnreadNotificationKeys),
      takeEvery(actions.LOAD_NOTIFICATIONS, loadNotifications),
      takeEvery(actions.MARK_AS_READ, markAsRead)
    ])
  }
}

export function* loadInitialUnreadNotificationKeys() {
  const limit = 100
  let page = 1
  const keys = []

  do {
    const query = {
      where: 'read == false and relPrincipal.username == :currentUsername',
      limit,
      page
    }

    const result = yield call(rest.fetchEntities, 'Notification', query, {method: 'GET'})
    keys.push(...result.map(e => e.key))

    page++
  } while (keys.length === (page - 1) * limit)

  yield put(actions.setUnreadNotificationKeys(keys))
}

export function* loadNotifications({payload: {offset}}) {
  const queryParams = {
    _limit: LIMIT,
    _offset: offset
  }
  const notificationsResponse = yield call(rest.requestSaga, 'client/notifications', {method: 'GET', queryParams})
  const notifications = yield all(notificationsResponse.body.data.map(v => call(notificationTransform, v)))

  notifications.reduce((acc, v) => {
    return {...acc, [v.key]: v}
  }, {})

  if (Object.keys(notifications).length < LIMIT) {
    yield put(actions.setMoreNotificationsAvailable(false))
  }

  yield put(actions.setNotifications(notifications))
}

export function* markAsRead({payload: {notificationKey}}) {<commit_msg>fix(app-extensions): fix load notifications<commit_after>import {takeEvery, all, call, put} from 'redux-saga/effects'

import rest from '../../../rest'
import {notificationTransform} from '../../api'
import * as actions from './actions'

export const notificationCenterSelector = state => state.notification.center
export const notificationToasterSelector = state => state.notification.toaster

const LIMIT = 10

export default function* sagas(accept) {
  if (!accept) {
    yield all([
      call(loadInitialUnreadNotificationKeys),
      takeEvery(actions.LOAD_NOTIFICATIONS, loadNotifications),
      takeEvery(actions.MARK_AS_READ, markAsRead)
    ])
  }
}

export function* loadInitialUnreadNotificationKeys() {
  const limit = 100
  let page = 1
  const keys = []

  do {
    const query = {
      where: 'read == false and relPrincipal.username == :currentUsername',
      limit,
      page
    }

    const result = yield call(rest.fetchEntities, 'Notification', query, {method: 'GET'})
    keys.push(...result.map(e => e.key))

    page++
  } while (keys.length === (page - 1) * limit)

  yield put(actions.setUnreadNotificationKeys(keys))
}

export function* loadNotifications({payload: {offset}}) {
  const queryParams = {
    _limit: LIMIT,
    _offset: offset
  }
  const notificationsResponse = yield call(rest.requestSaga, 'client/notifications', {method: 'GET', queryParams})
  const notificationList = yield all(notificationsResponse.body.data.map(v => call(notificationTransform, v)))
  const notifications = notificationList.reduce((acc, v) => {
    return {...acc, [v.key]: v}
  }, {})

  if (Object.keys(notifications).length < LIMIT) {
    yield put(actions.setMoreNotificationsAvailable(false))
  }

  yield put(actions.setNotifications(notifications))
}

export function* markAsRead({payload: {notificationKey}}) {
{'fraction_non_alphanumeric': 0.13346228239845262, 'fraction_numerical': 0.0038684719535783366, 'mean_word_length': 4.134751773049645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2768704', 'n_tokens_mistral': 1075, 'n_tokens_neox': 1052, 'n_words': 259}