blob: 84d022d8fdbcfd9237934e04094ca28c60cd4653 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
idillon-sfl5d174552022-08-23 14:34:24 -040018import { Card, CardActionArea, CardContent, CircularProgress, Typography } from '@mui/material';
simon5da8ca62022-11-09 15:21:25 -050019import { Conversation } from 'jami-web-common/dist/Conversation';
simon07b4eb02022-09-29 17:50:26 -040020import { useEffect, useState } from 'react';
simon5da8ca62022-11-09 15:21:25 -050021import { useNavigate } from 'react-router';
simon07b4eb02022-09-29 17:50:26 -040022
simon5da8ca62022-11-09 15:21:25 -050023import { useAuthContext } from '../contexts/AuthProvider';
Adrien Béraud150b4782021-04-21 19:40:59 -040024
simon5da8ca62022-11-09 15:21:25 -050025export default function ConversationsOverviewCard() {
simon94fe53e2022-11-10 12:51:58 -050026 const { axiosInstance, account } = useAuthContext();
simond47ef9e2022-09-28 22:24:28 -040027 const navigate = useNavigate();
simon5da8ca62022-11-09 15:21:25 -050028
29 const [conversationCount, setConversationCount] = useState<number | undefined>();
30
31 const accountId = account.getId();
Adrien Béraud150b4782021-04-21 19:40:59 -040032
33 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040034 const controller = new AbortController();
simon94fe53e2022-11-10 12:51:58 -050035 axiosInstance
36 .get<Conversation[]>('/conversations', {
37 signal: controller.signal,
38 })
39 .then(({ data }) => {
40 console.log(data);
41 setConversationCount(data.length);
simond47ef9e2022-09-28 22:24:28 -040042 });
simon5da8ca62022-11-09 15:21:25 -050043 return () => controller.abort(); // crash on React18
simon94fe53e2022-11-10 12:51:58 -050044 }, [axiosInstance, accountId]);
Adrien Béraud150b4782021-04-21 19:40:59 -040045
46 return (
simon3f5f3e72022-11-08 21:01:57 -050047 <Card onClick={() => navigate(`/`)}>
Adrien Béraud150b4782021-04-21 19:40:59 -040048 <CardActionArea>
49 <CardContent>
idillonfb2af5b2022-09-16 13:40:08 -040050 <Typography color="textSecondary" gutterBottom>
Adrien Béraud150b4782021-04-21 19:40:59 -040051 Conversations
52 </Typography>
53 <Typography gutterBottom variant="h5" component="h2">
simon5da8ca62022-11-09 15:21:25 -050054 {conversationCount != null ? conversationCount : <CircularProgress size={24} />}
Adrien Béraud150b4782021-04-21 19:40:59 -040055 </Typography>
56 </CardContent>
57 </CardActionArea>
58 </Card>
simond47ef9e2022-09-28 22:24:28 -040059 );
Adrien Béraud150b4782021-04-21 19:40:59 -040060}