feat: add help-urls (#585)
This commit is contained in:
committed by
GitHub
parent
ed2eed5110
commit
a1793efcc0
9
src/help-urls/data/api.js
Normal file
9
src/help-urls/data/api.js
Normal file
@@ -0,0 +1,9 @@
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
import { camelCaseObject, getConfig } from '@edx/frontend-platform';
|
||||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
|
||||
|
||||
export async function getHelpUrls() {
|
||||
const { data } = await getAuthenticatedHttpClient()
|
||||
.get(`${getConfig().STUDIO_BASE_URL}/api/contentstore/v1/help_urls`);
|
||||
return camelCaseObject(data);
|
||||
}
|
||||
13
src/help-urls/data/selectors.js
Normal file
13
src/help-urls/data/selectors.js
Normal file
@@ -0,0 +1,13 @@
|
||||
export const selectHelpUrlsByNames = (names) => (state) => {
|
||||
const urlsDictionary = {};
|
||||
|
||||
names.forEach(name => {
|
||||
urlsDictionary[name] = state.helpUrls.pages[name] || null;
|
||||
});
|
||||
|
||||
return urlsDictionary;
|
||||
};
|
||||
|
||||
export const getPages = (state) => state.helpUrls.pages;
|
||||
|
||||
export const getLoadingHelpUrlsStatus = (state) => state.helpUrls.loadingHelpUrlsStatus;
|
||||
27
src/help-urls/data/slice.js
Normal file
27
src/help-urls/data/slice.js
Normal file
@@ -0,0 +1,27 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
|
||||
const initialState = {
|
||||
loadingHelpUrlsStatus: '',
|
||||
pages: {},
|
||||
};
|
||||
|
||||
const slice = createSlice({
|
||||
name: 'helpUrls',
|
||||
initialState,
|
||||
reducers: {
|
||||
updatePages: (state, { payload }) => {
|
||||
state.pages = payload;
|
||||
},
|
||||
updateLoadingHelpUrlsStatus: (state, { payload }) => {
|
||||
state.loadingHelpUrlsStatus = payload.status;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const {
|
||||
updatePages,
|
||||
updateLoadingHelpUrlsStatus,
|
||||
} = slice.actions;
|
||||
|
||||
export const { reducer } = slice;
|
||||
24
src/help-urls/data/thunks.js
Normal file
24
src/help-urls/data/thunks.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import { RequestStatus } from '../../data/constants';
|
||||
|
||||
import { getHelpUrls } from './api';
|
||||
import { updateLoadingHelpUrlsStatus, updatePages } from './slice';
|
||||
|
||||
/* eslint-disable import/prefer-default-export */
|
||||
export function fetchHelpUrls() {
|
||||
return async (dispatch) => {
|
||||
dispatch(updateLoadingHelpUrlsStatus({ status: RequestStatus.IN_PROGRESS }));
|
||||
|
||||
try {
|
||||
const urls = await getHelpUrls();
|
||||
|
||||
dispatch(updatePages(urls));
|
||||
|
||||
dispatch(updateLoadingHelpUrlsStatus({ status: RequestStatus.SUCCESSFUL }));
|
||||
return true;
|
||||
} catch (error) {
|
||||
dispatch(updateLoadingHelpUrlsStatus({ status: RequestStatus.FAILED }));
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
22
src/help-urls/hooks.jsx
Normal file
22
src/help-urls/hooks.jsx
Normal file
@@ -0,0 +1,22 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import { isEmpty } from 'lodash';
|
||||
|
||||
import { fetchHelpUrls } from './data/thunks';
|
||||
import { getPages, selectHelpUrlsByNames } from './data/selectors';
|
||||
|
||||
const useHelpUrls = (tokenNames) => {
|
||||
const dispatch = useDispatch();
|
||||
const helpTokens = useSelector(selectHelpUrlsByNames(tokenNames));
|
||||
const pages = useSelector(getPages);
|
||||
|
||||
useEffect(() => {
|
||||
if (isEmpty(pages)) {
|
||||
dispatch(fetchHelpUrls());
|
||||
}
|
||||
}, []);
|
||||
|
||||
return helpTokens;
|
||||
};
|
||||
/* eslint-disable-next-line import/prefer-default-export */
|
||||
export { useHelpUrls };
|
||||
@@ -13,6 +13,7 @@ import { reducer as filesReducer } from './files-and-uploads/data/slice';
|
||||
import { reducer as courseTeamReducer } from './course-team/data/slice';
|
||||
import { reducer as CourseUpdatesReducer } from './course-updates/data/slice';
|
||||
import { reducer as processingNotificationReducer } from './generic/processing-notification/data/slice';
|
||||
import { reducer as helpUrlsReducer } from './help-urls/data/slice';
|
||||
|
||||
export default function initializeStore(preloadedState = undefined) {
|
||||
return configureStore({
|
||||
@@ -30,6 +31,7 @@ export default function initializeStore(preloadedState = undefined) {
|
||||
courseTeam: courseTeamReducer,
|
||||
courseUpdates: CourseUpdatesReducer,
|
||||
processingNotification: processingNotificationReducer,
|
||||
helpUrls: helpUrlsReducer,
|
||||
},
|
||||
preloadedState,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user