<%#
 Copyright 2013-2021 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<%_ const storeName = entityInstance + 'Store'; _%>
import React, { useState, useEffect } from 'react';
import { connect } from 'app/shared/util/typed-inject';
<%_ if (paginationInfiniteScroll) { _%>
import InfiniteScroll from '@larryyangsen/react-infinite-scroller';
<%_ } _%>
import { Link, RouteComponentProps } from 'react-router-dom';
import { Button, <% if (searchEngine) { %>Input, InputGroup, FormGroup, Form,<% } %>Col, Row, Table } from 'reactstrap';
import {
<%_ if (blobFields.length > 0) { _%>
<%_ if (fieldsContainBlobOrImage) { _%>
openFile,
<%_ } _%>
byteSize,
<%_ } _%>
Translate<% if (searchEngine) { %>, translate<% } %>
<% if (fieldsContainDate) { %>, TextFormat<% } %>
<%_ if (!paginationNo) { _%>
, getSortState
<%_ if (paginationPagination) { _%>
, JhiPagination, JhiItemCount
<%_ }
} _%>
} from 'react-jhipster';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

<%_ if (fieldsContainDuration) { _%>
import { DurationFormat } from 'app/shared/DurationFormat';
<%_ } _%>

import { I<%= entityReactName %> } from 'app/shared/model/<%= entityModelFileName %>.model';
import { APP_DATE_FORMAT, APP_LOCAL_DATE_FORMAT } from 'app/config/constants';
<%_ if (!paginationNo) { _%>
import { ASC, DESC, ITEMS_PER_PAGE, SORT } from 'app/shared/util/pagination.constants';
import { overridePaginationStateWithQueryParams } from 'app/shared/util/entity-utils';
<%_ } _%>

import { IRootStore } from 'app/shared/stores';
export interface I<%= entityReactName %>Props extends StoreProps, RouteComponentProps<{url: string}> {}

