aboutsummaryrefslogtreecommitdiff
path: root/src/components/Guestbook.jsx
blob: 498bbbeab036438ceb61306bd52485fc8ce75f38 (plain) (blame)
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
56
import { h, Component } from 'preact';
import { pb } from '../services/pocketbase';
import { formatDate } from "../util";
import sanitizeHtml from 'sanitize-html';

class Guestbook extends Component {
    state = {
        message: null,
        error: null
    };

    fetchMessages = async () => {
        try {
            const message = await pb.collection('guestbook').getFullList({
                sort: '-created',
            });
            this.setState({ message });
        } catch (error) {
            this.setState({ error: `Failed to fetch data: ${error.message}` });
            console.error('Failed to fetch data:', error);
        }
    }

    async componentDidMount() {
        await this.fetchMessages();
    }

    render() {
        const { message, error } = this.state;

        if (error) {
            return <p>{error}</p>;
        }

        if (!message) {
            return <p>Loading messages...</p>;
        }

        return (
            <div>
                <div class="grid">
                    {message.map((g) => (
                        <article class="card">
                            <h1>Message from: {g.name}</h1>
                            <small>{formatDate(g.created)}</small>
                            <div dangerouslySetInnerHTML={{__html: sanitizeHtml(g.message)}}/>
                            {g.website && <a href={g.website}>Website</a>}
                        </article>
                    ))}
                </div>
            </div>
        );
    }
}

export default Guestbook;