Skip to content

Commit 7f91dd8

Browse files
quinarygiofreddidierRTE
authored andcommitted
Add a method to tell the usercard template the entity used to send card (#2675)
Signed-off-by: Giovanni Ferrari <[email protected]>
1 parent 0c15ec6 commit 7f91dd8

File tree

7 files changed

+117
-3
lines changed

7 files changed

+117
-3
lines changed

src/docs/asciidoc/reference_doc/user_cards.adoc

+8
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ https://github.com/opfab/operatorfabric-core/tree/master/src/test/resources/bund
207207
== Get current process and current state of the card
208208
The template can know the process and the state of the card by calling the _usercardTemplateGateway.getCurrentProcess()_ and _usercardTemplateGateway.getCurrentState()_ functions. These functions will return a string corresponding to the process id (or state id).
209209

210+
== Receiving emitter entity of the card
211+
The template can receive the emitter entity of the card by implementing the _usercardTemplateGateway.setEntityUsedForSendingCard()_ function.
212+
This function will be called by OperatorFabric after loading the template and every time the card emitter changes (if the user can choose from multiple entities).
213+
214+
An example of _usercardTemplateGateway.setEntityUsedForSendingCard()_ usage can be found in the file
215+
https://github.com/opfab/operatorfabric-core/tree/master/src/test/resources/bundles/defaultProcess_V1/template/usercard_message.handlebars[src/test/resources/bundles/defaultProcess_V1/template/usercard_message.handlebars].
216+
217+
210218
== Send response automatically (experimental feature)
211219

212220
It is possible to configure a template to automatically send a response when sending a user card expecting an answers from one of the entities of the emitting user.

src/test/cypress/cypress/integration/UserCard.spec.js

+55
Original file line numberDiff line numberDiff line change
@@ -810,4 +810,59 @@ describe('User Card ', function () {
810810

811811
})
812812

813+
describe("Should receive card emitter from gateway", function() {
814+
815+
it('Check card emitter for operator1_fr', () => {
816+
817+
cy.loginOpFab('operator1_fr', 'test');
818+
cy.get('#opfab-navbarContent').find('#opfab-newcard-menu').click();
819+
820+
821+
// Test on Message card
822+
cy.get("#hidden_sender").should("exist");
823+
cy.get("#hidden_sender").should("have.value", "ENTITY1_FR");
824+
825+
826+
827+
});
828+
829+
it('Should receive card emitter changes for operator4_fr', () => {
830+
831+
cy.loginOpFab('operator4_fr', 'test');
832+
cy.get('#opfab-navbarContent').find('#opfab-newcard-menu').click();
833+
834+
// Test on Message card
835+
836+
// Check that card emitter is set to Control Center FR East
837+
cy.get('#of-usercard-card-emitter-selector').should("exist");
838+
cy.get('#of-usercard-card-emitter-selector').find('option:selected').should('have.text', 'Control Center FR East');
839+
840+
841+
cy.get("#hidden_sender").should("exist");
842+
cy.get("#hidden_sender").should("have.value", "ENTITY3_FR");
843+
844+
// Now we choose Control Center FR North as card emitter
845+
cy.get("#of-usercard-card-emitter-selector").find('select').select('Control Center FR South');
846+
cy.get("#hidden_sender").should("have.value", "ENTITY2_FR");
847+
848+
// Test on editing existing card, opened from the feed
849+
prepareAndSendCard();
850+
851+
cy.waitDefaultTime();
852+
853+
cy.get('of-light-card').eq(0).click()
854+
.find('[id^=opfab-feed-light-card]')
855+
.invoke('attr', 'data-urlId')
856+
.then((urlId) => {
857+
cy.waitDefaultTime();
858+
cy.hash().should('eq', '#/feed/cards/' + urlId);
859+
cy.get('#opfab-card-edit').click();
860+
861+
cy.get("#hidden_sender").should("exist");
862+
cy.get("#hidden_sender").should("have.value", "ENTITY2_FR");
863+
})
864+
865+
});
866+
})
867+
813868
})

src/test/resources/bundles/defaultProcess_V1/template/usercard_message.handlebars

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
<label>HIDDEN STATE MOCK </label>
2525
<input id="hidden_state" name="hidden_state">
2626
</div>
27+
<div hidden="true" class="opfab-input" style="margin-right:5px">
28+
<label>HIDDEN SENDER MOCK </label>
29+
<input id="hidden_sender" name="hidden_sender">
30+
</div>
2731
</tr>
2832

