<commit_before>/* eslint-disable react/prop-types, react/no-multi-comp */
import * as React from 'react';
import { withRouter } from 'react-router';
import Helmet from 'react-helmet';
import { getDatePart } from 'helpers/date';
import makePage from 'components/page';
import AutoLoadFinder from 'components/finder/autoload';
import QueryStateContainer from 'helpers/query-state-container';
import RoutingConstants from 'config/routing-constants';
import { translate } from 'config/strings';
import storage from 'helpers/storage';
import PetitionModel from './model';
import PetitionAPI from './api';
import { CITY_PORTAL_PARAM, CITY_PORTAL_CLEAR_FILTER } from './api';
import { retrieveCityPortalId } from './api';


const handleCityPortalIdFilter = (config) => {
  /**
   * Determine the current ID filter and write it to storage
   */
  let cityPortalId = config[CITY_PORTAL_PARAM];
  if (cityPortalId === CITY_PORTAL_CLEAR_FILTER) {
    cityPortalId = void 0;
  }
  storage.setItem(`iris-${CITY_PORTAL_PARAM}`, cityPortalId);
  return cityPortalId;
};


function getData(skip, batchSize, config) {
  const cityPortalId = handleCityPortalIdFilter(config);
  const searchParams = Object.assign({
    resolve: ['city', 'owner'].join(','),
    limit: batchSize,
    offset: skip,
  }, config, {
    city: config.city || void 0,
    [CITY_PORTAL_PARAM]: cityPortalId,
    sort: config.title_ft && 'score' || config.sort || void 0,
    state: config.state || void 0,
    title_ft: config.title_ft || void 0,
  });
  return PetitionAPI.search(searchParams);
}

const openPetitionEditor = (id, router) => {
  router.push(RoutingConstants.PETITION.pathname.replace(':id', id));
};

const columns = [
  { label: translate('petitions.petition') },
  { label: translate('petitions.city') },
  { label: translate('petitions.owner') },
  { label: translate('petitions.supporters') },
  { label: translate('petitions.created') },
  { label: translate('petitions.state') }
];


const ItemsTemplate = ({ item, router }) => {
  const petition = PetitionModel(item);
  if (petition && petition.id) {
    let state = petition.state.name;
    if (petition.state.parent) {
      state = petition.state.parent + '.' + state;
    }
    return (
      <tr
        style={{ cursor: 'pointer' }}
        onClick={() => openPetitionEditor(petition.id, router)}
      >
        <td>{petition.title}</td>
        <td>{petition.city}</td>
        <td>{petition.owner}</td>
        <td>{petition.supporters.count}</td>
        <td>{getDatePart(petition.dc.created)}</td>
        <td>{state}</td>
      </tr>
    );
  }
  return null;
};

const PetitionQueryStateContainer = (location, router, pathname) => {
  /**
   * Extend the 'QueryStateContainer'.
   * Reason:
   *  We need to get the 'city.portal.id' filter from the local storage if
   *  current 'query' does not contain any information which portal to filter
   *  for.
   *  QueryStateContainer.get() is used to set the `value` of the SearchForm
   *  fields (e.g. dropdown,...). We use this place here to 'sync' the
   *  components
   *  In that case, we also update the location's queries
   */
  return Object.assign(
    {},
    QueryStateContainer(location, router, pathname),
    {
      get() {
        const query = location.query;
        const cityPortal = retrieveCityPortalId(location);
        if (cityPortal.retrievedFromStorage) {
          // retrieved from storage (not query parameters). hence, we need
          // to update the query parameters
          query[CITY_PORTAL_PARAM] = cityPortal.id;
          router.push({ pathname, query });
        }
        return query;
      }
    }
  );
};

