Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.9 kB
6
Indexable
// Customizable Area Start

import { IBlock } from '../../../../framework/src/IBlock';
import { Message } from '../../../../framework/src/Message';
import { BlockComponent } from '../../../../framework/src/BlockComponent';
import MessageEnum, {
  getName,
} from '../../../../framework/src/Messages/MessageEnum';
import { runEngine } from '../../../../framework/src/RunEngine';
import StorageProvider from '../../../../framework/src/StorageProvider';
import * as helpers from '../../../../framework/src/Helpers'

import {
  Conversation,
  Client as ConversationsClient,
} from '@twilio/conversations';

export const configJSON = require('../config');

export interface Props {
  navigation: any;
  id: string;
  // Customizable Area Start
  // Customizable Area End
}

interface S {
  // Customizable Area Start
  like: boolean;

  // Customizable Area End
}

interface SS {
  // Customizable Area Start
  id: any;
  // Customizable Area End
}

export default class ChatIconsController extends BlockComponent<Props, S, SS> {
  // Customizable Area Start
  likeButtonApiCall: any;

  // Customizable Area End

  constructor(props: Props) {
    super(props);
    this.receive = this.receive.bind(this);

    this.subScribedMessages = [
      // Customizable Area Start
      getName(MessageEnum.NavigationPayLoadMessage),
      getName(MessageEnum.RestAPIResponceMessage),
      // Customizable Area End
    ];

    // Customizable Area Start

    this.state = {
      like: false,
    };
    runEngine.attachBuildingBlock(this as IBlock, this.subScribedMessages)
  }

  async componentDidMount(): Promise<void> {
    console.log('componentDidMount liked',this.props?.isLiked)
    this.setState({like:this.props?.isLiked ? true : false})
  }

  async receive(from: string, message: Message) {

    // Customizable Area Start

    // Navigation passed Data
    if (
      getName(MessageEnum.RestAPIResponceMessage) === message.id &&
      this.likeButtonApiCall != null &&
      this.likeButtonApiCall === message.getData(getName(MessageEnum.RestAPIResponceDataMessage))
    ) {

      let responseJson = message.getData(
        getName(MessageEnum.RestAPIResponceSuccessMessage)
      );
    
      if (responseJson !== undefined && responseJson?.errors === undefined) {
        console.log(responseJson?.data?.attributes?.messages,"responsejaons")

        // this.setState({messages:responseJson?.data?.attributes?.messages.reverse()})
     


      } else {
        let errorReponse = message.getData(
          getName(MessageEnum.RestAPIResponceErrorMessage)
        );
        console.log("Error response", errorReponse)
        console.log(responseJson)
      }

    }
    
    
    // Customizable Area End

  }

  likeChatApiCall = async () => {
    this.setState({ like: !this.state.like });
    console.log('like', this.props?.likeid);
    const token = await StorageProvider.get('USER_TOKEN');
    const header = {
      'Content-Type': 'application/json',
      token,
    };
    const is_favourite = true
    const requestMessage = new Message(
      getName(MessageEnum.RestAPIRequestMessage)
    );
    this.likeButtonApiCall = requestMessage.messageId;
    requestMessage.addData(
      getName(MessageEnum.RestAPIResponceEndPointMessage),
      `bx_block_chat/chats/${this.props?.likeid}/make_favourite`
    );
    requestMessage.addData(
      getName(MessageEnum.RestAPIRequestHeaderMessage),
      JSON.stringify(header)
    );
    const httpBody = {
      "is_favourite": true
    }

    requestMessage.addData(
      getName(MessageEnum.RestAPIRequestBodyMessage),
      JSON.stringify(httpBody),
    )
    requestMessage.addData(
      getName(MessageEnum.RestAPIRequestMethodMessage),
      'PUT'
    );

    runEngine.sendMessage(requestMessage.id, requestMessage);
    return true;
  };



  // Customizable Area End
}

// Customizable Area End