loadDataIfNeeded()

in src/amo/pages/Collection/index.js [191:288]


  loadDataIfNeeded(prevProps?: InternalProps) {
    const {
      collection,
      creating,
      errorHandler,
      filters,
      loading,
      location,
      match: { params },
    } = this.props;

    if (errorHandler.hasError()) {
      log.warn('Not loading data because of an error.');
      return;
    }

    if (creating || loading) {
      return;
    }

    let collectionChanged = false;
    let addonsPageChanged = false;

    if (prevProps && prevProps.location && location) {
      if (prevProps.location.pathname !== location.pathname) {
        collectionChanged = true;
      }
    }

    if (prevProps && !deepEqual(prevProps.filters, filters)) {
      addonsPageChanged = true;
    }

    if (collection) {
      let isSameCollectionUser;
      // Is `userId` a numeric ID?
      if (/^\d+$/.test(params.userId)) {
        isSameCollectionUser = `${collection.authorId}` === params.userId;
      } else {
        isSameCollectionUser =
          collection.authorUsername.toLowerCase() ===
          params.userId.toLowerCase();
      }

      if (
        collection.slug.toLowerCase() !== params.slug.toLowerCase() ||
        isSameCollectionUser === false
      ) {
        collectionChanged = true;
      }
    }

    // See: https://github.com/mozilla/addons-frontend/issues/4271
    if (!collectionChanged && collection) {
      if (params.slug !== collection.slug || !/^\d+$/.test(params.userId)) {
        const { editing, lang, clientApp } = this.props;

        const path = editing
          ? collectionEditUrl({ collection })
          : collectionUrl({ collection });

        this.props.dispatch(
          sendServerRedirect({
            status: 301,
            url: `/${lang}/${clientApp}${path}`,
          }),
        );
        return;
      }
    }

    if (!collection || collectionChanged) {
      this.props.dispatch(
        fetchCurrentCollection({
          errorHandlerId: errorHandler.id,
          filters,
          slug: params.slug,
          // It is possible that `userId` is  a `username` (string value) for
          // backward compatibility purpose.
          // $FlowIgnore
          userId: Number(params.userId) || params.userId,
        }),
      );

      return;
    }

    if (collection && addonsPageChanged && collection.numberOfAddons) {
      this.props.dispatch(
        fetchCurrentCollectionPage({
          errorHandlerId: errorHandler.id,
          filters,
          slug: params.slug,
          userId: Number(params.userId),
        }),
      );
    }
  }