blob: d233df000ee707c9755cfad9b98d986e9b70f32d [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';
simon20076982022-10-11 15:04:13 -040019import { Conversation } from 'jami-web-common';
simon07b4eb02022-09-29 17:50:26 -040020import { useEffect, useState } from 'react';
Adrien Béraudab519ff2022-05-03 15:34:48 -040021import { useNavigate, useParams } from 'react-router';
simon07b4eb02022-09-29 17:50:26 -040022
simon07b4eb02022-09-29 17:50:26 -040023import authManager from '../AuthManager';
Adrien Béraud150b4782021-04-21 19:40:59 -040024
Adrien Béraud150b4782021-04-21 19:40:59 -040025export default function ConversationsOverviewCard(props) {
simond47ef9e2022-09-28 22:24:28 -040026 const navigate = useNavigate();
simon80b7b3b2022-09-28 17:50:10 -040027 let accountId = useParams().accountId;
28 if (props.accountId) {
29 accountId = props.accountId;
30 }
simond47ef9e2022-09-28 22:24:28 -040031 const [loaded, setLoaded] = useState(false);
32 const [conversations, setConversations] = useState([]);
Adrien Béraud150b4782021-04-21 19:40:59 -040033
34 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040035 const controller = new AbortController();
36 authManager
37 .fetch(`/api/accounts/${accountId}/conversations`, { signal: controller.signal })
38 .then((res) => res.json())
39 .then((result) => {
40 console.log(result);
41 setLoaded(true);
42 setConversations(Object.values(result).map((c) => Conversation.from(accountId, c)));
43 });
idillon28996962022-09-06 17:49:41 -040044 // return () => controller.abort() // crash on React18
simond47ef9e2022-09-28 22:24:28 -040045 }, [accountId]);
Adrien Béraud150b4782021-04-21 19:40:59 -040046
47 return (
Michelle Sepkap Simeb3dd3122022-11-03 02:12:39 -040048 <Card onClick={() => navigate(`/deprecated-account/${accountId}`)}>
Adrien Béraud150b4782021-04-21 19:40:59 -040049 <CardActionArea>
50 <CardContent>
idillonfb2af5b2022-09-16 13:40:08 -040051 <Typography color="textSecondary" gutterBottom>
Adrien Béraud150b4782021-04-21 19:40:59 -040052 Conversations
53 </Typography>
54 <Typography gutterBottom variant="h5" component="h2">
idillon28996962022-09-06 17:49:41 -040055 {loaded ? conversations.length : <CircularProgress size={24} />}
Adrien Béraud150b4782021-04-21 19:40:59 -040056 </Typography>
57 </CardContent>
58 </CardActionArea>
59 </Card>
simond47ef9e2022-09-28 22:24:28 -040060 );
Adrien Béraud150b4782021-04-21 19:40:59 -040061}