mirror of
https://github.com/rjNemo/react_template
synced 2026-06-06 07:36:42 +00:00
createTodo mutation
This commit is contained in:
parent
61201094db
commit
b72c13100a
3 changed files with 61 additions and 12 deletions
|
|
@ -1,29 +1,55 @@
|
||||||
import React, { FC } from 'react';
|
import React, { ChangeEvent, FC, FormEvent, useState } from 'react';
|
||||||
|
|
||||||
import TodoList from './TodoList';
|
import TodoList from './TodoList';
|
||||||
import { Button } from '../../components/button';
|
import { Button } from '../../components/button';
|
||||||
import { Container } from '../../components/container';
|
import { Container } from '../../components/container';
|
||||||
|
|
||||||
import { useListTodos } from '../../core/services/todo';
|
import { useListTodos } from '../../core/services/listTodo';
|
||||||
|
import { useCreateTodo } from '../../core/services/createTodo';
|
||||||
|
|
||||||
export const Home: FC = () => {
|
export const Home: FC = () => {
|
||||||
|
const [todoTitle, setTodoTitle] = useState<string>('');
|
||||||
const { loading, error, data } = useListTodos();
|
const { loading, error, data } = useListTodos();
|
||||||
|
const { createTodo } = useCreateTodo(todoTitle);
|
||||||
|
|
||||||
if (error) {
|
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||||
return <p>Sorry...</p>;
|
setTodoTitle(() => e.target.value);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
createTodo();
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container>
|
<Container>
|
||||||
<h1>Your tasks</h1>
|
<h1>Your tasks</h1>
|
||||||
<p>Hi there!</p>
|
<p>Hi there!</p>
|
||||||
{!loading && !!data && <TodoList todos={data.listTodos} />}
|
{error ? (
|
||||||
<Button primary onClick={() => console.log('Clicked Normal Button')}>
|
<h3>{error.message}. Sorry...</h3>
|
||||||
Normal Button
|
) : (
|
||||||
</Button>
|
!loading &&
|
||||||
<Button primary onClick={() => console.log('Clicked Primary Button')}>
|
!!data && (
|
||||||
Primary Button
|
<>
|
||||||
</Button>
|
<TodoList todos={data.listTodos} />
|
||||||
|
<form onSubmit={handleSubmit}>
|
||||||
|
<input type="text" value={todoTitle} onChange={handleChange} />
|
||||||
|
<Button
|
||||||
|
primary={false}
|
||||||
|
onClick={() => console.log('Clicked Normal Button')}
|
||||||
|
>
|
||||||
|
Normal Button
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
primary
|
||||||
|
onClick={() => console.log('Clicked Primary Button')}
|
||||||
|
>
|
||||||
|
Primary Button
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
)}
|
||||||
</Container>
|
</Container>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
23
src/core/services/createTodo.ts
Normal file
23
src/core/services/createTodo.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { gql, useMutation } from '@apollo/client';
|
||||||
|
|
||||||
|
import Todo from '../models/todo';
|
||||||
|
|
||||||
|
const CREATE_TODO = gql`
|
||||||
|
mutation CreateTodo($title: String!) {
|
||||||
|
createTodo(title: $title) {
|
||||||
|
todo {
|
||||||
|
todoId
|
||||||
|
title
|
||||||
|
isDone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
export const useCreateTodo = (todoTitle: string) => {
|
||||||
|
const [createTodo, result] = useMutation<
|
||||||
|
{ createTodo: Todo },
|
||||||
|
{ title: string }
|
||||||
|
>(CREATE_TODO, { variables: { title: todoTitle } });
|
||||||
|
return { createTodo, result };
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue