Skip to content

Commit aaecc39

Browse files
committed
feat(graphql): integrate graphql - task-details-page
https://github.com/martis-git/learn-frontend/issues/279
1 parent 0a22f23 commit aaecc39

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import React from 'react'
22
import { RouteComponentProps } from "react-router-dom";
3-
import {Alert, Spin } from "antd";
3+
import { Alert, Spin } from "antd";
44
import { TaskCard } from "shared/components";
5-
import { useFetch } from "shared/hooks";
5+
import { useTodoQuery } from "./query.gen";
66

77
type Props = RouteComponentProps<{
88
id: string;
99
}>;
1010

1111
const TaskDetails = (props: Props) => {
1212
const { id } = props.match.params;
13-
const { data, loading, error } = useFetch<any>(`todos/${id}`);
13+
const { data, loading, error } = useTodoQuery({ variables: { id } });
14+
const { todo } = data || {};
1415

1516
if (loading) return <Spin />;
1617
if (error) return <Alert message={error.message || String(error)} type="error" showIcon />;
17-
if (data === null) return <>Not Found</>;
18-
return <TaskCard {...data} />;
18+
if (!todo) return <>Not Found</>;
19+
return <TaskCard {...todo} />;
1920
}
2021

2122
export default TaskDetails
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/** @generated THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. */
2+
import * as Types from '../../models';
3+
4+
import { gql } from '@apollo/client';
5+
import * as Apollo from '@apollo/client';
6+
export type TodoQueryVariables = Types.Exact<{
7+
id: Types.Scalars['ID'];
8+
}>;
9+
10+
11+
export type TodoQuery = { readonly todo?: Types.Maybe<{ readonly id?: Types.Maybe<string>, readonly title?: Types.Maybe<string>, readonly user?: Types.Maybe<{ readonly id?: Types.Maybe<string>, readonly name?: Types.Maybe<string> }> }> };
12+
13+
14+
export const TodoDocument = gql`
15+
query Todo($id: ID!) {
16+
todo(id: $id) {
17+
id
18+
title
19+
user {
20+
id
21+
name
22+
}
23+
}
24+
}
25+
`;
26+
27+
/**
28+
* __useTodoQuery__
29+
*
30+
* To run a query within a React component, call `useTodoQuery` and pass it any options that fit your needs.
31+
* When your component renders, `useTodoQuery` returns an object from Apollo Client that contains loading, error, and data properties
32+
* you can use to render your UI.
33+
*
34+
* @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options;
35+
*
36+
* @example
37+
* const { data, loading, error } = useTodoQuery({
38+
* variables: {
39+
* id: // value for 'id'
40+
* },
41+
* });
42+
*/
43+
export function useTodoQuery(baseOptions?: Apollo.QueryHookOptions<TodoQuery, TodoQueryVariables>) {
44+
return Apollo.useQuery<TodoQuery, TodoQueryVariables>(TodoDocument, baseOptions);
45+
}
46+
export function useTodoLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<TodoQuery, TodoQueryVariables>) {
47+
return Apollo.useLazyQuery<TodoQuery, TodoQueryVariables>(TodoDocument, baseOptions);
48+
}
49+
export type TodoQueryHookResult = ReturnType<typeof useTodoQuery>;
50+
export type TodoLazyQueryHookResult = ReturnType<typeof useTodoLazyQuery>;
51+
export type TodoQueryResult = Apollo.QueryResult<TodoQuery, TodoQueryVariables>;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
query Todo($id: ID!) {
2+
todo(id: $id) {
3+
id
4+
title
5+
user {
6+
id
7+
name
8+
}
9+
}
10+
}

examples/graphql/src/shared/components/task-card/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ type Props = import("models").Todo;
99

1010
const ICON_STYLE = { color: '#08c' };
1111

12+
/**
13+
* @remark Вынесено в отдельную функцию, т.к. в дальнейшем логика может стать сложнее
14+
*/
1215
const getTaskStatus = (completed: boolean) => {
1316
if (completed) return 1;
1417
return 0;

0 commit comments

Comments
 (0)