forked from wix-incubator/fed-entry-level-exam
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sort functionality to server and client
- Loading branch information
Showing
6 changed files
with
178 additions
and
69 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,104 @@ | ||
import React from 'react'; | ||
import './App.scss'; | ||
import {createApiClient, Ticket} from './api'; | ||
import {createApiClient, Ticket, Sort, SortDirection, SortCriteria} from './api'; | ||
|
||
export type AppState = { | ||
tickets?: Ticket[], | ||
search: string; | ||
tickets?: Ticket[], | ||
search: string; | ||
sort?: Sort | ||
} | ||
|
||
const api = createApiClient(); | ||
|
||
export class App extends React.PureComponent<{}, AppState> { | ||
|
||
state: AppState = { | ||
search: '' | ||
} | ||
|
||
searchDebounce: any = null; | ||
|
||
async componentDidMount() { | ||
this.setState({ | ||
tickets: await api.getTickets() | ||
}); | ||
} | ||
|
||
renderTickets = (tickets: Ticket[]) => { | ||
|
||
const filteredTickets = tickets | ||
.filter((t) => (t.title.toLowerCase() + t.content.toLowerCase()).includes(this.state.search.toLowerCase())); | ||
|
||
|
||
return (<ul className='tickets'> | ||
{filteredTickets.map((ticket) => (<li key={ticket.id} className='ticket'> | ||
<h5 className='title'>{ticket.title}</h5> | ||
<footer> | ||
<div className='meta-data'>By {ticket.userEmail} | { new Date(ticket.creationTime).toLocaleString()}</div> | ||
</footer> | ||
</li>))} | ||
</ul>); | ||
} | ||
|
||
onSearch = async (val: string, newPage?: number) => { | ||
|
||
clearTimeout(this.searchDebounce); | ||
|
||
this.searchDebounce = setTimeout(async () => { | ||
this.setState({ | ||
search: val | ||
}); | ||
}, 300); | ||
} | ||
|
||
render() { | ||
const {tickets} = this.state; | ||
|
||
return (<main> | ||
<h1>Tickets List</h1> | ||
<header> | ||
<input type="search" placeholder="Search..." onChange={(e) => this.onSearch(e.target.value)}/> | ||
</header> | ||
{tickets ? <div className='results'>Showing {tickets.length} results</div> : null } | ||
{tickets ? this.renderTickets(tickets) : <h2>Loading..</h2>} | ||
</main>) | ||
} | ||
state: AppState = { | ||
search: '' | ||
} | ||
|
||
searchDebounce: any = null; | ||
|
||
async componentDidMount() { | ||
this.setState({ | ||
tickets: await api.getTickets() | ||
}); | ||
} | ||
|
||
renderSort = () => { | ||
const {sort} = this.state; | ||
const sortBy = sort && sort.by; | ||
const direction = (sort && sort.direction) || ''; | ||
return ( | ||
<div className='sort'> | ||
<label>Sort By: </label> | ||
{this.renderSortButton('Title', 'title', sortBy)} | ||
{this.renderSortButton('Date', 'date', sortBy)} | ||
{this.renderSortButton('Email', 'email', sortBy)} | ||
<label id='sort-direction'>{ direction ? (direction === 'ASC' ? ' Ascending' : ' Descending') : '' }</label> | ||
</div> | ||
); | ||
} | ||
|
||
renderSortButton = (text: string, criteria: SortCriteria, currentSortBy: SortCriteria | undefined) => { | ||
return <button className={criteria === currentSortBy ? 'selected' : ''} id={`sort-${criteria}`} | ||
onClick={() => {this.getSortedItems({by: criteria})} }>{text}</button>; | ||
} | ||
|
||
getSortedItems = async (sortData: Sort) => { | ||
const {sort} = this.state; | ||
if ((sort && sort.by) === sortData.by) { | ||
sortData.direction = (sort && sort.direction) === 'ASC' ? 'DESC' : 'ASC'; | ||
} else { | ||
sortData.direction = 'ASC'; | ||
} | ||
const tickets = await api.getTickets(sortData); | ||
this.setState({ | ||
tickets, | ||
sort:sortData | ||
}); | ||
} | ||
|
||
renderTickets = (tickets: Ticket[]) => { | ||
|
||
const filteredTickets = tickets | ||
.filter((t) => (t.title.toLowerCase() + t.content.toLowerCase()).includes(this.state.search.toLowerCase())); | ||
|
||
|
||
return (<ul className='tickets'> | ||
{filteredTickets.map((ticket) => (<li key={ticket.id} className='ticket'> | ||
<h5 className='title'>{ticket.title}</h5> | ||
<footer> | ||
<div | ||
className='meta-data'>By {ticket.userEmail} | {new Date(ticket.creationTime).toLocaleString()}</div> | ||
</footer> | ||
</li>))} | ||
</ul>); | ||
} | ||
|
||
onSearch = async (val: string, newPage?: number) => { | ||
|
||
clearTimeout(this.searchDebounce); | ||
|
||
this.searchDebounce = setTimeout(async () => { | ||
this.setState({ | ||
search: val | ||
}); | ||
}, 300); | ||
} | ||
|
||
render() { | ||
const {tickets} = this.state; | ||
|
||
return (<main> | ||
<h1>Tickets List</h1> | ||
<header> | ||
<input type="search" placeholder="Search..." onChange={(e) => this.onSearch(e.target.value)}/> | ||
</header> | ||
{this.renderSort()} | ||
{tickets ? <div className='results'>Showing {tickets.length} results</div> : null} | ||
{tickets ? this.renderTickets(tickets) : <h2>Loading..</h2>} | ||
</main>) | ||
} | ||
} | ||
|
||
export default App; | ||
export default App; |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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