Skip to content

Commit 0924671

Browse files
committed
chore: add comments for improved code documentation
1 parent 76a7d39 commit 0924671

File tree

11 files changed

+493
-39
lines changed

11 files changed

+493
-39
lines changed

src/components/App.jsx

Lines changed: 87 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,72 @@
1+
/**
2+
* React component representing the main application.
3+
* @extends Component
4+
*/
15
import { Component } from 'react';
6+
7+
// Importing the fetchImages function from the services/api module
8+
import fetchImages from 'services/api';
9+
10+
// Importing various React components for the application
211
import Searchbar from './Searchbar';
312
import ImageGallery from './ImageGallery';
413
import ImagePortalWelcome from './ImagePortalWelcome';
5-
import { LoadMoreBtn } from './Button';
6-
import { SearchResultInfo } from './SearchResultInfo';
14+
import LoadMoreBtn from './Button';
15+
import SearchResultInfo from './SearchResultInfo';
716
import ErrorAlert from './ErrorAlert';
817
import NoResultsAlert from './NoResultsAlert';
18+
import Loader from './Loader';
919

10-
import { Container } from 'react-bootstrap';
11-
12-
import fetchImages from 'services/api';
13-
20+
// Importing Bootstrap styles and components
1421
import 'bootstrap/dist/css/bootstrap.min.css';
15-
import { Loader } from './Loader';
22+
import { Container } from 'react-bootstrap';
1623

24+
/**
25+
* Main App component representing the image search application.
26+
* @class
27+
*/
1728
class App extends Component {
18-
constructor(props) {
19-
super(props);
20-
21-
this.state = {
22-
images: [],
23-
searchQuery: '',
24-
currentPage: 1,
25-
isLoading: false,
26-
error: null,
27-
totalHits: null,
28-
};
29-
}
29+
/**
30+
* State of the App component.
31+
* @type {Object}
32+
* @property {Array} images - Array to store fetched images.
33+
* @property {string} searchQuery - Current search query.
34+
* @property {number} currentPage - Current page number for paginated results.
35+
* @property {boolean} isLoading - Flag indicating whether images are being loaded.
36+
* @property {string|null} error - Error message if image fetching fails.
37+
* @property {number|null} totalHits - Total number of hits for the current search query.
38+
*/
39+
state = {
40+
images: [],
41+
searchQuery: '',
42+
currentPage: 1,
43+
isLoading: false,
44+
error: null,
45+
totalHits: null,
46+
};
3047

48+
/**
49+
* Fetches images from the Pixabay API based on the current search query and page number.
50+
* Updates the component state accordingly.
51+
* @async
52+
* @function
53+
* @returns {Promise<void>}
54+
*/
3155
getImages = async () => {
3256
const { searchQuery, currentPage } = this.state;
3357

58+
// If there is no search query, return early
3459
if (!searchQuery) {
3560
return;
3661
}
3762

3863
try {
64+
// Set loading state to true
3965
this.setState({
4066
isLoading: true,
4167
});
4268

69+
// Fetch images from Pixabay API
4370
const { hits, totalHits } = await fetchImages(searchQuery, currentPage);
4471

4572
// Append new images to the existing ones
@@ -49,16 +76,25 @@ class App extends Component {
4976
totalHits: totalHits || 0,
5077
}));
5178
} catch (error) {
79+
// Handle errors by setting the error state
5280
this.setState({
5381
error: error.message,
5482
});
5583
} finally {
84+
// Set loading state to false regardless of success or failure
5685
this.setState({
5786
isLoading: false,
5887
});
5988
}
6089
};
6190

