Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

functions.ts 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { IReduxState } from '../app/types';
  2. import { IAnswerData } from './types';
  3. /**
  4. * Selector creator for determining if poll results should be displayed or not.
  5. *
  6. * @param {string} id - Id of the poll.
  7. * @returns {Function}
  8. */
  9. export function shouldShowResults(id: string) {
  10. return function(state: IReduxState) {
  11. return Boolean(state['features/polls']?.polls[id].showResults);
  12. };
  13. }
  14. /**
  15. * Selector creator for polls.
  16. *
  17. * @param {string} pollId - Id of the poll to get.
  18. * @returns {Function}
  19. */
  20. export function getPoll(pollId: string) {
  21. return function(state: IReduxState) {
  22. return state['features/polls'].polls[pollId];
  23. };
  24. }
  25. /**
  26. * Selector for calculating the number of unread poll messages.
  27. *
  28. * @param {IReduxState} state - The redux state.
  29. * @returns {number} The number of unread messages.
  30. */
  31. export function getUnreadPollCount(state: IReduxState) {
  32. const { nbUnreadPolls } = state['features/polls'];
  33. return nbUnreadPolls;
  34. }
  35. /**
  36. * Determines if the submit poll answer button should be disabled.
  37. *
  38. * @param {Array<boolean>} checkBoxStates - The states of the checkboxes.
  39. * @returns {boolean}
  40. */
  41. export function isSubmitAnswerDisabled(checkBoxStates: Array<boolean>) {
  42. return !checkBoxStates.find(checked => checked);
  43. }
  44. /**
  45. * Check if the input array has identical answers.
  46. *
  47. * @param {Array<IAnswerData>} currentAnswers - The array of current answers to compare.
  48. * @returns {boolean} - Returns true if the answers are identical.
  49. */
  50. export function hasIdenticalAnswers(currentAnswers: Array<IAnswerData>): boolean {
  51. const nonEmptyCurrentAnswers = currentAnswers.filter((answer): boolean => answer.name !== '');
  52. const currentAnswersSet = new Set(nonEmptyCurrentAnswers.map(answer => answer.name));
  53. return currentAnswersSet.size !== nonEmptyCurrentAnswers.length;
  54. }