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

change class component into functional component #20

Open
wants to merge 2 commits 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
15,322 changes: 0 additions & 15,322 deletions package-lock.json

This file was deleted.

44 changes: 0 additions & 44 deletions src/App.js

This file was deleted.

61 changes: 61 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';
import { ChangeEvent } from 'react';

import SearchBox from './components/search-box/search-box.component';
import CardList from './components/card-list/card-list.component';

//tsfication
import { getData } from './components/utils/data.utils';

//hooks
import { useState, useEffect } from 'react';

import './App.css';

export type Monster = {
id: string;
name: string;
email: string;
};

const App = () => {
//initial value
const [monsters, setMonsters] = useState<Monster[]>([]);
const [searchField, setSearchField] = useState('');
const [filterMonster, setFilterMonster] = useState(monsters);

useEffect(() => {
const fetchUsers = async () => {
const users = await getData<Monster[]>(
'https://jsonplaceholder.typicode.com/users'
);
setMonsters(users);
};

fetchUsers();
}, []);

//changeEvent takes an argument
const onSearchChange = (e: ChangeEvent<HTMLInputElement>): void => {
const searchFieldString = e.target.value.toLocaleLowerCase();
setSearchField(searchFieldString);
};

useEffect(() => {
const newFilteredMonsters = monsters.filter((monster) =>
monster.name.toLowerCase().includes(searchField.toLowerCase())
);

setFilterMonster(newFilteredMonsters);
}, [searchField, monsters]);

return (
<div className='App'>
<h1>Monsters Rolodex</h1>
<SearchBox onSearchChange={onSearchChange} />
<CardList monsters={filterMonster} />
</div>
);
};

export default App;
13 changes: 0 additions & 13 deletions src/components/card-list/card-list.component.jsx

This file was deleted.

19 changes: 19 additions & 0 deletions src/components/card-list/card-list.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import Card from '../card/card.component';

import './card-list.styles.css';

import { Monster } from '../../App';

type CardListTs = {
monsters: Monster[];
};
const CardList = ({ monsters }: CardListTs) => (
<div className='card-list'>
{monsters.map((monster) => (
<Card key={monster.id} monster={monster} />
))}
</div>
);

export default CardList;
14 changes: 0 additions & 14 deletions src/components/card/card.component.jsx

This file was deleted.

23 changes: 23 additions & 0 deletions src/components/card/card.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import './card.styles.css';

import { Monster } from '../../App';

type CardCompo = {
monster: Monster;
};
const Card = (props: CardCompo) => {
const { id, name, email } = props.monster;
return (
<div className='card-container'>
<img
alt='monster'
src={`https://robohash.org/${id}?set=set2&size=180x180`}
/>
<h2> {name} </h2>
<p> {email} </p>
</div>
);
};

export default Card;
12 changes: 0 additions & 12 deletions src/components/search-box/search-box.component.jsx

This file was deleted.

20 changes: 20 additions & 0 deletions src/components/search-box/search-box.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import './search-box.styles.css';
// TYPESCRIPT
import { ChangeEventHandler } from 'react';

// objects started with type keywords in react
type SearchBoxProps = {
onSearchChange: ChangeEventHandler<HTMLInputElement>;
};

const SearchBox = ({ onSearchChange }: SearchBoxProps) => (
<input
className='search-box'
type='search'
placeholder='search monsters'
onChange={onSearchChange}
/>
);

export default SearchBox;
4 changes: 4 additions & 0 deletions src/components/utils/data.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const getData = async <T>(url: string): Promise<T> => {
const response = await fetch(url);
return response.json();
};
1 change: 1 addition & 0 deletions src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="react-scripts" />
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"]
}
Loading