Skip to content

Commit

Permalink
feat: activation / deactivation of location sharing
Browse files Browse the repository at this point in the history
closes #107
  • Loading branch information
pyphilia committed Jul 10, 2019
1 parent 29e1b66 commit 455e554
Show file tree
Hide file tree
Showing 18 changed files with 307 additions and 10 deletions.
2 changes: 2 additions & 0 deletions public/app/config/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module.exports = {
SET_LANGUAGE_CHANNEL: 'user:lang:set',
SET_DEVELOPER_MODE_CHANNEL: 'user:developer-mode:set',
GET_DEVELOPER_MODE_CHANNEL: 'user:developer-mode:get',
SET_GEOLOCATION_ENABLED_CHANNEL: 'user:geolocation-enabled:set',
GET_GEOLOCATION_ENABLED_CHANNEL: 'user:geolocation-enabled:get',
GET_APP_INSTANCE_RESOURCES_CHANNEL: 'app-instance-resources:get',
POST_APP_INSTANCE_RESOURCE_CHANNEL: 'app-instance-resource:post',
PATCH_APP_INSTANCE_RESOURCE_CHANNEL: 'app-instance-resource:patch',
Expand Down
2 changes: 2 additions & 0 deletions public/app/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ const DATABASE_PATH = `${VAR_FOLDER}/db.json`;
const TMP_FOLDER = 'tmp';
const DEFAULT_LANG = 'en';
const DEFAULT_DEVELOPER_MODE = false;
const DEFAULT_GEOLOCATION_ENABLED = false;

module.exports = {
DEFAULT_DEVELOPER_MODE,
DEFAULT_GEOLOCATION_ENABLED,
DOWNLOADABLE_MIME_TYPES,
TMP_FOLDER,
RESOURCE,
Expand Down
6 changes: 6 additions & 0 deletions public/app/config/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const ERROR_GETTING_DEVELOPER_MODE =
'There was an error getting the developer mode';
const ERROR_SETTING_DEVELOPER_MODE =
'There was an error setting the developer mode';
const ERROR_GETTING_GEOLOCATION_ENABLED =
'There was an error getting the geolocation active enabled';
const ERROR_SETTING_GEOLOCATION_ENABLED =
'There was an error setting the geolocation enabled';
const ERROR_GETTING_DATABASE = 'There was an error getting the database.';
const ERROR_SETTING_DATABASE = 'There was an error updating the database.';
const SUCCESS_SYNCING_MESSAGE = 'Space was successfully synced';
Expand All @@ -42,6 +46,8 @@ const ERROR_SYNCING_MESSAGE = 'There was an error syncing the space.';
module.exports = {
ERROR_GETTING_DEVELOPER_MODE,
ERROR_SETTING_DEVELOPER_MODE,
ERROR_GETTING_GEOLOCATION_ENABLED,
ERROR_SETTING_GEOLOCATION_ENABLED,
ERROR_GETTING_LANGUAGE,
ERROR_SETTING_LANGUAGE,
ERROR_DOWNLOADING_MESSAGE,
Expand Down
39 changes: 39 additions & 0 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
DATABASE_PATH,
DEFAULT_LANG,
DEFAULT_DEVELOPER_MODE,
DEFAULT_GEOLOCATION_ENABLED,
} = require('./app/config/config');
const {
LOAD_SPACE_CHANNEL,
Expand All @@ -44,6 +45,8 @@ const {
GET_APP_INSTANCE_CHANNEL,
GET_DEVELOPER_MODE_CHANNEL,
SET_DEVELOPER_MODE_CHANNEL,
GET_GEOLOCATION_ENABLED_CHANNEL,
SET_GEOLOCATION_ENABLED_CHANNEL,
GET_DATABASE_CHANNEL,
SET_DATABASE_CHANNEL,
SHOW_SYNC_SPACE_PROMPT_CHANNEL,
Expand Down Expand Up @@ -341,6 +344,42 @@ app.on('ready', async () => {
}
});

// called when getting geolocation enabled
ipcMain.on(GET_GEOLOCATION_ENABLED_CHANNEL, () => {
try {
const geolocationEnabled =
db.get('user.geolocationEnabled').value() ||
DEFAULT_GEOLOCATION_ENABLED;
mainWindow.webContents.send(
GET_GEOLOCATION_ENABLED_CHANNEL,
geolocationEnabled
);
} catch (e) {
logger.error(e);
mainWindow.webContents.send(
GET_GEOLOCATION_ENABLED_CHANNEL,
ERROR_GENERAL
);
}
});

// called when setting geolocation enabled
ipcMain.on(SET_GEOLOCATION_ENABLED_CHANNEL, (event, geolocationEnabled) => {
try {
db.set('user.geolocationEnabled', geolocationEnabled).write();
mainWindow.webContents.send(
SET_GEOLOCATION_ENABLED_CHANNEL,
geolocationEnabled
);
} catch (e) {
logger.error(e);
mainWindow.webContents.send(
SET_GEOLOCATION_ENABLED_CHANNEL,
ERROR_GENERAL
);
}
});

// called when getting AppInstanceResources
ipcMain.on(GET_APP_INSTANCE_RESOURCES_CHANNEL, (event, data = {}) => {
const defaultResponse = [];
Expand Down
22 changes: 18 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
getUserFolder,
getLanguage,
getDeveloperMode,
getGeolocationEnabled,
} from './actions/user';
import { DEFAULT_LANGUAGE } from './config/constants';
import './App.css';
Expand All @@ -48,10 +49,12 @@ export class App extends Component {
dispatchGetUserFolder: PropTypes.func.isRequired,
dispatchGetLanguage: PropTypes.func.isRequired,
dispatchGetDeveloperMode: PropTypes.func.isRequired,
dispatchGetGeolocationEnabled: PropTypes.func.isRequired,
lang: PropTypes.string,
i18n: PropTypes.shape({
changeLanguage: PropTypes.func.isRequired,
}).isRequired,
geolocationEnabled: PropTypes.bool.isRequired,
};

static defaultProps = {
Expand All @@ -64,25 +67,34 @@ export class App extends Component {
dispatchGetUserFolder,
dispatchGetLanguage,
dispatchGetDeveloperMode,
dispatchGetGeolocationEnabled,
} = this.props;

dispatchGetLanguage();
dispatchGetDeveloperMode();
dispatchGetUserFolder();
dispatchGetGeolocationEnabled();
}

componentDidMount() {
const { dispatchGetGeolocation } = this.props;
dispatchGetGeolocation();
this.updateWindowDimensions();
window.addEventListener('resize', this.updateWindowDimensions);
}

componentDidUpdate({ lang: prevLang }) {
const { lang, i18n } = this.props;
componentDidUpdate({
lang: prevLang,
geolocationEnabled: prevLocationEnabled,
dispatchGetGeolocation,
}) {
const { lang, i18n, geolocationEnabled } = this.props;
if (lang !== prevLang) {
i18n.changeLanguage(lang);
}

// fetch geolocation only if enabled
if (geolocationEnabled && geolocationEnabled !== prevLocationEnabled) {
dispatchGetGeolocation();
}
}

componentWillUnmount() {
Expand Down Expand Up @@ -124,13 +136,15 @@ export class App extends Component {

const mapStateToProps = ({ User }) => ({
lang: User.getIn(['current', 'lang']),
geolocationEnabled: User.getIn(['current', 'geolocationEnabled']),
});

const mapDispatchToProps = {
dispatchGetGeolocation: getGeolocation,
dispatchGetUserFolder: getUserFolder,
dispatchGetLanguage: getLanguage,
dispatchGetDeveloperMode: getDeveloperMode,
dispatchGetGeolocationEnabled: getGeolocationEnabled,
};

const ConnectedApp = connect(
Expand Down
2 changes: 2 additions & 0 deletions src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe('<App />', () => {
dispatchGetUserFolder: jest.fn(),
dispatchGetLanguage: jest.fn(),
dispatchGetDeveloperMode: jest.fn(),
dispatchGetGeolocationEnabled: jest.fn(),
geolocationEnabled: false,
};
const component = shallow(<App {...props} />);
it('renders correctly', () => {
Expand Down
67 changes: 67 additions & 0 deletions src/actions/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
FLAG_SETTING_DEVELOPER_MODE,
GET_DEVELOPER_MODE_SUCCEEDED,
SET_DEVELOPER_MODE_SUCCEEDED,
FLAG_GETTING_GEOLOCATION_ENABLED,
FLAG_SETTING_GEOLOCATION_ENABLED,
GET_GEOLOCATION_ENABLED_SUCCEEDED,
SET_GEOLOCATION_ENABLED_SUCCEEDED,
} from '../types';
import {
ERROR_GETTING_GEOLOCATION,
Expand All @@ -21,13 +25,17 @@ import {
ERROR_SETTING_LANGUAGE,
ERROR_SETTING_DEVELOPER_MODE,
ERROR_GETTING_DEVELOPER_MODE,
ERROR_SETTING_GEOLOCATION_ENABLED,
ERROR_GETTING_GEOLOCATION_ENABLED,
} from '../config/messages';
import {
GET_USER_FOLDER_CHANNEL,
GET_LANGUAGE_CHANNEL,
SET_LANGUAGE_CHANNEL,
GET_DEVELOPER_MODE_CHANNEL,
SET_DEVELOPER_MODE_CHANNEL,
GET_GEOLOCATION_ENABLED_CHANNEL,
SET_GEOLOCATION_ENABLED_CHANNEL,
} from '../config/channels';
import { createFlag } from './common';
import { ERROR_GENERAL } from '../config/errors';
Expand All @@ -37,6 +45,12 @@ const flagGettingLanguage = createFlag(FLAG_GETTING_LANGUAGE);
const flagSettingLanguage = createFlag(FLAG_SETTING_LANGUAGE);
const flagGettingDeveloperMode = createFlag(FLAG_GETTING_DEVELOPER_MODE);
const flagSettingDeveloperMode = createFlag(FLAG_SETTING_DEVELOPER_MODE);
const flagGettingGeolocationEnabled = createFlag(
FLAG_GETTING_GEOLOCATION_ENABLED
);
const flagSettingGeolocationEnabled = createFlag(
FLAG_SETTING_GEOLOCATION_ENABLED
);

const getGeolocation = async () => async dispatch => {
// only fetch location if online
Expand Down Expand Up @@ -175,11 +189,64 @@ const setDeveloperMode = async developerMode => dispatch => {
}
};

const getGeolocationEnabled = async () => dispatch => {
try {
dispatch(flagGettingGeolocationEnabled(true));
window.ipcRenderer.send(GET_GEOLOCATION_ENABLED_CHANNEL);
window.ipcRenderer.once(
GET_GEOLOCATION_ENABLED_CHANNEL,
(event, geolocationEnabled) => {
if (geolocationEnabled === ERROR_GENERAL) {
toastr.error(ERROR_MESSAGE_HEADER, ERROR_GETTING_GEOLOCATION_ENABLED);
} else {
dispatch({
type: GET_GEOLOCATION_ENABLED_SUCCEEDED,
payload: geolocationEnabled,
});
}
dispatch(flagGettingGeolocationEnabled(false));
}
);
} catch (e) {
console.error(e);
toastr.error(ERROR_MESSAGE_HEADER, ERROR_GETTING_GEOLOCATION_ENABLED);
}
};

const setGeolocationEnabled = async geolocationEnabled => dispatch => {
try {
dispatch(flagSettingGeolocationEnabled(true));
window.ipcRenderer.send(
SET_GEOLOCATION_ENABLED_CHANNEL,
geolocationEnabled
);
window.ipcRenderer.once(
SET_GEOLOCATION_ENABLED_CHANNEL,
(event, enabled) => {
if (enabled === ERROR_GENERAL) {
toastr.error(ERROR_MESSAGE_HEADER, ERROR_SETTING_GEOLOCATION_ENABLED);
} else {
dispatch({
type: SET_GEOLOCATION_ENABLED_SUCCEEDED,
payload: enabled,
});
}
dispatch(flagSettingGeolocationEnabled(false));
}
);
} catch (e) {
console.error(e);
toastr.error(ERROR_MESSAGE_HEADER, ERROR_SETTING_GEOLOCATION_ENABLED);
}
};

export {
getUserFolder,
getGeolocation,
getLanguage,
setLanguage,
getDeveloperMode,
setDeveloperMode,
getGeolocationEnabled,
setGeolocationEnabled,
};
2 changes: 2 additions & 0 deletions src/components/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Styles from '../Styles';
import MainMenu from './common/MainMenu';
import LanguageSelect from './common/LanguageSelect';
import DeveloperSwitch from './common/DeveloperSwitch';
import GeolocationControl from './common/GeolocationControl';

class Settings extends Component {
state = {
Expand Down Expand Up @@ -102,6 +103,7 @@ class Settings extends Component {
<FormGroup>
<LanguageSelect />
<DeveloperSwitch />
<GeolocationControl />
</FormGroup>
</main>
</div>
Expand Down
16 changes: 13 additions & 3 deletions src/components/SpacesNearby.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import MainMenu from './common/MainMenu';
import { getSpacesNearby } from '../actions';
import SpaceGrid from './space/SpaceGrid';
import Loader from './common/Loader';
import GeolocationControl from './common/GeolocationControl';

class SpacesNearby extends Component {
state = {
Expand All @@ -35,6 +36,7 @@ class SpacesNearby extends Component {
geolocation: PropTypes.instanceOf(Map),
spaces: PropTypes.instanceOf(Set).isRequired,
activity: PropTypes.bool,
geolocationEnabled: PropTypes.bool.isRequired,
};

static defaultProps = {
Expand Down Expand Up @@ -76,7 +78,7 @@ class SpacesNearby extends Component {
};

render() {
const { classes, theme, spaces, activity } = this.props;
const { classes, theme, spaces, activity, geolocationEnabled } = this.props;
const { open } = this.state;

if (activity) {
Expand All @@ -93,6 +95,12 @@ class SpacesNearby extends Component {
);
}

const geolocationContent = geolocationEnabled ? (
<SpaceGrid spaces={spaces} />
) : (
<GeolocationControl isButton />
);

return (
<div className={classes.root}>
<CssBaseline />
Expand Down Expand Up @@ -135,12 +143,12 @@ class SpacesNearby extends Component {
<MainMenu />
</Drawer>
<main
className={classNames(classes.content, {
className={classNames('Main', classes.content, {
[classes.contentShift]: open,
})}
>
<div className={classes.drawerHeader} />
<SpaceGrid spaces={spaces} />
{geolocationContent}
</main>
</div>
);
Expand All @@ -151,6 +159,7 @@ const mapStateToProps = ({ User, Space }) => ({
geolocation: User.getIn(['current', 'geolocation']),
spaces: Space.getIn(['nearby', 'content']),
activity: Space.getIn(['nearby', 'activity']),
geolocationEnabled: User.getIn(['current', 'geolocationEnabled']),
});

const mapDispatchToProps = {
Expand All @@ -161,6 +170,7 @@ const ConnectedComponent = connect(
mapStateToProps,
mapDispatchToProps
)(SpacesNearby);

const StyledComponent = withStyles(Styles, { withTheme: true })(
ConnectedComponent
);
Expand Down
Loading

0 comments on commit 455e554

Please sign in to comment.