Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Sep 17, 2024
1 parent fa5d711 commit d727d81
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 7 deletions.
26 changes: 26 additions & 0 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Accessory } from '../utils/types/Accessory';
import { Phone } from '../utils/types/Phone';
import { Product } from '../utils/types/Product';
import { Tablet } from '../utils/types/Tablet';

const BASE_URL =
'https://raw.githubusercontent.com/mate-academy/react_phone-catalog/f064fa3751d4adbc9a531a51805d593af585860b/public/';

function wait(delay: number): Promise<void> {
return new Promise(resolve => {
setTimeout(resolve, delay);
});
}

function get<T>(url: string): Promise<T> {
const fullUrl = BASE_URL + url + '.json';

return wait(300)
.then(() => fetch(fullUrl))
.then(res => res.json());
}

export const getPhones = () => get<Phone[]>('/api/phones');
export const getTablets = () => get<Tablet[]>('/api/tablets');
export const getProducts = () => get<Product[]>('/api/products');
export const getAccessories = () => get<Accessory[]>('/api/accessories');
7 changes: 3 additions & 4 deletions src/app/reduxHooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TypedUseSelectorHook, useDispatch } from "react-redux";
import { AppDispatch, RootState } from "./store";
import { useSelector } from "react-redux";

import { TypedUseSelectorHook, useDispatch } from 'react-redux';
import { AppDispatch, RootState } from './store';
import { useSelector } from 'react-redux';

export const useAppDispatch: () => AppDispatch = useDispatch;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
14 changes: 14 additions & 0 deletions src/app/slices/accessoriesSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { Accessory } from '../../utils/types/Accessory';

const initialState = null as Accessory[] | null;

export const accessoriesSlice = createSlice({
name: 'accessories',
initialState: initialState,
reducers: {
setAccessories: (_, action: PayloadAction<Accessory[]>) => action.payload,
},
});

export const accessoriesActions = accessoriesSlice.actions;
14 changes: 14 additions & 0 deletions src/app/slices/phonesSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { Phone } from '../../utils/types/Phone';

const initialState = null as Phone[] | null;

export const phonesSlice = createSlice({
name: 'phones',
initialState: initialState,
reducers: {
setPhones: (_, action: PayloadAction<Phone[]>) => action.payload,
},
});

export const phonesActions = phonesSlice.actions;
14 changes: 14 additions & 0 deletions src/app/slices/productsSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { Product } from '../../utils/types/Product';

const initialState = null as Product[] | null;

export const productsSlice = createSlice({
name: 'products',
initialState: initialState,
reducers: {
setProducts: (_, action: PayloadAction<Product[]>) => action.payload,
},
});

export const productsActions = productsSlice.actions;
14 changes: 14 additions & 0 deletions src/app/slices/tabletsSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { Tablet } from '../../utils/types/Tablet';

const initialState = null as Tablet[] | null;

export const tabletsSlice = createSlice({
name: 'tablets',
initialState: initialState,
reducers: {
setTablets: (_, action: PayloadAction<Tablet[]>) => action.payload,
},
});

export const tabletsActions = tabletsSlice.actions;
16 changes: 13 additions & 3 deletions src/app/store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { combineSlices, configureStore } from "@reduxjs/toolkit";
import { combineSlices, configureStore } from '@reduxjs/toolkit';
import { accessoriesSlice } from './slices/accessoriesSlice';
import { phonesSlice } from './slices/phonesSlice';
import { productsSlice } from './slices/productsSlice';
import { tabletsSlice } from './slices/tabletsSlice';

const rootReducer = combineSlices(
accessoriesSlice,
phonesSlice,
productsSlice,
tabletsSlice,
);

const rootReducer = combineSlices();
export const store = configureStore({
reducer: rootReducer,
});

export type RootState = ReturnType<typeof rootReducer>;
export type AppDispatch = typeof store.dispatch;
export type AppDispatch = typeof store.dispatch;

0 comments on commit d727d81

Please sign in to comment.