├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── images │ ├── search.svg │ └── zero.svg ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.css ├── App.test.tsx ├── App.tsx ├── components │ ├── EmptyResultView.css │ ├── EmptyResultView.tsx │ ├── Search.css │ ├── Search.tsx │ ├── SearchView.css │ ├── SearchView.tsx │ ├── UserView.tsx │ ├── UsersEmptyView.css │ ├── UsersEmptyView.tsx │ ├── UsersView.css │ └── UsersView.tsx ├── config.tsx ├── hooks │ └── useUsers.tsx ├── index.css ├── index.tsx ├── logo.svg ├── models │ ├── Action.tsx │ ├── SearchPayload.tsx │ ├── State.tsx │ ├── User.tsx │ ├── UsersSearchResponse.tsx │ └── UsersState.tsx ├── react-app-env.d.ts ├── redux │ ├── actions │ │ └── usersActions.tsx │ ├── reducers │ │ ├── index.tsx │ │ ├── users.test.tsx │ │ └── users.tsx │ ├── selectors │ │ └── usersSelector.tsx │ └── store.tsx ├── serviceWorker.ts └── setupTests.ts ├── tsconfig.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | .env 21 | 22 | npm-debug.log* 23 | yarn-debug.log* 24 | yarn-error.log* 25 | 26 | .idea 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `yarn start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `yarn test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `yarn build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `yarn eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-user-search-react-redux-typescript", 3 | "version": "0.1.0", 4 | "homepage": ".", 5 | "private": true, 6 | "dependencies": { 7 | "@fortawesome/fontawesome-svg-core": "^1.2.30", 8 | "@fortawesome/free-solid-svg-icons": "^5.14.0", 9 | "@fortawesome/react-fontawesome": "^0.1.11", 10 | "@testing-library/jest-dom": "^4.2.4", 11 | "@testing-library/react": "^9.5.0", 12 | "@testing-library/user-event": "^7.2.1", 13 | "@types/jest": "^24.9.1", 14 | "@types/node": "^12.12.54", 15 | "@types/react": "^16.9.47", 16 | "@types/react-dom": "^16.9.8", 17 | "@types/react-redux": "^7.1.9", 18 | "@types/react-router": "^5.1.8", 19 | "@types/react-router-dom": "^5.1.5", 20 | "@types/redux-api-middleware": "^3.2.2", 21 | "bootstrap": "^4.5.2", 22 | "connected-react-router": "^6.8.0", 23 | "history": "^5.0.0", 24 | "react": "^16.13.1", 25 | "react-bootstrap": "^1.3.0", 26 | "react-cookie": "^4.0.3", 27 | "react-dom": "^16.13.1", 28 | "react-redux": "^7.2.1", 29 | "react-router": "^5.2.0", 30 | "react-router-dom": "^5.2.0", 31 | "react-scripts": "3.4.3", 32 | "react-swipeable-views": "^0.13.9", 33 | "redux": "^4.0.5", 34 | "redux-api-middleware": "^3.2.1", 35 | "redux-history": "^1.0.2", 36 | "redux-thunk": "^2.3.0", 37 | "typescript": "^3.7.5" 38 | }, 39 | "scripts": { 40 | "start": "react-scripts start", 41 | "build": "react-scripts build", 42 | "test": "react-scripts test", 43 | "eject": "react-scripts eject" 44 | }, 45 | "eslintConfig": { 46 | "extends": "react-app" 47 | }, 48 | "browserslist": { 49 | "production": [ 50 | ">0.2%", 51 | "not dead", 52 | "not op_mini all" 53 | ], 54 | "development": [ 55 | "last 1 chrome version", 56 | "last 1 firefox version", 57 | "last 1 safari version" 58 | ] 59 | }, 60 | "devDependencies": { 61 | "@types/redux-logger": "^3.0.8", 62 | "redux-logger": "latest" 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaraco/github-user-search-react-redux-typescript/2592e2cd1ba1d8b2a097b6c42eff788e59ea628f/public/favicon.ico -------------------------------------------------------------------------------- /public/images/search.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | search 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /public/images/zero.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | zero 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaraco/github-user-search-react-redux-typescript/2592e2cd1ba1d8b2a097b6c42eff788e59ea628f/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaraco/github-user-search-react-redux-typescript/2592e2cd1ba1d8b2a097b6c42eff788e59ea628f/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | UserView-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zaraco/github-user-search-react-redux-typescript/2592e2cd1ba1d8b2a097b6c42eff788e59ea628f/src/App.css -------------------------------------------------------------------------------- /src/App.test.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import App from './App'; 4 | import {Provider} from "react-redux"; 5 | import initStore from "./redux/store"; 6 | 7 | test('renders user link', () => { 8 | const store = initStore(); 9 | const { getByText } = render(); 10 | const linkElement = getByText(/user/i); 11 | expect(linkElement).toBeInTheDocument(); 12 | }); 13 | -------------------------------------------------------------------------------- /src/App.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import 'bootstrap/dist/css/bootstrap.min.css'; 3 | import './App.css'; 4 | import {Container} from "react-bootstrap"; 5 | import {Switch, Route} from "react-router"; 6 | import {HashRouter as Router} from "react-router-dom"; 7 | import Search from "./components/Search"; 8 | 9 | function App() { 10 | 11 | return ( 12 | <> 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | ); 22 | } 23 | 24 | export default App; 25 | -------------------------------------------------------------------------------- /src/components/EmptyResultView.css: -------------------------------------------------------------------------------- 1 | 2 | .content .image-search { 3 | margin-top: 50px; 4 | margin-bottom: 50px; 5 | } 6 | 7 | .content .text-muted { 8 | font-size: 20px; 9 | } 10 | -------------------------------------------------------------------------------- /src/components/EmptyResultView.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import {Form, Image, Row, Col} from "react-bootstrap"; 3 | import './EmptyResultView.css'; 4 | 5 | export default function EmptyResultView() { 6 | return ( 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | Enter a login, name, or a company you are looking for. 21 | 22 | 23 |
24 | 25 |
26 |
27 | ) 28 | } 29 | -------------------------------------------------------------------------------- /src/components/Search.css: -------------------------------------------------------------------------------- 1 | @media(min-width : 768px) { 2 | .tab-content { 3 | width: 100% !important; 4 | } 5 | .tab-content > .tab-pane { 6 | display: block !important; 7 | opacity: 1 !important; 8 | width: 50% !important; 9 | padding: 10px; 10 | float: left !important; 11 | } 12 | .nav-tabs { 13 | display: none !important; 14 | } 15 | } 16 | 17 | .content .text { 18 | font-size: 30px; 19 | margin-top: 70px; 20 | margin-bottom: 40px; 21 | text-align: center; 22 | } 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/components/Search.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import useUsers from "../hooks/useUsers"; 3 | import {Row, Col, Container, Alert, Tabs, Tab} from "react-bootstrap"; 4 | import SearchView from "./SearchView"; 5 | import UsersView from "./UsersView"; 6 | import UsersEmptyView from "./UsersEmptyView"; 7 | import EmptyResultView from "./EmptyResultView"; 8 | import './Search.css' 9 | 10 | export default function Search() { 11 | const {users, organizations, submittedSearch, error} = useUsers(); 12 | 13 | 14 | return ( 15 |
16 | 17 | 18 |

19 | Search for Github Users 20 |

21 | 22 | 23 | 24 | 25 | 26 | {(error.search !== "") ? {error.search} : ""} 27 | 28 |
29 | 30 | {(submittedSearch.q === "") ? 31 | : 32 | 33 | 34 | {(users.length > 0) ? 35 | 36 | : 37 | } 38 | 39 | 40 | {(organizations.length > 0) ? 41 | 42 | : 43 | } 44 | 45 | 46 | } 47 |
48 | ) 49 | } 50 | -------------------------------------------------------------------------------- /src/components/SearchView.css: -------------------------------------------------------------------------------- 1 | .content .search { 2 | filter: grayscale(90%); 3 | -webkit-box-shadow: 0 0 10px 0 rgba(0,0,0,0.1); 4 | -moz-box-shadow: 0 0 10px 0 rgba(0,0,0,0.1); 5 | box-shadow: 0 0 10px 0 rgba(0,0,0,0.1); 6 | border: none; 7 | } 8 | 9 | .content .search:hover { 10 | filter: none; 11 | color: #272727; 12 | } 13 | 14 | 15 | .content .btn:hover { 16 | background-color: #979797; 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/components/SearchView.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import {Button, Col, Form, Row, Spinner} from "react-bootstrap"; 3 | import useUsers from "../hooks/useUsers"; 4 | import './SearchView.css'; 5 | 6 | 7 | export default function SearchView() { 8 | const {getSearch, search, setSearchForm, isLoading} = useUsers(); 9 | 10 | const changeHandler = (event: React.ChangeEvent) => { 11 | setSearchForm({ 12 | q: event.target.value, 13 | order: "", 14 | sort: "" 15 | }) 16 | }; 17 | 18 | const clickHandler = (event: React.MouseEvent) => { 19 | event.preventDefault(); 20 | getSearch(search) 21 | }; 22 | 23 | return ( 24 | <> 25 | 26 | 27 |
28 | 29 | 30 | 31 |
32 | 33 | 34 | {(isLoading.search) ? 35 | 39 | : 40 | 43 | } 44 | 45 |
46 | 47 | ) 48 | } 49 | -------------------------------------------------------------------------------- /src/components/UserView.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import {User} from "../models/User"; 3 | import {Image, Row, Col} from 'react-bootstrap' 4 | 5 | 6 | export default function UserView(props: { user: User }) { 7 | return ( 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |

{props.user.login}

16 | 17 |
18 | 19 | 20 | 21 | ) 22 | } 23 | -------------------------------------------------------------------------------- /src/components/UsersEmptyView.css: -------------------------------------------------------------------------------- 1 | 2 | .content .image-no-user { 3 | margin-top: 30px; 4 | } 5 | 6 | .content .text .text-no-user { 7 | font-size: 15px; 8 | } 9 | 10 | .content .btn-no-user:hover { 11 | background-color: #dbadb2; 12 | } 13 | -------------------------------------------------------------------------------- /src/components/UsersEmptyView.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import useUsers from "../hooks/useUsers"; 3 | import {Col, Form, Image, Row, Button} from "react-bootstrap"; 4 | import './UsersEmptyView.css'; 5 | 6 | export default function UsersEmptyView() { 7 | const {reset} = useUsers(); 8 | 9 | const clickHandler = (event: React.MouseEvent) => { 10 | event.preventDefault(); 11 | reset() 12 | }; 13 | return ( 14 |
15 | 16 | 17 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | Hmm... We didn't find any users... 28 | 29 |
30 | 31 |
32 | 33 | 34 | 35 | 36 | 37 |
38 | ) 39 | } 40 | -------------------------------------------------------------------------------- /src/components/UsersView.css: -------------------------------------------------------------------------------- 1 | .badge-users:hover { 2 | background-color: #979797; 3 | } 4 | 5 | .btn-show { 6 | border: none; 7 | background-color: white; 8 | 9 | } 10 | 11 | .fontawesome-users { 12 | cursor: pointer; 13 | margin-left: 10px; 14 | } 15 | 16 | .fontawesome-organizations { 17 | cursor: pointer; 18 | margin-left: 10px; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /src/components/UsersView.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import {User} from "../models/User"; 3 | import {Badge, Table, Button, Row, Col} from "react-bootstrap"; 4 | import UserView from "./UserView"; 5 | import './UsersView.css'; 6 | import useUsers from "../hooks/useUsers"; 7 | import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' 8 | import {faChevronDown, faChevronUp} from "@fortawesome/free-solid-svg-icons"; 9 | 10 | 11 | export default function UsersView(props: { users: Array, title: string, tableHeader: string }) { 12 | 13 | const {showMore, setShowMore, getSearch, submittedSearch, setSearchForm} = useUsers(); 14 | 15 | let usersCompact; 16 | 17 | if (!showMore){ 18 | 19 | usersCompact = props.users.slice(0, 5) 20 | } 21 | else { 22 | usersCompact = props.users 23 | }; 24 | 25 | const usersView = usersCompact.map((user) => ( 26 | 27 | )); 28 | 29 | const clickHandler = (event: React.MouseEvent) => { 30 | event.preventDefault(); 31 | setShowMore(!showMore) 32 | } 33 | 34 | 35 | const sortNameHandler = (event: React.MouseEvent) => { 36 | let order = (submittedSearch.order === "asc") ? "desc" : "asc"; 37 | 38 | setSearchForm({ 39 | ...submittedSearch, 40 | sort: "", 41 | order: order 42 | }) 43 | 44 | getSearch({ 45 | ...submittedSearch, 46 | sort: "", 47 | order: order 48 | }) 49 | }; 50 | 51 | const sortContributionHandler = (event: React.MouseEvent) => { 52 | let order = (submittedSearch.order === "asc") ? "desc" : "asc"; 53 | setSearchForm({ 54 | ...submittedSearch, 55 | sort: "repositories", 56 | order: order 57 | }) 58 | getSearch({ 59 | ...submittedSearch, 60 | sort: "repositories", 61 | order: order 62 | }) 63 | }; 64 | 65 | return ( 66 | <> 67 | {props.title} {props.users.length} 68 | 69 | 70 | 71 | 75 | 79 | 80 | 81 | 82 | {usersView} 83 | 84 |
72 | {props.tableHeader} 73 | { (submittedSearch.sort === "") ? : "" } 74 | 76 | Contributions 77 | { (submittedSearch.sort === "repositories") ? : "" } 78 |
85 | 86 | 87 | 90 | 91 | 92 | 93 | ) 94 | } 95 | -------------------------------------------------------------------------------- /src/config.tsx: -------------------------------------------------------------------------------- 1 | import {SearchPayload} from "./models/SearchPayload"; 2 | 3 | export const API_BASE_URL = process.env.REACT_APP_API_BASE_URL; 4 | 5 | export const API_HEADERS = { 6 | 'Accept': 'application/json', 7 | 'Content-Type': 'application/json', 8 | "X-Requested-With": "XMLHttpRequest" 9 | }; 10 | 11 | export const API_ENDPOINTS = { 12 | search: (params: SearchPayload) => `${API_BASE_URL}/search/users?q=${encodeURI(params.q)}&sort=${encodeURI(params.sort)}&order=${encodeURI(params.order)}`, 13 | }; 14 | -------------------------------------------------------------------------------- /src/hooks/useUsers.tsx: -------------------------------------------------------------------------------- 1 | import {useDispatch, useSelector} from "react-redux"; 2 | import {usersSelector} from "../redux/selectors/usersSelector"; 3 | import {useCallback} from "react"; 4 | import {actionGetSearch, actionReset, actionSearchForm, actionShowMore} from "../redux/actions/usersActions"; 5 | import {UsersSearchResponse} from "../models/UsersSearchResponse"; 6 | import {SearchPayload} from "../models/SearchPayload"; 7 | 8 | const useUsers = () => { 9 | const dispatch = useDispatch(); 10 | 11 | const users = useSelector(usersSelector.users); 12 | const organizations = useSelector(usersSelector.organizations); 13 | const search = useSelector(usersSelector.search); 14 | const submittedSearch = useSelector(usersSelector.submittedSearch); 15 | const error = useSelector(usersSelector.error); 16 | const isLoading = useSelector(usersSelector.isLoading); 17 | const showMore = useSelector(usersSelector.showMore); 18 | 19 | const getSearch = useCallback((payload: SearchPayload) => { 20 | dispatch(actionGetSearch(payload)); 21 | },[]); 22 | 23 | const setSearchForm = useCallback((payload: SearchPayload) => { 24 | dispatch(actionSearchForm(payload)); 25 | },[]); 26 | 27 | const setShowMore = useCallback((payload: boolean) => { 28 | dispatch(actionShowMore(payload)); 29 | },[]); 30 | 31 | const reset = useCallback(() => { 32 | dispatch(actionReset()); 33 | },[]); 34 | 35 | 36 | 37 | 38 | return { 39 | users, 40 | organizations, 41 | getSearch, 42 | setSearchForm, 43 | search, 44 | submittedSearch, 45 | error, 46 | isLoading, 47 | showMore, 48 | setShowMore, 49 | reset 50 | 51 | }; 52 | 53 | }; 54 | 55 | export default useUsers; 56 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | import {Provider} from 'react-redux'; 7 | import initStore from './redux/store'; 8 | 9 | const store = initStore(); 10 | 11 | ReactDOM.render( 12 | 13 | 14 | 15 | 16 | , 17 | document.getElementById('root') 18 | ); 19 | 20 | // If you want your app to work offline and load faster, you can change 21 | // unregister() to register() below. Note this comes with some pitfalls. 22 | // Learn more about service workers: https://bit.ly/CRA-PWA 23 | serviceWorker.unregister(); 24 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/models/Action.tsx: -------------------------------------------------------------------------------- 1 | export interface Action { 2 | readonly type: T; 3 | readonly payload?: P; 4 | } 5 | -------------------------------------------------------------------------------- /src/models/SearchPayload.tsx: -------------------------------------------------------------------------------- 1 | export interface SearchPayload { 2 | q: string, 3 | sort: string, 4 | order: string 5 | } 6 | -------------------------------------------------------------------------------- /src/models/State.tsx: -------------------------------------------------------------------------------- 1 | import {UsersState} from "./UsersState"; 2 | 3 | export interface State { 4 | users: UsersState 5 | } 6 | -------------------------------------------------------------------------------- /src/models/User.tsx: -------------------------------------------------------------------------------- 1 | export interface User { 2 | login: string, 3 | id: number, 4 | node_id: string, 5 | avatar_url: string, 6 | gravatar_id: string, 7 | url: string, 8 | html_url: string, 9 | followers_url: string, 10 | following_url: string, 11 | gists_url: string, 12 | starred_url: string, 13 | subscriptions_url: string, 14 | organizations_url: string, 15 | repos_url: string, 16 | events_url: string, 17 | received_events_url: string, 18 | type: string, 19 | site_admin: boolean, 20 | name: string, 21 | company: string, 22 | blog: string, 23 | location: string, 24 | email: string, 25 | hireable: string, 26 | bio: string, 27 | twitter_username: string, 28 | public_repos: number, 29 | public_gists: number, 30 | followers: number, 31 | following: number, 32 | created_at: string, 33 | updated_at: string 34 | } 35 | -------------------------------------------------------------------------------- /src/models/UsersSearchResponse.tsx: -------------------------------------------------------------------------------- 1 | import {User} from "./User"; 2 | 3 | export interface UsersSearchResponse { 4 | total_count: number, 5 | incomplete_results: boolean, 6 | items: Array 7 | } 8 | -------------------------------------------------------------------------------- /src/models/UsersState.tsx: -------------------------------------------------------------------------------- 1 | import {User} from "./User"; 2 | import {SearchPayload} from "./SearchPayload"; 3 | 4 | export interface UsersState { 5 | users: Array, 6 | organizations: Array, 7 | search: SearchPayload, 8 | submittedSearch: SearchPayload, 9 | isLoading: {search: boolean}, 10 | error: {search: string} 11 | showMore: boolean 12 | } 13 | -------------------------------------------------------------------------------- /src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /src/redux/actions/usersActions.tsx: -------------------------------------------------------------------------------- 1 | import {createAction} from "redux-api-middleware"; 2 | import {API_ENDPOINTS, API_HEADERS} from "../../config"; 3 | import {SearchPayload} from "../../models/SearchPayload"; 4 | import {Dispatch} from "redux"; 5 | 6 | export const actionGetSearch = (payload: SearchPayload) => { 7 | return createAction({ 8 | types: ['GET_SEARCH_REQUEST', 'GET_SEARCH_SUCCESS', 'GET_SEARCH_FAILURE'], 9 | endpoint: () => API_ENDPOINTS.search(payload), 10 | method: 'GET', 11 | headers: API_HEADERS 12 | }); 13 | }; 14 | 15 | export const actionSearchForm = (payload: SearchPayload) => (dispatch: Dispatch) => { 16 | return dispatch({ 17 | type: 'SEARCH_FORM', 18 | payload 19 | }); 20 | }; 21 | 22 | export const actionShowMore = (payload: boolean) => (dispatch: Dispatch) => { 23 | return dispatch({ 24 | type: 'SHOW_MORE', 25 | payload 26 | }); 27 | }; 28 | 29 | export const actionReset = () => (dispatch: Dispatch) => { 30 | return dispatch({ 31 | type: 'RESET' 32 | }); 33 | }; 34 | -------------------------------------------------------------------------------- /src/redux/reducers/index.tsx: -------------------------------------------------------------------------------- 1 | import {combineReducers} from 'redux'; 2 | import {users} from "./users"; 3 | 4 | export default combineReducers({ 5 | users 6 | }); 7 | -------------------------------------------------------------------------------- /src/redux/reducers/users.test.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import {UsersState} from "../../models/UsersState"; 3 | import {Action} from "../../models/Action"; 4 | import {users} from "./users"; 5 | 6 | const initialState: UsersState = { 7 | users: [], 8 | organizations: [], 9 | search: { 10 | q: "", 11 | sort: "", 12 | order: "" 13 | }, 14 | submittedSearch: { 15 | q: "", 16 | sort: "", 17 | order: "" 18 | }, 19 | isLoading: { 20 | search: false 21 | }, 22 | error: { 23 | search: "" 24 | }, 25 | showMore: false 26 | }; 27 | 28 | test('show more', () => { 29 | const action: Action = { 30 | type: "SHOW_MORE", 31 | payload: true 32 | }; 33 | const result = users(initialState, action) 34 | expect(result.showMore).toEqual(true); 35 | }); 36 | 37 | test('no action', () => { 38 | const action: Action = { 39 | type: "", 40 | payload: null 41 | }; 42 | const result = users(initialState, action) 43 | expect(result).toEqual(initialState); 44 | }); 45 | -------------------------------------------------------------------------------- /src/redux/reducers/users.tsx: -------------------------------------------------------------------------------- 1 | import {UsersState} from "../../models/UsersState"; 2 | import {Action} from "../../models/Action"; 3 | import {User} from "../../models/User"; 4 | 5 | const initialState: UsersState = { 6 | users: [], 7 | organizations: [], 8 | search: { 9 | q: "", 10 | sort: "", 11 | order: "" 12 | }, 13 | submittedSearch: { 14 | q: "", 15 | sort: "", 16 | order: "" 17 | }, 18 | isLoading: { 19 | search: false 20 | }, 21 | error: { 22 | search: "" 23 | }, 24 | showMore: false 25 | 26 | }; 27 | 28 | export function users(state = initialState, action: Action): UsersState { 29 | switch (action.type) { 30 | case "GET_SEARCH_SUCCESS": 31 | let users: Array = []; 32 | let organizations: Array = []; 33 | if (action.payload && action.payload.items) { 34 | let items: Array = action.payload.items; 35 | users = items.filter(function (item: User) { 36 | return (item.type === "User"); 37 | }); 38 | organizations = items.filter(function (item: User) { 39 | return (item.type === "Organization"); 40 | }); 41 | } 42 | return { 43 | ...state, 44 | users: users, 45 | organizations: organizations, 46 | isLoading: { 47 | ...state.isLoading, 48 | search: false 49 | }, 50 | submittedSearch: state.search, 51 | error: { 52 | ...state.error, 53 | search: "" 54 | }, 55 | }; 56 | case "GET_SEARCH_FAILURE": 57 | return { 58 | ...state, 59 | isLoading: { 60 | ...state.isLoading, 61 | search: false 62 | }, 63 | error: { 64 | ...state.error, 65 | search: action.payload.message 66 | }, 67 | }; 68 | case "GET_SEARCH_REQUEST": 69 | return { 70 | ...state, 71 | isLoading: { 72 | ...state.isLoading, 73 | search: true 74 | } 75 | }; 76 | case "SEARCH_FORM": 77 | return { 78 | ...state, 79 | search: action.payload 80 | }; 81 | 82 | case "SHOW_MORE": 83 | return { 84 | ...state, 85 | showMore: action.payload 86 | }; 87 | 88 | case "RESET": 89 | return initialState; 90 | 91 | 92 | default: 93 | return state; 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/redux/selectors/usersSelector.tsx: -------------------------------------------------------------------------------- 1 | import {State} from "../../models/State"; 2 | 3 | export const usersSelector = { 4 | users: (state: State) => state.users.users, 5 | organizations: (state: State) => state.users.organizations, 6 | search: (state: State) => state.users.search, 7 | submittedSearch: (state: State) => state.users.submittedSearch, 8 | isLoading: (state: State) => state.users.isLoading, 9 | error: (state: State) => state.users.error, 10 | showMore: (state: State) => state.users.showMore, 11 | 12 | }; 13 | -------------------------------------------------------------------------------- /src/redux/store.tsx: -------------------------------------------------------------------------------- 1 | import {createStore, applyMiddleware, compose} from 'redux'; 2 | import { apiMiddleware } from "redux-api-middleware"; 3 | // @ts-ignore 4 | import reducers from './reducers'; 5 | import {routerMiddleware} from "connected-react-router"; 6 | import thunkMiddleware from 'redux-thunk'; 7 | import { createLogger } from 'redux-logger'; 8 | 9 | // @ts-ignore 10 | const devTools = window.__REDUX_DEVTOOLS_EXTENSION__ ? window.__REDUX_DEVTOOLS_EXTENSION__({name: 'Armanco'}) : (f) => f; 11 | 12 | const logger = createLogger({ 13 | collapsed: true, 14 | }); 15 | 16 | export default function initStore() { 17 | const middlewares = [ 18 | apiMiddleware, 19 | thunkMiddleware, 20 | // @ts-ignore 21 | routerMiddleware(), 22 | logger 23 | ]; 24 | 25 | return createStore( 26 | reducers, 27 | compose(applyMiddleware(...middlewares), devTools), 28 | ); 29 | }; 30 | -------------------------------------------------------------------------------- /src/serviceWorker.ts: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | type Config = { 24 | onSuccess?: (registration: ServiceWorkerRegistration) => void; 25 | onUpdate?: (registration: ServiceWorkerRegistration) => void; 26 | }; 27 | 28 | export function register(config?: Config) { 29 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 30 | // The URL constructor is available in all browsers that support SW. 31 | const publicUrl = new URL( 32 | process.env.PUBLIC_URL, 33 | window.location.href 34 | ); 35 | if (publicUrl.origin !== window.location.origin) { 36 | // Our service worker won't work if PUBLIC_URL is on a different origin 37 | // from what our page is served on. This might happen if a CDN is used to 38 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 39 | return; 40 | } 41 | 42 | window.addEventListener('load', () => { 43 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 44 | 45 | if (isLocalhost) { 46 | // This is running on localhost. Let's check if a service worker still exists or not. 47 | checkValidServiceWorker(swUrl, config); 48 | 49 | // Add some additional logging to localhost, pointing developers to the 50 | // service worker/PWA documentation. 51 | navigator.serviceWorker.ready.then(() => { 52 | console.log( 53 | 'This web app is being served cache-first by a service ' + 54 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 55 | ); 56 | }); 57 | } else { 58 | // Is not localhost. Just register service worker 59 | registerValidSW(swUrl, config); 60 | } 61 | }); 62 | } 63 | } 64 | 65 | function registerValidSW(swUrl: string, config?: Config) { 66 | navigator.serviceWorker 67 | .register(swUrl) 68 | .then(registration => { 69 | registration.onupdatefound = () => { 70 | const installingWorker = registration.installing; 71 | if (installingWorker == null) { 72 | return; 73 | } 74 | installingWorker.onstatechange = () => { 75 | if (installingWorker.state === 'installed') { 76 | if (navigator.serviceWorker.controller) { 77 | // At this point, the updated precached content has been fetched, 78 | // but the previous service worker will still serve the older 79 | // content until all client tabs are closed. 80 | console.log( 81 | 'New content is available and will be used when all ' + 82 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 83 | ); 84 | 85 | // Execute callback 86 | if (config && config.onUpdate) { 87 | config.onUpdate(registration); 88 | } 89 | } else { 90 | // At this point, everything has been precached. 91 | // It's the perfect time to display a 92 | // "Content is cached for offline use." message. 93 | console.log('Content is cached for offline use.'); 94 | 95 | // Execute callback 96 | if (config && config.onSuccess) { 97 | config.onSuccess(registration); 98 | } 99 | } 100 | } 101 | }; 102 | }; 103 | }) 104 | .catch(error => { 105 | console.error('Error during service worker registration:', error); 106 | }); 107 | } 108 | 109 | function checkValidServiceWorker(swUrl: string, config?: Config) { 110 | // Check if the service worker can be found. If it can't reload the page. 111 | fetch(swUrl, { 112 | headers: { 'Service-Worker': 'script' } 113 | }) 114 | .then(response => { 115 | // Ensure service worker exists, and that we really are getting a JS file. 116 | const contentType = response.headers.get('content-type'); 117 | if ( 118 | response.status === 404 || 119 | (contentType != null && contentType.indexOf('javascript') === -1) 120 | ) { 121 | // No service worker found. Probably a different app. Reload the page. 122 | navigator.serviceWorker.ready.then(registration => { 123 | registration.unregister().then(() => { 124 | window.location.reload(); 125 | }); 126 | }); 127 | } else { 128 | // Service worker found. Proceed as normal. 129 | registerValidSW(swUrl, config); 130 | } 131 | }) 132 | .catch(() => { 133 | console.log( 134 | 'No internet connection found. App is running in offline mode.' 135 | ); 136 | }); 137 | } 138 | 139 | export function unregister() { 140 | if ('serviceWorker' in navigator) { 141 | navigator.serviceWorker.ready 142 | .then(registration => { 143 | registration.unregister(); 144 | }) 145 | .catch(error => { 146 | console.error(error.message); 147 | }); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/setupTests.ts: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "strict": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "module": "esnext", 16 | "moduleResolution": "node", 17 | "resolveJsonModule": true, 18 | "isolatedModules": true, 19 | "noEmit": true, 20 | "jsx": "react" 21 | }, 22 | "include": [ 23 | "src" 24 | ] 25 | } 26 | --------------------------------------------------------------------------------