Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing done #47

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
# production
/build

#
*.swp

# misc
.DS_Store
.env.local
Expand Down
28 changes: 28 additions & 0 deletions backend/todo/migrations/0003_auto_20191009_1311.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 2.2.5 on 2019-10-09 13:11

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('todo', '0002_todo_done'),
]

operations = [
migrations.AddField(
model_name='todo',
name='date',
field=models.IntegerField(default=1),
),
migrations.AddField(
model_name='todo',
name='month',
field=models.IntegerField(default=1),
),
migrations.AddField(
model_name='todo',
name='year',
field=models.IntegerField(default=2019),
),
]
3 changes: 3 additions & 0 deletions backend/todo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ class Todo(models.Model):
title = models.CharField(max_length=120)
content = models.TextField()
done = models.BooleanField(default=False)
year = models.IntegerField(default=2019)
month = models.IntegerField(default=1)
date = models.IntegerField(default=1)
11 changes: 10 additions & 1 deletion backend/todo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def index(request, id=None):
'id': todo.id,
'title': todo.title,
'content': todo.content,
'year': todo.year,
'month': todo.month,
'date': todo.date,
'done': todo.done,
}
return JsonResponse(response_dict, safe=False)
Expand All @@ -30,14 +33,20 @@ def index(request, id=None):
body = request.body.decode()
title = json.loads(body)['title']
content = json.loads(body)['content']
dueDate = json.loads(body)['dueDate']
except (KeyError, JSONDecodeError) as e:
return HttpResponseBadRequest()
todo = Todo(title=title, content=content, done=False)
todo = Todo(title=title, content=content,
year=dueDate['year'], month=dueDate['month'],
date=dueDate['date'], done=False)
todo.save()
response_dict = {
'id': todo.id,
'title': todo.title,
'content': todo.content,
'year': todo.year,
'month': todo.month,
'date': todo.date,
'done': todo.done,
}
return HttpResponse(json.dumps(response_dict), status=201)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"react-router-dom": "^5.0.1",
"react-scripts": "3.1.1",
"redux": "^4.0.4",
"redux-thunk": "^2.3.0"
"redux-thunk": "^2.3.0",
"semantic-ui-react": "^0.88.1"
},
"scripts": {
"start": "react-scripts start",
Expand Down
4 changes: 3 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import './App.css';

import TodoList from './containers/TodoList/TodoList';
import TodoCalendar from './containers/TodoCalendar/TodoCalendar';
import RealDetail from './containers/TodoList/RealDetail/RealDetail';
import NewTodo from './containers/TodoList/NewTodo/NewTodo';

Expand All @@ -14,12 +15,13 @@ function App(props) {
<div className="App" >
<Switch>
<Route path='/todos' exact render={() => <TodoList title="My TODOs!" />} />
<Route path='/calendar' exact component={TodoCalendar} />
<Route path='/todos/:id' exact component={RealDetail} />
<Route path='/new-todo' exact component={NewTodo} />
<Redirect exact from='/' to='todos' />
<Route render={() => <h1>Not Found</h1>} />
</Switch>
</div >
</div>
</ConnectedRouter>
);
}
Expand Down
20 changes: 20 additions & 0 deletions src/components/Calendar/Calendar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.cell .date {
font-size: 20px;
}

.todoTitle {
cursor: pointer;
}

.notdone {
color: blue;
}

.done {
text-decoration: line-through;
color: #adb5bd;
}

.sunday {
color: red;
}
87 changes: 87 additions & 0 deletions src/components/Calendar/Calendar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React, { Component } from 'react';
import { Table } from 'semantic-ui-react'

import './Calendar.css';

const CALENDAR_HEADER = (
<Table.Header>
<Table.Row>
<Table.HeaderCell className="sunday">Sun</Table.HeaderCell>
<Table.HeaderCell>Mon</Table.HeaderCell>
<Table.HeaderCell>Tue</Table.HeaderCell>
<Table.HeaderCell>Wed</Table.HeaderCell>
<Table.HeaderCell>Thu</Table.HeaderCell>
<Table.HeaderCell>Fri</Table.HeaderCell>
<Table.HeaderCell>Sat</Table.HeaderCell>
</Table.Row>
</Table.Header>
);

const renderCalenderBody = (dates, todos, clickDone) => {
let i = 0;
const rows = [];
for (let week=0; week<5; week++){
let day = 0; // Sunday

let row = [];
for (let day=0; day<7; day++) {
const date = dates[i];
if (date !== undefined && day === date.getDay()) {
row.push(
<Table.Cell className={`cell ${day === 0 && 'sunday'}`} key={7*week+day}>
<div className="date">{date.getDate()}</div>
{
todos.filter(todo => {
return todo.year === date.getFullYear() &&
todo.month === date.getMonth() &&
todo.date === date.getDate();
}).map(todo => {
return (
<div
key={todo.id}
className={`todoTitle ${todo.done ? 'done':'notdone'}`}
onClick={() => clickDone(todo.id)}>
{todo.title}
</div>
)
})
}
</Table.Cell>
)
i++;
} else {
row.push(<Table.Cell key={7*week+day}> </Table.Cell>)
}
}
rows.push(row);
}

return (
<Table.Body>
{rows.map((row, i) => (<Table.Row key={i}>{row}</Table.Row>))}
</Table.Body>
);
}

