From e17e57aba51aeae3269cdebca0a24b6ddc562cfb Mon Sep 17 00:00:00 2001 From: Radi Totev Date: Fri, 1 Jul 2022 19:38:06 +0100 Subject: [PATCH] feat: update keyboard shortcuts client endpoint (#46378) * feat: update keyboard shortcuts client endpoint * Update client/src/redux/settings/settings-sagas.js Co-authored-by: Oliver Eyton-Williams * Update putUpdateMyKeyboardShortcuts return type * Use new saga in shortcuts-modal Co-authored-by: Oliver Eyton-Williams --- .../src/client-only-routes/show-settings.tsx | 3 ++- client/src/redux/index.js | 4 +-- .../src/redux/keyboard-shortcuts-mode-saga.js | 27 ------------------- client/src/redux/settings/action-types.js | 1 + client/src/redux/settings/index.js | 11 ++++++++ client/src/redux/settings/settings-sagas.js | 18 +++++++++++-- .../Challenges/components/shortcuts-modal.tsx | 4 +-- client/src/utils/ajax.ts | 6 +++++ 8 files changed, 40 insertions(+), 34 deletions(-) delete mode 100644 client/src/redux/keyboard-shortcuts-mode-saga.js diff --git a/client/src/client-only-routes/show-settings.tsx b/client/src/client-only-routes/show-settings.tsx index bdfd8925644..0e4872e5884 100644 --- a/client/src/client-only-routes/show-settings.tsx +++ b/client/src/client-only-routes/show-settings.tsx @@ -33,6 +33,7 @@ import { updateMyQuincyEmail, updateMySound, updateMyTheme, + updateMyKeyboardShortcuts, updateUserFlag, verifyCert } from '../redux/settings'; @@ -79,7 +80,7 @@ const mapDispatchToProps = { toggleNightMode: (theme: Themes) => updateMyTheme({ theme }), toggleSoundMode: (sound: boolean) => updateMySound({ sound }), toggleKeyboardShortcuts: (keyboardShortcuts: boolean) => - updateUserFlag({ keyboardShortcuts }), + updateMyKeyboardShortcuts({ keyboardShortcuts }), updateInternetSettings: updateUserFlag, updateIsHonest: updateMyHonesty, updatePortfolio: updateMyPortfolio, diff --git a/client/src/redux/index.js b/client/src/redux/index.js index 4fd573a0060..8efc20a08f3 100644 --- a/client/src/redux/index.js +++ b/client/src/redux/index.js @@ -19,7 +19,6 @@ import hardGoToEpic from './hard-go-to-epic'; import { createReportUserSaga } from './report-user-saga'; import { actionTypes as settingsTypes } from './settings/action-types'; import { createShowCertSaga } from './show-cert-saga'; -import { createKeyboardShortcuts } from './keyboard-shortcuts-mode-saga'; import updateCompleteEpic from './update-complete-epic'; import { createUserTokenSaga } from './user-token-saga'; import { createSaveChallengeSaga } from './save-challenge-saga'; @@ -82,7 +81,6 @@ export const sagas = [ ...createFetchUserSaga(actionTypes), ...createShowCertSaga(actionTypes), ...createReportUserSaga(actionTypes), - ...createKeyboardShortcuts({ ...actionTypes, ...settingsTypes }), ...createUserTokenSaga(actionTypes), ...createSaveChallengeSaga(actionTypes) ]; @@ -752,6 +750,8 @@ export const reducer = handleActions( payload ? spreadThePayloadOnUser(state, payload) : state, [settingsTypes.updateMyThemeComplete]: (state, { payload }) => payload ? spreadThePayloadOnUser(state, payload) : state, + [settingsTypes.updateMyKeyboardShortcutsComplete]: (state, { payload }) => + payload ? spreadThePayloadOnUser(state, payload) : state, [settingsTypes.updateMyHonestyComplete]: (state, { payload }) => payload ? spreadThePayloadOnUser(state, payload) : state, [settingsTypes.updateMyQuincyEmailComplete]: (state, { payload }) => diff --git a/client/src/redux/keyboard-shortcuts-mode-saga.js b/client/src/redux/keyboard-shortcuts-mode-saga.js deleted file mode 100644 index 65fb6c4a0e8..00000000000 --- a/client/src/redux/keyboard-shortcuts-mode-saga.js +++ /dev/null @@ -1,27 +0,0 @@ -/* eslint-disable require-yield */ - -import { takeEvery } from 'redux-saga/effects'; -import store from 'store'; - -const shortcutsKey = 'fcc-keyboard-shortcuts'; - -export function setKeyboardShortcuts(setting) { - store.set(shortcutsKey, setting); -} - -function* updateLocalKeyboardShortcutsSaga({ payload }) { - const { user, keyboardShortcuts } = payload ?? {}; - if (user) { - const { keyboardShortcuts = false } = user; - setKeyboardShortcuts(keyboardShortcuts); - } else if (typeof keyboardShortcuts !== 'undefined') { - setKeyboardShortcuts(keyboardShortcuts); - } -} - -export function createKeyboardShortcuts(types) { - return [ - takeEvery(types.fetchUserComplete, updateLocalKeyboardShortcutsSaga), - takeEvery(types.updateUserFlagComplete, updateLocalKeyboardShortcutsSaga) - ]; -} diff --git a/client/src/redux/settings/action-types.js b/client/src/redux/settings/action-types.js index ed770cf7952..22957c0614d 100644 --- a/client/src/redux/settings/action-types.js +++ b/client/src/redux/settings/action-types.js @@ -12,6 +12,7 @@ export const actionTypes = createTypes( ...createAsyncTypes('updateMySocials'), ...createAsyncTypes('updateMySound'), ...createAsyncTypes('updateMyTheme'), + ...createAsyncTypes('updateMyKeyboardShortcuts'), ...createAsyncTypes('updateMyHonesty'), ...createAsyncTypes('updateMyQuincyEmail'), ...createAsyncTypes('updateMyPortfolio'), diff --git a/client/src/redux/settings/index.js b/client/src/redux/settings/index.js index 984a2354dee..bff8e41af6a 100644 --- a/client/src/redux/settings/index.js +++ b/client/src/redux/settings/index.js @@ -84,6 +84,17 @@ export const updateMyThemeComplete = createAction( ); export const updateMyThemeError = createAction(types.updateMyThemeError); +export const updateMyKeyboardShortcuts = createAction( + types.updateMyKeyboardShortcuts +); +export const updateMyKeyboardShortcutsComplete = createAction( + types.updateMyKeyboardShortcutsComplete, + checkForSuccessPayload +); +export const updateMyKeyboardShortcutsError = createAction( + types.updateMyKeyboardShortcutsError +); + export const updateMyHonesty = createAction(types.updateMyHonesty); export const updateMyHonestyComplete = createAction( types.updateMyHonestyComplete, diff --git a/client/src/redux/settings/settings-sagas.js b/client/src/redux/settings/settings-sagas.js index c0e7de1b3d5..358f6b68a72 100644 --- a/client/src/redux/settings/settings-sagas.js +++ b/client/src/redux/settings/settings-sagas.js @@ -22,7 +22,8 @@ import { putVerifyCert, putUpdateMyPortfolio, putUpdateMyTheme, - putUpdateMySound + putUpdateMySound, + putUpdateMyKeyboardShortcuts } from '../../utils/ajax'; import { certMap } from '../../resources/cert-and-project-map'; import { completedChallengesSelector } from '..'; @@ -54,7 +55,9 @@ import { updateMyThemeComplete, updateMyThemeError, updateMySoundComplete, - updateMySoundError + updateMySoundError, + updateMyKeyboardShortcutsComplete, + updateMyKeyboardShortcutsError } from './'; function* submitNewAboutSaga({ payload }) { @@ -130,6 +133,16 @@ function* updateMyThemeSaga({ payload: update }) { } } +function* updateMyKeyboardShortcutsSaga({ payload: update }) { + try { + const { data } = yield call(putUpdateMyKeyboardShortcuts, update); + yield put(updateMyKeyboardShortcutsComplete({ ...data, payload: update })); + yield put(createFlashMessage({ ...data })); + } catch (e) { + yield put(updateMyKeyboardShortcutsError); + } +} + function* updateMyHonestySaga({ payload: update }) { try { const { data } = yield call(putUpdateMyHonesty, update); @@ -227,6 +240,7 @@ export function createSettingsSagas(types) { takeEvery(types.updateMyHonesty, updateMyHonestySaga), takeEvery(types.updateMySound, updateMySoundSaga), takeEvery(types.updateMyTheme, updateMyThemeSaga), + takeEvery(types.updateMyKeyboardShortcuts, updateMyKeyboardShortcutsSaga), takeEvery(types.updateMyQuincyEmail, updateMyQuincyEmailSaga), takeEvery(types.updateMyPortfolio, updateMyPortfolioSaga), takeLatest(types.submitNewAbout, submitNewAboutSaga), diff --git a/client/src/templates/Challenges/components/shortcuts-modal.tsx b/client/src/templates/Challenges/components/shortcuts-modal.tsx index 5ca44206761..3c258ee60d7 100644 --- a/client/src/templates/Challenges/components/shortcuts-modal.tsx +++ b/client/src/templates/Challenges/components/shortcuts-modal.tsx @@ -6,7 +6,7 @@ import { bindActionCreators, Dispatch } from 'redux'; import { createSelector } from 'reselect'; import { closeModal, isShortcutsModalOpenSelector } from '../redux'; -import { updateUserFlag } from '../../../redux/settings'; +import { updateMyKeyboardShortcuts } from '../../../redux/settings'; import { userSelector } from '../../../redux'; import { User } from '../../../redux/prop-types'; import KeyboardShortcutsSettings from '../../../components/settings/keyboard-shortcuts'; @@ -31,7 +31,7 @@ const mapDispatchToProps = (dispatch: Dispatch) => { closeShortcutsModal: () => closeModal('shortcuts'), toggleKeyboardShortcuts: (keyboardShortcuts: boolean) => - updateUserFlag({ keyboardShortcuts }) + updateMyKeyboardShortcuts({ keyboardShortcuts }) }, dispatch ); diff --git a/client/src/utils/ajax.ts b/client/src/utils/ajax.ts index a76d7797c3c..573e0783f20 100644 --- a/client/src/utils/ajax.ts +++ b/client/src/utils/ajax.ts @@ -324,6 +324,12 @@ export function putUpdateMyTheme( return put('/update-my-theme', update); } +export function putUpdateMyKeyboardShortcuts( + update: Record +): Promise> { + return put('/update-my-keyboard-shortcuts', update); +} + export function putUpdateMyHonesty( update: Record ): Promise> {