Skip to content

Commit

Permalink
cree slices para post y coment
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdlg committed Oct 13, 2024
1 parent efeb6c4 commit 406a79f
Show file tree
Hide file tree
Showing 16 changed files with 3,471 additions and 26 deletions.
3,343 changes: 3,343 additions & 0 deletions social-network/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion social-network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"preview": "vite preview"
},
"dependencies": {
"@reduxjs/toolkit": "^2.2.8",
"react": "^18.3.1",
"react-dom": "^18.3.1"
"react-dom": "^18.3.1",
"react-redux": "^9.1.2"
},
"devDependencies": {
"@eslint/js": "^9.11.1",
Expand Down
27 changes: 3 additions & 24 deletions social-network/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import { useSelector } from 'react-redux'

function App() {
const [count, setCount] = useState(0)

const post = useSelector((state) => state.post);
return (
<>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.jsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
<h1>Hola</h1>
</>
)
}
Expand Down
File renamed without changes.
6 changes: 5 additions & 1 deletion social-network/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import App from './App.jsx'
import './index.css'
import { store } from './redux/store.js'
import {Provider} from 'react-redux';

createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
<Provider store = {store}>
<App />
</Provider>
</StrictMode>,
)
8 changes: 8 additions & 0 deletions social-network/src/redux/slice/coments/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import state from "../coments/state";
import { ACTIONSCOMENTS } from "./types";

export default{
[ACTIONSCOMENTS.SET_STATUS]:(state, action)=>{
state.status = action.payload;
}
};
25 changes: 25 additions & 0 deletions social-network/src/redux/slice/coments/slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createSlice } from "@reduxjs/toolkit"

const initialState = {
coment:{
postId: null,
id: null,
name: '',
email: '',
body: ''
}
}

export const comentSlice = createSlice(
{
name: 'coment',
initialState,
reducers:{
setComent:(state, action)=>{
state.coment = action.payload;
}
}
});

export const {setComent} = comentSlice.actions;
export default comentSlice.reducer;
8 changes: 8 additions & 0 deletions social-network/src/redux/slice/coments/state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
status:{
title:'Success information',
type:'success'
},
condition: 'no init',
coments:[]
}
9 changes: 9 additions & 0 deletions social-network/src/redux/slice/coments/thunks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createAsyncThunk } from "@reduxjs/toolkit";
import { ACTIONSCOMENTS } from "./types";

export const thunks = {
[ACTIONSCOMENTS.FETCH_COMENTS]: createAsyncThunk({id},'coments/fetchComents', async()=>{
const response = await fetch(`https://jsonplaceholder.typicode.com/comments?postId=`+{id});
return response.json();
}),
};
4 changes: 4 additions & 0 deletions social-network/src/redux/slice/coments/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const ACTIONSCOMENTS = {
SET_STATUS: 'SET_STATUS',
FETCH_COMENTS: 'FETCH_COMENTS'
};
8 changes: 8 additions & 0 deletions social-network/src/redux/slice/posts/reducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import state from "./state";
import { ACTIONSPOST } from "./types";

export default{
[ACTIONSPOST.SET_STATUS]:(state, action)=>{
state.status = action.payload;
},
};
24 changes: 24 additions & 0 deletions social-network/src/redux/slice/posts/slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createSlice } from "@reduxjs/toolkit";

const initialState = {
post:{
userId: null,
id: null,
title: '',
body: ''
}
}

export const postSlice = createSlice(
{
name: 'post',
initialState,
reducers:{
setPost:(state, action)=>{
state.post = action.payload;
},
},
});

export const {setPost} = postSlice.actions;
export default postSlice.reducer;
8 changes: 8 additions & 0 deletions social-network/src/redux/slice/posts/state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
status:{
title:'Success information',
type:'success'
},
condition: 'no init',
posts:[]
}
9 changes: 9 additions & 0 deletions social-network/src/redux/slice/posts/thunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createAsyncThunk } from "@reduxjs/toolkit";
import { ACTIONSPOST } from "./types";

export const thunks = {
[ACTIONSPOST.FETCH_POST]: createAsyncThunk('post/fetchPosts', async()=>{
const response = await fetch(`https://jsonplaceholder.typicode.com/posts`);
return response.json();
}),
};
4 changes: 4 additions & 0 deletions social-network/src/redux/slice/posts/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const ACTIONSPOST = {
SET_STATUS: 'SET_STATUS',
FETCH_POST: 'FETCH_POST'
};
10 changes: 10 additions & 0 deletions social-network/src/redux/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {configureStore} from '@reduxjs/toolkit';
import postSlice from './slice/posts/slice';
import { comentSlice } from './slice/coments/slice';

export const store = configureStore({
reducers:{
post: postSlice,
coment : comentSlice
}
});

0 comments on commit 406a79f

Please sign in to comment.