refactor: Refactor Feed component to functional with hooks

This commit is contained in:
2025-07-07 18:01:06 +00:00
committed by Tanner Collin
parent 034c440e46
commit 443115ac0f

View File

@@ -1,28 +1,22 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import { Helmet } from 'react-helmet';
import localForage from 'localforage';
import { sourceLink, infoLine, logos } from './utils.js';
class Feed extends React.Component {
constructor(props) {
super(props);
function Feed({ updateCache }) {
const [stories, setStories] = useState(() => JSON.parse(localStorage.getItem('stories')) || false);
const [error, setError] = useState(false);
this.state = {
stories: JSON.parse(localStorage.getItem('stories')) || false,
error: false,
};
}
componentDidMount() {
useEffect(() => {
fetch('/api')
.then(res => res.json())
.then(
(result) => {
const updated = !this.state.stories || this.state.stories[0].id !== result.stories[0].id;
const updated = !stories || stories[0].id !== result.stories[0].id;
console.log('updated:', updated);
this.setState({ stories: result.stories });
setStories(result.stories);
localStorage.setItem('stories', JSON.stringify(result.stories));
if (updated) {
@@ -33,21 +27,17 @@ class Feed extends React.Component {
.then(result => {
localForage.setItem(x.id, result.story)
.then(console.log('preloaded', x.id, x.title));
this.props.updateCache(x.id, result.story);
updateCache(x.id, result.story);
}, error => {}
);
});
}
},
(error) => {
this.setState({ error: true });
setError(true);
}
);
}
render() {
const stories = this.state.stories;
const error = this.state.error;
}, [updateCache]);
return (
<div className='container'>
@@ -79,7 +69,6 @@ class Feed extends React.Component {
}
</div>
);
}
}
export default Feed;