Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
11 kB
2
Indexable
Never
import React, { useMemo, useState } from 'react';
import { Button, Card, Carousel, Colors, GridList, Text, View } from 'react-native-ui-lib';
import { abbreviateNumber, nFormatter } from '../../../utils/number_formatter';
import { ScrollView } from 'react-native';
import { InfoBoxSocialComparison } from './InfoBoxSocialComparison';
import { InfoBox } from '../InfoBox';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import { InfoBoxOverview } from '../InfoBoxOverview';
import { parse } from 'querystring';

type Props = {
  report: any;
};

const SocialComparison = ({ report }: Props) => {
  const memoized: any = useMemo(() => {
    const data = report && report?.data;

    let competitions: any = [];
    let myScore = [];

    let isError = false;
    let isScorerError = false;
    let isCompetitionError = false;
    let isSocialError = false;
    let errorMessageType;

    const score = data?.score;
    const competition = data?.competition;

    if (!data) {
      isError = true;
      errorMessageType = 'errorMessage';
    }


    const parseCompany: any = [];
    const parseCompanyData = (company: any) => {

      if (!isSocialError) {
        company.social.forEach((socialItem: any) => {
          if (socialItem.type != 'linkedin') {
            let numberOfPosts;
            let numberOfPostsChange;

            let averageLikesPerPost;
            let averageLikesPerPostPrevious;
            let averageLikesPerPostChange;

            let averageSharesPerPost;
            let averageSharesPerPostPrevious;
            let averageSharesPerPostChange;

            let averageCommentsPerPost;
            let averageCommentsPerPostPrevious;
            let averageCommentsPerPostChange;

            if (report.data.type === 'MONTHLY') {
              if (socialItem.type === 'facebook') {
                numberOfPosts = socialItem.numberOfPostsLastMonth; // Total posts or posts monthly?
                numberOfPostsChange =
                  socialItem.numberOfPostsLastMonth -
                  socialItem.numberOfPostsPastMonth;

                averageLikesPerPost = socialItem.averageReactions;
                averageLikesPerPostPrevious =
                  socialItem.reactionsPastMonth /
                  socialItem.numberOfPostsPastMonth;
                averageLikesPerPostChange =
                  averageLikesPerPost - averageLikesPerPostPrevious;

                averageSharesPerPost = socialItem.averageShares;
                averageSharesPerPostPrevious =
                  socialItem.sharesPastMonth / socialItem.numberOfPostsPastMonth;
                averageSharesPerPostChange =
                  averageSharesPerPost - averageSharesPerPostPrevious;

                averageCommentsPerPost = socialItem.averageComments;
                averageCommentsPerPostPrevious =
                  socialItem.commentPastMonth / socialItem.numberOfPostsPastMonth;
                averageCommentsPerPostChange =
                  averageCommentsPerPost - averageCommentsPerPostPrevious;
              } else {
                numberOfPosts = socialItem.numberOfPostsLastMonth; // Total posts or posts monthly?
                numberOfPostsChange =
                  socialItem.numberOfPostsLastMonth -
                  socialItem.numberOfPostsPastMonth;

                averageLikesPerPost = socialItem.averageLikes;
                averageLikesPerPostPrevious =
                  socialItem.likesPastMonth / socialItem.numberOfPostsPastMonth;
                averageLikesPerPostChange =
                  averageLikesPerPost - averageLikesPerPostPrevious;

                averageSharesPerPost = socialItem.averageShares;
                averageSharesPerPostPrevious =
                  socialItem.sharesPastMonth / socialItem.numberOfPostsPastMonth;
                averageSharesPerPostChange =
                  averageSharesPerPost - averageSharesPerPostPrevious;

                averageCommentsPerPost = socialItem.averageComments;
                averageCommentsPerPostPrevious =
                  socialItem.commentPastMonth / socialItem.numberOfPostsPastMonth;
                averageCommentsPerPostChange =
                  averageCommentsPerPost - averageCommentsPerPostPrevious;
              }
            } else if (socialItem.type === 'facebook') {
              numberOfPosts = socialItem.numberOfPostsLastWeek; // Total posts or posts Weekly?
              numberOfPostsChange =
                socialItem.numberOfPostsLastWeek -
                socialItem.numberOfPostsPastWeek;

              averageLikesPerPost = socialItem.averageReactions;
              averageLikesPerPostPrevious =
                socialItem.reactionsPastWeek / socialItem.numberOfPostsPastWeek;
              averageLikesPerPostChange =
                averageLikesPerPost - averageLikesPerPostPrevious;

              averageSharesPerPost = socialItem.averageShares;
              averageSharesPerPostPrevious =
                socialItem.sharesPastWeek / socialItem.numberOfPostsPastWeek;
              averageSharesPerPostChange =
                averageSharesPerPost - averageSharesPerPostPrevious;

              averageCommentsPerPost = socialItem.averageComments;
              averageCommentsPerPostPrevious =
                socialItem.commentPastWeek / socialItem.numberOfPostsPastWeek;
              averageCommentsPerPostChange =
                averageCommentsPerPost - averageCommentsPerPostPrevious;
            } else {
              numberOfPosts = socialItem.numberOfPostsLastWeek; // Total posts or posts Weekly?
              numberOfPostsChange =
                socialItem.numberOfPostsLastWeek -
                socialItem.numberOfPostsPastWeek;

              averageLikesPerPost = socialItem.averageLikes;
              averageLikesPerPostPrevious =
                socialItem.likesPastWeek / socialItem.numberOfPostsPastWeek;
              averageLikesPerPostChange =
                averageLikesPerPost - averageLikesPerPostPrevious;

              averageSharesPerPost = socialItem.averageShares;
              averageSharesPerPostPrevious =
                socialItem.sharesPastWeek / socialItem.numberOfPostsPastWeek;
              averageSharesPerPostChange =
                averageSharesPerPost - averageSharesPerPostPrevious;

              averageCommentsPerPost = socialItem.averageComments;
              averageCommentsPerPostPrevious =
                socialItem.commentPastWeek / socialItem.numberOfPostsPastWeek;
              averageCommentsPerPostChange =
                averageCommentsPerPost - averageCommentsPerPostPrevious;
            }

            parseCompany.push({
              icon: socialItem.type,
              name: company.name,
              status: socialItem.status,
              values: [
                {
                  // Post
                  value: numberOfPosts,
                  exchangeValue: numberOfPostsChange,
                },
                {
                  // Avg. likes
                  value: averageLikesPerPost,
                  exchangeValue: averageLikesPerPostChange,
                  warnIndex: 2,
                },
                {
                  // Avg. Shares
                  value: averageSharesPerPost,
                  exchangeValue: averageSharesPerPostChange,
                },
                {
                  // Avg. Comment
                  value: averageCommentsPerPost,
                  exchangeValue: averageCommentsPerPostChange,
                },
              ],
            });
          }
        });
      }

    };
    if (!isError) {
      if (!competition || competition.length === 0) {
        isCompetitionError = true;
        errorMessageType = 'competitorNotExist';
      }

      if (!isCompetitionError && !isError) {
        competition.map((singleCompetition: any) =>
          competitions.push(parseCompanyData(singleCompetition)),
        );
      }

      if (score) {
        const social = score?.social;

        if (!social || social.length === 0) {
          isSocialError = true;
          errorMessageType = 'companySocialNotExist';
        }

        if (isSocialError && isCompetitionError) {
          errorMessageType = 'CompetitorAndSocialNotExist';
        }
      } else {
        isScorerError = true;
        errorMessageType = 'errorMessage';
      }

      if (!isSocialError && !isScorerError) {
        myScore = parseCompanyData(score);
      }
    }
    return { parseCompany }
  }, [report])
  const [openButtonIndex, setOpenButtonIndex] = useState(-1);

  const handleButtonClick = (index: number) => {
    if (openButtonIndex === index) {
      setOpenButtonIndex(-1);
    } else {
      setOpenButtonIndex(index);
    }
  };
  console.log(memoized?.parseCompany)
  return (

    // <Carousel loop showCounter pageControlPosition="under">
    //   {memoized?.data.map((data: any, index: any) => (
    //     <View key={index}>
    //       <Button
    //         center
    //         onPress={() => handleButtonClick(index)}
    //         style={{
    //           marginBottom: 8,
    //           height: 80,
    //           shadowColor: Colors.grey30,
    //           elevation: 14,
    //           borderColor:
    //             openButtonIndex === index ? Colors.white : Colors.grey30,
    //         }}
    //         backgroundColor={
    //           openButtonIndex === index ? Colors.orange40 : Colors.white
    //         }
    //         outlineWidth={openButtonIndex === index ? 2 : 0}>
    //         <Text
    //           style={{
    //             color: openButtonIndex === index ? Colors.white : Colors.grey30,
    //             position: 'absolute',
    //           }}
    //           text60BO
    //           margin-20>
    //           {data.header}
    //         </Text>
    //         <Text
    //           style={{
    //             position: 'absolute',
    //             left: 0,
    //           }}
    //           margin-30>
    //           {openButtonIndex === index ? (
    //             <MaterialIcons
    //               name="unfold-less"
    //               color={Colors.white}
    //               size={20}
    //             />
    //           ) : (
    //             <MaterialIcons
    //               name="unfold-more"
    //               color={Colors.orange40}
    //               size={20}
    //             />
    //           )}
    //         </Text>
    //         <Text
    //           style={{
    //             color:
    //               openButtonIndex === index ? Colors.white : Colors.orange40,
    //             position: 'absolute',
    //             right: 0,
    //           }}
    //           text50BO
    //           margin-20>
    //           {abbreviateNumber(data.value)}
    //         </Text>
    //       </Button>
    //       {openButtonIndex === index && (
    //         <View flex-1>
    //           <GridList
    //             numColumns={2}
    //             scrollEnabled={false}
    //             data={data.values}
    //             keyExtractor={(item, index) => index.toString()}
    //             renderItem={({ item }) => <InfoBoxSocialComparison item={item} />}
    //             itemSpacing={3}
    //           />
    //         </View>
    //       )}
    //     </View>
    //   ))}
    // </Carousel>
    <></>
  )
};

export default SocialComparison;