Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: review pr #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/pages/giphy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useEffect, useState } from 'react';
import styles from './index.module.scss';

const BASE64_SECRET_API_KEY = 'R2M3MTMxamlKdXZJN0lkTjBIWjFEN25oMG93NUJVNmc=';

function GiphyPage() {
const [image, setImage] = useState();
const [query, setQuery] = useState('');

async function fetchResponse(q) {
setImage(null);

const response = await fetch(
`https://api.giphy.com/v1/gifs/search?type=gifs&limit=1&api_key=${atob(BASE64_SECRET_API_KEY)}&q=${q}`
);

return response.json();
}

async function handleQueryChange(newQueryValue) {
if (newQueryValue) {
const response = await fetchResponse(newQueryValue);
setImage(response.data[0]);
} else {
setImage(undefined);
}

setQuery(newQueryValue);
}

useEffect(() => {
const newQuery = window.location.search.replace('?q=', '');
if (newQuery) {
setQuery(newQuery);
fetchResponse(newQuery).then((response) => {
setImage(response.data[0]);
});
}
}, []);

return (
<div className={styles.root}>
<form className={styles.formWrapper}>
<input name="q" onChange={(e) => handleQueryChange(e.target.value)} type="search" value={query} />
<button onClick={() => handleQueryChange('')} type="button">
Clear
</button>
</form>
<div className={styles.imageWrapper}>
{image?.images?.downsized_large?.url && (
<img alt={image.title} src={image.images.downsized_large.url} />
)}
</div>
</div>
);
}

export default GiphyPage;
19 changes: 19 additions & 0 deletions src/pages/giphy/index.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.root {
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
gap: 8px;
}

.formWrapper {
width: 100%;
display: flex;
justify-content: center;
gap: 8px;
}

.imageWrapper {
display: flex;
justify-content: center;
}