mirror of
https://github.com/sergeyyarkov/todo-app.git
synced 2026-10-11 10:29:14 +03:00
delete category cache
This commit is contained in:
parent
cf21c291fc
commit
a7a895648f
@ -4,6 +4,7 @@ const CREATE_CATEGORY = gql`
|
|||||||
mutation createCategory($title: String!) {
|
mutation createCategory($title: String!) {
|
||||||
createCategory(title: $title) {
|
createCategory(title: $title) {
|
||||||
id
|
id
|
||||||
|
title
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|||||||
11
client/src/apollo/mutations/categories/deleteCategoty.js
Normal file
11
client/src/apollo/mutations/categories/deleteCategoty.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import gql from 'graphql-tag'
|
||||||
|
|
||||||
|
const DELETE_CATEGORY = gql`
|
||||||
|
mutation deleteCategory($id: ID!) {
|
||||||
|
deleteCategory(id: $id) {
|
||||||
|
id
|
||||||
|
title
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
export default DELETE_CATEGORY
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
import gql from 'graphql-tag'
|
||||||
|
|
||||||
|
const UPDATE_CATEGORY = gql`
|
||||||
|
mutation updateCategory($id: ID! $title: String!) {
|
||||||
|
updateCategory(id: $id title: $title) {
|
||||||
|
id
|
||||||
|
title
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
export default UPDATE_CATEGORY
|
||||||
@ -1,15 +1,43 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { useHistory } from "react-router-dom";
|
||||||
import useStyles from '../styles'
|
import useStyles from '../styles'
|
||||||
import WarningIcon from '@material-ui/icons/Warning';
|
import WarningIcon from '@material-ui/icons/Warning';
|
||||||
import { useParams } from 'react-router-dom'
|
import { useParams } from 'react-router-dom'
|
||||||
import { Button, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle } from '@material-ui/core'
|
import { Button, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle } from '@material-ui/core'
|
||||||
|
import { useMutation } from '@apollo/react-hooks'
|
||||||
|
|
||||||
|
import GET_CATEGORIES from '../../../apollo/queries/categories/categories'
|
||||||
|
import DELETE_CATEGORY from '../../../apollo/mutations/categories/deleteCategoty'
|
||||||
|
|
||||||
const DeleteCategoryDialog = ({ isOpenDialog, handleCloseDialog }) => {
|
const DeleteCategoryDialog = ({ isOpenDialog, handleCloseDialog }) => {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
|
const history = useHistory();
|
||||||
const { id } = useParams()
|
const { id } = useParams()
|
||||||
|
|
||||||
|
const [deleteCategory] = useMutation(DELETE_CATEGORY, {
|
||||||
|
update(cache, { data: { deleteCategory } }) {
|
||||||
|
const { categories } = cache.readQuery({ query: GET_CATEGORIES })
|
||||||
|
console.log(categories, deleteCategory)
|
||||||
|
cache.writeQuery({
|
||||||
|
query: GET_CATEGORIES,
|
||||||
|
data: { categories: categories.filter(category => category.id !== deleteCategory.id) }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const handleClickDelete = () => {
|
const handleClickDelete = () => {
|
||||||
console.log('Request on delete category:', id)
|
try {
|
||||||
|
deleteCategory({
|
||||||
|
variables: {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
})
|
||||||
handleCloseDialog()
|
handleCloseDialog()
|
||||||
|
history.push("/");
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -5,12 +5,21 @@ import { useMutation } from '@apollo/react-hooks'
|
|||||||
import CategoryIcon from '@material-ui/icons/Category';
|
import CategoryIcon from '@material-ui/icons/Category';
|
||||||
|
|
||||||
import CREATE_CATEGORY from '../../../apollo/mutations/categories/createCategory'
|
import CREATE_CATEGORY from '../../../apollo/mutations/categories/createCategory'
|
||||||
|
import GET_CATEGORIES from '../../../apollo/queries/categories/categories';
|
||||||
|
|
||||||
const CreateCategoryModal = ({ isModalOpen, handleCloseModal }) => {
|
const CreateCategoryModal = ({ isModalOpen, handleCloseModal }) => {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
|
|
||||||
// create catehory mutation
|
// create category mutation
|
||||||
const [updateTodo] = useMutation(CREATE_CATEGORY)
|
const [createCategory] = useMutation(CREATE_CATEGORY, {
|
||||||
|
update(cache, { data: { createCategory } }) {
|
||||||
|
const { categories } = cache.readQuery({ query: GET_CATEGORIES })
|
||||||
|
cache.writeQuery({
|
||||||
|
query: GET_CATEGORIES,
|
||||||
|
data: { categories: categories.concat([createCategory]) }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const [fieldsData, setFieldsData] = React.useState({
|
const [fieldsData, setFieldsData] = React.useState({
|
||||||
title: '',
|
title: '',
|
||||||
@ -20,7 +29,7 @@ const CreateCategoryModal = ({ isModalOpen, handleCloseModal }) => {
|
|||||||
try {
|
try {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
const elements = e.target.elements
|
const elements = e.target.elements
|
||||||
updateTodo({
|
createCategory({
|
||||||
variables: {
|
variables: {
|
||||||
title: elements.title.value
|
title: elements.title.value
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,20 +1,31 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import useStyles from '../styles'
|
import useStyles from '../styles'
|
||||||
import { Dialog, DialogContent, DialogTitle, DialogActions, Button, TextField } from '@material-ui/core'
|
import { Dialog, DialogContent, DialogTitle, DialogActions, Button, TextField } from '@material-ui/core'
|
||||||
|
import { useMutation } from '@apollo/react-hooks'
|
||||||
import EditIcon from '@material-ui/icons/Edit'
|
import EditIcon from '@material-ui/icons/Edit'
|
||||||
|
|
||||||
|
import UPDATE_CATEGORY from '../../../apollo/mutations/categories/updateCategory'
|
||||||
|
|
||||||
const UpdateCategoryModal = ({ isModalOpen, handleCloseModal, selectedCategory, setSelectedCategory }) => {
|
const UpdateCategoryModal = ({ isModalOpen, handleCloseModal, selectedCategory, setSelectedCategory }) => {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
|
|
||||||
|
// update category mutation
|
||||||
|
const [updateCategory] = useMutation(UPDATE_CATEGORY)
|
||||||
|
|
||||||
const handleFormSubmit = e => {
|
const handleFormSubmit = e => {
|
||||||
|
try {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
const elements = e.target.elements
|
const elements = e.target.elements
|
||||||
const data = {
|
updateCategory({
|
||||||
|
variables: {
|
||||||
id: selectedCategory.id,
|
id: selectedCategory.id,
|
||||||
title: elements.title.value
|
title: elements.title.value
|
||||||
}
|
}
|
||||||
|
})
|
||||||
handleCloseModal()
|
handleCloseModal()
|
||||||
console.log('Request on update category:', data)
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleFieldChange = e => {
|
const handleFieldChange = e => {
|
||||||
|
|||||||
@ -30,7 +30,6 @@ const Category = () => {
|
|||||||
{({ loading, error, data }) => {
|
{({ loading, error, data }) => {
|
||||||
if (loading) return <LinearProgress />
|
if (loading) return <LinearProgress />
|
||||||
if (error) return `Error! ${error.message}`;
|
if (error) return `Error! ${error.message}`;
|
||||||
if (data.category.todos <= 0) return 'В этой категории задач пока что нету'
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<UpdateCategoryModal isModalOpen={isModalOpen} handleCloseModal={handleCloseModal} selectedCategory={selectedCategory} setSelectedCategory={setSelectedCategory} />
|
<UpdateCategoryModal isModalOpen={isModalOpen} handleCloseModal={handleCloseModal} selectedCategory={selectedCategory} setSelectedCategory={setSelectedCategory} />
|
||||||
@ -43,8 +42,7 @@ const Category = () => {
|
|||||||
Сортируйте свои повседневные дела используя категории.
|
Сортируйте свои повседневные дела используя категории.
|
||||||
</Typography>
|
</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
{/* Здесь берем значение slug из query и передаем полученные данные в prop data !!!! */}
|
{data.category.todos <= 0 ? <Grid item xs={12}><Typography>В этой категории задач пока что нету</Typography></Grid> : <Todos data={data.category.todos} />}
|
||||||
<Todos data={data.category.todos} />
|
|
||||||
</Grid>
|
</Grid>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user