Функция уведомления Javascript, но нет выходного текста - PullRequest
0 голосов
/ 17 ноября 2018

Я написал функцию уведомления для своего приложения Vue и импортировал ее в свой магазин (Vuex).

export default function getNotificationText(notification) {
  // Parent types.
  const DRIVE = 'drv'
  const DRIVE_GALLERY = 'dgl'
  const EXPERIENCE = 'exp'
  const EXPERIENCE_GALLERY = 'egl'
  const STAY = 'sty'
  const STAY_GALLERY = 'sgl'
  const LOCATION_PHOTO = 'lpt'
  const DRIVE_PHOTO = 'dpt'
  const EXPERIENCE_PHOTO = 'ept'
  const STAY_PHOTO = 'spt'
  const LOCATION_REVIEW = 'lrv'
  const DRIVE_REVIEW = 'drv'
  const EXPERIENCE_REVIEW = 'erv'
  const STAY_REVIEW = 'srv'
  const LOCATION_QUESTION = 'lqt'
  const DRIVE_QUESTION = 'dqt'
  const EXPERIENCE_QUESTION = 'eqt'
  const STAY_QUESTION = 'sqt'
  const TRIP = 'trp'
  const TRIP_PHOTO = 'tpt'
  const DRIVE_GALLERY_COMMENT = 'dgc'
  const EXPERIENCE_GALLERY_COMMENT = 'egc'
  const STAY_GALLERY_COMMENT = 'sgc'
  const LOCATION_PHOTO_COMMENT = 'lpc'
  const DRIVE_PHOTO_COMMENT = 'dpc'
  const EXPERIENCE_PHOTO_COMMENT = 'epc'
  const STAY_PHOTO_COMMENT = 'spc'
  const LOCATION_REVIEW_COMMENT = 'lrc'
  const DRIVE_REVIEW_COMMENT = 'drc'
  const EXPERIENCE_REVIEW_COMMENT = 'erc'
  const STAY_REVIEW_COMMENT = 'src'
  const LOCATION_QUESTION_COMMENT = 'lqc'
  const DRIVE_QUESTION_COMMENT = 'dqc'
  const EXPERIENCE_QUESTION_COMMENT = 'eqc'
  const STAY_QUESTION_COMMENT = 'sqc'
  const TRIP_COMMENT = 'trc'
  const TRIP_PHOTO_COMMENT = 'tpc'
  const USER = 'usr'
  const notificationText = ''
  // Build user text.
  const userText = notification.sender.first_name + ' ' + notification.sender.last_name
  // Build action text.
  const action = notification.action
  const actionText = ''
  // actions started
  const COMMENT = 'cmt'
  const LOVE = 'lov'
  const FOLLOW = 'flw'
  const ACCEPT_BOOKING = 'apb'
  const ADD_PHOTO = 'adp'
  const ADD_REVIEW = 'adr'
  const ADD_QUESTION = 'adq'
  if (action == COMMENT) {
    const actionText = 'commented on your'
  } else if (action === LOVE) {
    const actionText = 'liked your'
  } else if (action === FOLLOW) {
    const actionText = 'started following you'
  } else if (action === ACCEPT_BOOKING) {
    const actionText = 'accepted your booking'
  } else if (action === ADD_PHOTO) {
    const actionText = 'added a new photo on your'
  } else if (action === ADD_REVIEW) {
    const actionText = 'added a new review on your '
  } else if (action === ADD_QUESTION) {
    const actionText = 'asked a new question on your'
  }
  // Build parent text.
  const parentType = notification.parent.type
  const parentText = ''
  if (action === ADD_PHOTO || action === ADD_REVIEW || action === ADD_QUESTION) {
    if (parentType === DRIVE_PHOTO || parentType === DRIVE_REVIEW || parentType === DRIVE_QUESTION) {
      const parentText = 'drive'
    } else if (parentType === EXPERIENCE_PHOTO || parentType === EXPERIENCE_REVIEW || parentType === EXPERIENCE_QUESTION) {
      const parentText = 'experience'
    } else if (parentType === STAY_PHOTO || parentType === STAY_REVIEW || parentType === STAY_QUESTION) {
      const parentText = 'stay'
    }
  } else if (action === COMMENT || action === LOVE) {
    if (parentType === DRIVE_GALLERY || parentType === EXPERIENCE_GALLERY || parentType === STAY_GALLERY || parentType === LOCATION_PHOTO || parentType === DRIVE_PHOTO || parentType === EXPERIENCE_PHOTO || parentType === STAY_PHOTO || parentType === TRIP_PHOTO) {
      const parentText = 'photo'
    } else if (parentType === LOCATION_REVIEW || parentType === DRIVE_REVIEW || parentType === EXPERIENCE_REVIEW || parentType === STAY_REVIEW) {
      const parentText = 'review'
    } else if (parentType === LOCATION_QUESTION || parentType === DRIVE_QUESTION || parentType === EXPERIENCE_QUESTION || parentType === STAY_QUESTION) {
      const parentText = 'question'
    } else if (parentType === TRIP) {
      const parentText = 'trip'
    } else if (['DRIVE_GALLERY_COMMENT', 'EXPERIENCE_GALLERY_COMMENT', 'STAY_GALLERY_COMMENT', 'LOCATION_PHOTO_COMMENT', 'DRIVE_PHOTO_COMMENT', 'EXPERIENCE_PHOTO_COMMENT', 'STAY_PHOTO_COMMENT', 'LOCATION_REVIEW_COMMENT', 'DRIVE_REVIEW_COMMENT', 'EXPERIENCE_REVIEW_COMMENT', 'STAY_REVIEW_COMMENT', 'LOCATION_QUESTION_COMMENT', 'DRIVE_QUESTION_COMMENT', 'EXPERIENCE_QUESTION_COMMENT', 'STAY_QUESTION_COMMENT', 'TRIP_COMMENT', 'TRIP_PHOTO_COMMENT'].indexOf(parentType) > -1) {
      const parentText = 'comment'
      console.log(parentText)
    }
  }
  // Combine texts.
  const outputText = userText + ' ' + actionText
  if (parentText) {
    const outputText = parentText
  }
  // Return output text.
  return outputText
}

Я хочу показать выходной текст в соответствии с уже заданными условиями userTextкак пользователь firstname и lastname, но я не получил ActionText или ParentText.Внутри оператора if работает нормально, и я могу получить console.log, но вне оператора if это не работает.Пожалуйста, помогите мне.

Вот ссылка Pastebin для кода: https://pastebin.com/n0TaXxcJ

1 Ответ

0 голосов
/ 17 ноября 2018
// Combine texts.
let outputText = userText + ' ' + actionText
if (parentText) {
  outputText = outputText + parentText
}

return outputText

В конце вы переопределяете outputText. Кроме того, вы определили его как другую переменную, вместо этого используйте let.

Кроме того, лучше использовать пакеты типа config вместо копирования / вставки const DRIVE = 'drv' и других везде. Вы должны вносить изменения везде, если решите что-то изменить иначе.


Edit:

Вы можете заменить это

// Combine texts.
const outputText = userText + ' ' + actionText
if (parentText) {
  const outputText = parentText
}
// Return output text.
return outputText

блок с return userText + ' ' + actionText + ' ' + parentText и получить тот же эффект. Если parentText пуст, ничего не будет добавлено, верно?

...