All files / nexshop-web-contents/src/component contents-main.js

100% Statements 70/70
100% Branches 4/4
100% Functions 13/13
100% Lines 70/70
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 1731x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x       42x   42x 42x 42x 42x 42x 42x 42x 42x 42x       4x   4x   4x 4x       3x   3x 3x   3x       1x       3x   3x 3x 3x   3x 3x   3x       1x       1x 1x   1x   1x       2x   2x 2x       4x 4x   4x 4x 4x 4x       1x   1x             42x   42x                                                     1x 42x 42x 42x 42x   42x     1x 42x 42x 42x 42x   42x                                     1x        
import React, {Component} from 'react';
import {bindActionCreators} from "redux";
import {connect} from "react-redux";
import PropTypes from 'prop-types';
import {NavigationBarView} from 'nexshop-web-navigation';
import {contentsActions, playlistActions, sceneActions} from "nexshop-web-store";
import {actions as dialogActions} from "nexshop-web-dialog";
import {actions as popupActions} from "nexshop-web-popup";
import ContentsMainView from "./contents-main-view";
import {openFloatingView} from "../action/floating-view";
import '../asset/css/index.scss';
 
const CONTENTS = 'contents';
const {selectUploadFiles, uploadFilesAsync, createNewFolderAsync, fetchExistContentNameAsync, replaceContentNameValidity} = contentsActions;
 
class ContentsMain extends Component {
    constructor(props) {
        super(props);
 
        this.onCreatePlaylistMenuClicked = this.onCreatePlaylistMenuClicked.bind(this);
        this.onCreateSceneMenuClicked = this.onCreateSceneMenuClicked.bind(this);
        this.onAddButtonClicked = this.onAddButtonClicked.bind(this);
        this.onPlaylistCreated = this.onPlaylistCreated.bind(this);
        this.onSceneCreated = this.onSceneCreated.bind(this);
        this.onCreateNewFolderClicked = this.onCreateNewFolderClicked.bind(this);
        this.uploadFiles = this.uploadFiles.bind(this);
        this.openDialogAndClosePopup = this.openDialogAndClosePopup.bind(this);
        this.closeDialog = this.closeDialog.bind(this);
    }
 
    onAddButtonClicked(e) {
        e.stopPropagation();
 
        const {addMenuVisibility, actions: {closeAllPopup, openPopup}} = this.props;
 
        closeAllPopup();
        !addMenuVisibility && openPopup('add-menu');
    }
 
    onPlaylistCreated(title, resolution) {
        const {actions: {createPlaylist, closeDialog}, history} = this.props;
 
        createPlaylist(title, resolution);
        closeDialog();
 
        history.push('/contents/create-playlist');
    }
 
    onCreatePlaylistMenuClicked() {
        this.openDialogAndClosePopup('create-playlist');
    }
 
    onSceneCreated(title, resolution) {
        const {actions: {createScene, closeDialog}, history} = this.props;
 
        const {text, orientation} = resolution;
        let width = text.split('x')[0];
        let height = text.split('x')[1];
 
        createScene(title, {width, height}, orientation);
        closeDialog();
 
        history.push('/contents/scene');
    }
 
    onCreateSceneMenuClicked() {
        this.openDialogAndClosePopup('create-scene');
    }
 
    onCreateNewFolderClicked() {
        const {actions: {closePopup}, breadcrumb} = this.props;
        const currentFolderId = breadcrumb[breadcrumb.length-1].id;
 
        this.props.actions.createNewFolderAsync(currentFolderId);
 
        closePopup('add-menu');
    }
 
    openDialogAndClosePopup(dialogName) {
        const {openDialog, closePopup} = this.props.actions;
 
        openDialog(dialogName);
        closePopup('add-menu');
    }
 
    uploadFiles(files) {
        const {actions: {selectUploadFiles, uploadFilesAsync, openFloatingView, closePopup}, breadcrumb} = this.props;
        const currentFolderId = breadcrumb[breadcrumb.length-1].id;
 
        selectUploadFiles(files);
        uploadFilesAsync(files, currentFolderId);
        openFloatingView();
        closePopup('add-menu');
    }
 
    closeDialog() {
        const {actions: {closeDialog}} = this.props;
 
        closeDialog();
    }
 
    render() {
        const {
            addMenuVisibility, actions: {closePopup, fetchExistContentNameAsync, replaceContentNameValidity},
            createPlaylistDialogVisibility, createSceneDialogVisibility, breadcrumb, contentNameExisted
        } = this.props;
 
        return (
            <div>
                <NavigationBarView activeMenu={CONTENTS}/>
                <ContentsMainView
                    onCreateSceneMenuClicked={this.onCreateSceneMenuClicked}
                    onSceneCreated={this.onSceneCreated}
                    onCreatePlaylistMenuClicked={this.onCreatePlaylistMenuClicked}
                    onCreateNewFolderClicked={this.onCreateNewFolderClicked}
                    onPlaylistCreated={this.onPlaylistCreated}
                    onAddButtonClicked={this.onAddButtonClicked}
                    addMenuVisibility={addMenuVisibility}
                    onSelectFiles={this.uploadFiles}
                    fetchExistContentNameAsync={fetchExistContentNameAsync}
                    closeAddMenu={closePopup}
                    createPlaylistDialogVisibility={createPlaylistDialogVisibility}
                    createSceneDialogVisibility={createSceneDialogVisibility}
                    closeDialog={this.closeDialog}
                    breadcrumb={breadcrumb}
                    contentNameExisted={contentNameExisted}
                    replaceContentNameValidity={replaceContentNameValidity}
                    history={this.props.history}
                />
            </div>
        );
    }
}
 
const mapStateToProps = (state) => {
    const {popup, dialog, contents: {contentNameExisted, breadcrumb}} = state;
    const addMenuVisibility = popup['add-menu'] && popup['add-menu'].visibility;
    const createSceneDialogVisibility = dialog === 'create-scene';
    const createPlaylistDialogVisibility = dialog === 'create-playlist';
 
    return {addMenuVisibility, createPlaylistDialogVisibility, createSceneDialogVisibility, contentNameExisted, breadcrumb};
};
 
const mapDispatchToProps = (dispatch) => {
    const {openDialog, closeDialog} = dialogActions;
    const {createPlaylist} = playlistActions;
    const {createScene} = sceneActions;
    const {openPopup, closePopup, closeAllPopup} = popupActions;
 
    return {
        actions: bindActionCreators({
            openDialog,
            closeDialog,
            openPopup,
            closePopup,
            closeAllPopup,
            selectUploadFiles,
            uploadFilesAsync,
            openFloatingView,
            createPlaylist,
            createScene,
            createNewFolderAsync,
            fetchExistContentNameAsync,
            replaceContentNameValidity,
        }, dispatch)
    }
};
 
ContentsMainView.propTypes = {
    closeAddMenu: PropTypes.func.isRequired
};
 
export default connect(mapStateToProps, mapDispatchToProps)(ContentsMain)