UNPKG

@open-tender/cloud

Version:

A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our cloud-based Order API.

56 lines (55 loc) 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.postsReducer = exports.selectPosts = exports.resetPosts = exports.fetchPosts = void 0; const tslib_1 = require("tslib"); const toolkit_1 = require("@reduxjs/toolkit"); const initialState = { posts: [], loading: 'idle', error: null }; exports.fetchPosts = (0, toolkit_1.createAsyncThunk)('posts/getPosts', (slug, { getState, rejectWithValue }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const { api } = getState().config; if (!api) return; try { const api = getState().config.api; if (!api) return null; const { data } = yield api.getPosts(slug); return data; } catch (err) { const error = err; return rejectWithValue(error); } })); const postsSlice = (0, toolkit_1.createSlice)({ name: 'posts', initialState: initialState, reducers: { resetPosts: () => initialState }, extraReducers: builder => { builder .addCase(exports.fetchPosts.fulfilled, (state, action) => { state.posts = action.payload || []; state.loading = 'idle'; state.error = null; }) .addCase(exports.fetchPosts.pending, state => { state.loading = 'pending'; }) .addCase(exports.fetchPosts.rejected, (state, action) => { state.loading = 'idle'; state.error = action.payload; }); } }); exports.resetPosts = postsSlice.actions.resetPosts; const selectPosts = (state) => { const { posts, loading, error } = state.posts; return { posts, loading, error }; }; exports.selectPosts = selectPosts; exports.postsReducer = postsSlice.reducer;