-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate Persons Index to React (#10378)
* Add new Persons Page * new lib code * export strings * remove jquery code * remove XMLHTTPRequest * undo persons.js linting * Fix tests * fix tests v2
- Loading branch information
1 parent
5623850
commit 00dd39e
Showing
7 changed files
with
124 additions
and
93 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,39 +1,5 @@ | ||
<% provide(:title, t('layouts.navigation.persons')) %> | ||
|
||
<div class="container" id="persons-index"> | ||
<h2><%= yield(:title) %></h2> | ||
|
||
<div id="persons-query-fields" class="form-inline bs-table-query-fields"> | ||
<div class="form-group"> | ||
<%= select_tag(:region, region_option_tags(selected_id: params[:region], real_only: true), class: "form-control") %> | ||
</div> | ||
<div id="search-box" class="form-group"> | ||
<div class="input-group"> | ||
<%= content_tag :span, ui_icon("search"), class: "input-group-addon" %> | ||
<%= text_field_tag :search, params[:search], placeholder: t('.name_or_wca_id'), class: "form-control" %> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<%= wca_table table_class: "persons-table", floatThead: false, | ||
data: { toggle: "table", pagination: true, side_pagination: "server", url: persons_path, | ||
query_params: "personsTableAjax.queryParams", mobile_responsive: true, ajax: "personsTableAjax.doAjax" } do %> | ||
<thead> | ||
<tr> | ||
<th class="name" data-field="name"><%=t '.name' %></th> | ||
<th class="wca-id" data-field="wca_id"><%=t 'common.user.wca_id' %></th> | ||
<th class="country" data-field="country"><%=t '.country' %></th> | ||
<th class="competitions-count" data-field="competitions_count"><%=t 'layouts.navigation.competitions' %></th> | ||
<th class="podiums-count" data-field="podiums_count"><%=t '.podiums' %></th> | ||
</tr> | ||
</thead> | ||
<% end %> | ||
<%= react_component("Persons/List") %> | ||
</div> | ||
|
||
<script> | ||
$(function() { | ||
$('.persons-table').bootstrapTable('getOptions').formatNoMatches = function() { | ||
return '<%=j t('.no_persons_found') %>'; | ||
} | ||
}); | ||
</script> |
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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React, { useState } from 'react'; | ||
import { | ||
Header, Input, Pagination, Table, | ||
} from 'semantic-ui-react'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import I18n from '../../../lib/i18n'; | ||
import useDebounce from '../../../lib/hooks/useDebounce'; | ||
import { getPersons } from '../api/getPersons'; | ||
import Loading from '../../Requests/Loading'; | ||
import WCAQueryClientProvider from '../../../lib/providers/WCAQueryClientProvider'; | ||
import { RegionSelector } from '../../CompetitionsOverview/CompetitionsFilters'; | ||
import { personUrl } from '../../../lib/requests/routes.js.erb'; | ||
import { countries } from '../../../lib/wca-data.js.erb'; | ||
|
||
export default function Wrapper() { | ||
return ( | ||
<WCAQueryClientProvider> | ||
<PersonList /> | ||
</WCAQueryClientProvider> | ||
); | ||
} | ||
|
||
function PersonList() { | ||
const [query, setQuery] = useState(''); | ||
const [page, setPage] = useState(1); | ||
const [region, setRegion] = useState('all'); | ||
|
||
const debouncedSearch = useDebounce(query, 600); | ||
|
||
const { data, isLoading } = useQuery({ | ||
queryKey: ['persons', debouncedSearch, region, page], | ||
queryFn: () => getPersons(page, countries.byIso2[region]?.id ?? region, debouncedSearch), | ||
}); | ||
|
||
if (isLoading) { | ||
return <Loading />; | ||
} | ||
|
||
return ( | ||
<> | ||
<Header> | ||
{I18n.t('layouts.navigation.persons')} | ||
</Header> | ||
<RegionSelector region={region} dispatchFilter={({ region: r }) => setRegion(r)} /> | ||
<Input type="text" placeholder={I18n.t('persons.index.name_or_wca_id')} value={query} onChange={(d) => setQuery(d.target.value)} /> | ||
<Table striped> | ||
<Table.Header> | ||
<Table.Row> | ||
<Table.HeaderCell> | ||
{I18n.t('persons.index.name')} | ||
</Table.HeaderCell> | ||
<Table.HeaderCell> | ||
{I18n.t('common.user.wca_id')} | ||
</Table.HeaderCell> | ||
<Table.HeaderCell> | ||
{I18n.t('persons.index.country')} | ||
</Table.HeaderCell> | ||
<Table.HeaderCell> | ||
{I18n.t('layouts.navigation.competitions')} | ||
</Table.HeaderCell> | ||
<Table.HeaderCell> | ||
{I18n.t('persons.index.podiums')} | ||
</Table.HeaderCell> | ||
</Table.Row> | ||
</Table.Header> | ||
<Table.Body> | ||
{data.rows.length === 0 ? ( | ||
<Table.Row> | ||
<Table.Cell>{I18n.t('persons.index.no_persons_found')}</Table.Cell> | ||
</Table.Row> | ||
) : data.rows.map((row) => ( | ||
<Table.Row key={row.wca_id}> | ||
<Table.Cell> | ||
<a href={personUrl(row.wca_id)}>{row.name}</a> | ||
</Table.Cell> | ||
<Table.Cell> | ||
{row.wca_id} | ||
</Table.Cell> | ||
<Table.Cell> | ||
{countries.byIso2[row.country].name} | ||
</Table.Cell> | ||
|
||
<Table.Cell> | ||
{row.competitions_count} | ||
</Table.Cell> | ||
<Table.Cell> | ||
{row.podiums_count} | ||
</Table.Cell> | ||
</Table.Row> | ||
))} | ||
</Table.Body> | ||
</Table> | ||
<Pagination | ||
defaultActivePage={page} | ||
totalPages={Math.ceil(data.total / 10)} | ||
onPageChange={(e, p) => setPage(p.activePage)} | ||
/> | ||
</> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { fetchJsonOrError } from '../../../lib/requests/fetchWithAuthenticityToken'; | ||
import { personsUrl } from '../../../lib/requests/routes.js.erb'; | ||
// eslint-disable-next-line import/prefer-default-export | ||
export async function getPersons(page, region, query) { | ||
const { data } = await fetchJsonOrError( | ||
`${personsUrl}?search=${query}&order=asc&offset=${(page - 1) * 10}&limit=10®ion=${region ?? 'all'}`, | ||
{ | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
}, | ||
); | ||
return data; | ||
} |
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 |
---|---|---|
|
@@ -74,3 +74,4 @@ translations: | |
- "*.attempts.*" | ||
- "*.time_limit.*" | ||
- "*.users.edit.*" | ||
- "*.persons.index.*" |
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