Skip to content

Commit

Permalink
Merge pull request #7935 from michaelchadwick/frontend-3457-fix-back-…
Browse files Browse the repository at this point in the history
…to-programs-link

add query param for chosen school on programs route
  • Loading branch information
dartajax authored Jul 8, 2024
2 parents 2815bde + e657786 commit bac6e27
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/frontend/app/components/programs/root.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<FaIcon @icon="building-columns" @fixedWidth={{true}} />
{{#if (gt @schools.length 1)}}
<select
{{on "change" (pick "target.value" (set this.selectedSchoolId))}}
{{on "change" (pick "target.value" @setSchoolId)}}
aria-label={{t "general.schools"}}
data-test-school-selector
>
Expand Down
7 changes: 3 additions & 4 deletions packages/frontend/app/components/programs/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { dropTask } from 'ember-concurrency';
export default class ProgramRootComponent extends Component {
@service currentUser;
@service permissionChecker;
@tracked selectedSchoolId;
@tracked showNewProgramForm = false;
@tracked newProgram;

Expand Down Expand Up @@ -41,12 +40,12 @@ export default class ProgramRootComponent extends Component {
}

get bestSelectedSchool() {
if (this.selectedSchoolId) {
return findById(this.args.schools.slice(), this.selectedSchoolId);
if (this.args.schoolId) {
return findById(this.args.schools, this.args.schoolId);
}

const schoolId = this.user?.belongsTo('school').id();
return schoolId ? findById(this.args.schools.slice(), schoolId) : this.args.schools.slice()[0];
return schoolId ? findById(this.args.schools, schoolId) : this.args.schools[0];
}

saveNewProgram = dropTask(async (newProgram) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/frontend/app/controllers/programs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Controller from '@ember/controller';
import { tracked } from '@glimmer/tracking';

export default class ProgramsController extends Controller {
queryParams = [{ schoolId: 'school' }];
@tracked schoolId;
}
6 changes: 5 additions & 1 deletion packages/frontend/app/templates/programs.hbs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
{{page-title (t "general.programs")}}
<Programs::Root @schools={{this.model}} />
<Programs::Root
@setSchoolId={{set this.schoolId}}
@schoolId={{this.schoolId}}
@schools={{this.model}}
/>
30 changes: 29 additions & 1 deletion packages/frontend/tests/acceptance/programs-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { click, fillIn, find, currentURL, currentRouteName, visit } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupAuthentication } from 'ilios-common';

import { setupApplicationTest } from 'frontend/tests/helpers';
import page from 'frontend/tests/pages/programs';
import percySnapshot from '@percy/ember';
Expand Down Expand Up @@ -88,6 +87,35 @@ module('Acceptance | Programs', function (hooks) {
});
});

module('User in multiple schools', function (hooks) {
hooks.beforeEach(async function () {
this.school1 = this.server.create('school');
this.school2 = this.server.create('school');
this.program1 = this.server.create('program', { school: this.school1 });
this.program2 = this.server.create('program', { school: this.school2 });
this.user = await setupAuthentication();
});

test('remember non-default school filter choice', async function (assert) {
await page.visit();

assert.strictEqual(currentURL(), '/programs');
assert.strictEqual(page.root.schoolFilter.schools.length, 2);

assert.strictEqual(page.root.schoolFilter.selectedSchool, '1');

await page.root.schoolFilter.select(2);
assert.strictEqual(page.root.schoolFilter.selectedSchool, '2');
assert.strictEqual(currentURL(), '/programs?school=2');

await click('.list tbody tr:nth-of-type(1) td:nth-of-type(1) a');
assert.strictEqual(currentURL(), '/programs/2');
await click('.backtolink a');
assert.strictEqual(page.root.schoolFilter.selectedSchool, '2');
assert.strictEqual(currentURL(), '/programs?school=2');
});
});

test('filters options', async function (assert) {
const schools = this.server.createList('school', 2);
await setupAuthentication({ school: schools[1] });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,18 @@ module('Integration | Component | programs/root', function (hooks) {
});

test('school filter works', async function (assert) {
assert.expect(10);
setupPermissionChecker(this, true);
this.set('schools', this.schools);
await render(hbs`<Programs::Root @schools={{this.schools}} />`);
this.set('setSchoolId', (schoolId) => {
assert.strictEqual(schoolId, '3');
this.set('schoolId', schoolId);
});
await render(hbs`<Programs::Root
@schools={{this.schools}}
@setSchoolId={{this.setSchoolId}}
@schoolId={{this.schoolId}}
/>`);
assert.strictEqual(component.schoolFilter.selectedSchool, '2');
assert.strictEqual(component.list.items.length, 3);
assert.strictEqual(component.list.items[0].title, 'program 3');
Expand Down

0 comments on commit bac6e27

Please sign in to comment.