import React, { useState } from 'react'
import useAPIError from './useAPIError'
import ModalWrapper from '../Modal/ModalWrapper'
import TokenProjectSignup from '../Modal/TokenProjectSignup'
import MainHeading from '../Titles/MainHeading'
import ResetUserSettings from '../Buttons/ResetUserSettings'

import styles from './APIErrorMessage.module.scss'
import Button from '../Buttons/Button'
import ButtonWrapper from '../Buttons/ButtonWrapper'

const APIErrorDebugInformation = ({ error }) => {
  const [showDebugInformation, setShowDebugInformation] = useState(false)

  return (
    <div className={styles.debugWrapper}>
      {error.debug && showDebugInformation
        ? (
          <div className={styles.debugInformation}>
            <textarea
              className={styles.debugText}
              onClick={(e) => {
                e.target.focus()
                e.target.select()
              }}
              defaultValue={
              typeof error.debug === 'object'
                ? JSON.stringify(error.debug, null, '\t')
                : error.debug
            }
            />
          </div>
          )
        : null}
      <div className={styles.debugActions}>
        <ButtonWrapper>
          <Button
            icon='update'
            label='Refresh Page'
            onClick={(e) => {
              e.preventDefault()
              window.location.reload()
              return false
            }}
          />
          {error.debug
            ? (
              <Button
                icon='eye'
                label={showDebugInformation ? 'Hide Debug Details' : 'Show Debug Details'}
                className={styles.buttonDebug}
                onClick={() => {
                  setShowDebugInformation(!showDebugInformation)
                }}
              />
              )
            : null}
          <ResetUserSettings />
        </ButtonWrapper>
        If this error continues please contact <a href='mailto:extensions@envato.com'>extensions@envato.com</a>.
      </div>
    </div>
  )
}

const APIErrorMessage = () => {
  const { errors, removeError } = useAPIError()

  const shouldOpenErrorModal = errors.length > 0

  if (!shouldOpenErrorModal) {
    return null
  }

  return (
    <>
      {errors.map(error => {
        if (error.code === 'invalid_subscription') {
          return (
            <TokenProjectSignup showWelcomeMessaging key={error.code} onCloseCallback={() => { removeError(error) }} />
          )
        }
        if (error.code === 'zip_failure') {
          return (
            <ModalWrapper key={error.code} isOpen onCloseCallback={() => { removeError(error) }}>
              <MainHeading title='Template Kit Install Error' />
              <p className={styles.copy}>There was an issue installing this template kit. Please try again.</p>
              {error.message}
              <APIErrorDebugInformation error={error} />
            </ModalWrapper>
          )
        }
        if (error.code === 'missing_permissions') {
          return (
            <ModalWrapper key={error.code} isOpen onCloseCallback={() => { removeError(error) }}>
              <MainHeading title='Error' />
              <p className={styles.copy}>
                {error.message}
              </p>
              <APIErrorDebugInformation error={error} />
            </ModalWrapper>
          )
        }
        if (error.code === 'generic_api_error') {
          return (
            <ModalWrapper key={error.code} isOpen onCloseCallback={() => { removeError(error) }}>
              <MainHeading title='Unexpected Error' />
              <p className={styles.copy}>Sorry there was an unexpected error from API call:</p>
              {error.message}
              <APIErrorDebugInformation error={error} />
            </ModalWrapper>
          )
        }
        return null
      })}
    </>
  )
}

export default APIErrorMessage
import React, { useState } from 'react'
import useAPIError from './useAPIError'
import ModalWrapper from '../Modal/ModalWrapper'
import TokenProjectSignup from '../Modal/TokenProjectSignup'
import MainHeading from '../Titles/MainHeading'
import ResetUserSettings from '../Buttons/ResetUserSettings'

import styles from './APIErrorMessage.module.scss'
import Button from '../Buttons/Button'
import ButtonWrapper from '../Buttons/ButtonWrapper'

