Skip to content

Commit

Permalink
feat(admin): add multiple child organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
P-Jeremy authored Jan 31, 2025
1 parent ff1abca commit 0d382ec
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 34 deletions.
4 changes: 2 additions & 2 deletions admin/app/adapters/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export default class OrganizationAdapter extends ApplicationAdapter {
return this.ajax(url, 'POST', { data: payload });
}

attachChildOrganization({ childOrganizationId, parentOrganizationId }) {
attachChildOrganization({ childOrganizationIds, parentOrganizationId }) {
const url = `${this.host}/${this.namespace}/organizations/${parentOrganizationId}/attach-child-organization`;
return this.ajax(url, 'POST', { data: { childOrganizationId } });
return this.ajax(url, 'POST', { data: { childOrganizationIds } });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import { tracked } from '@glimmer/tracking';
import { t } from 'ember-intl';

export default class OrganizationsChildrenAttachChildFormComponent extends Component {
@tracked childOrganization = '';
@tracked childOrganizationIds = '';

@action
childOrganizationInputValueChanged(event) {
this.childOrganization = event.target.value;
this.childOrganizationIds = event.target.value;
}

@action
submitForm(event) {
event.preventDefault();
this.args.onFormSubmitted(this.childOrganization);
this.childOrganization = '';
this.args.onFormSubmitted(this.childOrganizationIds);
this.childOrganizationIds = '';
}

<template>
Expand All @@ -29,9 +29,9 @@ export default class OrganizationsChildrenAttachChildFormComponent extends Compo
>
<div class="organization__attach-child-form__content">
<PixInput
@id="child-organization"
@id="child-organizations"
@subLabel={{t "components.organizations.children.attach-child-form.input-information"}}
value={{this.childOrganization}}
value={{this.childOrganizationIds}}
{{on "change" this.childOrganizationInputValueChanged}}
>
<:label>{{t "components.organizations.children.attach-child-form.input-label"}}</:label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export default class AuthenticatedOrganizationsGetChildrenController extends Con
@service store;

@action
async handleFormSubmitted(childOrganizationId) {
async handleFormSubmitted(childOrganizationIds) {
const organizationAdapter = this.store.adapterFor('organization');
const parentOrganizationId = this.model.organization.id;

try {
await organizationAdapter.attachChildOrganization({ childOrganizationId, parentOrganizationId });
await organizationAdapter.attachChildOrganization({ childOrganizationIds, parentOrganizationId });
this.pixToast.sendSuccessNotification({
message: this.intl.t('pages.organization-children.notifications.success.attach-child-organization'),
});
Expand Down
4 changes: 2 additions & 2 deletions admin/mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,8 @@ function _configureOrganizationsRoutes(context) {

context.post('/admin/organizations/:organizationId/attach-child-organization', (schema, request) => {
const parentOrganization = schema.organizations.find(request.params.organizationId);
const { childOrganizationId } = JSON.parse(request.requestBody);
const childOrganization = schema.organizations.find(childOrganizationId);
const { childOrganizationIds } = JSON.parse(request.requestBody);
const childOrganization = schema.organizations.find(childOrganizationIds[0]);

childOrganization.update({
parentOrganizationId: parentOrganization.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module('Acceptance | Organizations | Children', function (hooks) {
const screen = await visit(`/organizations/${parentOrganizationId}/children`);

// then
assert.dom(screen.getByRole('form', { name: `Formulaire d'ajout d'une organisation fille` })).exists();
assert.dom(screen.getByRole('form', { name: `Formulaire d'ajout d'organisation(s) fille(s)` })).exists();
});

module('when attaching child organization', function () {
Expand All @@ -75,7 +75,7 @@ module('Acceptance | Organizations | Children', function (hooks) {
const parentOrganization = this.server.create('organization', { id: 1, name: 'Parent Organization Name' });
this.server.create('organization', { id: 2, name: 'Child Organization Name' });
const screen = await visit(`/organizations/${parentOrganization.id}/children`);
await fillByLabel(`Ajouter une organisation fille ID de l'organisation à ajouter`, '2');
await fillByLabel(`Ajouter une ou plusieurs organisations filles ID d'organisation(s) à ajouter`, '2');

// when
await clickByName('Ajouter');
Expand All @@ -102,7 +102,9 @@ module('Acceptance | Organizations | Children', function (hooks) {
const screen = await visit(`/organizations/${parentOrganizationId}/children`);

// then
assert.dom(screen.queryByRole('form', { name: `Formulaire d'ajout d'une organisation fille` })).doesNotExist();
assert
.dom(screen.queryByRole('form', { name: `Formulaire d'ajout d'organisation(s) fille(s)` }))
.doesNotExist();
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ module('Integration | Component | organizations/children/attach-child-form', fu
const screen = await renderScreen(<template><AttachChildForm /></template>);

// then
assert.dom(screen.getByRole('form', { name: `Formulaire d'ajout d'une organisation fille` })).exists();
assert.dom(screen.getByRole('form', { name: `Formulaire d'ajout d'organisation(s) fille(s)` })).exists();
assert
.dom(screen.getByRole('textbox', { name: `Ajouter une organisation fille ID de l'organisation à ajouter` }))
.dom(
screen.getByRole('textbox', {
name: `Ajouter une ou plusieurs organisations filles ID d'organisation(s) à ajouter`,
}),
)
.exists();
assert.dom(screen.getByRole('button', { name: 'Ajouter' })).exists();
});
Expand All @@ -25,15 +29,19 @@ module('Integration | Component | organizations/children/attach-child-form', fu
// given
const onFormSubmitted = sinon.stub();
const screen = await renderScreen(<template><AttachChildForm @onFormSubmitted={{onFormSubmitted}} /></template>);
await fillByLabel(`Ajouter une organisation fille ID de l'organisation à ajouter`, '12345');
await fillByLabel(`Ajouter une ou plusieurs organisations filles ID d'organisation(s) à ajouter`, '12345');

// when
await clickByName('Ajouter');

// then
assert.true(onFormSubmitted.calledOnceWithExactly('12345'));
assert
.dom(screen.getByRole('textbox', { name: `Ajouter une organisation fille ID de l'organisation à ajouter` }))
.dom(
screen.getByRole('textbox', {
name: `Ajouter une ou plusieurs organisations filles ID d'organisation(s) à ajouter`,
}),
)
.hasValue('');
});
});
Expand Down
6 changes: 3 additions & 3 deletions admin/tests/unit/adapters/organization-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ module('Unit | Adapters | organization', function (hooks) {
module('#attachChildOrganization', function () {
test('sends an HTTP POST request', async function (assert) {
// given
const childOrganizationId = 123;
const childOrganizationIds = '123,456';
const parentOrganizationId = 2;

// when
await adapter.attachChildOrganization({ childOrganizationId, parentOrganizationId });
await adapter.attachChildOrganization({ childOrganizationIds, parentOrganizationId });

// then
assert.true(
adapter.ajax.calledOnceWithExactly(
'http://localhost:3000/api/admin/organizations/2/attach-child-organization',
'POST',
{ data: { childOrganizationId } },
{ data: { childOrganizationIds } },
),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ module('Unit | Component | organizations/children/attach-child-form', function (
});

module('when form input value changes', function () {
test('updates "childOrganization" component attribute', function (assert) {
test('updates "childOrganizationIds" component attribute', function (assert) {
// given
const event = { target: { value: '5432' } };
const event = { target: { value: '5432,789' } };

// when
component.childOrganizationInputValueChanged(event);

// then
assert.strictEqual(component.childOrganization, '5432');
assert.strictEqual(component.childOrganizationIds, '5432,789');
});
});

Expand All @@ -32,7 +32,7 @@ module('Unit | Component | organizations/children/attach-child-form', function (
const onFormSubmitted = sinon.stub();
const event = { preventDefault: sinon.stub() };

component.childOrganization = '1234';
component.childOrganizationIds = '1234';
component.args.onFormSubmitted = onFormSubmitted;

// when
Expand All @@ -41,7 +41,7 @@ module('Unit | Component | organizations/children/attach-child-form', function (
// then
assert.true(event.preventDefault.calledOnce);
assert.true(onFormSubmitted.calledOnceWithExactly('1234'));
assert.strictEqual(component.childOrganization, '');
assert.strictEqual(component.childOrganizationIds, '');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module('Unit | Controller | authenticated/organizations/get/children', function
assert.true(store.adapterFor.calledWithExactly('organization'));
assert.true(
organizationAdapter.attachChildOrganization.calledWithExactly({
childOrganizationId: '1234',
childOrganizationIds: '1234',
parentOrganizationId: '12',
}),
);
Expand Down
6 changes: 3 additions & 3 deletions admin/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@
},
"children": {
"attach-child-form": {
"input-information": "ID of organisation to add",
"input-label": "Add a child organisation",
"name": "Add a child organisation form"
"input-information": "ID of organisation(s) to add",
"input-label": "Add one or more child organisations",
"name": "Add child organisation(s) form"
}
},
"children-list": {
Expand Down
6 changes: 3 additions & 3 deletions admin/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,9 @@
},
"children": {
"attach-child-form": {
"input-information": "ID de l'organisation à ajouter",
"input-label": "Ajouter une organisation fille",
"name": "Formulaire d'ajout d'une organisation fille"
"input-information": "ID d'organisation(s) à ajouter",
"input-label": "Ajouter une ou plusieurs organisations filles",
"name": "Formulaire d'ajout d'organisation(s) fille(s)"
}
},
"children-list": {
Expand Down

0 comments on commit 0d382ec

Please sign in to comment.