Skip to content

Commit

Permalink
feat(admin): add autonomous courses list filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeyffrey committed Jan 10, 2025
1 parent b71a251 commit aa7dd49
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 34 deletions.
109 changes: 75 additions & 34 deletions admin/app/components/autonomous-courses/list/index.gjs
Original file line number Diff line number Diff line change
@@ -1,39 +1,80 @@
import PixInput from '@1024pix/pix-ui/components/pix-input';
import { fn } from '@ember/helper';
import { action } from '@ember/object';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { t } from 'ember-intl';

import ListItem from './item';

<template>
<div class="content-text content-text--small">
<div class="table-admin">
<table>
<caption class="screen-reader-only">{{t "components.autonomous-courses.list.title"}}</caption>
<thead>
<tr>
<th scope="col" class="table__column table__column--id">{{t
"components.autonomous-courses.list.headers.id"
}}</th>
<th scope="col">{{t "components.autonomous-courses.list.headers.name"}}</th>
<th scope="col" class="table__column table__medium">{{t
"components.autonomous-courses.list.headers.createdAt"
}}</th>
<th scope="col" class="table__column table__medium">{{t
"components.autonomous-courses.list.headers.status"
}}</th>
</tr>
</thead>

{{#if @items}}
<tbody>
{{#each @items as |autonomousCourseListItem|}}
<ListItem @item={{autonomousCourseListItem}} />
{{/each}}
</tbody>
{{/if}}
</table>

{{#unless @items}}
<div class="table__empty">{{t "components.autonomous-courses.list.no-result"}}</div>
{{/unless}}
export default class AutonomousCoursesList extends Component {
@tracked items = this.args.items;

@action
triggerFiltering(key, event) {
const normalizeText = (text) =>
text
.toUpperCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.trim();

const valueToSearch = normalizeText(event.target.value);

this.items = this.args.items.filter((item) => {
if (!valueToSearch) return true;
return normalizeText(item[key]).includes(valueToSearch);
});
}

<template>
<div class="content-text content-text--small">
<div class="table-admin">
<table>
<caption class="screen-reader-only">{{t "components.autonomous-courses.list.title"}}</caption>
<thead>
<tr>
<th scope="col" class="table__column table__column--id">{{t
"components.autonomous-courses.list.headers.id"
}}</th>
<th scope="col">{{t "components.autonomous-courses.list.headers.name"}}</th>
<th scope="col" class="table__column table__medium">{{t
"components.autonomous-courses.list.headers.createdAt"
}}</th>
<th scope="col" class="table__column table__medium">{{t
"components.autonomous-courses.list.headers.status"
}}</th>
</tr>
<tr>
<td>
<PixInput id="id" type="text" oninput={{fn this.triggerFiltering "id"}} placeholder="Filtrer par ID" />
</td>
<td>
<PixInput
id="id"
type="text"
oninput={{fn this.triggerFiltering "name"}}
placeholder="Filtrer par nom"
/>
</td>
<td></td>
<td></td>
</tr>
</thead>

{{#if this.items}}
<tbody>
{{#each this.items as |autonomousCourseListItem|}}
<ListItem @item={{autonomousCourseListItem}} />
{{/each}}
</tbody>
{{/if}}
</table>

{{#unless @items}}
<div class="table__empty">{{t "components.autonomous-courses.list.no-result"}}</div>
{{/unless}}
</div>
</div>
</div>
</template>
</template>
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render } from '@1024pix/ember-testing-library';
import { fillIn } from '@ember/test-helpers';
import List from 'pix-admin/components/autonomous-courses/list';
import { module, test } from 'qunit';

Expand Down Expand Up @@ -59,4 +60,56 @@ module('Integration | Component | AutonomousCourses | List', function (hooks) {
// then
assert.dom(screen.getByText('Archivé')).exists();
});

module('filterable columns', function () {
test('it should be possible to filter ID column', async function (assert) {
// given
const autonomousCoursesList = [
{
id: '1002',
name: 'Parcours autonome qui va être filtré',
createdAt: new Date('2023-01-01'),
},
{
id: '999',
name: 'Parcours autonome avec un 9 dedans',
createdAt: new Date('2023-01-01'),
archivedAt: new Date('2023-02-02'),
},
];

// when
const screen = await render(<template><List @items={{autonomousCoursesList}} /></template>);
await fillIn(screen.getByPlaceholderText('Filtrer par ID'), 9);

// then
assert.dom(screen.getByText('Parcours autonome avec un 9 dedans')).exists();
assert.dom(screen.queryByText('Parcours autonome qui va être filtré')).doesNotExist();
});

test('it should be possible to filter name column', async function (assert) {
// given
const autonomousCoursesList = [
{
id: '1002',
name: 'Parcours autonome qui va être filtré',
createdAt: new Date('2023-01-01'),
},
{
id: '999',
name: 'Parcours autonome avec un 9 dedans',
createdAt: new Date('2023-01-01'),
archivedAt: new Date('2023-02-02'),
},
];

// when
const screen = await render(<template><List @items={{autonomousCoursesList}} /></template>);
await fillIn(screen.getByPlaceholderText('Filtrer par nom'), 'qui va être filtré');

// then
assert.dom(screen.getByText('Parcours autonome qui va être filtré')).exists();
assert.dom(screen.queryByText('Parcours autonome avec un 9 dedans')).doesNotExist();
});
});
});

0 comments on commit aa7dd49

Please sign in to comment.