const renderCalendar = (dates, todos, clickDone) => (
<Table striped style={{"height": "600px", "width": "600px"}}>
{CALENDAR_HEADER}
{renderCalenderBody(dates, todos, clickDone)}
</Table>
)

const Calendar = (props) => {
const dates = [];
const year = props.year;
const month = props.month - 1;
let date = 1;
let maxDate = (new Date(year, month + 1, 0)).getDate();

for (let date=1; date<=maxDate; date++) {
dates.push(new Date(year, month, date));
}

return renderCalendar(dates, props.todos, props.clickDone);
}

export default Calendar
34 changes: 34 additions & 0 deletions src/components/Calendar/Calendar.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';
import { shallow, mount } from 'enzyme';
import Calendar from './Calendar';


describe('<Calendar />', () => {
const stubTodos=[
{id:1, title:"Go Home", year:2020, month:9, date:8, done:true},
{id:2, title:"SWPP Team Meeting", year:2020, month:9, date:9, done:true},
{id:3, title:"Take a nap", year:2020, month:9, date:9, done:false},
];
//since here, month 9 is same as October in Date.getMonth function

it('should render without errors', () => {
const component = shallow(<Calendar />);
const wrapper = component.find('Table');
expect(wrapper.length).toBe(1);
});

it('should render dates properly',()=>{
const component= mount(<Calendar year={2020} month={10} todos={stubTodos}/>);
const wrapper=component.find('.date');
expect(wrapper.length).toBe(31);
});

it('should handle clicks', () => {
const mockClickDone = jest.fn();
const component= mount(<Calendar year={2020} month={10} todos={stubTodos} clickDone={mockClickDone}/>);
const wrapper = component.find('.todoTitle.notdone');
expect(wrapper.length).toBe(1);
wrapper.simulate('click');
expect(mockClickDone).toHaveBeenCalledTimes(1);
});
});
8 changes: 7 additions & 1 deletion src/components/Todo/Todo.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@
margin-left: 1rem;
color: orange;
font-weight: 800;
}
}

.Todo .due {
flex: 1;
text-align: left;
word-break: break-all;
}
1 change: 1 addition & 0 deletions src/components/Todo/Todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Todo = (props) => {
{props.title}
</div>
{props.done && <div className="done-mark">&#x2713;</div>}
<div className="due"> due: {props.year}.{props.month+1}.{props.date} </div>
<button onClick={props.clickDone}
className={(props.done) ? "undoneButton" : "doneButton"}>
{(props.done) ? 'Undone' : 'Done'}</button>
Expand Down
10 changes: 10 additions & 0 deletions src/containers/TodoCalendar/TodoCalendar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.link{
text-align: left;
}

.header {
text-align: left;
font-size: 40px;
margin-left: 10rem;
font-weight: bold;
}
70 changes: 70 additions & 0 deletions src/containers/TodoCalendar/TodoCalendar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React, { Component } from 'react';

import { NavLink } from 'react-router-dom';

import { connect } from 'react-redux';
import { withRouter } from 'react-router';
import Calendar from '../../components/Calendar/Calendar';

import * as actionCreators from '../../store/actions/index';

import './TodoCalendar.css';

class TodoCalendar extends Component {
state = {
year: 2019,
month: 10,
}
componentDidMount() {
this.props.onGetAll();
}

handleClickPrev = () => {
this.setState({
year: this.state.month === 1 ? this.state.year - 1 : this.state.year,
month: this.state.month === 1 ? 12 : this.state.month - 1
})
}

handleClickNext = () => {
this.setState({
year: this.state.month === 12 ? this.state.year + 1 : this.state.year,
month: this.state.month === 12 ? 1 : this.state.month + 1
})
}

render() {
return (
<div>
<div className="link"><NavLink to='/todos' exact>See TodoList</NavLink></div>
<div className="header">
<button onClick={this.handleClickPrev}> prev month </button>
{this.state.year}.{this.state.month}
<button onClick={this.handleClickNext}> next month </button>
</div>
<Calendar
year={this.state.year}
month={this.state.month}
todos={this.props.storedTodos}
clickDone={this.props.onToggleTodo}/>
</div>
);
}
}

const mapStateToProps = state => {
return {
storedTodos: state.td.todos,
};
}

const mapDispatchToProps = dispatch => {
return {
onToggleTodo: (id) =>
dispatch(actionCreators.toggleTodo(id)),
onGetAll: () =>
dispatch(actionCreators.getTodos())
}
}

export default connect(mapStateToProps, mapDispatchToProps)(withRouter(TodoCalendar));
Loading