91+
/**
92+
* Lifecycle method that gets called whenever the component updates.
93+
* Calls the getImages method if the searchQuery or currentPage changes.
94+
* @param {Object} _prevProps - Previous properties.
95+
* @param {Object} prevState - Previous state.
96+
* @returns {Promise<void>}
97+
*/
6298
async componentDidUpdate(_prevProps, prevState) {
6399
if (
64100
prevState.searchQuery !== this.state.searchQuery ||
@@ -68,12 +104,22 @@ class App extends Component {
68104
}
69105
}
70106

107+
/**
108+
* Updates the currentPage in the state, triggering a new set of images to be fetched.
109+
* @function
110+
* @returns {void}
111+
*/
71112
loadMoreImages = () => {
72113
this.setState(prev => ({
73114
currentPage: prev.currentPage + 1,
74115
}));
75116
};
76117

118+
/**
119+
* Handles form submission, updates the searchQuery, resets images, and sets currentPage to 1.
120+
* @param {string} searchQuery - The new search query.
121+
* @returns {void}
122+
*/
77123
handleSubmit = searchQuery => {
78124
if (searchQuery === this.state.searchQuery) {
79125
return;
@@ -86,36 +132,55 @@ class App extends Component {
86132
});
87133
};
88134

135+
/**
136+
* Renders the main application component with various child components based on the state.
137+
* @function
138+
* @returns {JSX.Element} - The rendered React element.
139+
*/
89140
render() {
90141
const { images, searchQuery, isLoading, error, totalHits } = this.state;
91142

92143
return (
93144
<>
145+
{/* Search bar component */}
94146
<Searchbar onSubmit={this.handleSubmit} />
147+
148+
{/* Main container for the application */}
95149
<Container className="d-flex flex-column justify-content-center mb-5 mx-auto">
96-
{isLoading && <Loader />}
97-
{error && <ErrorAlert errorMessage={error} />}
150+
{isLoading && <Loader />}{' '}
151+
{/* Loader component while images are being fetched */}
152+
{error && <ErrorAlert errorMessage={error} />}{' '}
153+
{/* Error alert component */}
98154
{images.length > 0 && !error && (
99155
<>
156+
{/* Information about the search results */}
100157
<SearchResultInfo
101158
searchQuery={searchQuery}
102159
totalHits={totalHits}
103160
/>
161+
162+
{/* Image gallery component */}
104163
<ImageGallery images={images} />
164+
165+
{/* Load more button if there are more images to load */}
105166
{totalHits > images.length && (
106167
<LoadMoreBtn onClick={this.loadMoreImages} />
107168
)}
108169
</>
109170
)}
171+
{/* No results alert if no images are found */}
110172
{!error && searchQuery && totalHits === 0 && (
111173
<NoResultsAlert searchQuery={searchQuery} />
112174
)}
113-
175+
{/* Welcome message for the image portal if no search query is present */}
114176
{!searchQuery && <ImagePortalWelcome />}
115177
</Container>
116178
</>
117179
);
118180
}
119181
}
120182

183+
/**
184+
* Exporting the App component as the default export of the module.
185+
*/
121186
export default App;

src/components/Button.jsx

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
1+
import PropTypes from 'prop-types';
12
import { Container, Row, Col } from 'react-bootstrap';
23
import Button from 'react-bootstrap/Button';
34

4-
export const LoadMoreBtn = ({ onClick }) => {
5+
/**
6+
* React component representing the "Load More" button for fetching additional images.
7+
* @function
8+
* @param {Object} props - React component properties.
9+
* @param {Function} props.onClick - Function to be called when the button is clicked.
10+
* @returns {JSX.Element} - The rendered React element.
11+
*/
12+
const LoadMoreBtn = ({ onClick }) => {
13+
/**
14+
* Handles the click event on the "Load More" button and invokes the provided onClick function.
15+
* @function
16+
* @returns {void}
17+
*/
18+
const handleButtonClick = () => {
19+
onClick();
20+
};
21+
522
return (
623
<Row>
24+
{/* Container for the button */}
725
<Container>
826
<Row className="justify-content-center mt-3">
27+
{/* Column for responsive layout */}
928
<Col xs={12} md="auto">
10-
<Button variant="primary" className="w-100" onClick={onClick}>
29+
{/* Bootstrap Button component with "Load More" label */}
30+
<Button
31+
variant="primary"
32+
className="w-100"
33+
onClick={handleButtonClick}
34+
>
1135
Load More
1236
</Button>
1337
</Col>
@@ -16,3 +40,20 @@ export const LoadMoreBtn = ({ onClick }) => {
1640
</Row>
1741
);
1842
};
43+
44+
/**
45+
* PropTypes for the LoadMoreBtn component.
46+
* @static
47+
* @type {Object}
48+
*/
49+
LoadMoreBtn.propTypes = {
50+
/**
51+
* Function to be called when the "Load More" button is clicked.
52+
*/
53+
onClick: PropTypes.func.isRequired,
54+
};
55+
56+
/**
57+
* Exporting the LoadMoreBtn component as the default export of the module.
58+
*/
59+
export default LoadMoreBtn;

src/components/ErrorAlert.jsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
import PropTypes from 'prop-types';
12
import { Container, Row, Col, Alert } from 'react-bootstrap';
23

4+
/**
5+
* React component representing an error alert with a message.
6+
* @function
7+
* @param {Object} props - React component properties.
8+
* @param {string} props.errorMessage - The error message to be displayed.
9+
* @returns {JSX.Element} - The rendered React element.
10+
*/
311
function ErrorAlert({ errorMessage }) {
412
return (
513
<Container>
614
<Row className="justify-content-center mt-5">
715
<Col xs={12} md={6}>
16+
{/* Bootstrap Alert component with a danger variant */}
817
<Alert variant="danger">
918
<Alert.Heading>Something went wrong...</Alert.Heading>
1019
<p>{errorMessage}</p>
@@ -19,4 +28,19 @@ function ErrorAlert({ errorMessage }) {
1928
);
2029
}
2130

31+
/**
32+
* PropTypes for the ErrorAlert component.
33+
* @static
34+
* @type {Object}
35+
*/
36+
ErrorAlert.propTypes = {
37+
/**
38+
* The error message to be displayed in the alert.
39+
*/
40+
errorMessage: PropTypes.string.isRequired,
41+
};
42+
43+
/**
44+
* Exporting the ErrorAlert component as the default export of the module.
45+
*/
2246
export default ErrorAlert;

0 commit comments

Comments
 (0)