-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mobx models #8
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,65 @@ | ||
import {FC, useCallback, useEffect, useState} from "react"; | ||
import {FC, useEffect, useState} from "react"; | ||
import {useNavigate, useParams} from "react-router-dom"; | ||
import {Item} from "../../../types/Item"; | ||
import {api} from "../../../api"; | ||
import {Button, Container, Form} from "react-bootstrap"; | ||
import {useLoader} from "../../../hooks/useLoader"; | ||
import {useLoaderModel} from "../../../hooks/useLoaderModel"; | ||
import {ConfirmModal} from "../../common/ConfirmModal/ConfirmModal"; | ||
import {observer} from "mobx-react-lite"; | ||
import {ItemModel} from "../../../models/ItemModel"; | ||
import {useQuery} from "@tanstack/react-query"; | ||
|
||
export const ItemPage: FC = () => { | ||
const { id } = useParams<{ id: string }>(); | ||
const navigate = useNavigate(); | ||
|
||
const loader = useLoader(); | ||
|
||
// Не использовать локальный стейт для демонстрации | ||
const [item, setItem] = useState<Item | null>(null); | ||
const [error, setError] = useState<string | null>(null); | ||
const [removeConfirmationOpened, setRemoveConfirmationOpened] = useState(false); | ||
type ItemResponseError = {error: string}; | ||
|
||
const fetch = useCallback(() => { | ||
const hideLoader = loader.show(); | ||
// Специально не передал пропсом | ||
// В демо тоже должен лежать отдельно в стейте (не в стейте списка для главной страницы) | ||
// что бы проверить инвалидацию при переходе между страницами | ||
api.getItem(Number(id)) | ||
.then((response) => { | ||
if ('error' in response) { | ||
setError(response.error); | ||
return; | ||
} | ||
function isErrorItem(data: Item | ItemResponseError | undefined): data is ItemResponseError { | ||
return Boolean(data && 'error' in data); | ||
} | ||
|
||
setItem(response); | ||
}) | ||
.finally(hideLoader); | ||
}, []); | ||
type ViewProps = { | ||
itemModel: ItemModel; | ||
refetchItem(): void; | ||
} | ||
|
||
useEffect(() => { | ||
fetch(); | ||
}, []); | ||
|
||
function renderContent() { | ||
if (error) { | ||
return ( | ||
<div style={{ color: 'red' }}>{error}</div> | ||
) | ||
} | ||
const ItemPageView: FC<ViewProps> = observer((props) => { | ||
const { itemModel } = props; | ||
const navigate = useNavigate(); | ||
|
||
if (!item) { | ||
return null; | ||
} | ||
const loader = useLoaderModel(); | ||
|
||
return ( | ||
return ( | ||
<Container className='mt-3'> | ||
<h1 className='mb-4'>Item</h1> | ||
<Form> | ||
<Form.Group className="mb-3"> | ||
<Form.Check | ||
type="checkbox" | ||
label="Checked" | ||
checked={item.checked} | ||
onChange={() => setItem({ ...item, checked: !item.checked })} | ||
checked={itemModel.checked} | ||
onChange={() => itemModel.toggle()} | ||
/> | ||
</Form.Group> | ||
<Form.Group className="mb-3"> | ||
<Form.Label>Text</Form.Label> | ||
<Form.Control | ||
value={item.text} | ||
onChange={(e) => setItem({ ...item, text: e.target.value })} | ||
value={itemModel.text} | ||
onChange={(e) => itemModel.text = e.target.value} | ||
/> | ||
</Form.Group> | ||
</Form> | ||
); | ||
} | ||
|
||
return ( | ||
<Container className='mt-3'> | ||
<h1 className='mb-4'>Item</h1> | ||
{renderContent()} | ||
<div> | ||
<Button | ||
className='m-1' | ||
onClick={() => { | ||
if (!item) return; | ||
const hideLoader = loader.show(); | ||
api.updateItem(item) | ||
.then(fetch) | ||
api.updateItem(itemModel.toJs()) | ||
.then(props.refetchItem) | ||
.finally(hideLoader); | ||
}} | ||
>Save</Button> | ||
<Button | ||
className='m-1' | ||
variant='danger' | ||
onClick={() => setRemoveConfirmationOpened(true)} | ||
onClick={() => itemModel.isRemoving = true} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут тоже надо менять состояние стора через экшн. |
||
>Remove</Button> | ||
<Button className='m-1' href='/' variant='secondary'>Back</Button> | ||
</div> | ||
|
@@ -100,17 +71,57 @@ export const ItemPage: FC = () => { | |
<li>Come back here and check that data will update</li> | ||
</ul> | ||
<ConfirmModal | ||
show={removeConfirmationOpened} | ||
show={itemModel.isRemoving} | ||
title='Remove item' | ||
onApply={() => { | ||
if (!item) return; | ||
const hideLoader = loader.show(); | ||
api.removeItem(item.id) | ||
api.removeItem(itemModel.id) | ||
.then(() => navigate('/')) | ||
.finally(hideLoader); | ||
}} | ||
onCancel={() => setRemoveConfirmationOpened(false)} | ||
onCancel={() => itemModel.isRemoving = false} | ||
/> | ||
</Container> | ||
) | ||
}; | ||
}); | ||
|
||
export const ItemPage: FC = observer(() => { | ||
const { id } = useParams<{ id: string }>(); | ||
const loader = useLoaderModel(); | ||
|
||
const {data, refetch, isFetching, isError: isItemError} = useQuery({ | ||
queryKey: ['item', id], | ||
queryFn: () => api.getItem(Number(id)), | ||
}); | ||
|
||
useEffect(() => { | ||
if (isFetching) { | ||
return loader.show(); | ||
} | ||
}, [isFetching, loader]); | ||
|
||
const [itemModel, setItemModel] = useState<ItemModel | null>(null); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
if(isItemError || isErrorItem(data)) { | ||
setError((data as ItemResponseError)?.error || 'Item data loading error'); | ||
} else if(data) { | ||
setItemModel(new ItemModel({ item: data })) | ||
} | ||
},[data, isItemError]) | ||
|
||
if (error) { | ||
return ( | ||
<div style={{ color: 'red' }}>{error}</div> | ||
) | ||
} | ||
|
||
if (!itemModel) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<ItemPageView itemModel={itemModel} refetchItem={refetch}/> | ||
) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я понимаю, что код демонстративный, но тут точно надо хэндлить две асинхронных операции одновременно: из There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вот эта как раз часть приближена к реальному использованию. Обсудим голосом, это очень важно. Спасибо! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это не будет работать корректно, потому что вызвано вне экшна. Надо или сделать экшн и вызвать его так
itemModel.setText(e.target.value)
, или обернуть код из onChange вrunInAction
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Разобрались, что работает из-за того, что
text
имеет сеттер