<commit_before>/* @flow */
/* eslint-disable react/no-unused-prop-types */
import invariant from 'invariant';
import * as React from 'react';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { oneLine } from 'common-tags';

import { withRenderedErrorHandler } from 'core/errorHandler';
import { setLatestReview } from 'amo/actions/reviews';
import { selectLatestUserReview } from 'amo/reducers/reviews';
import * as reviewsApi from 'amo/api/reviews';
import AddonReview from 'amo/components/AddonReview';
import AuthenticateButton from 'core/components/AuthenticateButton';
import ReportAbuseButton from 'amo/components/ReportAbuseButton';
import {
  ADDON_TYPE_DICT,
  ADDON_TYPE_EXTENSION,
  ADDON_TYPE_LANG,
  ADDON_TYPE_OPENSEARCH,
  ADDON_TYPE_STATIC_THEME,
  ADDON_TYPE_THEME,
  validAddonTypes as defaultValidAddonTypes,
} from 'core/constants';
import translate from 'core/i18n/translate';
import log from 'core/logger';
import UserRating from 'ui/components/UserRating';
import type { AppState } from 'amo/store';
import type { ErrorHandlerType } from 'core/errorHandler';
import type { UserReviewType } from 'amo/actions/reviews';
import type {
  GetLatestReviewParams,
  SubmitReviewParams,
} from 'amo/api/reviews';
import type { DispatchFunc } from 'core/types/redux';
import type { ApiState } from 'core/reducers/api';
import type { AddonType, AddonVersionType } from 'core/types/addons';
import type { ReactRouterLocationType } from 'core/types/router';
import type { I18nType } from 'core/types/i18n';

import './styles.scss';

type LoadSavedReviewFunc = ({|
  addonId: $PropertyType<GetLatestReviewParams, 'addon'>,
  addonSlug: string,
  apiState: ApiState,
  userId: $PropertyType<GetLatestReviewParams, 'user'>,
  versionId: $PropertyType<GetLatestReviewParams, 'version'>,
|}) => Promise<any>;

type SubmitReviewFunc = (SubmitReviewParams) => Promise<void>;

type Props = {|
  addon: AddonType,
  location: ReactRouterLocationType,
  onReviewSubmitted?: () => void,
  version: AddonVersionType,
|};

type DispatchMappedProps = {|
  loadSavedReview: LoadSavedReviewFunc,
  submitReview: SubmitReviewFunc,
|};

type InternalProps = {|
  ...Props,
  ...DispatchMappedProps,
  apiState: ApiState,
  errorHandler: ErrorHandlerType,
  i18n: I18nType,
  userId: number,
  userReview?: UserReviewType | null,
|};

type State = {|
  showTextEntry: boolean,
|};

export class RatingManagerBase extends React.Component<InternalProps, State> {
  constructor(props: InternalProps) {
    super(props);
    this.state = { showTextEntry: false };
  }

  componentDidMount() {
    const {
      addon,
      apiState,
      loadSavedReview,
      userId,
      userReview,
      version,
    } = this.props;

    if (userId && userReview === undefined) {
      log.debug(`Loading a saved rating (if it exists) for user ${userId}`);
      loadSavedReview({
        apiState,
        userId,
        addonId: addon.id,
        addonSlug: addon.slug,
        versionId: version.id,
      });
    }
  }

  onSelectRating = (rating: number) => {
    const { userReview, version } = this.props;

    const params = {
      errorHandler: this.props.errorHandler,
      rating,
      apiState: this.props.apiState,
      addonId: this.props.addon.id,
      reviewId: undefined,
      versionId: version.id,
    };

    if (userReview) {
      log.debug(`Editing reviewId ${userReview.id}`);
      if (userReview.versionId === params.versionId) {
        params.reviewId = userReview.id;

        log.debug(oneLine`Updating reviewId ${userReview.id} for
          versionId ${params.versionId || '[empty]'}`);
      } else {
        // Since we have a version mismatch, submit the review against the
        // current most version, similar to how new reviews are created.
        params.versionId =
          this.props.addon.current_version &&
          this.props.addon.current_version.id;

        log.debug(oneLine`Submitting a new review for
          versionId ${params.versionId || '[empty]'}`);
      }
    } else {
      log.debug(oneLine`Submitting a new review for
        versionId ${params.versionId || '[empty]'}`);
    }

    return this.props.submitReview(params).then(() => {
      this.setState({ showTextEntry: true });
    });
  };

