css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <link href="https://fonts.googleapis.com/css?family=Quicksand:300,400,700" rel="stylesheet">

<div id="container"></div>
            
          
!
            
              body {
  background-image: url(http://c2064.in-berlin.de/fcc/ability3-bg.jpg);
  background-size: 100%;
  font-family: "Quicksand", "Courier New", sans-serif;
}

#container {
  width: 1200px;
  margin: 50px auto;
  padding: 20px;
  border-radius: 20px;
  background-color: rgba(255,255,255,0.3);
  border: 1px solid lightred;
  
  .image-row {
    
    .image {
      
      img {
      margin-bottom: 20px; 
        width: 200px;
      }
    }

    table {
      td {
        border-color: rgba(128,128,128,0.9);
      }
      
    }
  }  
  
}

h2 {
  font-size: 2.7em;
  margin-bottom: 20px;
}

h2, h4 {
  text-align: center;
}

.image-search {
  margin: 0 auto;
  width: 1100px;
  input {
    line-height: 1.8em;
    padding: 5px;
  }
  
  h3 {
    margin: 40px;
    text-align: center;
  }
  
  a, a:active {
    color: darkblue;
  }
  
  button {
    font-size: 1.3em;
  }

  .pager-wrapper {
    text-align: center;
    font-size: 1.7em;
    
    .pager {
      display: inline-block;
      cursor: pointer;
      color: rgba(0,0,50,0.5);
      margin: 20px;

      &.disabled, &.disabled:hover {
        display: none;
      }
      
      &:hover {
        color: darkblue;
      }
    }
  }
  
  .dropdown {
    float: left;
    
    .dropdown-menu {
      margin: 0;
      
      li {
        padding: 5px;
        
        &:hover {
          background-color: lightgray;
          text-decoration: none;
        }

        a {
          color: black;        
        }
      }
    }
    
    button {
      font-size: 1.2em;
    }
  }
}

            
          
!
            
              // React/Redux Boilerplate with Reducers and Actions

// React /////////////////////////////////////////////////////////
const Component = React.Component;

// Redux /////////////////////////////////////////////////////////
const Provider = ReactRedux.Provider;
const connect = ReactRedux.connect;
const createStore = Redux.createStore;
const combineReducers = Redux.combineReducers;
const bindActionCreators = Redux.bindActionCreators;
const thunkMiddleware = ReduxThunk.default;
const applyMiddleware = Redux.applyMiddleware;

// DropDownMenu ///////////////////////////////////////////////////
class DropDownMenuClass extends Component {
  constructor(props) {
    super(props);
    
    this.props.requestSearchTerms();
    setInterval( () => this.props.requestSearchTerms(), 10000);
  }
  
  render() {
    console.log(this.props);
    return (
      <div className="dropdown">
        <button 
          className="btn btn-default dropdown-toggle"
          type="button"
          data-toggle="dropdown">
          Recent
          <span className="caret"></span>
        </button>
        <ul className="dropdown-menu">
          { this.props.searchTerms.searchTerms.map((searchTerm) => 
            <li key={searchTerm} onClick={() => this.props.onChangeSearchTerm(searchTerm)}>
              <a href="#">{searchTerm}</a>
            </li>)}
        </ul>
      </div>
    )
  }
}

const DropDownMenu = connect(
  ({searchTerms}) => { return {searchTerms}; },
  (dispatch) => bindActionCreators({requestSearchTerms}, dispatch)
)(DropDownMenuClass);

// StateComponent ///////////////////////////////////////////////////
class ImageSearchClass extends Component {
  constructor(props) {
    super(props);
    
    this.state = {
      searchTerm: "",
      error: "..."
    };
  }
  
  componentDidMount() {
    this.textInput && this.textInput.focus();
  }
  
  componentDidUpdate() {
    this.textInput && this.textInput.focus();
  }
  
  onSearchClick(page) {
    this.props.searchAction(this.state.searchTerm, page)
  }
  
  onChangeSearchTerm(searchTerm) {
    this.state.searchTerm = searchTerm;
    this.props.searchAction(searchTerm, 1)
  }
    