const APIErrorDebugInformation = ({ error }) => {
  const [showDebugInformation, setShowDebugInformation] = useState(false)

  return (
    <div className={styles.debugWrapper}>
      {error.debug && showDebugInformation
        ? (
          <div className={styles.debugInformation}>
            <textarea
              className={styles.debugText}
              onClick={(e) => {
                e.target.focus()
                e.target.select()
              }}
              defaultValue={
              typeof error.debug === 'object'
                ? JSON.stringify(error.debug, null, '\t')
                : error.debug
            }
            />
          </div>
          )
        : null}
      <div className={styles.debugActions}>
        <ButtonWrapper>
          <Button
            icon='update'
            label='Refresh Page'
            onClick={(e) => {
              e.preventDefault()
              window.location.reload()
              return false
            }}
          />
          {error.debug
            ? (
              <Button
                icon='eye'
                label={showDebugInformation ? 'Hide Debug Details' : 'Show Debug Details'}
                className={styles.buttonDebug}
                onClick={() => {
                  setShowDebugInformation(!showDebugInformation)
                }}
              />
              )
            : null}
          <ResetUserSettings />
        </ButtonWrapper>
        If this error continues please contact <a href='mailto:extensions@envato.com'>extensions@envato.com</a>.
      </div>
    </div>
  )
}

const APIErrorMessage = () => {
  const { errors, removeError } = useAPIError()

  const shouldOpenErrorModal = errors.length > 0

  if (!shouldOpenErrorModal) {
    return null
  }

  return (
    <>
      {errors.map(error => {
        if (error.code === 'invalid_subscription') {
          return (
            <TokenProjectSignup showWelcomeMessaging key={error.code} onCloseCallback={() => { removeError(error) }} />
          )
        }
        if (error.code === 'zip_failure') {
          return (
            <ModalWrapper key={error.code} isOpen onCloseCallback={() => { removeError(error) }}>
              <MainHeading title='Template Kit Install Error' />
              <p className={styles.copy}>There was an issue installing this template kit. Please try again.</p>
              {error.message}
              <APIErrorDebugInformation error={error} />
            </ModalWrapper>
          )
        }
        if (error.code === 'missing_permissions') {
          return (
            <ModalWrapper key={error.code} isOpen onCloseCallback={() => { removeError(error) }}>
              <MainHeading title='Error' />
              <p className={styles.copy}>
                {error.message}
              </p>
              <APIErrorDebugInformation error={error} />
            </ModalWrapper>
          )
        }
        if (error.code === 'generic_api_error') {
          return (
            <ModalWrapper key={error.code} isOpen onCloseCallback={() => { removeError(error) }}>
              <MainHeading title='Unexpected Error' />
              <p className={styles.copy}>Sorry there was an unexpected error from API call:</p>
              {error.message}
              <APIErrorDebugInformation error={error} />
            </ModalWrapper>
          )
        }
        return null
      })}
    </>
  )
}

export default APIErrorMessage
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://www.xn--tkrt-lzabc.com/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-2.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-3.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-4.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-5.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-6.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-7.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-8.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-9.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-10.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-11.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-12.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-13.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-14.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-15.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-16.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-17.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-18.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-19.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-20.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-21.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-22.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-23.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-24.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-25.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-26.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-27.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-28.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-29.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-30.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-31.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-32.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-33.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-34.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-35.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-36.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-37.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-38.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-39.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-40.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-41.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-42.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-43.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-44.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-45.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-46.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-47.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-48.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-49.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-50.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-51.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-52.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-53.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-54.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-55.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-56.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-57.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-58.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-59.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-60.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-61.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-62.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-63.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-64.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-65.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-66.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-67.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-68.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-69.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-70.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-71.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-72.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-73.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-74.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-75.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-76.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-77.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-78.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-79.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-80.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-81.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-82.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-83.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-84.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-85.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-post-86.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-taxonomies-post_tag-1.xml</loc></sitemap><sitemap><loc>http://www.xn--tkrt-lzabc.com/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
