2016-03-21 16:55:32 +01:00
package org.gcube.portlets.user.joinvre.client.ui ;
import org.gcube.portlets.user.joinvre.client.JoinService ;
import org.gcube.portlets.user.joinvre.client.JoinServiceAsync ;
2016-03-31 12:33:44 +02:00
import org.gcube.portlets.user.joinvre.client.responsive.ResponsivePanel ;
2016-03-21 16:55:32 +01:00
import org.gcube.portlets.user.joinvre.shared.VRE ;
import com.github.gwtbootstrap.client.ui.Button ;
import com.github.gwtbootstrap.client.ui.HelpBlock ;
2017-07-05 18:27:46 +02:00
import com.github.gwtbootstrap.client.ui.Icon ;
2016-03-21 16:55:32 +01:00
import com.github.gwtbootstrap.client.ui.Modal ;
import com.github.gwtbootstrap.client.ui.TextArea ;
import com.google.gwt.core.client.GWT ;
2017-07-05 18:27:46 +02:00
import com.google.gwt.dom.client.Element ;
2016-03-21 16:55:32 +01:00
import com.google.gwt.event.dom.client.ClickEvent ;
2017-07-05 18:27:46 +02:00
import com.google.gwt.event.dom.client.ClickHandler ;
2016-03-21 16:55:32 +01:00
import com.google.gwt.uibinder.client.UiBinder ;
import com.google.gwt.uibinder.client.UiField ;
import com.google.gwt.uibinder.client.UiHandler ;
2017-07-05 18:27:46 +02:00
import com.google.gwt.user.client.Window.Location ;
2016-03-21 16:55:32 +01:00
import com.google.gwt.user.client.rpc.AsyncCallback ;
import com.google.gwt.user.client.ui.Composite ;
2017-07-05 18:27:46 +02:00
import com.google.gwt.user.client.ui.HTML ;
2016-03-21 16:55:32 +01:00
import com.google.gwt.user.client.ui.Widget ;
public class RequestMembershipDialog extends Composite {
2017-07-05 18:27:46 +02:00
private static final String OPTIONAL_COMMENT_TEXT = " You can add an optional comment here, it will be attached to your request and read by this VRE Moderators. " ;
2016-03-21 16:55:32 +01:00
private final JoinServiceAsync joinService = GWT . create ( JoinService . class ) ;
private static RequestAccessModalUiBinder uiBinder = GWT
. create ( RequestAccessModalUiBinder . class ) ;
interface RequestAccessModalUiBinder extends
UiBinder < Widget , RequestMembershipDialog > {
}
@UiField Modal m ;
@UiField Button close ;
@UiField Button confirmRequest ;
@UiField TextArea optionalText ;
@UiField HelpBlock helpBlock ;
2017-07-05 18:27:46 +02:00
@UiField Icon loading ;
2016-03-31 12:33:44 +02:00
private VRE myVRE = null ;
private ResponsivePanel responsivePanel ;
2017-07-05 18:27:46 +02:00
@UiField HelpBlock touGatewayBlock ;
@UiField HTML touText ;
2016-03-21 16:55:32 +01:00
2016-03-31 12:33:44 +02:00
public RequestMembershipDialog ( ResponsivePanel responsivePanel , VRE myVRE ) {
2016-03-21 16:55:32 +01:00
initWidget ( uiBinder . createAndBindUi ( this ) ) ;
this . myVRE = myVRE ;
2016-03-31 12:33:44 +02:00
this . responsivePanel = responsivePanel ;
2016-03-21 16:55:32 +01:00
optionalText . setWidth ( " 95% " ) ;
2017-07-05 18:27:46 +02:00
optionalText . setPlaceholder ( OPTIONAL_COMMENT_TEXT ) ;
2016-03-21 16:55:32 +01:00
}
public void show ( ) {
m . setTitle ( " Join request for " + myVRE . getName ( ) ) ;
2017-07-05 18:27:46 +02:00
loading . setVisible ( true ) ;
joinService . getTermsOfUse ( this . myVRE . getId ( ) , new AsyncCallback < String > ( ) {
@Override
public void onFailure ( Throwable caught ) {
m . setTitle ( " Ops, an error occurred please check your connection and try again " ) ;
confirmRequest . setText ( " Try again " ) ;
confirmRequest . addClickHandler ( new ClickHandler ( ) {
@Override
public void onClick ( ClickEvent event ) {
Location . reload ( ) ;
}
} ) ;
loading . setVisible ( false ) ;
touGatewayBlock . setVisible ( true ) ;
m . show ( ) ;
}
@Override
public void onSuccess ( String result ) {
loading . setVisible ( false ) ;
if ( result ! = null ) { // terms of use exist
String text = " By using <b> " + myVRE . getName ( ) + " </b> VRE services, you agree to the Terms of Use below. Please read it carefully. " ;
helpBlock . setHTML ( text ) ;
String buttonText = " Accept Terms of Use & Request Access " ;
confirmRequest . setText ( buttonText ) ;
m . addStyleName ( " modal-custom " ) ;
( ( Element ) m . getElement ( ) . getChildNodes ( ) . getItem ( 1 ) ) . addClassName ( " modal-body-custom " ) ;
touText . setHTML ( result ) ;
}
m . show ( ) ;
}
} ) ;
2016-03-21 16:55:32 +01:00
m . show ( ) ;
}
@UiHandler ( " close " )
void handleClick ( ClickEvent e ) {
m . hide ( ) ;
}
@UiHandler ( " confirmRequest " )
void confirm ( ClickEvent e ) {
2016-08-25 15:43:58 +02:00
String text = optionalText . getText ( ) ;
confirmRequest . setEnabled ( false ) ;
2017-07-05 18:27:46 +02:00
joinService . addMembershipRequest ( myVRE , text , new AsyncCallback < Void > ( ) {
2016-03-21 16:55:32 +01:00
@Override
public void onSuccess ( Void result ) {
confirmRequest . removeFromParent ( ) ;
optionalText . removeFromParent ( ) ;
m . setTitle ( " Thank you, your request has been sent successfully " ) ;
helpBlock . setText ( " You will receive an email as soon as your request will be processed. " ) ;
2016-03-31 12:33:44 +02:00
responsivePanel . setPending ( myVRE ) ;
confirmRequest . setEnabled ( false ) ;
2017-07-05 18:27:46 +02:00
touText . removeFromParent ( ) ;
2016-03-21 16:55:32 +01:00
}
@Override
public void onFailure ( Throwable caught ) {
confirmRequest . removeFromParent ( ) ;
optionalText . removeFromParent ( ) ;
m . setTitle ( " An error occurred! Your request has not been sent " ) ;
helpBlock . setText ( " An email with the cause of the error has been sent to the support team, we'll be back to you shortly. " ) ;
2017-07-05 18:27:46 +02:00
touText . removeFromParent ( ) ;
2016-03-21 16:55:32 +01:00
}
} ) ;
}
}