Created
September 4, 2023 21:52
-
-
Save 1njected/de66d4784393b230ff503594c54812ac to your computer and use it in GitHub Desktop.
customers.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
import { Link, useLocation, useNavigate } from 'react-router-dom'; | |
import { Button, Table } from 'reactstrap'; | |
import { Translate, getSortState } from 'react-jhipster'; | |
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | |
import { faSort, faSortUp, faSortDown } from '@fortawesome/free-solid-svg-icons'; | |
import { ASC, DESC, SORT } from 'app/shared/util/pagination.constants'; | |
import { overrideSortStateWithQueryParams } from 'app/shared/util/entity-utils'; | |
import { useAppDispatch, useAppSelector } from 'app/config/store'; | |
import { getEntities } from './customers.reducer'; | |
export const Customers = () => { | |
const dispatch = useAppDispatch(); | |
const location = useLocation(); | |
const navigate = useNavigate(); | |
const [sortState, setSortState] = useState(overrideSortStateWithQueryParams(getSortState(location, 'id'), location.search)); | |
const customersList = useAppSelector(state => state.jvulnapp.customers.entities); | |
const loading = useAppSelector(state => state.jvulnapp.customers.loading); | |
const getAllEntities = () => { | |
dispatch( | |
getEntities({ | |
sort: `${sortState.sort},${sortState.order}`, | |
}) | |
); | |
}; | |
const sortEntities = () => { | |
getAllEntities(); | |
const endURL = `?sort=${sortState.sort},${sortState.order}`; | |
if (location.search !== endURL) { | |
navigate(`${location.pathname}${endURL}`); | |
} | |
}; | |
useEffect(() => { | |
sortEntities(); | |
}, [sortState.order, sortState.sort]); | |
const sort = p => () => { | |
setSortState({ | |
...sortState, | |
order: sortState.order === ASC ? DESC : ASC, | |
sort: p, | |
}); | |
}; | |
const handleSyncList = () => { | |
sortEntities(); | |
}; | |
const getSortIconByFieldName = (fieldName: string) => { | |
const sortFieldName = sortState.sort; | |
const order = sortState.order; | |
if (sortFieldName !== fieldName) { | |
return faSort; | |
} else { | |
return order === ASC ? faSortUp : faSortDown; | |
} | |
}; | |
return ( | |
<div> | |
<h2 id="customers-heading" data-cy="CustomersHeading"> | |
Customers | |
<div className="d-flex justify-content-end"> | |
<Button className="me-2" color="info" onClick={handleSyncList} disabled={loading}> | |
<FontAwesomeIcon icon="sync" spin={loading} /> Refresh list | |
</Button> | |
<Link to="/customers/new" className="btn btn-primary jh-create-entity" id="jh-create-entity" data-cy="entityCreateButton"> | |
<FontAwesomeIcon icon="plus" /> | |
Create a new Customers | |
</Link> | |
</div> | |
</h2> | |
<div className="table-responsive"> | |
{customersList && customersList.length > 0 ? ( | |
<Table responsive> | |
<thead> | |
<tr> | |
<th className="hand" onClick={sort('id')}> | |
ID <FontAwesomeIcon icon={getSortIconByFieldName('id')} /> | |
</th> | |
<th className="hand" onClick={sort('firstname')}> | |
Firstname <FontAwesomeIcon icon={getSortIconByFieldName('firstname')} /> | |
</th> | |
<th className="hand" onClick={sort('lastname')}> | |
Lastname <FontAwesomeIcon icon={getSortIconByFieldName('lastname')} /> | |
</th> | |
<th className="hand" onClick={sort('email')}> | |
Email <FontAwesomeIcon icon={getSortIconByFieldName('email')} /> | |
</th> | |
<th /> | |
</tr> | |
</thead> | |
<tbody> | |
{customersList.map((customers, i) => ( | |
<tr key={`entity-${i}`} data-cy="entityTable"> | |
<td> | |
<Button tag={Link} to={`/customers/${customers.id}`} color="link" size="sm"> | |
{customers.id} | |
</Button> | |
</td> | |
<td><span dangerouslySetInnerHTML={{__html: customers.firstname}} /></td> | |
<td>{customers.lastname}</td> | |
<td>{customers.email}</td> | |
<td className="text-end"> | |
<div className="btn-group flex-btn-group-container"> | |
<Button tag={Link} to={`/customers/${customers.id}`} color="info" size="sm" data-cy="entityDetailsButton"> | |
<FontAwesomeIcon icon="eye" /> <span className="d-none d-md-inline">View</span> | |
</Button> | |
<Button tag={Link} to={`/customers/${customers.id}/edit`} color="primary" size="sm" data-cy="entityEditButton"> | |
<FontAwesomeIcon icon="pencil-alt" /> <span className="d-none d-md-inline">Edit</span> | |
</Button> | |
<Button tag={Link} to={`/customers/${customers.id}/delete`} color="danger" size="sm" data-cy="entityDeleteButton"> | |
<FontAwesomeIcon icon="trash" /> <span className="d-none d-md-inline">Delete</span> | |
</Button> | |
</div> | |
</td> | |
</tr> | |
))} | |
</tbody> | |
</Table> | |
) : ( | |
!loading && <div className="alert alert-warning">No Customers found</div> | |
)} | |
</div> | |
</div> | |
); | |
}; | |
export default Customers; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment