Skip to content

Commit

Permalink
Create postReducer.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ExLuZiVe53 committed Oct 28, 2023
1 parent ae706e4 commit d62688e
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/redux/postReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createAsyncThunk } from '@reduxjs/toolkit';
import { findPostById } from 'services/api';

// !==============================DAL (Data Accsess Layer)=================
// створюємо асинхрону санку, яка приймає в собі два обов'язкові агрументи:
// 1) Так званий префікс санки, тобто кожна THUNK має мати унікальний префікс
// 2) Асинхрона колбек функція 'async () => {}', дана функція приймає якісь дані (data) та thunkApi

export const requestPostDetails = createAsyncThunk(
'postDetails/get',

async (postId, thunkApi) => {
try {
// робимо мережевий запит
const postData = await findPostById(postId);
return postData; //Буде записано в action.payload
} catch (error) {
// після невдалого запиту ми повернемо thunkApi та викличемо спеціальний метод rejectWithVale і передамо об'єкт помилки
return thunkApi.rejectWithValue(error.message);
// rejected - виникне тільки коли ми руками переведемо проміс
}
}
);

// !========================End/ DAL =================================

// створюємо початковий стан state
const INITIAL_STATE = {
postDetailsData: null,
isLoading: false,
error: null,
posts: [],
};

0 comments on commit d62688e

Please sign in to comment.