upt

parent 03ea679c
......@@ -9,6 +9,7 @@
"version": "0.0.0",
"dependencies": {
"@reduxjs/toolkit": "^2.5.0",
"axios": "^1.7.9",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-redux": "^9.2.0",
......@@ -1738,6 +1739,23 @@
"dev": true,
"license": "Python-2.0"
},
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==",
"license": "MIT"
},
"node_modules/axios": {
"version": "1.7.9",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.7.9.tgz",
"integrity": "sha512-LhLcE7Hbiryz8oMDdDptSrWowmB4Bl6RCt6sIJKpRB4XtVf0iEgewX3au/pJqm+Py1kCASkb/FFKjxQaLtxJvw==",
"license": "MIT",
"dependencies": {
"follow-redirects": "^1.15.6",
"form-data": "^4.0.0",
"proxy-from-env": "^1.1.0"
}
},
"node_modules/balanced-match": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
......@@ -1870,6 +1888,18 @@
"dev": true,
"license": "MIT"
},
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"license": "MIT",
"dependencies": {
"delayed-stream": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
......@@ -1940,6 +1970,15 @@
"dev": true,
"license": "MIT"
},
"node_modules/delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
"license": "MIT",
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/electron-to-chromium": {
"version": "1.5.86",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.86.tgz",
......@@ -2313,6 +2352,40 @@
"dev": true,
"license": "ISC"
},
"node_modules/follow-redirects": {
"version": "1.15.9",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz",
"integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"license": "MIT",
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/form-data": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.1.tgz",
"integrity": "sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==",
"license": "MIT",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/fsevents": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
......@@ -2627,6 +2700,27 @@
"node": ">=8.6"
}
},
"node_modules/mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime-types": {
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"license": "MIT",
"dependencies": {
"mime-db": "1.52.0"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/minimatch": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz",
......@@ -2822,6 +2916,12 @@
"node": ">= 0.8.0"
}
},
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==",
"license": "MIT"
},
"node_modules/punycode": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",
......
......@@ -11,6 +11,7 @@
},
"dependencies": {
"@reduxjs/toolkit": "^2.5.0",
"axios": "^1.7.9",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-redux": "^9.2.0",
......
import axios from 'axios'
export const axiosBase = axios.create({
baseURL: 'https://ajs-21-default-rtdb.firebaseio.com/'
})
\ No newline at end of file
import { addRandomContact } from "../store/slices/contact.slice";
import { useNavigate } from "react-router-dom";
import { changeTitle } from "../store/slices/home.slice";
import { useAppDispatch, useAppSelector } from "../store/hooks";
import { useAppSelector, useAppDispatch } from "../store/hooks"
import { getHome, postHome } from "../store/slices/home/home.async"
const Home = () => {
const dispatch = useAppDispatch()
const navigate = useNavigate()
const state = useAppSelector(state => state)
const home = useAppSelector(state => state.home)
console.log(state, "HOME");
const onClick = () => {
dispatch(addRandomContact())
if(home.isLoad) return <div>Loading...</div>
const getHandler = () => {
dispatch(getHome())
}
const onEditTitle = () => {
dispatch(changeTitle('HOME SLICE'))
const postHandler = () => {
dispatch(postHome('POST ASYNC TEST'))
}
return (
<div>
{state.home.homeTitle}
<button onClick={onClick}>add random contact</button>
<button onClick={onEditTitle}>edit</button>
<button onClick={() => navigate('/contacts')}>navigate</button>
<button onClick={postHandler}>POST</button>
<button onClick={getHandler}>GET</button>
</div>
)
}
......
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
interface IState {
homeTitle: string
}
const initialState: IState = {
homeTitle: ''
}
export const homeSlice = createSlice({
name: 'home',
initialState,
reducers: {
changeTitle: (state, action: PayloadAction<string>) => {
state.homeTitle = action.payload
}
}
})
export const {changeTitle} = homeSlice.actions
\ No newline at end of file
import { createAsyncThunk } from "@reduxjs/toolkit";
import { axiosBase } from "../../../config/axiosBase";
export const getHome = createAsyncThunk(
'home/getHome',
async () => {
const {data} = await axiosBase.get('home.json')
return data
}
)
export const postHome = createAsyncThunk(
'home/postHome',
async (text: string) => {
const {data} = await axiosBase.post('home.json', {text})
return data
}
)
\ No newline at end of file
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { getHome, postHome } from './home.async'
interface IState {
homeTitle: string
isLoad: boolean
}
const initialState: IState = {
homeTitle: '',
isLoad: false
}
export const homeSlice = createSlice({
name: 'home',
initialState,
reducers: {
changeTitle: (state, action: PayloadAction<string>) => {
state.homeTitle = action.payload
}
},
extraReducers(builder) {
builder
.addCase(getHome.pending, (state) => {
state.isLoad = true
})
.addCase(getHome.rejected, (state) => {
state.isLoad = false
})
.addCase(getHome.fulfilled, (state, action) => {
console.log(action.payload)
state.isLoad = false
})
// POST HOME
.addCase(postHome.pending, (state) => {
state.isLoad = true
})
.addCase(postHome.rejected, (state) => {
state.isLoad = false
})
.addCase(postHome.fulfilled, (state, action) => {
console.log(action.payload)
state.isLoad = false
})
},
})
export const {changeTitle} = homeSlice.actions
\ No newline at end of file
import { configureStore } from '@reduxjs/toolkit'
import { contactSlice } from './slices/contact.slice'
import { homeSlice } from './slices/home.slice'
import { contactSlice } from './slices/contact/contact.slice'
import { homeSlice } from './slices/home/home.slice'
const store = configureStore({
reducer: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment