Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
9.4 kB
1
Indexable
Never
import React, { memo, useMemo, useState } from 'react';
import { Button, Card, Carousel, Colors, GridList, GridListItem, 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: any = [];

    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,
                },
              ],
            });
          }
        });
      }
      console.log(parseCompany)
    };
    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])

  

  console.log(memoized?.parseCompany)
  return (

    <Carousel loop showCounter pageControlPosition="under">
      {memoized?.parseCompany.map((data: any, index: any) => (
        <View center>
          <Text orange40 text50BO>{data.name}</Text>
          <Text grey30 text60BO>{data.icon}</Text>
          {
            data?.values.map((value, Subindex) => (
              <View center>
                <Card>
                  <Text>
                    {value.value.toFixed(2)}
                  </Text>
                  <Text>
                    {value.exchangeValue.toFixed(2)}
                  </Text>
                </Card>
              </View>
            ))
          }
        </View>
      ))}
    </Carousel>

  )
};

export default SocialComparison;