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

added loading spinner to the search button #54

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
13 changes: 10 additions & 3 deletions src/components/Form.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import React, { useState } from "react";
import React, { useContext, useState } from "react";
import { PhotoContext } from "../context/PhotoContext";
import Spinner from "./Spinner";

const Form = ({ handleSubmit, history }) => {
const{loading}=useContext(PhotoContext)
const [searchEntry, setSearchEntry] = useState("");
// update search text state
const updateSearchInput = e => {
setSearchEntry(e.target.value);
};




return (
<form
className="search-form"
Expand All @@ -23,13 +30,13 @@ const Form = ({ handleSubmit, history }) => {
className={`search-button ${searchEntry.trim() ? "active" : null}`}
disabled={!searchEntry.trim()}
>
<svg height="32" width="32">
{searchEntry&&loading?<Spinner/>: <svg height="32" width="32">
<path
d="M19.427 21.427a8.5 8.5 0 1 1 2-2l5.585 5.585c.55.55.546 1.43 0 1.976l-.024.024a1.399 1.399 0 0 1-1.976 0l-5.585-5.585zM14.5 21a6.5 6.5 0 1 0 0-13 6.5 6.5 0 0 0 0 13z"
fill="#ffffff"
fillRule="evenodd"
/>
</svg>
</svg>}
</button>
</form>
);
Expand Down
10 changes: 10 additions & 0 deletions src/components/Spinner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';

const Spinner = () => {
return (
<div className="spinner">
</div>
);
}

export default Spinner;
2 changes: 2 additions & 0 deletions src/context/PhotoContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const PhotoContextProvider = props => {
const [images, setImages] = useState([]);
const [loading, setLoading] = useState(true);
const runSearch = query => {
setLoading(true)
axios
.get(
`https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=${apiKey}&tags=${query}&per_page=24&format=json&nojsoncallback=1`
Expand All @@ -16,6 +17,7 @@ const PhotoContextProvider = props => {
setLoading(false);
})
.catch(error => {
setLoading(false)
console.log(
"Encountered an error with fetching and parsing data",
error
Expand Down
12 changes: 12 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ input {
margin-left: auto;
margin-right: auto;
}
.spinner {
border: 6px solid #f3f3f3;
/* Light grey */
border-top: 6px solid black;
/* Blue */
border-radius: 50%;
width: 15px;
height: 15px;
animation: spin 2s linear infinite;
margin-left: auto;
margin-right: auto;
}

@keyframes spin {
0% {
Expand Down