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.

            
              @import url('https://fonts.googleapis.com/css?family=Montserrat');

*,*:before,*:after {
  box-sizing: border-box;
}

::-webkit-scrollbar {
  width: 0;
}

body, input, button {
  font-family: 'Montserrat', sans-serif;
}

body {
  height: 100vh;
}

$primary: #53a7ee;
$secondary: #9e9e9e;

input, select {
  color: gray;
  margin: 0 5px;
  border: none;
  padding: 5px;
  outline: none;
  background: none;
  border-bottom: solid 2px lightgray;
  &:focus {
    color: $primary;
    border-bottom-color: $primary;
  }
}
select {
  color: $primary;
}

button {
  color: white;
  cursor: pointer;
  border: none;
  padding: 5px;
  background: $primary;
}

#app {
  height: 100vh;
  display: flex;
  margin: auto;
  max-width: 500px;
  background: white;
  box-shadow: 0 0 20px 1px $secondary;
  flex-direction: column;
}

header {
  height: 200px;
  display: flex;
  position: relative;
  background: url('https://images.pexels.com/photos/756799/pexels-photo-756799.jpeg?h=350&auto=compress&cs=tinysrgb')no-repeat center center;
  background-size: cover;
  > * {
    flex: 1;
  }
}

.task-completion {
  color: lighten($secondary, 20%);
  right: 20px;
  bottom: 12px;
  position: absolute;
  font-size: 14px;
  font-weight: 100;
}


.date {
  padding: 0 0 10px 10px;
  border-bottom: solid 4px $primary;
  p {
    color: white;
    height: 100%;
    display: flex;
    font-size: 40px;
    margin-top: 0;
    flex-direction: column;
    justify-content: flex-end;
    span {
      color: lighten($secondary, 20%);
      font-size: 13px;
      font-weight: initial;
    }
  }
}

.type-of-tasks {
  display: flex;
  background: linear-gradient(0deg, rgba(0, 0, 60, .6), rgba(0,0,0,.3));
  padding-right: 10px;
  justify-content: flex-end;
  p {
    color: white;
    display: flex;
    font-size: 40px;
    align-self: center;
    align-items: flex-end;
    margin-left: 10px;
    margin-right: 10px;
    flex-direction: column;
    span {
      color: lighten($secondary, 20%);
      font-size: 13px;
    }
  }
}

/*
SEARCH FIELD
*/
#search-field {
  padding: 10px 10px;
  // border: solid 1px red;
  display: inline-block;
  margin-right: auto;
  position: relative;
  .fa-search {
    position: absolute;
    right: 5px;
    top: 50%;
    transform: translate(0,-50%) rotate(90deg);
    color: lighten($secondary, 10%);
  }
  input {
    color: $primary;
    width: 100%;
    font-size: 14px;
  }
}

::-webkit-input-placeholder {
  color: lightgray;
  font-style: italic
}

/*
TASKS
*/
ul {
  padding: 0 10px;
  list-style: none;
  overflow-y: scroll;
  li {
    padding: 10px 0 10px 15px;
    border-bottom: solid 1px lightgray;
    &:last-child {
      border-bottom: none;
    }
    div {
      display: flex;
      align-items: center;
      p {
        margin: 0 auto 0 0;
        display: flex;
        font-size: 19px;
        flex-direction: column;
        span {
          color: $secondary;
          font-size: 13px;
          margin-top: 2px;
        }
      }
      button {
        color: $primary;
        margin: 0 10px;
        background: none;
      }
    }
  }
}


/*
ADD TASK FORM
*/
#add-task-form {
  height: 55px;
  padding: 10px;
  display: flex;
  margin-top: auto;
  > * {
    flex: 1;
  }
  button {
    color: white;
    padding: 5px;
    &:hover {
      background: darken($primary, 10%);
    }
  }
}

#edit-task-form {
  padding: 0 8px 0 0;
  display: flex;
  input {
    flex: 1;
  }
  button {
    color: white;
  }
}


ul li button:hover:not(.delete-btn) {
  color: darken($primary, 18%);
}

/*
CLASSES
*/
.completed {
  color: gray;
  text-decoration: line-through;
}

.delete-btn:hover {
  color: red;
}

