/
AllQuestions.tsx
55 lines (51 loc) · 1.58 KB
/
AllQuestions.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { useEffect, useState } from 'react';
import { Card } from '../components/Card';
import { ddbGetAllQueryResponse } from '../types';
import { AskQuestionButton } from '../components/AskQuestionButton';
import moment from 'moment';
import { ddbGetAllQuestionsFromAllUsers } from '../graphql/questions';
export const AllQuestions = () => {
const [questions, setQuestions] = useState<ddbGetAllQueryResponse[]>([]);
useEffect(() => {
const fetchQuestions = async () => {
const response = await ddbGetAllQuestionsFromAllUsers();
setQuestions(response || []);
console.log(response);
};
fetchQuestions();
}, []);
const renderQuestions = () => {
const sortedQuestions = questions.sort(
(a, b) => moment(b.createdAt).valueOf() - moment(a.createdAt).valueOf()
);
return (
<>
<div className='absolute right-0 -mt-3 z-40'>
<AskQuestionButton />
</div>
<div className="mt-2 border-b-2 absolute left-36 text-3xl text-blue-500 font-bold mb-2 w-full">
<h1 className='ml-2'>Recent questions</h1>
</div>
<div className='flex flex-col items-center w-full mt-16'>
{sortedQuestions.map((question: ddbGetAllQueryResponse, index) => (
<Card
key={index}
title={question.title}
body={question.body}
author={question.author}
createdAt={question.createdAt}
quesId={question.quesId}
updatedAt={question.updatedAt}
tags={question.tags}
/>
))}
</div>
</>
);
}
return (
<>
{renderQuestions()}
</>
)
};