Subversion Repositories SmartDukaan

Rev

Rev 2119 | Rev 2208 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.catalog.dashboard.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;

public class CatalogDashboard implements EntryPoint {

    @UiTemplate("CatalogDashboard.ui.xml")
    interface CatalogBinder extends UiBinder<DockLayoutPanel, CatalogDashboard> {
    }
    
    private static final CatalogBinder binder = GWT.create(CatalogBinder.class);
    private final LoginServiceAsync loginService = GWT.create(LoginService.class);
    
    @UiField TopPanel topPanel;
    @UiField Shortcuts shortcuts;
    //@UiField Filters filters;
    @UiField ItemList itemList;
    @UiField ItemDetails itemDetails;
    @UiField ItemActions itemActions;

    RootLayoutPanel root;
    LoginScreen login;

    public void onModuleLoad() {
        root = RootLayoutPanel.get();
        root.getElement().setScrollLeft(10);
        login = new LoginScreen();
        //initMainDB("Testing");
        root.add(login);
        login.setLoginListener(new LoginScreen.LoginListener() {
            @Override
            public void onLoginPressed(String username, String password) {
                authenticateUser(username, password);
            }
        });
    }

    private void authenticateUser(String username, String password) {
        final String uname = username;
        loginService.authenticateUser(username, password, new AsyncCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result != null) {
                    GWT.log(result + " logged in catalog dashboard");
                    root.clear();
                    initMainDB(uname);
                    login.clearFields();
                } else {
                    login.setErrorText("Invalid username/password");    
                }
            }
            @Override
            public void onFailure(Throwable caught) {
                login.setErrorText("Error in authentication");
            }
        });
    }

    private void initMainDB(String username) {
        DockLayoutPanel dashboard = binder.createAndBindUi(this);
        itemActions.setItemDetails(itemDetails);
        itemList.setItemDetails(itemDetails);
        topPanel.changeGreeting(username);

        topPanel.getSignOutLink().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                root.clear();
                root.add(login);
            }
        });
        root.clear();
        root.add(dashboard);
        
        shortcuts.getCatalogTree().setTreeListener(new CatalogTree.TreeListener() {
            @Override
            public void onTreeItemClicked(String itemsType) {
                //showWaitCursor();
                if(CatalogTree.ALL_ITEMS.equals(itemsType)) {
                    itemList.loadAllItems();
                } else if(CatalogTree.ALL_ACTIVE_ITEMS.equals(itemsType)) { 
                    itemList.loadAllActiveItems();
                } else if(CatalogTree.BEST_DEALS.equals(itemsType)) {
                    itemList.loadBestDeals();
                } else if(CatalogTree.BEST_SELLERS.equals(itemsType)) {
                    itemList.loadBestSellers();
                } else if(CatalogTree.LATEST_ARRIVALS.equals(itemsType)) {
                    itemList.loadLatestArrivals();
                }
            }
        });
    }
}