2933

@@ -52,5 +56,10 @@
5256
card: card
5357
};
5458
55-
}
59+
};
60+
61+
usercardTemplateGateway.setEntityUsedForSendingCard = function(senderEntity) {
62+
document.getElementById("hidden_sender").value = senderEntity;
63+
};
64+
5665
</script>

ui/main/src/app/modules/usercard/selectCardEmitterForm/usercard-select-card-emitter-form.component.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
* This file is part of the OperatorFabric project.
88
*/
99

10-
import {Component, Input, OnInit} from '@angular/core';
10+
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
1111
import {FormControl, FormGroup} from '@angular/forms';
12+
import {takeUntil, debounceTime, Subject} from 'rxjs';
1213

1314

1415
@Component({
@@ -20,6 +21,11 @@ export class UsercardSelectCardEmitterFormComponent implements OnInit {
2021
@Input() public userEntitiesAllowedToSendCardOptions;
2122
@Input() public initialPublisher;
2223

24+
@Output() public cardEmitterChange: EventEmitter<any> = new EventEmitter<any>();
25+
26+
27+
unsubscribe$: Subject<void> = new Subject<void>();
28+
2329
selectCardEmitterForm: FormGroup;
2430

2531
constructor() {
@@ -30,10 +36,35 @@ export class UsercardSelectCardEmitterFormComponent implements OnInit {
3036
this.selectCardEmitterForm = new FormGroup({
3137
cardEmitter: new FormControl('')
3238
});
39+
3340
this.selectCardEmitterForm.get('cardEmitter').setValue(this.initialPublisher);
41+
this.listenForEmitterChange();
3442
}
3543

3644
public getSelectedCardEmitter() {
3745
return this.selectCardEmitterForm.value['cardEmitter'];
3846
}
47+
48+
private listenForEmitterChange() {
49+
this.selectCardEmitterForm.get('cardEmitter').valueChanges
50+
.pipe(
51+
takeUntil(this.unsubscribe$),
52+
debounceTime(10)
53+
)
54+
.subscribe((state) => {
55+
if (!!state) {
56+
this.cardEmitterChange.emit(
57+
{
58+
'emitter': this.selectCardEmitterForm.get('cardEmitter').value
59+
});
60+
61+
}
62+
});
63+
}
64+
65+
66+
ngOnDestroy() {
67+
this.unsubscribe$.next();
68+
this.unsubscribe$.complete();
69+
}
3970
}

ui/main/src/app/modules/usercard/usercard.component.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050

5151
<div *ngIf="userEntitiesAllowedToSendCardOptions.length > 1">
5252
<of-usercard-select-card-emitter-form [userEntitiesAllowedToSendCardOptions]="userEntitiesAllowedToSendCardOptions"
53-
[initialPublisher]="this.publisherForCreatingUsercard" #cardEmitterForm>
53+
[initialPublisher]="this.publisherForCreatingUsercard" #cardEmitterForm
54+
(cardEmitterChange)="cardEmitterChanged($event)">
5455
</of-usercard-select-card-emitter-form>
5556
</div>
5657

ui/main/src/app/modules/usercard/usercard.component.ts

+4
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ export class UserCardComponent implements OnInit {
233233
this.loadTemplate();
234234
}
235235

236+
public cardEmitterChanged(event: any) {
237+
usercardTemplateGateway.setEntityUsedForSendingCard(event.emitter);
238+
}
236239

237240
private setFieldsVisibility() {
238241
if (!!this.userCardConfiguration) {
@@ -267,6 +270,7 @@ export class UserCardComponent implements OnInit {
267270
setTimeout(() => { // wait for DOM rendering
268271
this.reinsertScripts();
269272
this.setInitialDateFormValues();
273+
usercardTemplateGateway.setEntityUsedForSendingCard(this.findPublisherForCreatingUsercard())
270274
}, 10);
271275
},
272276
error: (error) => {

ui/main/src/assets/js/usercardTemplateGateway.js

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ const usercardTemplateGateway = {
7070
this.startDate = null;
7171
this.endDate = null;
7272
this.lttd = null;
73+
74+
// OpFab calls this function to inform the template on sender entity
75+
this.setEntityUsedForSendingCard = function (senderEntity) {
76+
// This function should be overridden in the template.
77+
};
78+
7379
}
7480
};
7581

0 commit comments

Comments
 (0)