Skip to content

Commit bfa5e21

Browse files
Add loading for items
1 parent 119f789 commit bfa5e21

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/components/Admin/DashboardStore/DashboardStore.hooks.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ import { IStore, StoreService } from '../../../services/admin/store';
55
interface IDashboardStoreHooks {
66
form: FormInstance<IStore>
77
items: IStore[]
8+
articlesLoading: boolean
9+
addArticleLoading: boolean
810
onAddArticle: (values: IStore) => void
911
deleteArticle: (articleId: string) => void
1012
}
1113

1214
export const useDashboardStore = (): IDashboardStoreHooks => {
1315
const [form] = Form.useForm();
1416
const [items, setItems] = useState<IStore[]>([]);
17+
const [articlesLoading, setArticlesLoading] = useState<boolean>(false);
18+
const [addArticleLoading, setAddArticleLoading] = useState<boolean>(false);
1519

1620
const onAddArticle: FormProps<IStore>['onFinish'] = useCallback(({ name, price }): void => {
17-
StoreService.addArticle({ name, price, }).then((res) => setItems((prevState) => [...prevState, res]));
21+
setAddArticleLoading(true);
22+
StoreService.addArticle({ name, price, })
23+
.then((res) => setItems((prevState) => [...prevState, res]))
24+
.finally(() => setAddArticleLoading(false));
1825
form.resetFields();
1926
}, []);
2027

@@ -25,12 +32,15 @@ export const useDashboardStore = (): IDashboardStoreHooks => {
2532
}, []);
2633

2734
useEffect(() => {
28-
StoreService.getAllArticles().then(setItems);
35+
setArticlesLoading(true);
36+
StoreService.getAllArticles().then(setItems).finally(() => setArticlesLoading(false));
2937
}, []);
3038

3139
return {
3240
form,
3341
items,
42+
articlesLoading,
43+
addArticleLoading,
3444
onAddArticle,
3545
deleteArticle,
3646
};

src/components/Admin/DashboardStore/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
22
import Navigation from '../../../pages/admin/Navigation';
3-
import { Button, Divider, Flex, Form, Input, InputNumber, Typography } from 'antd';
3+
import { Button, Divider, Flex, Form, Input, InputNumber, Skeleton, Typography } from 'antd';
44
import { formattedPrice } from '../../../lib/form';
55
import { CloseCircleOutlined } from '@ant-design/icons';
66
import { useDashboardStore } from './DashboardStore.hooks';
77

88
const DashboardStore: React.FC = () => {
9-
const { form, items, onAddArticle, deleteArticle } = useDashboardStore();
9+
const { form, items, articlesLoading, addArticleLoading, onAddArticle, deleteArticle } = useDashboardStore();
1010
return (
1111
<Navigation>
1212
<p className="text-xl mb-2">Liste des articles : </p>
@@ -18,12 +18,12 @@ const DashboardStore: React.FC = () => {
1818
<InputNumber min={0} step={0.5} placeholder="Prix"/>
1919
</Form.Item>
2020
<Form.Item>
21-
<Button type="primary" htmlType="submit" className="button">Ajouter</Button>
21+
<Button type="primary" htmlType="submit" className="button" loading={addArticleLoading}>Ajouter</Button>
2222
</Form.Item>
2323
</Form>
2424
<Divider/>
2525
<Flex gap={8} className="mt-4">
26-
{items.length > 0 ? items.map((article) => (
26+
{articlesLoading ? <Skeleton.Image active /> : items.length > 0 ? items.map((article) => (
2727
<div key={article.id} className="flex bg-white rounded-md p-2 aspect-[1/1] w-28">
2828
<Flex vertical justify="space-between" gap={8} className="w-full">
2929
<Flex align="center" justify="space-between" className="w-full">

0 commit comments

Comments
 (0)