This commit is contained in:
Sergey Yarkov 2020-06-16 19:43:35 +03:00
parent f5eaf9e525
commit 248c505a97
2 changed files with 13 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import { useParams } from 'react-router-dom'
import { Button, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle } from '@material-ui/core'
import { useMutation } from '@apollo/react-hooks'
import GET_TODOS from '../../../apollo/queries/todos/todos'
import GET_CATEGORIES from '../../../apollo/queries/categories/categories'
import DELETE_CATEGORY from '../../../apollo/mutations/categories/deleteCategoty'
@ -21,6 +22,16 @@ const DeleteCategoryDialog = ({ isOpenDialog, handleCloseDialog }) => {
query: GET_CATEGORIES,
data: { categories: categories.filter(category => category.id !== deleteCategory.id) }
})
try {
const { todos } = cache.readQuery({ query: GET_TODOS })
cache.writeQuery({
query: GET_TODOS,
data: { todos: todos.filter(todo => todo.category.id !== deleteCategory.id) }
})
} catch {
return null
}
}
})

View File

@ -16,6 +16,8 @@ const categoryResolvers = {
},
deleteCategory: async (_, { id }) => {
const deletedCategory = await Category.findByIdAndRemove(id)
await Todo.deleteMany({ categoryId: id })
return deletedCategory
},
updateCategory: (_, { id, title }) => Category.findByIdAndUpdate(id, { $set: { title, updatedAt: new Date().toISOString() } }, { new: true })