mirror of
https://github.com/sergeyyarkov/todo-app.git
synced 2026-10-11 10:29:14 +03:00
todo create & update cache
This commit is contained in:
parent
741dd99a0b
commit
cf21c291fc
10
client/src/apollo/mutations/categories/createCategory.js
Normal file
10
client/src/apollo/mutations/categories/createCategory.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import gql from 'graphql-tag'
|
||||||
|
|
||||||
|
const CREATE_CATEGORY = gql`
|
||||||
|
mutation createCategory($title: String!) {
|
||||||
|
createCategory(title: $title) {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
export default CREATE_CATEGORY
|
||||||
@ -6,7 +6,6 @@ const CREATE_TODO = gql`
|
|||||||
id
|
id
|
||||||
title
|
title
|
||||||
description
|
description
|
||||||
status
|
|
||||||
deadline
|
deadline
|
||||||
category {
|
category {
|
||||||
id
|
id
|
||||||
|
|||||||
18
client/src/apollo/mutations/todos/deleteTodo.js
Normal file
18
client/src/apollo/mutations/todos/deleteTodo.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import gql from 'graphql-tag'
|
||||||
|
|
||||||
|
const DELETE_TODO = gql`
|
||||||
|
mutation DeleteTodo($id: ID!) {
|
||||||
|
deleteTodo(id: $id) {
|
||||||
|
id
|
||||||
|
title
|
||||||
|
description
|
||||||
|
deadline
|
||||||
|
category {
|
||||||
|
id
|
||||||
|
title
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
export default DELETE_TODO
|
||||||
18
client/src/apollo/mutations/todos/updateTodo.js
Normal file
18
client/src/apollo/mutations/todos/updateTodo.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import gql from 'graphql-tag';
|
||||||
|
|
||||||
|
const UPDATE_TODO = gql`
|
||||||
|
mutation UpdateTodo($id: ID! $title: String! $description: String! $deadline: String! $categoryId: String!) {
|
||||||
|
updateTodo(id: $id title: $title description: $description deadline: $deadline categoryId: $categoryId) {
|
||||||
|
id
|
||||||
|
title
|
||||||
|
description
|
||||||
|
deadline
|
||||||
|
category {
|
||||||
|
id
|
||||||
|
title
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
export default UPDATE_TODO
|
||||||
@ -6,7 +6,6 @@ const GET_TODOS = gql`
|
|||||||
id
|
id
|
||||||
title
|
title
|
||||||
description
|
description
|
||||||
status
|
|
||||||
deadline
|
deadline
|
||||||
category {
|
category {
|
||||||
id
|
id
|
||||||
|
|||||||
@ -8,7 +8,6 @@ query getTodosByCategory($id: ID!) {
|
|||||||
id
|
id
|
||||||
title
|
title
|
||||||
description
|
description
|
||||||
status
|
|
||||||
deadline
|
deadline
|
||||||
category {
|
category {
|
||||||
id
|
id
|
||||||
|
|||||||
@ -2,12 +2,35 @@ import React from 'react';
|
|||||||
import useStyles from '../styles'
|
import useStyles from '../styles'
|
||||||
import WarningIcon from '@material-ui/icons/Warning';
|
import WarningIcon from '@material-ui/icons/Warning';
|
||||||
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_TODOS from '../../../apollo/queries/todos/todos'
|
||||||
|
import DELETE_TODO from '../../../apollo/mutations/todos/deleteTodo'
|
||||||
|
|
||||||
const DeleteTodoDialog = ({ isOpenDialog, handleCloseDialog, selectedTodo }) => {
|
const DeleteTodoDialog = ({ isOpenDialog, handleCloseDialog, selectedTodo }) => {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
|
|
||||||
|
const [deleteTodo] = useMutation(DELETE_TODO, {
|
||||||
|
update(cache, { data: { deleteTodo } }) {
|
||||||
|
const { todos } = cache.readQuery({ query: GET_TODOS })
|
||||||
|
cache.writeQuery({
|
||||||
|
query: GET_TODOS,
|
||||||
|
data: { todos: todos.filter(todo => todo.id !== deleteTodo.id) }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const handleClickDelete = () => {
|
const handleClickDelete = () => {
|
||||||
console.log('Request on delete todo:', selectedTodo.id, selectedTodo.title)
|
try {
|
||||||
|
deleteTodo({
|
||||||
|
variables: {
|
||||||
|
id: selectedTodo.id
|
||||||
|
}
|
||||||
|
})
|
||||||
handleCloseDialog()
|
handleCloseDialog()
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -1,26 +1,37 @@
|
|||||||
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 CategoryIcon from '@material-ui/icons/Category';
|
import CategoryIcon from '@material-ui/icons/Category';
|
||||||
|
|
||||||
|
import CREATE_CATEGORY from '../../../apollo/mutations/categories/createCategory'
|
||||||
|
|
||||||
const CreateCategoryModal = ({ isModalOpen, handleCloseModal }) => {
|
const CreateCategoryModal = ({ isModalOpen, handleCloseModal }) => {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
|
|
||||||
|
// create catehory mutation
|
||||||
|
const [updateTodo] = useMutation(CREATE_CATEGORY)
|
||||||
|
|
||||||
const [fieldsData, setFieldsData] = React.useState({
|
const [fieldsData, setFieldsData] = React.useState({
|
||||||
title: '',
|
title: '',
|
||||||
})
|
})
|
||||||
|
|
||||||
const handleFormSubmit = e => {
|
const handleFormSubmit = e => {
|
||||||
|
try {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
const elements = e.target.elements
|
const elements = e.target.elements
|
||||||
const data = {
|
updateTodo({
|
||||||
title: elements.title.value,
|
variables: {
|
||||||
|
title: elements.title.value
|
||||||
}
|
}
|
||||||
|
})
|
||||||
handleCloseModal()
|
handleCloseModal()
|
||||||
setFieldsData({
|
setFieldsData({
|
||||||
title: '',
|
title: '',
|
||||||
})
|
})
|
||||||
console.log('Request on create category:', data)
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleFieldChange = e => {
|
const handleFieldChange = e => {
|
||||||
|
|||||||
@ -3,24 +3,35 @@ import useStyles from '../styles'
|
|||||||
import { Dialog, DialogContent, DialogTitle, DialogActions, Button, TextField, Select, MenuItem, InputLabel, FormControl } from '@material-ui/core'
|
import { Dialog, DialogContent, DialogTitle, DialogActions, Button, TextField, Select, MenuItem, InputLabel, FormControl } from '@material-ui/core'
|
||||||
import EditIcon from '@material-ui/icons/Edit'
|
import EditIcon from '@material-ui/icons/Edit'
|
||||||
|
|
||||||
import { Query } from 'react-apollo'
|
// import { Query } from 'react-apollo'
|
||||||
import { useQuery } from '@apollo/react-hooks'
|
import { useQuery, useMutation } from '@apollo/react-hooks'
|
||||||
|
|
||||||
import GET_CATEGORIES from '../../../apollo/queries/categories/categories'
|
import GET_CATEGORIES from '../../../apollo/queries/categories/categories'
|
||||||
|
import UPDATE_TODO from '../../../apollo/mutations/todos/updateTodo';
|
||||||
|
|
||||||
const UpdateTodoModal = ({ isModalOpen, handleCloseModal, selectedTodo, setSelectedTodo }) => {
|
const UpdateTodoModal = ({ isModalOpen, handleCloseModal, selectedTodo, setSelectedTodo }) => {
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
|
|
||||||
|
// update todo mutation
|
||||||
|
const [updateTodo] = useMutation(UPDATE_TODO)
|
||||||
|
|
||||||
const handleFormSubmit = e => {
|
const handleFormSubmit = e => {
|
||||||
|
try {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
const elements = e.target.elements
|
const elements = e.target.elements
|
||||||
const data = {
|
updateTodo({
|
||||||
|
variables: {
|
||||||
id: selectedTodo.id,
|
id: selectedTodo.id,
|
||||||
title: elements.title.value,
|
title: elements.title.value,
|
||||||
description: elements.description.value,
|
description: elements.description.value,
|
||||||
categoryId: elements.category.value,
|
deadline: elements.deadline.value,
|
||||||
deadline: elements.deadline.value
|
categoryId: elements.category.value
|
||||||
}
|
}
|
||||||
|
})
|
||||||
handleCloseModal()
|
handleCloseModal()
|
||||||
console.log('Request on update todo:', data)
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleFieldChange = e => {
|
const handleFieldChange = e => {
|
||||||
|
|||||||
@ -82,7 +82,7 @@ const Todos = ({ data, fromTrashContainer }) => {
|
|||||||
{todo.category.title}
|
{todo.category.title}
|
||||||
<span className={classes.cardAlarm}>
|
<span className={classes.cardAlarm}>
|
||||||
<AlarmIcon className={classes.alarmIcon} color='action' />
|
<AlarmIcon className={classes.alarmIcon} color='action' />
|
||||||
{todo.deadline}
|
{new Date(todo.deadline).toLocaleString()}
|
||||||
</span>
|
</span>
|
||||||
</Typography>
|
</Typography>
|
||||||
<Typography className={classes.cardHeading} variant="h5" component="h2">
|
<Typography className={classes.cardHeading} variant="h5" component="h2">
|
||||||
|
|||||||
@ -4,24 +4,19 @@ import UpdateCategoryModal from '../../components/Modals/Category/UpdateCategory
|
|||||||
import DeleteCategoryDialog from '../../components/Dialogs/Category/DeleteCategoryDialog'
|
import DeleteCategoryDialog from '../../components/Dialogs/Category/DeleteCategoryDialog'
|
||||||
import Fab from '../../components/Fab/Fab'
|
import Fab from '../../components/Fab/Fab'
|
||||||
import { Grid, Typography, LinearProgress } from '@material-ui/core'
|
import { Grid, Typography, LinearProgress } from '@material-ui/core'
|
||||||
|
|
||||||
import categories from '../../db/categories.json'
|
|
||||||
|
|
||||||
import { Query } from 'react-apollo'
|
import { Query } from 'react-apollo'
|
||||||
import { useParams } from 'react-router-dom'
|
import { useParams } from 'react-router-dom'
|
||||||
|
|
||||||
import GET_TODOS_BY_CATEGORY from '../../apollo/queries/todos/todosByCategory'
|
import GET_TODOS_BY_CATEGORY from '../../apollo/queries/todos/todosByCategory'
|
||||||
|
|
||||||
const Category = () => {
|
const Category = () => {
|
||||||
const findCategoryById = id => categories.find(category => category._id.$oid === id)
|
|
||||||
|
|
||||||
const { id } = useParams()
|
const { id } = useParams()
|
||||||
const [isModalOpen, setIsModalOpen] = React.useState(false)
|
const [isModalOpen, setIsModalOpen] = React.useState(false)
|
||||||
const [isOpenDialog, setIsOpenDialog] = React.useState(false)
|
const [isOpenDialog, setIsOpenDialog] = React.useState(false)
|
||||||
const [selectedCategory, setSelectedCategory] = React.useState({ id: '', title: '' })
|
const [selectedCategory, setSelectedCategory] = React.useState({ id: '', title: '' })
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
setSelectedCategory({ id, title: findCategoryById(id).title })
|
setSelectedCategory({ id, title: '' })
|
||||||
}, [id]);
|
}, [id]);
|
||||||
|
|
||||||
const handleOpenModal = () => setIsModalOpen(true)
|
const handleOpenModal = () => setIsModalOpen(true)
|
||||||
@ -35,6 +30,7 @@ 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} />
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user