import {
  actionGetPinnedMessagesSuccess,
  actionGetPinnedMessagesReject,
} from '../action';

import { TPinnedMessages, TPinnedMessage } from '../../../typescript/redux/pinnedMessages/types'
import { handleSort } from '../../../helpers';


const asyncFindPinnedMessages= (messages:TPinnedMessages) => async (dispatch:any) => {
  try {
    const pinnedMessages = handleSort('updatedAt', messages.filter((el: TPinnedMessage) => el.pinned === true), true)
     if(pinnedMessages)  dispatch(actionGetPinnedMessagesSuccess(pinnedMessages))
  } catch (e) {
    dispatch(actionGetPinnedMessagesReject())
  } 
};


export { asyncFindPinnedMessages };




