Skip to content

Commit

Permalink
feat: add delete to the course title
Browse files Browse the repository at this point in the history
  • Loading branch information
marianzburlea committed Jul 17, 2019
1 parent 2adbe3b commit a9e180d
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 31 deletions.
57 changes: 29 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1",
"styled-components": "^4.3.0"
"styled-components": "^4.3.2"
},
"scripts": {
"start": "react-scripts start",
Expand Down
1 change: 1 addition & 0 deletions src/component/app/app.style.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export const StyledApp = styled.div`
display: grid;
grid-template-columns: 2fr 1fr;
padding: 1rem;
grid-gap: 1rem;
`
18 changes: 18 additions & 0 deletions src/component/app/course-item/course-item.component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react'
import { StyledCourseItem } from './course-item.style';

const CourseItem = ({ id, title, deleteItem, editItem }) => {
const handleDeleteItem = () => {
deleteItem && deleteItem(id)
}

return (
<StyledCourseItem>
<div>{title}</div>
<button onClick={editItem}>💾</button>
<button onClick={handleDeleteItem}>&times;</button>
</StyledCourseItem>
)
}

export default CourseItem
7 changes: 7 additions & 0 deletions src/component/app/course-item/course-item.style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import styled from 'styled-components'

export const StyledCourseItem = styled.div`
display: grid;
grid-template-columns: 1fr auto auto;
`

1 change: 1 addition & 0 deletions src/component/app/course-item/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './course-item.component'
30 changes: 28 additions & 2 deletions src/component/course-list/course-list.component.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import React, { useEffect, useReducer } from 'react'
import { db } from '../data/firebase'
import CourseItem from '../app/course-item';

const ADD_COURSE = '[Course list] -> ADD_COURSE'
const REMOVE_COURSE = '[Course list] -> REMOVE_COURSE'
const addCourseAction = course => ({
type: ADD_COURSE,
course,
})

const removeCourseAction = course => ({
type: REMOVE_COURSE,
course,
})

const courseListReducer = (state = [], action) => {
switch(action.type) {
case ADD_COURSE:
return [...state, { title: action.course.title, id: action.course.id }]
case REMOVE_COURSE:
return state.filter(course => course.id !== action.course.id)
default:
return state
}
Expand All @@ -25,23 +34,40 @@ const CourseList = () => {
.onSnapshot(snapList => {
snapList.docChanges().forEach(change => {
const title = change.doc.data()
console.log(title)
console.log(title, change.type)
if (change.type === 'added') {
dispatch(addCourseAction({
title: title.courseTitle,
id: change.doc.id,
}))
}
else if (change.type === 'removed') {
dispatch(removeCourseAction({
id: change.doc.id,
}))
}
})
})
}, [])

const deleteItem = id => {
console.log('deleteItem', id)
db
.collection('course')
.doc(id)
.delete()
.then(aaa => {
console.log(`Item with id: ${id} is no longer with us`)
})
.catch(message => console.log(`Weird message!`, message))
}

return (
<div>
<h1>
Course List
</h1>
{courseList.map(({ title, id }) => <div key={id}>{title}</div>)}
{courseList.map(course => <CourseItem key={course.id} {...{...course, deleteItem}} />)}
</div>
)
}
Expand Down

0 comments on commit a9e180d

Please sign in to comment.