simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 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-sfl | 5d17455 | 2022-08-23 14:34:24 -0400 | [diff] [blame] | 18 | import { Card, CardActionArea, CardContent, CircularProgress, Typography } from '@mui/material'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 19 | import { useEffect, useState } from 'react'; |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 20 | import { useNavigate } from 'react-router'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 21 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 22 | import { useAuthContext } from '../contexts/AuthProvider'; |
Misha Krieger-Raynauld | 6bbdacf | 2022-11-29 21:45:40 -0500 | [diff] [blame^] | 23 | import { Conversation } from '../models/Conversation'; |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 24 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 25 | export default function ConversationsOverviewCard() { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 26 | const { axiosInstance, account } = useAuthContext(); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 27 | const navigate = useNavigate(); |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 28 | |
| 29 | const [conversationCount, setConversationCount] = useState<number | undefined>(); |
| 30 | |
| 31 | const accountId = account.getId(); |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 32 | |
| 33 | useEffect(() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 34 | const controller = new AbortController(); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 35 | axiosInstance |
| 36 | .get<Conversation[]>('/conversations', { |
| 37 | signal: controller.signal, |
| 38 | }) |
| 39 | .then(({ data }) => { |
| 40 | console.log(data); |
| 41 | setConversationCount(data.length); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 42 | }); |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 43 | return () => controller.abort(); // crash on React18 |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 44 | }, [axiosInstance, accountId]); |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 45 | |
| 46 | return ( |
simon | 3f5f3e7 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 47 | <Card onClick={() => navigate(`/`)}> |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 48 | <CardActionArea> |
| 49 | <CardContent> |
idillon | fb2af5b | 2022-09-16 13:40:08 -0400 | [diff] [blame] | 50 | <Typography color="textSecondary" gutterBottom> |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 51 | Conversations |
| 52 | </Typography> |
| 53 | <Typography gutterBottom variant="h5" component="h2"> |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 54 | {conversationCount != null ? conversationCount : <CircularProgress size={24} />} |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 55 | </Typography> |
| 56 | </CardContent> |
| 57 | </CardActionArea> |
| 58 | </Card> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 59 | ); |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 60 | } |