Skip to content

Commit 09a6d0f

Browse files
authored
Display toasts in case of project creation (#1020)
Displays a success toast for successful project creation, or error toasts for failing project or experiment tasks.
1 parent 71e17d0 commit 09a6d0f

File tree

2 files changed

+66
-20
lines changed

2 files changed

+66
-20
lines changed

user-interface/src/main/java/life/qbic/datamanager/views/projects/overview/ProjectOverviewMain.java

+57-20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.io.Serial;
1414
import java.util.Arrays;
1515
import java.util.HashSet;
16+
import java.util.Locale;
17+
import java.util.Objects;
1618
import java.util.Set;
1719
import life.qbic.application.commons.ApplicationException;
1820
import life.qbic.application.commons.Result;
@@ -21,11 +23,12 @@
2123
import life.qbic.datamanager.views.general.Main;
2224
import life.qbic.datamanager.views.general.contact.Contact;
2325
import life.qbic.datamanager.views.notifications.CancelConfirmationDialogFactory;
24-
import life.qbic.datamanager.views.notifications.StyledNotification;
25-
import life.qbic.datamanager.views.notifications.SuccessMessage;
26+
import life.qbic.datamanager.views.notifications.MessageSourceNotificationFactory;
27+
import life.qbic.datamanager.views.notifications.Toast;
2628
import life.qbic.datamanager.views.projects.create.AddProjectDialog;
2729
import life.qbic.datamanager.views.projects.create.AddProjectDialog.ConfirmEvent;
2830
import life.qbic.datamanager.views.projects.overview.components.ProjectCollectionComponent;
31+
import life.qbic.datamanager.views.register.EmailConfirmationMain;
2932
import life.qbic.finances.api.FinanceService;
3033
import life.qbic.identity.api.UserInformationService;
3134
import life.qbic.logging.api.Logger;
@@ -62,6 +65,7 @@ public class ProjectOverviewMain extends Main {
6265
private final transient AddExperimentToProjectService addExperimentToProjectService;
6366
private final transient UserInformationService userInformationService;
6467
private final transient AuthenticationToUserIdTranslationService userIdTranslator;
68+
private final transient MessageSourceNotificationFactory messageSourceNotificationFactory;
6569

6670
public ProjectOverviewMain(@Autowired ProjectCollectionComponent projectCollectionComponent,
6771
ProjectCreationService projectCreationService, FinanceService financeService,
@@ -71,7 +75,8 @@ public ProjectOverviewMain(@Autowired ProjectCollectionComponent projectCollecti
7175
UserInformationService userInformationService,
7276
AuthenticationToUserIdTranslationService userIdTranslator,
7377
TerminologyService terminologyService,
74-
CancelConfirmationDialogFactory cancelConfirmationDialogFactory) {
78+
CancelConfirmationDialogFactory cancelConfirmationDialogFactory,
79+
MessageSourceNotificationFactory messageSourceNotificationFactory) {
7580
this.projectCollectionComponent = requireNonNull(projectCollectionComponent,
7681
"project collection component can not be null");
7782
this.projectCreationService = requireNonNull(projectCreationService,
@@ -110,6 +115,21 @@ public ProjectOverviewMain(@Autowired ProjectCollectionComponent projectCollecti
110115
this.getClass().getSimpleName(), System.identityHashCode(this),
111116
projectCollectionComponent.getClass().getSimpleName(),
112117
System.identityHashCode(projectCollectionComponent)));
118+
this.messageSourceNotificationFactory = messageSourceNotificationFactory;
119+
}
120+
121+
private static Toast notificationFor(NotificationType type, ConfirmEvent confirmEvent,
122+
MessageSourceNotificationFactory factory, Locale locale) {
123+
return switch (type) {
124+
case PROJECT_CREATED_SUCCESSFULLY -> factory.toast("project.created.success",
125+
new Object[]{confirmEvent.getProjectDesign().getProjectTitle()}, locale);
126+
case PROJECT_CREATION_FAILED ->
127+
factory.toast("project.created.error", new Object[]{}, locale);
128+
case EXPERIMENT_CREATED_SUCCESSFULLY -> factory.toast("experiment.created.success",
129+
new Object[]{confirmEvent.getExperimentalInformation().getExperimentName()}, locale);
130+
case EXPERIMENT_CREATION_FAILED ->
131+
factory.toast("experiment.created.error", new Object[]{}, locale);
132+
};
113133
}
114134

115135
private void addTitleAndDescription() {
@@ -158,11 +178,7 @@ private void createProject(ConfirmEvent confirmEvent) {
158178
.map(Contact::toDomainContact).orElse(null),
159179
confirmEvent.getProjectCollaborators().getProjectManager().toDomainContact(),
160180
funding);
161-
project
162-
.onValue(result -> onProjectCreated(confirmEvent))
163-
.onError(e -> {
164-
throw e;
165-
});
181+
handleResultProject(project, confirmEvent);
166182
var experiment = addExperimentToProjectService.addExperimentToProject(
167183
project.getValue().getId(),
168184
confirmEvent.getExperimentalInformation().getExperimentName(),
@@ -171,22 +187,43 @@ private void createProject(ConfirmEvent confirmEvent) {
171187
confirmEvent.getExperimentalInformation().getAnalytes(),
172188
confirmEvent.getExperimentalInformation().getSpeciesIcon().getLabel(),
173189
confirmEvent.getExperimentalInformation().getSpecimenIcon().getLabel());
174-
experiment.onError(e -> {
175-
throw e;
176-
});
190+
handleResultExperiment(experiment, confirmEvent);
191+
projectCollectionComponent.refresh();
192+
projectCollectionComponent.resetSearch();
193+
}
194+
195+
private void handleResultProject(Result<?, ?> result, ConfirmEvent event) {
196+
if (result.isError()) {
197+
processNotification(notificationFor(NotificationType.PROJECT_CREATION_FAILED, event,
198+
messageSourceNotificationFactory, getLocale()));
199+
return;
200+
}
201+
processNotification(notificationFor(NotificationType.PROJECT_CREATED_SUCCESSFULLY, event,
202+
messageSourceNotificationFactory, getLocale()));
203+
closeDialog(event);
177204
}
178205

179-
private void onProjectCreated(ConfirmEvent confirmEvent) {
180-
displaySuccessfulProjectCreationNotification();
181-
confirmEvent.getSource().close();
206+
private void handleResultExperiment(Result<?, ?> result, ConfirmEvent event) {
207+
if (result.isError()) {
208+
processNotification(notificationFor(NotificationType.EXPERIMENT_CREATION_FAILED, event,
209+
messageSourceNotificationFactory, getLocale()));
210+
return;
211+
}
212+
closeDialog(event);
213+
}
182214

183-
projectCollectionComponent.refresh();
184-
projectCollectionComponent.resetSearch();
215+
private void processNotification(Toast t) {
216+
t.open();
217+
}
218+
219+
private void closeDialog(ConfirmEvent event) {
220+
event.getSource().close();
185221
}
186222

187-
private void displaySuccessfulProjectCreationNotification() {
188-
var successMessage = new SuccessMessage("Project creation succeeded.", "");
189-
var notification = new StyledNotification(successMessage);
190-
notification.open();
223+
enum NotificationType {
224+
PROJECT_CREATED_SUCCESSFULLY,
225+
PROJECT_CREATION_FAILED,
226+
EXPERIMENT_CREATED_SUCCESSFULLY,
227+
EXPERIMENT_CREATION_FAILED,
191228
}
192229
}

user-interface/src/main/resources/messages/toast-notifications.properties

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ personal-access-token.created.success.message.type=html
2525
personal-access-token.created.success.message.text=Personal Access Token <strong>{0}</strong> was created.
2626
personal-access-token.created.success.duration=PT8s
2727
personal-access-token.created.success.level=success
28+
project.created.success.message.type=html
29+
project.created.success.message.text=New project <strong>{0}</strong> created.
30+
project.created.success.level=success
31+
project.created.error.message.type=html
32+
project.created.error.message.text=Project creation failed, please contact the <a href="mailto:[email protected]">QBiC support</a>.
33+
project.created.error.level=error
34+
experiment.created.error.message.type=html
35+
experiment.created.error.message.text=Experiment creation failed, please contact the <a href="mailto:[email protected]">QBiC support</a>.
36+
experiment.created.error.level=error
2837
sample-batch.deleted.success.message.type=html
2938
sample-batch.deleted.success.message.text=Deleted sample batch <strong>{0}</strong>.
3039
sample-batch.deleted.success.level=success

0 commit comments

Comments
 (0)