.disable-btn {
  color: gray;
  cursor: not-allowed;
  opacity: .2;
}
            
          
!
            
              class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      tasks: [
        {
          id: 1,
          task: 'Meeting Today',
          type: 'Business'
        },
        {
          id: 2,
          task: 'Work Out',
          type: 'Personal'
        }, {
          id: 3,
          task: 'Learn React',
          type: 'Personal'
        }
      ],
      searchTaskValue: '',
      completedTask: []
    }
  }
  
  deleteTask = (id) => {
    const {tasks, completedTask} = this.state;
    const filterTasks = tasks.filter(task => task.id !== id)
    const clearCompleted = completedTask.length > 0 && completedTask.filter(task => task.id !== id)
    
    this.setState({
      tasks: filterTasks,
      completedTask: clearCompleted
    })
  }
  
  addTask = (task, id, type) => {
    const {tasks} = this.state
    
    tasks.unshift({task, id, type})
    
    this.setState({
      tasks: tasks
    })
  }
  
  saveEditTask = (task, id) => {
    const { tasks } = this.state
    tasks.map(todo => {
      if(todo.id === id) {
        todo.task = task
      }
    })
    this.setState({tasks})
  }
  
  searchTask = (taskName) => {
    this.setState({searchTaskValue: taskName})
  }

  completeTask = (id) => {
    const {completedTask, tasks} = this.state
    const completed = tasks.filter(task => task.id === id)
    
    completedTask.push(...completed)
    this.setState({
      completedTask
    })
  }
  
  render() {
    const {tasks, searchTaskValue, completedTask} = this.state
    
    const calculateCompletedTask = (completedTask.length / tasks.length)* 100 ;
    const percentage = calculateCompletedTask.toFixed(0)
    // console.log(`${percentage}%`)
    
    // get todays date
    const d = new Date()
    const weekDay = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'];
    const months = ['Jan', 'Feb', 'Mar', 'Apr', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
    const day = weekDay[d.getDay()]
    const month = months[d.getMonth()]
    const date = d.getDate()
    const year = d.getFullYear()
    
    const searchFilter = tasks
                      .filter(todo => 
                       todo.task.toLowerCase().includes(searchTaskValue.toLowerCase()) 
                       || 
                       todo.type.toLowerCase().includes(searchTaskValue.toLowerCase()))
    
    
    return (
      <div id="app">

        <header>
          <div className="date">
            <TodaysDate day={day} month={month} date={date} year={year} />
          </div>
          <div className="type-of-tasks">
            <PersonalTask tasks={tasks} />
            <BusinessTask tasks={tasks} />
          </div>
          <div className="task-completion">
            <span>{percentage === 'NaN' ? 0 : percentage }% done</span>
          </div>
        </header>
        
        {tasks.length > 1 && <SearchTask searchTask={this.searchTask} />}
        
        <ul>
          {
            searchFilter.map(todo => 
            <TodoTask key={todo.id}
              {...todo}
              deleteTask={this.deleteTask}
              saveEditTask={this.saveEditTask}
              completeTask={this.completeTask}
              />)
          }
        </ul>
        
        <AddTaskForm addTask={this.addTask}/>
        
      </div>
    )
  }
}

// filter list to check type & retrieve length
const TypeCount = (list, type) => <p>{list.filter(l => l.type == type).length} <span>{type}</span></p>;

const PersonalTask = ({tasks}) => TypeCount(tasks, 'Personal');
const BusinessTask = ({tasks}) => TypeCount(tasks, 'Business');

const TodaysDate = ({day, month, date, year}) => <p>{day} <span>{month} {date}, {year}</span></p>


class SearchTask extends React.Component {
  
  searchTask = () => {
    const searchValue = this.searchInput.value
    const {searchTask} = this.props
    searchTask(searchValue);
  }
  
  render() {
    return (
      <div id="search-field">
        <input 
          type="text"
          placeholder="Search Task"
          onChange={this.searchTask}
          ref={input => this.searchInput = input} />
        <i className="fa fa-search"></i>
      </div>
    )
  }
}

class TodoTask extends React.Component{
  constructor(props) {
    super(props)
    this.state = {
      isEdit: false,
      isComplete: false
    }
  }
  
  deleteTask = () => {
    const { id , deleteTask } = this.props
    deleteTask(id)
  }
  
  editTask = () => {
    this.setState({
      isEdit: true
    })
  }
  
  completeTask = () => {
    const {completeTask, id} = this.props
    completeTask(id)
    this.setState({
      isComplete: true
    })
  }
  
  saveEditTask = (e) => {
    e.preventDefault()
    
    const {id} = this.props
    const {saveEditTask} = this.props
    
    if(!this.editInput.value) {
      null
    }
    else {
      saveEditTask(this.editInput.value, id)
      this.setState({
        isEdit: false
      })
    }
    
  }
  

  render() {
    const {task, type} = this.props
    const {isEdit, isComplete} = this.state
    const disableBtn = isComplete && 'disable-btn'
    
    return (
        <li>        
          {
          isEdit ?
            <form
              id="edit-task-form"
              onSubmit={this.saveEditTask}>
              <input 
                defaultValue={task}
                ref={editInput => this.editInput = editInput} 
                />
              <button>Save</button>
            </form>
            :
            <div>
            <p 
              className={isComplete && 'completed'}>
              {task} <span>{type}</span>
            </p>
              <button 
                className='delete-btn'
                onClick={this.deleteTask}>
                Delete
            </button>
              <button
                className={disableBtn}
                disabled={isComplete}
                onClick={this.editTask}>
                Edit
            </button>
              <button
                className={disableBtn}
                disabled={isComplete}
                onClick={this.completeTask}>
                Complete
            </button>
            </div>
        }
        </li>
    )
  }
}

class AddTaskForm extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      selected: ''
    }
  }
  
  handleSelectChange = (e) => {
    this.setState({selected: e.target.value})
  }
  
  addTask = (e) => {
    const {addTask} = this.props
    
    e.preventDefault()
    
    if(!this.taskInput.value || !this.state.selected) {
      null
    }
    else {
      const date = new Date()
      const id = date.getTime()
      const type = this.state.selected
      addTask(this.taskInput.value, id, type)  
      this.taskInput.value = ''
      this.setState({selected: ''})
    }
  }
  
  render() {
    const { selected } = this.state
    return (
      <form id="add-task-form" onSubmit={this.addTask}>
        
          <input 
            id="task"
            type="text"
            placeholder="Add a task"
            ref={taskInput => this.taskInput = taskInput}/>
        
    
        <select
          value={selected}
          onChange={this.handleSelectChange}
          // ref={select => this.selectOption = select}
          >
          <option value="">Type</option>
          <option value="Personal">Personal</option>
          <option value="Business">Business</option>
        </select>
        <button>Add</button>
      </form>
    )
  }
}


const root = document.getElementById('root')
ReactDOM.render(<App />, root)
            
          
!
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