  render() {
    let data = this.props.data;    
    
    if( data.error ) {
      var output = <div className="images error">{data.error}</div>
    } else if(data.isFetching) {
      var output =
          <div className="spinner">
            <img src="http://c2064.in-berlin.de/fcc/loading.gif" width="100"/>
          </div>
    } else if(data.images.length) {
      var output =
          <div className="images">
            {data.images.map(image => {
              return (
                <div key={image.url} className="image-row row">
                  <div className="col-md-3">
                    <a className="image col-md-3" href={image.url} target="_blank">
                      <img src={image.url} className="img-thumbnail" />
                    </a>
                  </div>
                  <div className="col-md-9">
                    <table className="table col-md-9">
                      <tbody>
                        <tr>
                          <td>Title:</td>
                          <td><strong>{image.title}</strong></td>
                        </tr>
                        <tr>
                          <td>Source:</td>
                          <td><a href={image.source} target="_blank">{image.source}</a></td>
                        </tr>
                      </tbody>
                    </table>
                  </div>
                </div>
              )
            })}
          </div>
    } else {
      var output = <div>No images found.</div>
    }
    
    if( data.pages > 0 ) {
      if( data.currentPage > data.pages ) data.currentPage = data.pages;
      let start = (
            <span 
              className="pager start"
              onClick={(e) => this.props.searchAction(this.state.searchTerm, 1)}
            >
              1
            </span>
          ),
          left = (
            <span 
              className="pager left"
              onClick={(e) => this.props.searchAction(this.state.searchTerm, data.currentPage-1)}
            >
              &lt;&lt;
            </span>
          ),
          middle = (
            <span 
              className="pager middle"
              onClick={(e) => this.props.searchAction(this.state.searchTerm, data.currentPage)}
            >
              {data.currentPage}
            </span>
          ),
          right = (
            <span 
              className="pager right"
              onClick={(e) => this.props.searchAction(this.state.searchTerm, data.currentPage+1)}
            >
              &gt;&gt;
            </span>
          ),
          end = (
            <span 
              className="pager left"
              onClick={(e) => this.props.searchAction(this.state.searchTerm, data.pages)}
            >
              {data.pages}
            </span>
          );

      var pager = (
        <div className="pager-wrapper">
          {start}
          {data.currentPage > 1? left:""}
          {data.currentPage > 1 && data.currentPage < data.pages? middle:""}
          {data.currentPage < data.pages? right:""}
          {end}
        </div>
      )
    } else {
      var pager = "";
    } 
  
    return (
    <div className="image-search">
        <input 
          onChange={(e) => {this.setState({searchTerm: e.target.value})}}
          onKeyPress={(e) => e.charCode === 13? this.button.click(): e }
          value={this.state.searchTerm}
          size="60"
          placeholder="Type in your search terms ..."
          ref={ input => { this.textInput = input; }}
          />
        <button
          type="button"
          className="btn btn-primary"
          onClick={() => { this.onSearchClick(1) }}
          ref={ button => this.button = button }
        >
          <i className="fa fa-free-code-camp"></i>
        </button>
        <DropDownMenu onChangeSearchTerm={(searchTerm) => this.onChangeSearchTerm(searchTerm)} />
        <h3>Images</h3>
        {pager}
        {output}
        {pager}
      </div>
    )
  }
}

const ImageSearch = connect(
  ({data}) => { return {data}; },
  (dispatch) => bindActionCreators({searchAction}, dispatch)
)(ImageSearchClass);


// Redux Reducers /////////////////////////////////////////////////////////
const ImageReducer = function(state = {
    searchTerm: "",
    errorMessage: "",
    currentPage: null,
    pages: null,
    isFetching: false,
    lastUpdated: new Date().getTime(),    
    images: []
  }, action) {

  switch( action.type ) {
  case SEARCH_REQUEST:
      return { ...state, isFetching: true };
  case SEARCH_FAILURE:
      return { ...state, images: [], errorMessage: action.payload };
  case SEARCH_SUCCESS:
      return { 
        ...state,
        images: action.payload.images,
        errorMessage: null,
        isFetching: false,
        pages: action.payload.pages,
        currentPage: action.payload.page 
    }
  }

  return state;
}
  
const searchTermReducer = function(state = { searchTerms: [] }, action) {

  switch( action.type ) {
  case NEW_SEARCHTERMS:
      return { 
        searchTerms: action.payload 
      }
  }

  return state;
}

const rootReducer = combineReducers({
  data:         ImageReducer,
  searchTerms:  searchTermReducer
});

// Redux Actions ////////////////////////////////////////////////////////////
const SEARCH_ACTION = "SEARCH_ACTION";
function searchAction(searchString, page) {
  return function(dispatch) {
    dispatch(searchRequest(searchString));
    
    return axios.get( "https://ancient-fjord-37343.herokuapp.com/api/imagesearch/" + searchString + "?page="+(page || 1) )
      .then( (response) => {
        if( response.data.error ) {
          dispatch(searchFailure(response.data.error));
        } else {
          dispatch(searchSuccess(response.data));
        }
      })
      .catch( (error) => {
          dispatch(searchFailure(error));
      });    
  };
}

const SEARCH_REQUEST = "SEARCH_REQUEST";
function searchRequest(searchString) {
  return {
    type: SEARCH_REQUEST,
    payload: searchString
  }
}

const SEARCH_FAILURE = "SEARCH_FAILURE";
function searchFailure(error) {
  return {
    type: SEARCH_FAILURE,
    payload: error
  }
}

const SEARCH_SUCCESS = "SEARCH_SUCCESS";
function searchSuccess(payload) {
  return {
    type: SEARCH_SUCCESS,
    payload
  }
}

const SEARCHTERM_REQUEST = "SEARCHTERM_REQUEST";
function requestSearchTerms() {
  return function(dispatch) {
    return axios.get( "https://ancient-fjord-37343.herokuapp.com/api/searchterms" )
      .then( (response) => {
        dispatch(newSearchTerms(response.data));
      });
  };
}

const NEW_SEARCHTERMS = "NEW_SEARCHTERMS";
function newSearchTerms(payload) {
  return {
    type: NEW_SEARCHTERMS,
    payload
  }
}

// App ///////////////////////////////////////////////////////////////////////
class App extends Component {
  constructor(props) {
    super(props);
    
    this.state = {
    }
  }

  render() {
    return (
      <div>
        <h2>Creative Commons Image Search</h2>
        <ImageSearch />
      </div>
    )
  }
}

ReactDOM.render(<Provider store={createStore(rootReducer,applyMiddleware(thunkMiddleware))}><App /></Provider>, document.getElementById("container"));

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console