export const <%= entityReactName %> = (props: I<%= entityReactName %>Props) => {
<%_ if (searchEngine) { _%>
const [search, setSearch] = useState('');
<%_ } _%>
<%_ if (!paginationNo) { _%>
    const [paginationState, setPaginationState] = useState(overridePaginationStateWithQueryParams(getSortState(props.location, ITEMS_PER_PAGE, '<%= primaryKey.name %>'), props.location.search));
<%_ } _%>
<%_ if (paginationInfiniteScroll) { _%>
const [sorting, setSorting] = useState(false);
<%_ } _%>

  const <%= entityInstance %>List = props.<%= entityInstance %>List;
  const loading = props.loading;
<%_ if (!paginationNo) { _%>
  const totalItems = props.totalItems;
<%_ } _%>
<%_ if (paginationInfiniteScroll) { _%>
  const links = props.links;
  const entity = props.entity;
  const updateSuccess = props.updateSuccess;
<%_ } _%>

<%_ if (paginationPagination || paginationInfiniteScroll || !paginationNo && !paginationInfiniteScroll) { _%>
const getAllEntities = () => {
<%_ if (searchEngine) { _%>
if (search) {
      props.searchEntities({
query: search,
page: paginationState.activePage - 1,
size: paginationState.itemsPerPage,
sort: `${paginationState.sort},${paginationState.order}`
        });
} else {
      props.getEntities({
page: paginationState.activePage - 1,
size: paginationState.itemsPerPage,
sort: `${paginationState.sort},${paginationState.order}`,
        });
}
<%_ } else { _%>
      props.getEntities({
page: paginationState.activePage - 1,
size: paginationState.itemsPerPage,
sort: `${paginationState.sort},${paginationState.order}`,
        });
<%_ } _%>
};
<%_ } _%>

<%_ if (paginationInfiniteScroll) { _%>
const resetAll = () => {
    props.reset();
setPaginationState({
...paginationState,
activePage: 1
});
    props.getEntities({});
};
<%_ } _%>

<%_ if (paginationNo || paginationInfiniteScroll) { _%>
useEffect(() => {
<%_ if (!paginationNo) { _%>
resetAll();
<%_ } else { _%>
    props.getEntities({});
<%_ } _%>
}, []);
<%_ } _%>

<%_ if (searchEngine) { _%>
const startSearching = (e) => {
if (search) {
<%_ if (paginationInfiniteScroll) { _%>
      props.reset();
<%_ } _%>
<%_ if (!paginationNo) { _%>
setPaginationState({
...paginationState,
activePage: 1
});
      props.searchEntities({
query: search,
page: paginationState.activePage - 1,
size: paginationState.itemsPerPage,
sort: `${paginationState.sort},${paginationState.order}`,
      });
<%_ } else { _%>
      props.searchEntities({query: search});
<%_ } _%>
}
e.preventDefault();
};

const clear = () => {
<%_ if (paginationInfiniteScroll) { _%>
    props.reset();
<%_ } _%>
setSearch('');
<%_ if (!paginationNo) { _%>
setPaginationState({
...paginationState,
activePage: 1
});
<%_ } _%>
    props.getEntities({});
};

const handleSearch = event => setSearch(event.target.value);
<%_ } _%>

<%_ if (paginationInfiniteScroll) { _%>
useEffect(() => {
if (updateSuccess) {
resetAll();
}
}, [updateSuccess]);

useEffect(() => {
getAllEntities()
}, [paginationState.activePage]);

const handleLoadMore = () => {
if ((window as any).pageYOffset > 0) {
setPaginationState({
...paginationState,
activePage: paginationState.activePage + 1
});
}
};
<%_ } _%>

<%_ if (!paginationNo) { _%>
<%_ if (paginationPagination) { _%>
const sortEntities = () => {
getAllEntities();
const endURL = `?page=${paginationState.activePage}&sort=${paginationState.sort},${paginationState.order}`;
if (props.location.search !== endURL) {
props.history.push(`${props.location.pathname}${endURL}`);
}
};
<%_ } _%>

useEffect(() => {
<%_ if (paginationInfiniteScroll) { _%>
if (sorting) {
getAllEntities();
setSorting(false);
}
<%_ } else { _%>
sortEntities();
<%_ } _%>
}, [<% if (paginationInfiniteScroll) { %>sorting<% } else { %>paginationState.activePage, paginationState.order, paginationState.sort<% } %><%_ if (searchEngine) { _%>, search<% } %>]);

<%_ if (paginationPagination) { _%>
useEffect(() => {
const params = new URLSearchParams(props.location.search);
const page = params.get('page');
const sort = params.get(SORT);
if (page && sort) {
const sortSplit = sort.split(',');
setPaginationState({
...paginationState,
activePage: +page,
sort: sortSplit[0],
order: sortSplit[1]
});
}
}, [props.location.search]);
<%_ } _%>

const sort = p => () => {
<%_ if (paginationInfiniteScroll) { _%>
    props.reset();
setPaginationState({
...paginationState,
activePage: 1,
order: paginationState.order === ASC ? DESC : ASC,
sort: p
});
setSorting(true);
<%_ } else { _%>
setPaginationState({
...paginationState,
order: paginationState.order === ASC ? DESC : ASC,
sort: p
});
<%_ } _%>
};

<%_ if (paginationPagination) { _%>
const handlePagination = currentPage =>
setPaginationState({
...paginationState,
activePage: currentPage
});
<%_ } _%>
<%_ } _%>

const handleSyncList = () => {
<%_ if (paginationNo) { _%>
      props.getEntities({});
<%_ } _%>
<%_ if (paginationPagination) { _%>
sortEntities();
<%_ } _%>
<%_ if (paginationInfiniteScroll) { _%>
resetAll();
<%_ } _%>
};

const { match } = props;

return (
<div>
    <h2 id="<%= entityFileName %>-heading" data-cy="<%= entityClass %>Heading">
        <Translate contentKey="<%= i18nKeyPrefix %>.home.title"><%= entityClassPluralHumanized %></Translate>
        <div className="d-flex justify-content-end">
            <Button className="mr-2" color="info" onClick={handleSyncList} disabled={loading}>
                <FontAwesomeIcon icon="sync" spin={loading} /> <Translate contentKey="<%= i18nKeyPrefix %>.home.refreshListLabel">Refresh List</Translate>
            </Button>
            <%_ if (!readOnly) { _%>
                <Link to={`${match.url}/new`} className="btn btn-primary jh-create-entity" id="jh-create-entity" data-cy="entityCreateButton">
                <FontAwesomeIcon icon="plus" />&nbsp;
                <Translate contentKey="<%= i18nKeyPrefix %>.home.createLabel">
                    Create new <%= entityClassHumanized %>
                </Translate>
                </Link>
            <%_ } _%>
        </div>
    </h2>
    <%_ if (searchEngine) { _%>
        <Row>
            <Col sm="12">
            <Form onSubmit={startSearching}>
                <FormGroup>
                    <InputGroup>
                        <Input type="text" name="search" defaultValue={search} onChange={handleSearch}
                               placeholder=<% if (enableTranslation) { %>{translate('<%= i18nKeyPrefix %>.home.search')}<% } else { %>"Search"<% } %>/>
                        <Button className="input-group-addon">
                            <FontAwesomeIcon icon="search" />
                        </Button>
                        <Button type="reset" className="input-group-addon" onClick={clear}>
                            <FontAwesomeIcon icon="trash" />
                        </Button>
                    </InputGroup>
                </FormGroup>
            </Form>
            </Col>
        </Row>
    <%_ } _%>
    <div className="table-responsive">
        <%_ if (paginationInfiniteScroll) { _%>
        <InfiniteScroll pageStart={paginationState.activePage}
                        loadMore={handleLoadMore}
                        hasMore={paginationState.activePage - 1 < links.next}
        loader={<div className="loader">Loading ...</div>}
        threshold={0}
        initialLoad={false}>
        <%_ } _%>
        {
        <%= entityInstance %>List && <%= entityInstance %>List.length > 0 ?(
        <Table responsive>
            <thead>
            <tr>
                <%_ for (field of fields) { _%>
                    <th<% if (!paginationNo) { %> className="hand" onClick={sort('<%= field.fieldName %>')} <%_ } _%>><Translate contentKey="<%= `${i18nKeyPrefix}.${field.fieldName}` %>"><%= field.fieldNameHumanized %></Translate><% if (!paginationNo) { %> <FontAwesomeIcon icon="sort" /><% } %></th>
                <%_ } _%>
                <%_ for (relationship of relationships) { _%>
                <%_ if (relationship.relationshipManyToOne
                        || (relationship.relationshipOneToOne && relationship.ownerSide)
                        || (relationship.relationshipManyToMany && relationship.ownerSide && paginationNo)) { _%>
                    <th<% if (!paginationNo) { %> <% } %>><Translate contentKey="<%= `${i18nKeyPrefix}.${relationship.relationshipName}` %>"><%= relationship.relationshipNameHumanized %></Translate><% if (!paginationNo) { %> <FontAwesomeIcon icon="sort" /><% } %></th>
                <%_ } _%>
                <%_ } _%>
                <th />
            </tr>
            </thead>
            <tbody>
            {
            <%= entityInstance %>List.map((<%= entityInstance %>, i) => (
            <tr key={`entity-${i}`} data-cy="entityTable">
                <td>
                    <Button tag={Link} to={`${match.url}/${<%= entityInstance %>.<%= primaryKey.name %>}`} color="link" size="sm">
                        {<%= entityInstance %>.<%= primaryKey.name %>}
                    </Button>
                </td>
                <%_ for (field of fields) {
                    const fieldType = field.fieldType;
                    const fieldName = field.fieldName;
                    const fieldIsEnum = field.fieldIsEnum;
                _%>
                <%_ if (fieldName !== primaryKey.name) { _%>
                <td>
                    <%_ if (field.fieldTypeBoolean) { _%>
                        {<%= entityInstance %>.<%= field.fieldName %> ? 'true' : 'false'}
                    <%_ } else if (field.fieldTypeTimed) { _%>
                        {<%= entityInstance %>.<%= fieldName %> ? <TextFormat type="date" value={<%= entityInstance %>.<%= fieldName %>} format={APP_DATE_FORMAT} />: null}
                    <%_ } else if (field.fieldTypeDuration) { _%>
                        {<%= entityInstance %>.<%= fieldName %> ? <DurationFormat value={<%= entityInstance %>.<%= fieldName %>} />: null}
                    <%_ } else if (field.fieldTypeLocalDate) { _%>
                        {<%= entityInstance %>.<%= fieldName %> ? <TextFormat type="date" value={<%= entityInstance %>.<%= fieldName %>} format={APP_LOCAL_DATE_FORMAT} />: null}
                    <%_ } else if (fieldIsEnum && enableTranslation) { _%>
                        <Translate contentKey={`<%= frontendAppName %>.<%= fieldType %>.${<%= entityInstance %>.<%= fieldName %>}`} />
                    <%_ } else if (field.fieldTypeBinary) { _%>
                    <%_
                        // blobFields
                    if (!field.blobContentTypeText) {
                    _%>
                        {<%= entityInstance %>.<%= fieldName %> ? (
                        <div>
                            {<%= entityInstance %>.<%= fieldName %>ContentType ? (
                            <a onClick={openFile(<%= entityInstance %>.<%= fieldName %>ContentType, <%= entityInstance %>.<%= fieldName %>)}>
                                <%_ if (field.blobContentTypeImage) { _%>
                                    <img src={`data:${<%= entityInstance %>.<%= fieldName %>ContentType};base64,${<%= entityInstance %>.<%= fieldName %>}`} style={{ maxHeight: '30px' }} />
                                <%_ } else { _%>
                                <Translate contentKey="entity.action.open">Open</Translate>
                                <%_ } _%>
                                &nbsp;
                            </a>
                            ) : null}
                            <span>{<%= entityInstance %>.<%= fieldName %>ContentType}, {byteSize(<%= entityInstance %>.<%= fieldName %>)}</span>
                        </div>
                        ) : null}
                    <%_ } else { _%>
                        {<%= entityInstance %>.<%= fieldName %>}
                    <%_ } _%>
                    <%_ } else { _%>
                        {<%= entityInstance %>.<%= fieldName %>}
                    <%_ } _%>
                </td>
                <%_ } _%>
                <%_ } _%>
                <%_ for (relationship of relationships) {
                    const ownerSide = relationship.ownerSide;
                    const relationshipFieldName = relationship.relationshipFieldName;
                    const relationshipFieldNamePlural = relationship.relationshipFieldNamePlural;
                    const otherEntityPkName = relationship.otherEntity.primaryKey && relationship.otherEntity.primaryKey.name || 'id';
                    const otherEntityStateName = relationship.otherEntityStateName;
                    const otherEntityField = relationship.otherEntityField; _%>
                <%_ if (relationship.relationshipManyToOne
                        || (relationship.relationshipOneToOne && ownerSide)
                        || (relationship.relationshipManyToMany && ownerSide && paginationNo)) { _%>
                <td>
                    <%_ if (relationship.otherEntityUser) { _%>
                    <%_ if (relationship.relationshipManyToMany) { _%>
                        {
                        (<%= entityInstance %>.<%= relationshipFieldNamePlural %>) ?
                        (<%= entityInstance %>.<%= relationshipFieldNamePlural %>.map((val, j) =>
                        <span key={j}>{val.<%= otherEntityField %>}{(j === <%= entityInstance %>.<%= relationshipFieldNamePlural %>.length - 1) ? '' : ', '}</span>
                        )
                        ) : null
                        }
                    <%_ } else { _%>
                        {<%= entityInstance + "." + relationshipFieldName %> ? <%= entityInstance + "." + relationshipFieldName + "." + otherEntityField %> : ''}
                    <%_ } _%>
                    <%_ } else { _%>
                    <%_ if (relationship.relationshipManyToMany) { _%>
                        {
                        (<%= entityInstance %>.<%= relationshipFieldNamePlural %>) ?
                        (<%= entityInstance %>.<%= relationshipFieldNamePlural %>.map((val, j) =>
                        <span key={j}><Link to={`<%= otherEntityStateName %>/${val.id}`}>{val.<%= otherEntityField %>}</Link>{(j === <%= entityInstance %>.<%= relationshipFieldNamePlural %>.length - 1) ? '' : ', '}</span>
                        )
                        ) : null
                        }
                    <%_ } else { _%>
                        {<%= entityInstance + "." + relationshipFieldName %> ?
                        <Link to={`<%= otherEntityStateName %>/${<%= entityInstance + "." + relationshipFieldName + "." + otherEntityPkName + "}" %>`}>
                        {<%= entityInstance + "." + relationshipFieldName + "." + otherEntityField %>}
                        </Link> : ''}
                    <%_ } _%>
                    <%_ } _%>
                </td>
                <%_ } _%>
                <%_ } _%>
                <td className="text-right">
                    <div className="btn-group flex-btn-group-container">
                        <Button tag={Link} to={`${match.url}/${<%= entityInstance %>.<%= primaryKey.name %>}`} color="info" size="sm" data-cy="entityDetailsButton">
                            <FontAwesomeIcon icon="eye" /> <span className="d-none d-md-inline" ><Translate contentKey="entity.action.view">View</Translate></span>
                        </Button>
                        <%_ if (!readOnly) { _%>
                            <Button tag={Link} to={`${match.url}/${<%= entityInstance %>.<%= primaryKey.name %>}/edit<%_ if (paginationPagination) { _%>?page=${paginationState.activePage}&sort=${paginationState.sort},${paginationState.order}<%_ } _%>`} color="primary" size="sm" data-cy="entityEditButton">
                                <FontAwesomeIcon icon="pencil-alt" /> <span className="d-none d-md-inline"><Translate contentKey="entity.action.edit">Edit</Translate></span>
                            </Button>
                            <Button tag={Link} to={`${match.url}/${<%= entityInstance %>.<%= primaryKey.name %>}/delete<%_ if (paginationPagination) { _%>?page=${paginationState.activePage}&sort=${paginationState.sort},${paginationState.order}<%_ } _%>`} color="danger" size="sm" data-cy="entityDeleteButton">
                                <FontAwesomeIcon icon="trash" /> <span className="d-none d-md-inline"><Translate contentKey="entity.action.delete">Delete</Translate></span>
                            </Button>
                        <%_ } _%>
                    </div>
                </td>
            </tr>
            ))
            }
            </tbody>
        </Table>
        ) : (
        !loading && <div className="alert alert-warning">
            <Translate contentKey="<%= i18nKeyPrefix %>.home.notFound">
                No <%= entityClassPluralHumanized %> found
            </Translate>
        </div>
        )
        }
        <%_ if (paginationInfiniteScroll) { _%>
        </InfiniteScroll>
        <%_ } _%>
    </div>
    <%_ if (!databaseTypeCassandra) { _%>
    <%_ if (paginationPagination) { _%>
        { totalItems ? (<div className={ <%= entityInstance %>List && <%= entityInstance %>List.length > 0 ? '' : 'd-none' }>
            <Row className="justify-content-center">
                <JhiItemCount
                        page={paginationState.activePage}
                        total={totalItems}
                        itemsPerPage={paginationState.itemsPerPage}
                        <% { if (enableTranslation) { %>i18nEnabled<% }} %>
                />
            </Row>
            <Row className="justify-content-center">
                <JhiPagination
                        activePage={paginationState.activePage}
                        onSelect={handlePagination}
                        maxButtons={5}
                        itemsPerPage={paginationState.itemsPerPage}
                        totalItems={totalItems}
                />
            </Row>
        </div>) : '' }
    <% } _%>
    <%_ } _%>
</div>
);
};

const mapStoreToProps = ({ <%= storeName %> }: IRootStore) => ({
  <%= entityInstance %>List: <%= storeName %>.entities,
  loading: <%= storeName %>.loading,
  <%_ if (pagination !== 'no') { _%>
  totalItems: <%= storeName %>.totalItems,
  <%_ } _%>
  <%_ if (pagination === 'infinite-scroll') { _%>
  links: <%= storeName %>.links,
  entity: <%= storeName %>.entity,
  updateSuccess: <%= storeName %>.updateSuccess,
  <%_ } _%>
 <%_ if (searchEngine === 'elasticsearch') { _%>
  searchEntities: <%= storeName %>.searchEntities,
 <%_ } _%>
 getEntities: <%= storeName %>.getEntities,
 <%_ if (pagination === 'infinite-scroll') { _%>
 reset: <%= storeName %>.reset
 <%_ } _%>
});

type StoreProps = ReturnType<typeof mapStoreToProps>;

export default connect(mapStoreToProps)(<%= entityReactName %>);
