Skip to content

Commit

Permalink
fix(updates): hide update options on platforms that do not support it
Browse files Browse the repository at this point in the history
  • Loading branch information
JPSchellenberg committed Apr 18, 2021
1 parent e6b33fb commit 27868c3
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 19 deletions.
7 changes: 7 additions & 0 deletions client/src/state/System/SystemAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import superagent from 'superagent';

import { ISystemState } from './SystemTypes';

export async function getSystem(): Promise<ISystemState> {
return (await superagent.get(`/api/v1/system`)).body;
}
37 changes: 37 additions & 0 deletions client/src/state/System/SystemActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as Sentry from '@sentry/browser';

import {
SYSTEM_GET_SYSTEM_REQUEST,
SYSTEM_GET_SYSTEM_ERROR,
SYSTEM_GET_SYSTEM_SUCCESS
} from './SystemTypes';

import * as API from './SystemAPI';

export function getSystem(): any {
return async (dispatch: any) => {
try {
dispatch({
payload: {},
type: SYSTEM_GET_SYSTEM_REQUEST
});

try {
const system = await API.getSystem();

dispatch({
payload: system,
type: SYSTEM_GET_SYSTEM_SUCCESS
});
return system;
} catch (error) {
Sentry.captureException(error);

dispatch({
payload: { error },
type: SYSTEM_GET_SYSTEM_ERROR
});
}
} catch (error) {}
};
}
29 changes: 29 additions & 0 deletions client/src/state/System/SystemReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as Sentry from '@sentry/browser';

import {
ISystemActionTypes,
ISystemState,
SYSTEM_GET_SYSTEM_SUCCESS
} from './SystemTypes';

export const initialState: ISystemState = {
platformSupportsUpdates: true
};

export default function settingsReducer(
state: ISystemState = initialState,
action: ISystemActionTypes
): ISystemState {
try {
switch (action.type) {
case SYSTEM_GET_SYSTEM_SUCCESS:
return action.payload;

default:
return state;
}
} catch (error) {
Sentry.captureException(error);
return state;
}
}
32 changes: 32 additions & 0 deletions client/src/state/System/SystemTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export interface IState {
system: ISystemState;
}

export interface ISystemState {
platformSupportsUpdates: boolean;
}

export const SYSTEM_GET_SYSTEM_REQUEST = 'SYSTEM_GET_SYSTEM_REQUEST';
export const SYSTEM_GET_SYSTEM_SUCCESS = 'SYSTEM_GET_SYSTEM_SUCCESS';
export const SYSTEM_GET_SYSTEM_ERROR = 'SYSTEM_GET_SYSTEM_ERROR';

export interface IGetSystemRequestAction {
payload: {};
type: typeof SYSTEM_GET_SYSTEM_REQUEST;
}

export interface IGetSystemSuccessAction {
payload: ISystemState;
type: typeof SYSTEM_GET_SYSTEM_SUCCESS;
}
export interface IGetSystemErrorAction {
payload: {
error: string;
};
type: typeof SYSTEM_GET_SYSTEM_ERROR;
}

export type ISystemActionTypes =
| IGetSystemErrorAction
| IGetSystemRequestAction
| IGetSystemSuccessAction;
13 changes: 10 additions & 3 deletions client/src/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import * as SettingsTypes from './Settings/SettingsTypes';
import SettingsReducer from './Settings/SettingsReducer';
import * as SettingsActions from './Settings/SettingsActions';

import * as SystemTypes from './System/SystemTypes';
import SystemReducer from './System/SystemReducer';
import * as SystemActions from './System/SystemActions';

import RunReducer from './Run/RunReducer';
import * as RunTypes from './Run/RunTypes';
import * as RunActions from './Run/RunActions';
Expand Down Expand Up @@ -48,7 +52,8 @@ const rootReducer = () =>
h5peditor: H5PEditorReducer,
analytics: AnalyticsReducer,
run: RunReducer,
settings: SettingsReducer
settings: SettingsReducer,
system: SystemReducer
});

const store = createStore(
Expand All @@ -62,14 +67,16 @@ export interface IState
NotificationsTypes.IState,
AnalyticsTypes.IState,
SettingsTypes.IState,
RunTypes.IState {}
RunTypes.IState,
SystemTypes.IState {}

export const actions = {
notifications: NotificationsActions,
h5peditor: H5PEditorActions,
analytics: AnalyticsActions,
settings: SettingsActions,
run: RunActions
run: RunActions,
system: SystemActions
};

export const selectors = {
Expand Down
2 changes: 2 additions & 0 deletions client/src/views/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export default function AppContainer() {
}
}
);

dispatch(actions.system.getSystem());
}, [dispatch, i18n]);

return (
Expand Down
39 changes: 23 additions & 16 deletions client/src/views/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import classnames from 'classnames';

import { useTranslation } from 'react-i18next';
import { useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';

import { createStyles, makeStyles, Theme } from '@material-ui/core/styles';
import Dialog from '@material-ui/core/Dialog';
Expand Down Expand Up @@ -35,6 +35,7 @@ import AccountSettingsList from './components/Settings/AccountSettingsList';
import SettingsLibraryManagement from './components/Settings/LibraryManagement';
import UpdateSettings from './components/Settings/UpdatesSettings';

import { IState } from '../state';
import { track } from '../state/track/actions';

const drawerWidth = 240;
Expand Down Expand Up @@ -100,6 +101,10 @@ export default function FullScreenDialog() {
const { t } = useTranslation();
const dispatch = useDispatch();

const platformSupportsUpdates = useSelector(
(state: IState) => state.system.platformSupportsUpdates
);

const [section, setSection] = React.useState('general');

const handleClickOpen = () => {
Expand Down Expand Up @@ -164,21 +169,23 @@ export default function FullScreenDialog() {
primary={t('settings.menu.general')}
/>
</ListItem>
<ListItem
button
key="updates"
onClick={() => setSection('updates')}
className={classnames({
[classes.selected]: section === 'updates'
})}
>
<ListItemIcon>
<UpdateIcon />
</ListItemIcon>
<ListItemText
primary={t('settings.menu.updates')}
/>
</ListItem>
{platformSupportsUpdates && (
<ListItem
button
key="updates"
onClick={() => setSection('updates')}
className={classnames({
[classes.selected]: section === 'updates'
})}
>
<ListItemIcon>
<UpdateIcon />
</ListItemIcon>
<ListItemText
primary={t('settings.menu.updates')}
/>
</ListItem>
)}
<ListItem
button
key="h5p-library-administration"
Expand Down
3 changes: 3 additions & 0 deletions server/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import h5pRoutes from './h5pRoutes';
import analyticRoutes from './analyticRoutes';
import settingsRoutes from './settingsRoutes';
import runRoutes from './runRoutes';
import updateRoutes from './systemRoutes';

import User from '../User';

Expand All @@ -43,6 +44,8 @@ export default function (
next();
});

router.use('/api/v1/system', updateRoutes());

router.use(
'/api/v1/settings',
settingsRoutes(serverConfig, browserWindow, app)
Expand Down
28 changes: 28 additions & 0 deletions server/src/routes/systemRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import express from 'express';
import * as Sentry from '@sentry/electron';

import { platformSupportsUpdates } from '../updater';

export default function (): express.Router {
const router = express.Router();

router.get(
`/`,
async (
req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
try {
res.status(200).json({
platformSupportsUpdates: platformSupportsUpdates()
});
} catch (error) {
Sentry.captureException(error);
res.status(500).json(error);
}
}
);

return router;
}

0 comments on commit 27868c3

Please sign in to comment.