Skip to content

Commit

Permalink
added search and pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
jakopako committed Mar 27, 2022
1 parent 4ddae13 commit 016ffb3
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 63 deletions.
96 changes: 96 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
"@types/node": "^16.11.26",
"@types/react": "^17.0.43",
"@types/react-dom": "^17.0.14",
"date-fns": "^2.28.0",
"fecha": "^4.2.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-paginate": "^8.1.2",
"react-router-dom": "^6.2.2",
"react-scripts": "5.0.0",
"typescript": "^4.6.3",
"web-vitals": "^2.1.4"
Expand Down
142 changes: 107 additions & 35 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,113 @@
import { Component } from 'react';
import './App.css';
import ConcertList from './components/ConcertList';
import SearchField from './components/SearchField';
import { Concert } from './model';
import { Route, Routes } from "react-router-dom";

const App: React.FC = () => {
const concerts = [
{
title: "Jugendblasorchester Zürich (JBOZ)",
location: 'Tonhalle',
city: 'Zurich',
comment: 'Jugendblasorchester Zürich JBOZ',
url: 'https://www.tonhalle-orchester.ch/en/concerts/kalender/jugendblasorchester-zrich-jboz-1439709/tz/',
date: new Date()
},
{
title: "MEDHANE",
location: 'Botanique',
city: 'Brussels',
comment: 'Rotonde',
url: 'https://botanique.be/en/concert/medhane-2022',
date: new Date()
},
{
title: "Yevgueni",
location: 'HetDepot',
city: 'Leuven',
comment: 'Met een nieuw album!',
url: 'https://www.hetdepot.be/concert/yevgueni-0',
date: new Date()
}
]
return (
<div className="App">
<span className='heading'>CrONCERT</span>
<SearchField></SearchField>
<ConcertList concerts={concerts}></ConcertList>
</div>
);
type State = {
baseUrl: string;
page: number;
totalPages: number;
concerts: Array<Concert>;
titleSearchTerm: string;
}

export default App;
type Props = {
history: History;
location: Location;
};

class App extends Component {
// const concerts = [
// {
// title: "Jugendblasorchester Zürich (JBOZ)",
// location: 'Tonhalle',
// city: 'Zurich',
// comment: 'Jugendblasorchester Zürich JBOZ',
// url: 'https://www.tonhalle-orchester.ch/en/concerts/kalender/jugendblasorchester-zrich-jboz-1439709/tz/',
// date: new Date()
// },
// {
// title: "MEDHANE",
// location: 'Botanique',
// city: 'Brussels',
// comment: 'Rotonde',
// url: 'https://botanique.be/en/concert/medhane-2022',
// date: new Date()
// },
// {
// title: "Yevgueni",
// location: 'HetDepot',
// city: 'Leuven',
// comment: 'Met een nieuw album!',
// url: 'https://www.hetdepot.be/concert/yevgueni-0',
// date: new Date()
// }
// ]

state: State = {
baseUrl: 'https://event-api-6bbi2ttrza-oa.a.run.app/api/events',
totalPages: 0,
page: 1,
concerts: [],
titleSearchTerm: ""
}

async getConcerts() {
const res = await fetch(this.state.baseUrl + '?page=' + this.state.page+'&title='+this.state.titleSearchTerm);
const res_json = await res.json();
this.setState({
totalPages: res_json['last_page'],
page: res_json['page'],
concerts: res_json['data']
})

}

async componentDidMount() {
this.getConcerts();
}

handlePageClick = (event: { selected: number; }) => {
this.setState({ page: event.selected + 1}, () => {
this.getConcerts();
});
};

handleSearch = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
this.setState({ titleSearchTerm: event.currentTarget.titlesearch.value, page: 1}, () => {
this.getConcerts();
})
}

render() {
return (
<div className="App">
<span className='heading'>CrONCERT</span>
<SearchField handleFormSubmit={this.handleSearch}></SearchField>
<ConcertList
concerts={this.state.concerts}
page={this.state.page}
totalPages={this.state.totalPages}
handlePagination={this.handlePageClick}></ConcertList>
</div>
);

}
}

// class App extends Component {
// render() {
// return (
// <div className="App">
// <Routes>
// <Route path="/" element={SearchPage} />
// </Routes>
// </div>
// );
// }
// }

export default App;
5 changes: 3 additions & 2 deletions src/components/ConcertItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { Concert } from '../model'
import { format } from 'fecha'
import { format, parseISO } from "date-fns";

const ConcertItem = ({title, location, city, comment, url, date}: Concert) => {
return (
Expand All @@ -11,7 +11,8 @@ const ConcertItem = ({title, location, city, comment, url, date}: Concert) => {
<div className='concertitem__field'><div className='concertitem__location_city'>{location}, {city}</div></div>
{/* <div className='concertitem__field'><div className='concertitem__city'>{city}</div></div> */}
{/* <div className='concertitem__field'><div className='concertitem__comment'>{comment}</div></div> */}
<div className='concertitem__field'><div className='concertitem__date'>{format(date, 'DD.MM.YYYY hh:mm')}</div></div>
{/* <div className='concertitem__field'><div className='concertitem__date'>{format(date, 'DD.MM.YYYY hh:mm')}</div></div> */}
<div className='concertitem__field'><div className='concertitem__date'>{format(parseISO(date), "dd.MM.yyyy HH:mm")}</div></div>
{/* <div className='concertitem__field'>{city}</div>
<div className='concertitem__field'> {comment}</div>
<div className='concertitem__field'>{format(date, 'DD.MM.YYYY hh:mm')}</div> */}
Expand Down
40 changes: 26 additions & 14 deletions src/components/ConcertList.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import React from 'react'
import { Concert } from '../model'
import ConcertItem from './ConcertItem'
import ReactPaginate from 'react-paginate';
import { NoConcerts } from './NoConcerts';

interface IConcerts {
concerts: Concert[]
interface Props {
concerts: Concert[];
page: number;
totalPages: number;
handlePagination: (selectedItem: { selected: number; }) => void;
}

const ConcertList = ({concerts}: IConcerts) => {
// const c1:Concert = {
// title: "title1",
// location: '',
// city: '',
// comment: '',
// date: new Date()
// }
const ConcertList = ({ concerts, page, totalPages, handlePagination }: Props) => {
return (
<div className='concertlist__box'>
{concerts.map((concert) => (
<ConcertItem title={concert.title} location={concert.location} city={concert.city} comment={concert.comment} url={concert.url} date={concert.date}></ConcertItem>
))}
{/* <ConcertItem title={c1.title} location={''} city={''} comment={''} date={new Date()} /> */}
<div>
{concerts && concerts.map((concert) => (
<ConcertItem key={concert.title + concert.date} title={concert.title} location={concert.location} city={concert.city} comment={concert.comment} url={concert.url} date={concert.date}></ConcertItem>
))}
{!concerts && (<NoConcerts/>)}
</div>
<div className='pagination'>
{concerts && (
<ReactPaginate
breakLabel="..."
nextLabel=" >"
onPageChange={handlePagination}
pageRangeDisplayed={3}
pageCount={totalPages}
previousLabel="< "
// renderOnZeroPageCount={null}
/>)}
</div>
</div>
)
}
Expand Down
Loading

0 comments on commit 016ffb3

Please sign in to comment.