export default withRouter(makePage(React.createClass({
  displayName: 'PetitionFinder',

  render() {
    const { location, router } = this.props;
    const title = translate('petitions.finder-title');
    return (
      <div className="container-fluid">
        <Helmet title={title} />
        <h1>{title}</h1>
        <AutoLoadFinder
          reloadToken={location}
          columns={columns}
          fetchData={getData}
          state={PetitionQueryStateContainer(location, router, RoutingConstants.PETITIONS.pathname)}
          itemsTemplate={ItemsTemplate}
          itemsTemplateProps={{ router }}
          searchConfig={[
            {
              name: 'title_ft',
              id: 'titleSearchField',
              addon: 'search',
              type: 'string',
              lg: 3,
              md: 6,
              placeholder: 'Title'
            },
            {
              name: 'city',
              id: 'citySearchField',
              addon: 'search',
              type: 'city-autocomplete',
              lg: 3,
              md: 6,
              placeholder: 'City',
              autoCompleteStyle: {
                borderRadius: '0 4px 4px 0',
              },
            },
            {
              name: 'state',
              id: 'statusField',
              type: 'multi-select',
              options: [
                { value: 'draft', label: 'Draft' },
                { value: 'rejected', label: 'Rejected' },
                { value: 'supportable.*', label: 'Supportable' },
                { value: 'supportable.pending', label: 'Pending' },
                { value: 'supportable.active', label: 'Active' },
                { value: 'supportable.winner', label: 'Winner' },
                { value: 'loser', label: 'Loser' },
                { value: 'processing.*', label: 'Processing' },
                { value: 'processing.sendLetterRequested', label: 'Send Letter' },
                { value: 'processing.waitForLetterResponse', label: 'Wait for Letter Response' },
                { value: 'processing.letterResponseArrived', label: 'Letter Response Arrived' },
                { value: 'processing.noLetterResponse', label: 'No Letter Response' },
                { value: 'closedWithoutLetterResponse', label: 'No Letter Closed' },
                { value: 'closed', label: 'Closed' },
                { value: 'deleted', label: 'Deleted' },
              ],
              lg: 6,
            },
            {
              lg: 6,
              xs: 4,
              name: 'clear-filters',
              label: 'Clear Filters',
              role: 'clear-filters',
            },
            {
              lg: 3,
              xs: 4,
              name: CITY_PORTAL_PARAM,
              label: 'In domain',
              showLabel: true,
              id: 'filterDomain',
              type: 'options',
              options: [
                { value: '', label: 'All domains' },
                { value: 'nwch:aaz2016', label: 'Aargauer Zeitung' },
                { value: 'nwch:bat2016', label: 'Badener Tagblatt' },
                { value: 'nwch:blz2016', label: 'Basellandsch. Zeitung' },
                { value: 'nwch:bzb2016', label: 'bz Basel' },
                { value: 'nwch:gtb2016', label: 'Grenchner Tagblatt' },
                { value: 'nwch:liz2016', label: 'Limmattaler Zeitung' },
                { value: 'nwch:ot2016', label: 'Oltner Tagblatt' },
                { value: 'nwch:soz2016', label: 'Solothurner Zeitung' },
              ]
            },
            {
              name: 'sort',
              label: 'Sorted by',
              showLabel: true,
              id: 'sortDropdown',
              type: 'options',
              lg: 3,
              xs: 4,
              pull: 'right',
              options: [
                { value: 'created', label: 'Creation Date' },
                { value: 'trending', label: 'Trending' },
                { value: 'supporters.amount', label: 'Amount Supporters' }
              ],
            }
          ]}
          uniqueItemProperty="id"
        />
      </div>
    );
  }
})));
<commit_msg>domain filter: change wording: domain -> brand<commit_after>/* eslint-disable react/prop-types, react/no-multi-comp */
import * as React from 'react';
import { withRouter } from 'react-router';
import Helmet from 'react-helmet';
import { getDatePart } from 'helpers/date';
import makePage from 'components/page';
import AutoLoadFinder from 'components/finder/autoload';
import QueryStateContainer from 'helpers/query-state-container';
import RoutingConstants from 'config/routing-constants';
import { translate } from 'config/strings';
import storage from 'helpers/storage';
import PetitionModel from './model';
import PetitionAPI from './api';
import { CITY_PORTAL_PARAM, CITY_PORTAL_CLEAR_FILTER } from './api';
import { retrieveCityPortalId } from './api';


const handleCityPortalIdFilter = (config) => {
  /**
   * Determine the current ID filter and write it to storage
   */
  let cityPortalId = config[CITY_PORTAL_PARAM];
  if (cityPortalId === CITY_PORTAL_CLEAR_FILTER) {
    cityPortalId = void 0;
  }
  storage.setItem(`iris-${CITY_PORTAL_PARAM}`, cityPortalId);
  return cityPortalId;
};


function getData(skip, batchSize, config) {
  const cityPortalId = handleCityPortalIdFilter(config);
  const searchParams = Object.assign({
    resolve: ['city', 'owner'].join(','),
    limit: batchSize,
    offset: skip,
  }, config, {
    city: config.city || void 0,
    [CITY_PORTAL_PARAM]: cityPortalId,
    sort: config.title_ft && 'score' || config.sort || void 0,
    state: config.state || void 0,
    title_ft: config.title_ft || void 0,
  });
  return PetitionAPI.search(searchParams);
}

const openPetitionEditor = (id, router) => {
  router.push(RoutingConstants.PETITION.pathname.replace(':id', id));
};

const columns = [
  { label: translate('petitions.petition') },
  { label: translate('petitions.city') },
  { label: translate('petitions.owner') },
  { label: translate('petitions.supporters') },
  { label: translate('petitions.created') },
  { label: translate('petitions.state') }
];


const ItemsTemplate = ({ item, router }) => {
  const petition = PetitionModel(item);
  if (petition && petition.id) {
    let state = petition.state.name;
    if (petition.state.parent) {
      state = petition.state.parent + '.' + state;
    }
    return (
      <tr
        style={{ cursor: 'pointer' }}
        onClick={() => openPetitionEditor(petition.id, router)}
      >
        <td>{petition.title}</td>
        <td>{petition.city}</td>
        <td>{petition.owner}</td>
        <td>{petition.supporters.count}</td>
        <td>{getDatePart(petition.dc.created)}</td>
        <td>{state}</td>
      </tr>
    );
  }
  return null;
};

