diff --git a/23_Fetching_Data_Using_Hooks/23_fetching_data_using_hooks_boilerplate/src/index.js b/23_Fetching_Data_Using_Hooks/23_fetching_data_using_hooks_boilerplate/src/index.js index e3a43c2..dfcbd93 100644 --- a/23_Fetching_Data_Using_Hooks/23_fetching_data_using_hooks_boilerplate/src/index.js +++ b/23_Fetching_Data_Using_Hooks/23_fetching_data_using_hooks_boilerplate/src/index.js @@ -1,362 +1,77 @@ -import React, { useState } from 'react' -import ReactDOM from 'react-dom' +import React, { useState, useEffect } from 'react' +import axios from 'axios' +import ReactDOM, { findDOMNode } from 'react-dom' + +const Country = ({ + country: { name, capital, flag, languages, population, currency }, +}) => { + const formattedCapital = + capital.length > 0 ? ( + <> + Capital: + {capital} + + ) : ( + '' + ) + const formattedLanguage = languages.length > 1 ? `Languages` : `Language` -const options = [ - { - value: '', - label: '-- Select Country--', - }, - { - value: 'Finland', - label: 'Finland', - }, - { - value: 'Sweden', - label: 'Sweden', - }, - { - value: 'Norway', - label: 'Norway', - }, - { - value: 'Denmark', - label: 'Denmark', - }, -] - -// mapping the options to list(array) of JSX options - -const selectOptions = options.map(({ value, label }) => ( - -)) + return ( +
+
+ {name} +
+

{name.toUpperCase()}

+
+

{formattedCapital}

+

+ {formattedLanguage}: + {languages.map((language) => language.name).join(', ')} +

+

+ Population: + {population.toLocaleString()} +

+

+ Currency: + {currency} +

+
+
+ ) +} const App = (props) => { - const initialState = { - firstName: '', - lastName: '', - email: '', - title: '', - country: '', - tel: '', - dateOfBirth: '', - favoriteColor: '', - weight: '', - gender: '', - file: '', - bio: '', - skills: { - html: false, - css: false, - javascript: false, - }, - touched: { - firstName: false, - lastName: false, - }, - } - const [formData, setFormData] = useState(initialState) - - const onChange = (e) => { - /* - we can get the name and value like: e.target.name, e.target.value - Wwe can also destructure name and value from e.target - const name = e.target.name - const value = e.target.value - */ - const { name, value, type, checked } = e.target - /* - [variablename] we can make a value stored in a certain variable could be a key for an object, in this case a key for the state - */ - - if (type === 'checkbox') { - setFormData({ - ...formData, - skills: { ...formData.skills, [name]: checked }, - }) - } else if (type === 'file') { - setFormData({ ...formData, [name]: e.target.files[0] }) - } else { - setFormData({ ...formData, [name]: value }) - } - } - const onSubmit = (e) => { - /* - e.preventDefault() - stops the default behavior of form element - specifically refreshing of page - */ - e.preventDefault() - const { - firstName, - lastName, - title, - email, - tel, - dateOfBirth, - favoriteColor, - weight, - country, - gender, - bio, - file, - skills, - } = formData - - const formattedSkills = [] - for (const key in skills) { - console.log(key) - if (skills[key]) { - formattedSkills.push(key.toUpperCase()) - } - } - const data = { - firstName, - lastName, - title, - email, - tel, - dateOfBirth, - favoriteColor, - weight, - country, - gender, - bio, - file, - skills: formattedSkills, - } - /* - the is the place where we connect backend api - to send the data to the database - */ - console.log(data) - } - const onBlur = (e) => { - const { name } = e.target - setFormData({ ...formData, touched: { ...formData.touched, [name]: true } }) - } - const validate = () => { - // Object to collect error feedback and to display on the form - const errors = { - firstName: '', - } - - if ( - (formData.touched.firstName && formData.firstName.length < 3) || - (formData.touched.firstName && formData.firstName.length > 12) - ) { - errors.firstName = 'First name must be between 2 and 12' + // setting initial state and method to update state + const [data, setData] = useState([]) + + useEffect(() => { + fetchData() + }, []) + + const fetchData = async () => { + const url = 'https://restcountries.eu/rest/v2/all' + try { + const response = await fetch(url) + const data = await response.json() + setData(data) + } catch (error) { + console.log(error) } - return errors } - // accessing the state value by destrutcturing the state - const { - firstName, - lastName, - title, - country, - email, - tel, - dateOfBirth, - favoriteColor, - weight, - gender, - bio, - } = formData - - const errors = validate() - return (
-

Add Student

-
-
-
- - -
- {errors.firstName && {errors.firstName}} -
-
- - -
-
- - -
-
- - -
-
- -
- - -
- -
- - -
-
- - -
-
- - -
-
-
- -
- -
-

Gender

-
- - -
-
- - -
-
- - -
-
- -
-

Select your skills

-
- - -
-
- - -
-
- - -
-
-
-
-