blob: 4ed0ed4fc49c0ae6f5bd84faafbb4185119b0c90 [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';
24import { apiUrl } from '../utils/constants';
Adrien Béraud150b4782021-04-21 19:40:59 -040025
simon5da8ca62022-11-09 15:21:25 -050026export default function ConversationsOverviewCard() {
27 const { token, account } = useAuthContext();
simond47ef9e2022-09-28 22:24:28 -040028 const navigate = useNavigate();
simon5da8ca62022-11-09 15:21:25 -050029
30 const [conversationCount, setConversationCount] = useState<number | undefined>();
31
32 const accountId = account.getId();
Adrien Béraud150b4782021-04-21 19:40:59 -040033
34 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040035 const controller = new AbortController();
simon5da8ca62022-11-09 15:21:25 -050036 fetch(new URL('/conversations', apiUrl), {
37 headers: {
38 Authorization: `Bearer ${token}`,
39 },
40 signal: controller.signal,
41 })
simond47ef9e2022-09-28 22:24:28 -040042 .then((res) => res.json())
simon5da8ca62022-11-09 15:21:25 -050043 .then((result: Conversation[]) => {
simond47ef9e2022-09-28 22:24:28 -040044 console.log(result);
simon5da8ca62022-11-09 15:21:25 -050045 setConversationCount(result.length);
simond47ef9e2022-09-28 22:24:28 -040046 });
simon5da8ca62022-11-09 15:21:25 -050047 return () => controller.abort(); // crash on React18
48 }, [token, accountId]);
Adrien Béraud150b4782021-04-21 19:40:59 -040049
50 return (
simon5da8ca62022-11-09 15:21:25 -050051 <Card onClick={() => navigate(`/account`)}>
Adrien Béraud150b4782021-04-21 19:40:59 -040052 <CardActionArea>
53 <CardContent>
idillonfb2af5b2022-09-16 13:40:08 -040054 <Typography color="textSecondary" gutterBottom>
Adrien Béraud150b4782021-04-21 19:40:59 -040055 Conversations
56 </Typography>
57 <Typography gutterBottom variant="h5" component="h2">
simon5da8ca62022-11-09 15:21:25 -050058 {conversationCount != null ? conversationCount : <CircularProgress size={24} />}
Adrien Béraud150b4782021-04-21 19:40:59 -040059 </Typography>
60 </CardContent>
61 </CardActionArea>
62 </Card>
simond47ef9e2022-09-28 22:24:28 -040063 );
Adrien Béraud150b4782021-04-21 19:40:59 -040064}