const PetitionQueryStateContainer = (location, router, pathname) => {
  /**
   * Extend the 'QueryStateContainer'.
   * Reason:
   *  We need to get the 'city.portal.id' filter from the local storage if
   *  current 'query' does not contain any information which portal to filter
   *  for.
   *  QueryStateContainer.get() is used to set the `value` of the SearchForm
   *  fields (e.g. dropdown,...). We use this place here to 'sync' the
   *  components
   *  In that case, we also update the location's queries
   */
  return Object.assign(
    {},
    QueryStateContainer(location, router, pathname),
    {
      get() {
        const query = location.query;
        const cityPortal = retrieveCityPortalId(location);
        if (cityPortal.retrievedFromStorage) {
          // retrieved from storage (not query parameters). hence, we need
          // to update the query parameters
          query[CITY_PORTAL_PARAM] = cityPortal.id;
          router.push({ pathname, query });
        }
        return query;
      }
    }
  );
};

export default withRouter(makePage(React.createClass({
  displayName: 'PetitionFinder',

  render() {
    const { location, router } = this.props;
    const title = translate('petitions.finder-title');
    return (
      <div className="container-fluid">
        <Helmet title={title} />
        <h1>{title}</h1>
        <AutoLoadFinder
          reloadToken={location}
          columns={columns}
          fetchData={getData}
          state={PetitionQueryStateContainer(location, router, RoutingConstants.PETITIONS.pathname)}
          itemsTemplate={ItemsTemplate}
          itemsTemplateProps={{ router }}
          searchConfig={[
            {
              name: 'title_ft',
              id: 'titleSearchField',
              addon: 'search',
              type: 'string',
              lg: 3,
              md: 6,
              placeholder: 'Title'
            },
            {
              name: 'city',
              id: 'citySearchField',
              addon: 'search',
              type: 'city-autocomplete',
              lg: 3,
              md: 6,
              placeholder: 'City',
              autoCompleteStyle: {
                borderRadius: '0 4px 4px 0',
              },
            },
            {
              name: 'state',
              id: 'statusField',
              type: 'multi-select',
              options: [
                { value: 'draft', label: 'Draft' },
                { value: 'rejected', label: 'Rejected' },
                { value: 'supportable.*', label: 'Supportable' },
                { value: 'supportable.pending', label: 'Pending' },
                { value: 'supportable.active', label: 'Active' },
                { value: 'supportable.winner', label: 'Winner' },
                { value: 'loser', label: 'Loser' },
                { value: 'processing.*', label: 'Processing' },
                { value: 'processing.sendLetterRequested', label: 'Send Letter' },
                { value: 'processing.waitForLetterResponse', label: 'Wait for Letter Response' },
                { value: 'processing.letterResponseArrived', label: 'Letter Response Arrived' },
                { value: 'processing.noLetterResponse', label: 'No Letter Response' },
                { value: 'closedWithoutLetterResponse', label: 'No Letter Closed' },
                { value: 'closed', label: 'Closed' },
                { value: 'deleted', label: 'Deleted' },
              ],
              lg: 6,
            },
            {
              lg: 6,
              xs: 4,
              name: 'clear-filters',
              label: 'Clear Filters',
              role: 'clear-filters',
            },
            {
              lg: 3,
              xs: 4,
              name: CITY_PORTAL_PARAM,
              label: 'Brands',
              showLabel: true,
              id: 'filterDomain',
              type: 'options',
              options: [
                { value: '', label: 'All brands' },
                { value: 'nwch:aaz2016', label: 'Aargauer Zeitung' },
                { value: 'nwch:bat2016', label: 'Badener Tagblatt' },
                { value: 'nwch:blz2016', label: 'Basellandsch. Zeitung' },
                { value: 'nwch:bzb2016', label: 'bz Basel' },
                { value: 'nwch:gtb2016', label: 'Grenchner Tagblatt' },
                { value: 'nwch:liz2016', label: 'Limmattaler Zeitung' },
                { value: 'nwch:ot2016', label: 'Oltner Tagblatt' },
                { value: 'nwch:soz2016', label: 'Solothurner Zeitung' },
              ]
            },
            {
              name: 'sort',
              label: 'Sorted by',
              showLabel: true,
              id: 'sortDropdown',
              type: 'options',
              lg: 3,
              xs: 4,
              pull: 'right',
              options: [
                { value: 'created', label: 'Creation Date' },
                { value: 'trending', label: 'Trending' },
                { value: 'supporters.amount', label: 'Amount Supporters' }
              ],
            }
          ]}
          uniqueItemProperty="id"
        />
      </div>
    );
  }
})));

{'fraction_non_alphanumeric': 0.13536921793802528, 'fraction_numerical': 0.006928850965548213, 'mean_word_length': 1.998268897864974, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 54, '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': '1865650', 'n_tokens_mistral': 4634, 'n_tokens_neox': 4396, 'n_words': 1064}