  getLogInPrompt(
    { addonType }: {| addonType: string |},
    {
      validAddonTypes = defaultValidAddonTypes,
    }: {|
      validAddonTypes: typeof defaultValidAddonTypes,
    |} = {},
  ) {
    const { i18n } = this.props;
    switch (addonType) {<commit_msg>Remove untested, unsupported code for editing reviews against old add-on versions (#6080)<commit_after>/* @flow */
/* eslint-disable react/no-unused-prop-types */
import invariant from 'invariant';
import * as React from 'react';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { oneLine } from 'common-tags';

import { withRenderedErrorHandler } from 'core/errorHandler';
import { setLatestReview } from 'amo/actions/reviews';
import { selectLatestUserReview } from 'amo/reducers/reviews';
import * as reviewsApi from 'amo/api/reviews';
import AddonReview from 'amo/components/AddonReview';
import AuthenticateButton from 'core/components/AuthenticateButton';
import ReportAbuseButton from 'amo/components/ReportAbuseButton';
import {
  ADDON_TYPE_DICT,
  ADDON_TYPE_EXTENSION,
  ADDON_TYPE_LANG,
  ADDON_TYPE_OPENSEARCH,
  ADDON_TYPE_STATIC_THEME,
  ADDON_TYPE_THEME,
  validAddonTypes as defaultValidAddonTypes,
} from 'core/constants';
import translate from 'core/i18n/translate';
import log from 'core/logger';
import UserRating from 'ui/components/UserRating';
import type { AppState } from 'amo/store';
import type { ErrorHandlerType } from 'core/errorHandler';
import type { UserReviewType } from 'amo/actions/reviews';
import type {
  GetLatestReviewParams,
  SubmitReviewParams,
} from 'amo/api/reviews';
import type { DispatchFunc } from 'core/types/redux';
import type { ApiState } from 'core/reducers/api';
import type { AddonType, AddonVersionType } from 'core/types/addons';
import type { ReactRouterLocationType } from 'core/types/router';
import type { I18nType } from 'core/types/i18n';

import './styles.scss';

type LoadSavedReviewFunc = ({|
  addonId: $PropertyType<GetLatestReviewParams, 'addon'>,
  addonSlug: string,
  apiState: ApiState,
  userId: $PropertyType<GetLatestReviewParams, 'user'>,
  versionId: $PropertyType<GetLatestReviewParams, 'version'>,
|}) => Promise<any>;

type SubmitReviewFunc = (SubmitReviewParams) => Promise<void>;

type Props = {|
  addon: AddonType,
  location: ReactRouterLocationType,
  onReviewSubmitted?: () => void,
  version: AddonVersionType,
|};

type DispatchMappedProps = {|
  loadSavedReview: LoadSavedReviewFunc,
  submitReview: SubmitReviewFunc,
|};

type InternalProps = {|
  ...Props,
  ...DispatchMappedProps,
  apiState: ApiState,
  errorHandler: ErrorHandlerType,
  i18n: I18nType,
  userId: number,
  userReview?: UserReviewType | null,
|};

type State = {|
  showTextEntry: boolean,
|};

export class RatingManagerBase extends React.Component<InternalProps, State> {
  constructor(props: InternalProps) {
    super(props);
    this.state = { showTextEntry: false };
  }

  componentDidMount() {
    const {
      addon,
      apiState,
      loadSavedReview,
      userId,
      userReview,
      version,
    } = this.props;

    if (userId && userReview === undefined) {
      log.debug(`Loading a saved rating (if it exists) for user ${userId}`);
      loadSavedReview({
        apiState,
        userId,
        addonId: addon.id,
        addonSlug: addon.slug,
        versionId: version.id,
      });
    }
  }

  onSelectRating = (rating: number) => {
    const { userReview, version } = this.props;

    const params = {
      errorHandler: this.props.errorHandler,
      rating,
      apiState: this.props.apiState,
      addonId: this.props.addon.id,
      reviewId: undefined,
      versionId: version.id,
    };

    if (userReview) {
      log.debug(`Editing reviewId ${userReview.id}`);
      params.reviewId = userReview.id;
    } else {
      log.debug(oneLine`Submitting a new review for
        versionId ${params.versionId || '[empty]'}`);
    }

    return this.props.submitReview(params).then(() => {
      this.setState({ showTextEntry: true });
    });
  };

  getLogInPrompt(
    { addonType }: {| addonType: string |},
    {
      validAddonTypes = defaultValidAddonTypes,
    }: {|
      validAddonTypes: typeof defaultValidAddonTypes,
    |} = {},
  ) {
    const { i18n } = this.props;
    switch (addonType) {
{'fraction_non_alphanumeric': 0.12236581674234485, 'fraction_numerical': 0.0032599837000814994, 'mean_word_length': 3.722374931280924, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7379608', 'n_tokens_mistral': 2755, 'n_tokens_neox': 2723, 'n_words': 616}