Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add defaultUsername option #150

Merged
merged 1 commit into from
Apr 20, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class App extends Component {
if (options.timeToClose > 0) {
const delay = options.timeToClose * 60 * 1000;
this.disconnectingEvent = setTimeout(
() => this.props.dispatch(AppUIActions.disconnectUser()),
() => window.location.reload(),
delay
);
}
Expand Down
15 changes: 6 additions & 9 deletions src/components/Sidebar.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import React, { useCallback } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import React from 'react';
import { useSelector } from 'react-redux';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import { NavLink } from 'react-router-dom';
import moment from 'moment';

import { disconnectUser } from 'slices/AppUISlice';

import secretin from 'utils/secretin';
import Icon from 'components/utilities/Icon';

Expand Down Expand Up @@ -57,10 +55,6 @@ function exportDb() {
}

function Sidebar() {
const dispatch = useDispatch();
const handleLogout = useCallback(() => {
dispatch(disconnectUser());
}, [dispatch]);
const currentUser = useSelector(state => state.AppUI.currentUser);
const isOnline = useSelector(state => state.AppUI.online);
return (
Expand Down Expand Up @@ -113,7 +107,10 @@ function Sidebar() {
</SidebarMenuLink>
<div className="sidebar-separator" />
<li className="sidebar-menu-item">
<a className="sidebar-menu-link" onClick={handleLogout}>
<a
className="sidebar-menu-link"
onClick={() => window.location.reload()}
>
<Icon id="logout" size="base" />
Log out
</a>
Expand Down
52 changes: 51 additions & 1 deletion src/components/options/OptionsContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ class OptionsContainer extends Component {
constructor(props) {
super(props);
this.onChangeTimeToClose = this.onChangeTimeToClose.bind(this);
this.onChangeDefaultUsername = this.onChangeDefaultUsername.bind(this);
this.onSaveDefaultUsername = this.onSaveDefaultUsername.bind(this);
this.state = {
defaultUsername: props.options.defaultUsername,
};
}

onChangeDefaultUsername({ value }) {
this.setState({ defaultUsername: value });
}

onChangeTimeToClose({ value }) {
Expand All @@ -33,8 +42,18 @@ class OptionsContainer extends Component {
});
}

onSaveDefaultUsername() {
this.props.actions.changeDefaultUsername({
defaultUsername: this.state.defaultUsername,
});
}

render() {
const { options, isOnline } = this.props;
const {
options,
isOnline,
options: { defaultUsername = '' },
} = this.props;
return (
<div className="page">
<div className="page-header">
Expand All @@ -44,6 +63,37 @@ class OptionsContainer extends Component {
</div>

<div className="page-content options">
<div className="options-section">
<h3 className="options-section-title">Experience</h3>
<div className="options-section-item">
<Input
name="defaultUsername"
label="Default username"
size="small"
value={this.state.defaultUsername}
onChange={this.onChangeDefaultUsername}
disabled={!isOnline}
/>
<div className="options-section-subitem-save">
<Button
type="button"
buttonStyle={
this.state.defaultUsername === defaultUsername
? 'default'
: 'primary'
}
onClick={this.onSaveDefaultUsername}
size="small"
disabled={
!isOnline || this.state.defaultUsername === defaultUsername
}
>
Save
</Button>
</div>
</div>
</div>

<div className="options-section">
<h3 className="options-section-title">Security</h3>

Expand Down
7 changes: 0 additions & 7 deletions src/models/SecretDataRecord.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import SecretFieldRecord from 'models/SecretFieldRecord';
import { Utils } from 'secretin';

class SecretDataRecord {
constructor(raw = {}) {
Expand All @@ -22,12 +21,6 @@ class SecretDataRecord {

static createWithDefaultFields(defaultFields) {
return defaultFields.reduce((record, field) => {
if (field.type === 'password') {
return record.addNewField({
...field,
content: Utils.PasswordGenerator.generatePassword(),
});
}
return record.addNewField(field);
}, new SecretDataRecord());
}
Expand Down
3 changes: 1 addition & 2 deletions src/slices/AppUISlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ export const {
} = AppUISlice.actions;

export const disconnectUser = () => dispatch => {
secretin.currentUser.disconnect();
dispatch(disconnectUserSuccess());
window.location.reload();
};

export const createUser = ({
Expand Down
14 changes: 10 additions & 4 deletions src/slices/EditSecretUISlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import { createSlice } from '@reduxjs/toolkit';
import { showSecretSuccess } from 'slices/ShowSecretUISlice';
import { hideModal } from 'slices/ShowSecretUISlice';
import { updateSecretSuccess } from 'slices/MetadataSlice';
import { disconnectUserSuccess } from 'slices/AppUISlice';

export const EditSecretUISlice = createSlice({
name: 'EditSecretUI',
initialState: {
function getInitialState() {
return {
isEditing: false,
data: null,
},
};
}

export const EditSecretUISlice = createSlice({
name: 'EditSecretUI',
initialState: getInitialState(),
reducers: {
updateData: (state, action) => {
state.isEditing = false;
Expand All @@ -36,6 +41,7 @@ export const EditSecretUISlice = createSlice({
[updateSecretSuccess]: state => {
state.isEditing = false;
},
[disconnectUserSuccess]: getInitialState,
},
});

Expand Down
4 changes: 4 additions & 0 deletions src/slices/ImportSlice.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createSlice } from '@reduxjs/toolkit';
import importers from 'utils/importers';
import { findKey } from 'lodash';
import { disconnectUserSuccess } from 'slices/AppUISlice';

const getInitialState = () => ({
importType: '',
Expand Down Expand Up @@ -80,6 +81,9 @@ export const ImportSlice = createSlice({
state.mandatoryFields[field.name].value = value;
},
},
extraReducers: {
[disconnectUserSuccess]: getInitialState,
},
});

// Action creators are generated for each case reducer function
Expand Down
12 changes: 9 additions & 3 deletions src/slices/MetadataSlice.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import secretin, { Errors } from 'utils/secretin';
import { createSlice } from '@reduxjs/toolkit';
import Secret from 'models/Secret';
import { disconnectUserSuccess } from 'slices/AppUISlice';

import {
createSecretSuccess,
Expand Down Expand Up @@ -43,11 +44,15 @@ const _rebuildMetadata = (state, action) => {
state.knownFriendList = Array.from(friendList).sort();
};

function getInitialState() {
return {
metadata: [],
};
}

export const MetadataSlice = createSlice({
name: 'Metadata',
initialState: {
metadata: [],
},
initialState: getInitialState(),
reducers: {
loadMetadataSuccess: _rebuildMetadata,
deleteSecretSuccess: _rebuildMetadata,
Expand Down Expand Up @@ -82,6 +87,7 @@ export const MetadataSlice = createSlice({
extraReducers: {
[createSecretSuccess]: _rebuildMetadata,
[loginUserSuccess]: _rebuildMetadata,
[disconnectUserSuccess]: getInitialState,
},
});

Expand Down
18 changes: 16 additions & 2 deletions src/slices/NewSecretUISlice.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createSlice } from '@reduxjs/toolkit';
import SecretDataRecord from 'models/SecretDataRecord';

import { createSecretSuccess } from 'slices/AppUISlice';
import { createSecretSuccess, disconnectUserSuccess } from 'slices/AppUISlice';
import secretin from 'utils/secretin';
import { Utils } from 'secretin';

const getInitialState = () => ({
shown: false,
Expand All @@ -23,6 +25,17 @@ export const NewSecretUISlice = createSlice({
state.title = `Untitled ${isFolder ? 'folder' : 'secret'}`;
state.folder = folder;
state.isFolder = isFolder;
const loginIndex = state.data.fields.findIndex(
fieldToUpdate => fieldToUpdate.label === 'login'
);
const passwordIndex = state.data.fields.findIndex(
fieldToUpdate => fieldToUpdate.label === 'password'
);
state.data.fields[loginIndex].content =
secretin.currentUser.options?.defaultUsername ?? '';
state.data.fields[
passwordIndex
].content = Utils.PasswordGenerator.generatePassword();
},
hideModal: () => getInitialState(),
changeTitle: (state, action) => {
Expand All @@ -38,7 +51,8 @@ export const NewSecretUISlice = createSlice({
},
},
extraReducers: {
[createSecretSuccess]: () => getInitialState(),
[createSecretSuccess]: getInitialState,
[disconnectUserSuccess]: getInitialState,
},
});

Expand Down
30 changes: 29 additions & 1 deletion src/slices/OptionsSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { createSlice } from '@reduxjs/toolkit';
import secretin from 'utils/secretin';
import uuid from 'uuid';

import { loginUserSuccess } from 'slices/AppUISlice';
import {
loginUserSuccess,
createUserSuccess,
disconnectUserSuccess,
} from 'slices/AppUISlice';

const getInitialState = () => ({
options: {},
Expand Down Expand Up @@ -91,6 +95,11 @@ export const OptionsSlice = createSlice({
state.options.timeToClose = timeToClose;
},

changeDefaultUsernameSuccess: (state, action) => {
const { defaultUsername } = action.payload;
state.options.defaultUsername = defaultUsername;
},

changeNewPass1: (state, action) => {
const newPass1 = action.payload;
state.newPass.newPass1 = newPass1.value;
Expand Down Expand Up @@ -152,6 +161,8 @@ export const OptionsSlice = createSlice({
const { options } = action.payload;
state.options = options;
},
[createUserSuccess]: getInitialState,
[disconnectUserSuccess]: getInitialState,
},
});

Expand All @@ -169,6 +180,7 @@ export const {
deactivateShortLoginSuccess,
activateShortLoginSuccess,
changeDelaySuccess,
changeDefaultUsernameSuccess,
changeNewPass1,
changeNewPass2,
showChangePassword,
Expand Down Expand Up @@ -282,6 +294,22 @@ export const toggleAutoLogout = ({ checked }) => (dispatch, getState) => {
return dispatch(changeTimeToClose({ timeToClose: delay }));
};

export const changeDefaultUsername = ({ defaultUsername }) => (
dispatch,
getState
) => {
secretin
.editOption('defaultUsername', defaultUsername)
.then(() => {
dispatch(changeDefaultUsernameSuccess({ defaultUsername }));
})
.catch(() => {
// Currently the UI can't display this error anyway
// dispatch(changeDelayFailure());
});
return true;
};

export const changeTimeToClose = ({ timeToClose }) => (dispatch, getState) => {
secretin
.editOption('timeToClose', timeToClose)
Expand Down
3 changes: 3 additions & 0 deletions src/slices/ShowSecretUISlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
deleteSecretUserRightsSuccess,
} from 'slices/MetadataSlice';

import { disconnectUserSuccess } from 'slices/AppUISlice';

const getInitialState = () => ({
secret: null,
errors: {},
Expand Down Expand Up @@ -101,6 +103,7 @@ export const ShowSecretUISlice = createSlice({
state.isUpdating = false;
state.errors = {};
},
[disconnectUserSuccess]: getInitialState,
},
});

Expand Down
3 changes: 3 additions & 0 deletions src/stylesheets/components/options/OptionsContainer.sass
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
.options-section-subitem
margin: 0 0 20px
padding-left: 32px

.options-section-subitem-save
margin: 10px 0 20px