37 | );
38 | }
39 |
40 | // route işlemini istediğimiz ana componentde yapabiliriz.
41 | // biz roota tıklayınca, productList componenti değişcek. o yüzden bir root'u productlis kısmını silip oraya ekleyeğic
42 | //(Grid.column withd={12} kısmının içi)
43 |
44 | // rout'un çalışabilmesi için,bizim ana birleşinizimi browser rooter ile sarmalamamız gerekli.=> index.js deki app
45 |
46 | // sana ana sayfa dediğimde(path olarak) bana bunu ver demiş oluyoruz.
47 | // exact => tam yol demek. default'u true.
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/src/store/reducers/cartReducer.js:
--------------------------------------------------------------------------------
1 | import { ADD_TO_CART, REMOVE_FROM_CART } from "../actions/cartActions";
2 | import { cartItems } from "../initialValues/cartItems";
3 |
4 | const initialState = {
5 | cartItems: cartItems,
6 | // x:1,
7 | // y:2 // birden çok eleman olabilir burada. o yüzden aşağıda cartItems'ı ayırdım
8 | };
9 |
10 | // gönderdiğim aksiyona göre sepetin son halini tuttuğumuz yer.
11 | // state=> başlangıç değerim
12 |
13 | export default function cartReducer(state = initialState, { type, payload }) {
14 | switch (type) {
15 |
16 | case ADD_TO_CART:
17 | let product = state.cartItems.find(c=> c.product.id === payload.id)
18 |
19 | if (product) {
20 | product.quantity ++; // bunu yapınca bir bir artar quantity. ama referanslar değişmez. yani sepeti güncellemez. bu yüzden refransı
21 | return{ // güncellemek gerek. o yüzden spread operatörü gerekli
22 |
23 | ...state
24 | // yepyeni bir cart objesini döndürmüş oluyorsun. newliyorsun bir nevi.
25 | }
26 | } else {
27 |
28 | return { // sepette eleman yoksa, yepyeni bir obje oluştur. o zaman mevcut cartItems a yeni bir eleman ekleyip, yeni array yapacam.
29 |
30 | ...state, // cartItems'ı ayırdım. sonra yeni bir array oluşturdum
31 | cartItems:[...state.cartItems,{quantity:1, product:payload}] // sadece bir elamanı değiştirmek istediğim için bu hareketi yapıyorum
32 | // [state'deki cartItems'ı spread et dedim]
33 | // payload'lada yenisi ekledim.
34 |
35 | }
36 |
37 | }
38 |
39 | case REMOVE_FROM_CART:
40 | return{
41 | ...state,
42 | cartItems:state.cartItems.filter(c=> c.product.id===payload.id) // eşit olanları filtreliyor. filter a yeni bir array oluştur. filtreme göre diyorum.
43 |
44 | }
45 |
46 | default:
47 | return state; // state'in kendisini döndür.
48 | }
49 | }
50 |
51 | // gönderilecek olan type a göre state'i değiştirmek gerekiyor. redux bunu bir şekilde kendi mimarisinde implement ediyor
52 |
53 | // mutebility : sepet elemanlarının değiştiğini redux, (bu hooklarda da geçerli) eğer referansı değiştiyse,
54 | // ben sepete yeni eleman eklendi yada silindi olarak anlarım diyor. sepetin değiştiğini anlarım diyor. o yüzden
55 | // sen benim abonelerimi bilgilendirmen için, ilgi state'in referansını değiştir diyor.
56 |
57 | // bu yüzden benim referansı değiştirmem gerek. o yüzden if blogu is coming
58 |
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/layouts/Navi.jsx:
--------------------------------------------------------------------------------
1 | import React,{useState} from "react";
2 | import { useHistory } from "react-router-dom";
3 | import { Container, Menu } from "semantic-ui-react";
4 | import CartSummary from "./CartSummary";
5 | import SignedIn from "./SignedIn";
6 | import SignedOut from "./SignedOut";
7 | import { useSelector } from "react-redux";
8 |
9 | export default function Navi() {
10 | // const diyerek destructor işlemi gerçekleştirecez => initialState öncekinde ürünler olduğu için, state'i boş arraydi. fakat bu durumda
11 | // içerisinde birşeyler olduğundan(state durumu) bunun da default'u ya truedur ya da false dur.
12 |
13 | const [isAuthenticated, setIsAuthenticated] = useState(true)
14 | const history = useHistory()
15 | const {cartItems} = useSelector(state => state.cart)
16 |
17 | function handleSignOut(){ // çıkış yapma işlemini handle edecez.
18 |
19 | setIsAuthenticated(false) // bu fonksiyonu çağıracak olan da alt component
20 | // offline olunca, product sayfasına geri dönmek için, burada useHistory'i kullanıyoruz
21 |
22 | history.push("/")
23 | }
24 |
25 | function handleSignIn() {
26 | setIsAuthenticated(true)
27 | }
28 |
29 | return (
30 |
31 |
53 |
54 | );
55 | }
56 |
57 | // eğer kişi authentice olduysa signed in i göster, authentice olmamışsa sign-out u göster diyor.
58 | // buna karar veren navi. state'i tutan navi. (Fatiq'in state mevzusu buradan geliyor.)
59 |
60 | // ** reactta bir componente, senin bir şarta göre birşeyler yapman gerekiyorsa, bir datayı tutman gerekiyorsa,
61 | // orada aklına state gelecek. yani o componentin datası. bu işin en basic kısmı. biz bir sonraki aşamada alt componentte data taşıycaz.
62 |
63 |
64 | // hangisini gösterceğine karar veren navi olduğundan senin state bilgisini burada tutman gerek
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `npm start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13 |
14 | The page will reload if you make edits.\
15 | You will also see any lint errors in the console.
16 |
17 | ### `npm test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `npm run build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `npm run eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35 |
36 | 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.
37 |
38 | 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.
39 |
40 | 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.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
48 | ### Code Splitting
49 |
50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51 |
52 | ### Analyzing the Bundle Size
53 |
54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55 |
56 | ### Making a Progressive Web App
57 |
58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59 |
60 | ### Advanced Configuration
61 |
62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63 |
64 | ### Deployment
65 |
66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67 |
68 | ### `npm run build` fails to minify
69 |
70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
71 |
--------------------------------------------------------------------------------
/src/pages/ProductList.jsx:
--------------------------------------------------------------------------------
1 | import React, { useState, useEffect } from "react";
2 | import { Icon, Menu, Button, Table } from "semantic-ui-react";
3 | import ProductService from "../Services/productService";
4 | import {Link} from "react-router-dom";
5 | import { useDispatch } from "react-redux";
6 | import {addToCart} from "../store/actions/cartActions"
7 | import { toast } from "react-toastify";
8 |
9 | export default function ProductList() {
10 |
11 | const dispatch = useDispatch() // bu benden çalıştıracağım fonksiyonun ismini istiyor
12 |
13 | const handleAddToCart=(product)=>{
14 | dispatch(addToCart(product))
15 | toast.success(`${product.productName} sepete eklendi !`,{ position: toast.POSITION.BOTTOM_RIGHT,})
16 |
17 | }
18 |
19 | const [products, setProducts] = useState([]);
20 |
21 | // const [state, setstate] = useState(initialState) // intialState başlangıç durumu. datanın başlangıcı boş array
22 |
23 | // sol taraf bir fonksiyon. sağ taraftaki ise bomboş bir array almış
24 | // useState bize bir nesne döndürüyor. onu da destructor edecez. döndürdüğü yerde bir data bir de fonksiyon döndürüyor.
25 |
26 | // benim products diye bir datam var. default değeri boş bir array. setProducts ise bunun hook'u
27 | // lifecycle hook.=> reactın yaşam döngüsüne müdehale etmeye yarıyor.
28 | // lifecycle hookun bir çoğu burada çözülüyor.
29 | useEffect(() => { // component yükelndiğinde yapılmasını istedğin kodu bu bloga yazıyoruz
30 |
31 | let productService = new ProductService()
32 |
33 | productService.getProducts().then(result => setProducts(result.data.data) // axios result da data dışında başka sey de döndürür. o yüzden data nın datası
34 |
35 |
36 | ).catch() // başarılı olursa then. başarısız olursa catch (promise yapısı)
37 |
38 |
39 |
40 | }, []) //=>bunu yapmayınca networke sürekli istekde bulunuyor.
41 | // bir nesnenin her değişikliğe uğradığında sayfanın yeninden render edilmesini istersek,
42 | // burada onu o array içine yazarak (state bilgisini) takibini yapabiliyoruz. aksi takdirde elemanlar değişiğinde yeni istek atıyor.
43 | // ürünler değişiyor. state değiştiği için yeniden istek atıyor vs. hooklarda çalışırken, bunları eklemek önemli.
44 |
45 |
46 | return (
47 | // hareketli kısım kanımca
48 |
100 | );
101 | }
102 |
103 | // ürünler bizim için bu sayfadaki state datasıdır. Bunun için hook tekniğini kullancağız.
104 |
105 |
106 | // aksiyonlara abone olmak için kullanılan hook : useDispatch
107 |
108 | // dispatch : fonksiyon çağırmak için kullanılan fonksiyon (reflection gibi)
109 |
110 |
111 | // onclick'e bu fonksiyonu ata diyorum
--------------------------------------------------------------------------------