-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pros: - [Strongly recomended](https://redux.js.org/style-guide/style-guide#put-as-much-logic-as-possible-in-reducers) - Easier testing - Easier mocking of libraries and use of storybook - More flexible notification handling
- Loading branch information
1 parent
ac03b96
commit 5784fd3
Showing
5 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import axios from 'axios'; | ||
// TODO: Read base url from env | ||
const baseURL = 'https://devbackend.simplq.me/v1'; | ||
|
||
/** | ||
* Async function for for sending request with Auth0 | ||
* | ||
* It combines usRequest and maqeRequest. Since | ||
* @auth0/auth0-react can work only wthin a component, | ||
* the whole auth object created with useAuth() must be | ||
* passed as parameter. | ||
* | ||
* @param {Object} auth object returned by useAuth() from @auth0/auth0-react. | ||
* @param {Object} payload object used in action creators in redux-toolkit. | ||
*/ | ||
const makeAuthedRequest = async (auth, request) => { | ||
// TODO: Is "audience" == "bseURL"? Read from env or use baseURL | ||
const accessToken = auth.isAuthenticated | ||
? await auth.getAccessTokenSilently({ audience: 'https://devbackend.simplq.me/v1' }) | ||
: 'anonymous'; | ||
|
||
return axios({ | ||
baseURL, | ||
...request, | ||
headers: { | ||
...request.headers, | ||
// Add the Authorization header to the existing headers | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}).then((response) => { | ||
return response.data; | ||
}); | ||
}; | ||
|
||
export default makeAuthedRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
import { combineReducers, configureStore } from '@reduxjs/toolkit'; | ||
import { combineReducers, configureStore, getDefaultMiddleware } from '@reduxjs/toolkit'; | ||
import queuesSlice from 'store/queuesSlice'; | ||
import appReducer from './appSlice'; | ||
|
||
export const rootReducer = combineReducers({ | ||
appReducer, | ||
queues: queuesSlice, | ||
}); | ||
|
||
export const store = configureStore({ | ||
reducer: rootReducer, | ||
middleware: getDefaultMiddleware({ | ||
serializableCheck: { | ||
// Ignore auth in async thunks | ||
ignoredActionPaths: ['meta.arg.auth'], | ||
}, | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* eslint-disable no-param-reassign */ | ||
|
||
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'; | ||
import { QueueRequestFactory } from 'api/requestFactory'; | ||
import makeAuthedRequest from 'api/axios-alt'; | ||
|
||
const fetchQueues = createAsyncThunk('queues/requestStatus', async (arg) => { | ||
const { auth } = arg; | ||
const authedRequest = makeAuthedRequest(auth, QueueRequestFactory.getMyQueues()); | ||
const response = await authedRequest; | ||
return response; | ||
}); | ||
|
||
const queuesSlice = createSlice({ | ||
name: 'queues', | ||
initialState: [], | ||
// No reducers for now | ||
reducers: {}, | ||
extraReducers: { | ||
// handle fulfiled request | ||
[fetchQueues.fulfilled]: (state, action) => { | ||
return action.payload.queues; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { queuesFetched } = queuesSlice.actions; | ||
|
||
export { fetchQueues }; | ||
|
||
export default queuesSlice.reducer; |