Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
import i18next from 'i18next';
import { apiClient } from '../../../utils/apiClient';
import * as ActionTypes from '../../../constants';
import type {
UpdatePreferencesRequestBody,
UserPreferences,
Error
} from '../../../../server/types';

function updatePreferences(formParams, dispatch) {
export interface BaseAction {
type: string;
}

export interface ActionWithValue extends BaseAction {
value: string | number | boolean;
}

export interface ActionWithError extends BaseAction {
error: Error['error'];
}

export interface ActionWithLanguage extends BaseAction {
language: string;
}

export interface State {
user: {
authenticated: boolean;
};
}

function updatePreferences(
formParams: UpdatePreferencesRequestBody,
dispatch: (
action: ActionWithValue | ActionWithError | ActionWithLanguage
) => void
) {
apiClient
.put('/preferences', formParams)
.then(() => {})
Expand All @@ -14,16 +46,18 @@ function updatePreferences(formParams, dispatch) {
});
}

export function setPreferencesTab(value) {
export function setPreferencesTab(value: number): ActionWithValue {
return {
type: ActionTypes.SET_PREFERENCES_TAB,
value
};
}

export function setFontSize(value) {
return (dispatch, getState) => {
// eslint-disable-line
export function setFontSize(value: UserPreferences['fontSize']) {
return (
dispatch: (action: ActionWithValue | ActionWithError) => {},
getState: () => State
) => {
dispatch({
type: ActionTypes.SET_FONT_SIZE,
value
Expand All @@ -40,8 +74,11 @@ export function setFontSize(value) {
};
}

export function setLineNumbers(value) {
return (dispatch, getState) => {
export function setLineNumbers(value: UserPreferences['lineNumbers']) {
return (
dispatch: (action: ActionWithValue | ActionWithError) => {},
getState: () => State
) => {
dispatch({
type: ActionTypes.SET_LINE_NUMBERS,
value
Expand All @@ -58,8 +95,13 @@ export function setLineNumbers(value) {
};
}

export function setAutocloseBracketsQuotes(value) {
return (dispatch, getState) => {
export function setAutocloseBracketsQuotes(
value: UserPreferences['autocloseBracketsQuotes']
) {
return (
dispatch: (action: ActionWithValue | ActionWithError) => {},
getState: () => State
) => {
dispatch({
type: ActionTypes.SET_AUTOCLOSE_BRACKETS_QUOTES,
value
Expand All @@ -76,8 +118,13 @@ export function setAutocloseBracketsQuotes(value) {
};
}

export function setAutocompleteHinter(value) {
return (dispatch, getState) => {
export function setAutocompleteHinter(
value: UserPreferences['autocompleteHinter']
) {
return (
dispatch: (action: ActionWithValue | ActionWithError) => {},
getState: () => State
) => {
dispatch({
type: ActionTypes.SET_AUTOCOMPLETE_HINTER,
value
Expand All @@ -94,8 +141,11 @@ export function setAutocompleteHinter(value) {
};
}

export function setAutosave(value) {
return (dispatch, getState) => {
export function setAutosave(value: UserPreferences['autosave']) {
return (
dispatch: (action: ActionWithValue | ActionWithError) => {},
getState: () => State
) => {
dispatch({
type: ActionTypes.SET_AUTOSAVE,
value
Expand All @@ -112,8 +162,11 @@ export function setAutosave(value) {
};
}

export function setLinewrap(value) {
return (dispatch, getState) => {
export function setLinewrap(value: UserPreferences['linewrap']) {
return (
dispatch: (action: ActionWithValue | ActionWithError) => {},
getState: () => State
) => {
dispatch({
type: ActionTypes.SET_LINEWRAP,
value
Expand All @@ -130,8 +183,11 @@ export function setLinewrap(value) {
};
}

export function setLintWarning(value) {
return (dispatch, getState) => {
export function setLintWarning(value: UserPreferences['lintWarning']) {
return (
dispatch: (action: ActionWithValue | ActionWithError) => {},
getState: () => State
) => {
dispatch({
type: ActionTypes.SET_LINT_WARNING,
value
Expand All @@ -148,8 +204,11 @@ export function setLintWarning(value) {
};
}

export function setTextOutput(value) {
return (dispatch, getState) => {
export function setTextOutput(value: UserPreferences['textOutput']) {
return (
dispatch: (action: ActionWithValue | ActionWithError) => {},
getState: () => State
) => {
dispatch({
type: ActionTypes.SET_TEXT_OUTPUT,
value
Expand All @@ -166,8 +225,11 @@ export function setTextOutput(value) {
};
}

export function setGridOutput(value) {
return (dispatch, getState) => {
export function setGridOutput(value: UserPreferences['gridOutput']) {
return (
dispatch: (action: ActionWithValue | ActionWithError) => {},
getState: () => State
) => {
dispatch({
type: ActionTypes.SET_GRID_OUTPUT,
value
Expand All @@ -184,12 +246,11 @@ export function setGridOutput(value) {
};
}

export function setTheme(value) {
// return {
// type: ActionTypes.SET_THEME,
// value
// };
return (dispatch, getState) => {
export function setTheme(value: UserPreferences['theme']) {
return (
dispatch: (action: ActionWithValue | ActionWithError) => {},
getState: () => State
) => {
dispatch({
type: ActionTypes.SET_THEME,
value
Expand All @@ -206,12 +267,11 @@ export function setTheme(value) {
};
}

export function setAutorefresh(value) {
// return {
// type: ActionTypes.SET_AUTOREFRESH,
// value
// };
return (dispatch, getState) => {
export function setAutorefresh(value: UserPreferences['autorefresh']) {
return (
dispatch: (action: ActionWithValue | ActionWithError) => {},
getState: () => State
) => {
dispatch({
type: ActionTypes.SET_AUTOREFRESH,
value
Expand All @@ -228,15 +288,23 @@ export function setAutorefresh(value) {
};
}

export function setAllAccessibleOutput(value) {
export function setAllAccessibleOutput(
value: UserPreferences['textOutput'] | UserPreferences['gridOutput']
) {
return (dispatch) => {
dispatch(setTextOutput(value));
dispatch(setGridOutput(value));
};
}

export function setLanguage(value, { persistPreference = true } = {}) {
return (dispatch, getState) => {
export function setLanguage(
value: UserPreferences['language'],
{ persistPreference = true } = {}
) {
return (
dispatch: (action: ActionWithLanguage | ActionWithError) => {},
getState: () => State
) => {
i18next.changeLanguage(value);
dispatch({
type: ActionTypes.SET_LANGUAGE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as ActionTypes from '../../../constants';
import i18n from '../../../i18n';
import { UserPreferences, AppThemeOptions } from '../../../../server/types';

export const initialState = {
interface UserPreferencesClient
extends Omit<UserPreferences, 'indentationAmount' | 'isTabIndent'> {
tabIndex: number;
}

export const initialState: UserPreferencesClient = {
tabIndex: 0,
fontSize: 18,
autosave: true,
Expand All @@ -10,14 +16,22 @@
lintWarning: false,
textOutput: false,
gridOutput: false,
theme: 'light',
theme: AppThemeOptions.LIGHT,
autorefresh: false,
language: i18n.language,
autocloseBracketsQuotes: true,
autocompleteHinter: false
};

const preferences = (state = initialState, action) => {
const preferences = (
state: UserPreferencesClient = initialState,
action: {
type: keyof ActionTypes;
value?: any;
preferences?: UserPreferencesClient;
language?: UserPreferences['language'];
}
) => {
switch (action.type) {
case ActionTypes.OPEN_PREFERENCES:
return Object.assign({}, state, { tabIndex: 0 });
Expand Down Expand Up @@ -58,4 +72,4 @@
}
};

export default preferences;

Check warning on line 75 in client/modules/IDE/reducers/preferences.ts

View workflow job for this annotation

GitHub Actions / Test and lint code base

Prefer named exports
2 changes: 1 addition & 1 deletion server/types/userPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type UpdatePreferencesResponseBody = UserPreferences | Error;
* user.controller.updatePreferences
*/
export interface UpdatePreferencesRequestBody {
preferences: UserPreferences;
preferences: Partial<UserPreferences>;
}

/**
Expand Down
Loading