This commit is contained in:
Alexander Bell 2018-10-02 15:07:28 +02:00
parent c93cdb1e43
commit 3d33a5b0d1
4 changed files with 72 additions and 13 deletions

View File

@ -51,6 +51,14 @@ const Edit = inject("rootStore") ( observer(
* toggles window: open and close * toggles window: open and close
* - encryptionkey: String * - encryptionkey: String
* encryption key of the password * encryption key of the password
* - updateDoc: function
* updates doc locally
* - deleteDoc: function
* deletes doc locally
* - editIndex: Int
* index of array with all docs. locale
* - toggleEditWindow: function
* Open or close edit window
*/ */
//Stored information //Stored information
@ -59,6 +67,7 @@ const Edit = inject("rootStore") ( observer(
this.handleChange = this.handleChange.bind(this); this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
this.dice = this.dice.bind(this); this.dice = this.dice.bind(this);
this.delete = this.delete.bind(this);
this.state = { this.state = {
url: '', url: '',
@ -132,6 +141,39 @@ const Edit = inject("rootStore") ( observer(
} }
delete() {
//Delete doc
alertify.confirm("Are you sure?", () => {
const id = this.props.data[this.props.editIndex].id;
const uid = this.stores.authStore.userData.uid;
const editIndex = this.props.editIndex;
db.collection("passwords/"+uid+"/passwords").doc(id).delete().then(() => {
//Delete doc locally
this.props.deleteDoc(editIndex);
this.props.toggleEditWindow();
alertify.success("Document removed!");
this.setState({
loading: false
});
}).catch((error) => {
console.error("Error updating the document: ", error);
alertify.error("Something went wrong! Please check your internet connection");
this.setState({
loading: false
});
});
this.setState({
loading: true
});
});
}
handleSubmit() { handleSubmit() {
const url = this.state.url; const url = this.state.url;
const login = encrypt(this.state.login, this.props.encryptionkey); const login = encrypt(this.state.login, this.props.encryptionkey);
@ -151,7 +193,7 @@ const Edit = inject("rootStore") ( observer(
db.collection("passwords/"+uid+"/passwords").doc(id).set(data) db.collection("passwords/"+uid+"/passwords").doc(id).set(data)
.then(() => { .then(() => {
console.log("Document updated"); console.log("Document updated");
alertify.success("Document successfully updated!"); alertify.success("Document updated!");
//Update a doc locally with the new values //Update a doc locally with the new values
this.props.updateDoc(Object.assign(data, {id:id}), editIndex); this.props.updateDoc(Object.assign(data, {id:id}), editIndex);
@ -225,12 +267,15 @@ const Edit = inject("rootStore") ( observer(
/> />
</Form.Field> </Form.Field>
<p>Login and password are going to be encrypted with the key you have set.</p> <p>Login and password are going to be encrypted with the key you have set.</p>
<Button loading={this.state.loading} type='submit' primary>Update</Button> <Button loading={this.state.loading} type='submit' primary>Update</Button>
<Button loading={this.state.loading} onClick={this.props.toggleEditWindow}><Icon name="x" />Cancel</Button> <Button loading={this.state.loading} type="button" onClick={this.props.toggleEditWindow}><Icon name="x" />Cancel</Button>
<br />
<br />
<Button loading={this.state.loading} type="button" onClick={this.delete} icon color="red"><Icon name="trash alternate" /></Button>
</Form> </Form>
</Segment> </Segment>
@ -249,7 +294,7 @@ const Edit = inject("rootStore") ( observer(
width: '100vw', width: '100vw',
height: '100vh', height: '100vh',
position: 'fixed', position: 'fixed',
zIndex: 1000, zIndex: 1,
top: 0, top: 0,
left: 0, left: 0,
backgroundColor: 'rgba(255,255,255,0.9)', backgroundColor: 'rgba(255,255,255,0.9)',

View File

@ -51,6 +51,10 @@ const New = inject("rootStore") ( observer(
* toggles window: open and close * toggles window: open and close
* - encryptionkey: String * - encryptionkey: String
* encryption key of the password * encryption key of the password
* - addDoc: function
* adds a new doc locally
* - toggleEditWindow: function
* Open or close edit window
*/ */
//Stored information //Stored information
@ -138,7 +142,7 @@ const New = inject("rootStore") ( observer(
db.collection("passwords/"+uid+"/passwords").add(data) db.collection("passwords/"+uid+"/passwords").add(data)
.then((docRef) => { .then((docRef) => {
console.log("Document written with ID: ", docRef.id); console.log("Document written with ID: ", docRef.id);
alertify.success("Document successfully added!"); alertify.success("Document added!");
//Add a new doc locally so no new data transfer from firestore //Add a new doc locally so no new data transfer from firestore
//is needed //is needed

View File

@ -2,7 +2,6 @@ import React, {Component} from 'react';
import jss from 'jss'; import jss from 'jss';
import preset from 'jss-preset-default'; import preset from 'jss-preset-default';
import { Container } from 'semantic-ui-react'; import { Container } from 'semantic-ui-react';
import history from '../../stores/functions/history';
/* /*
* Functions import * Functions import
@ -49,11 +48,6 @@ class Home extends Component {
} }
componentDidMount() {
history.push('/');
}
componentWillUnmount() { componentWillUnmount() {
this.sheet.detach() this.sheet.detach()
} }

View File

@ -59,6 +59,7 @@ const PasswordManager = inject("rootStore") ( observer(
this.onChangeInput = this.onChangeInput.bind(this); this.onChangeInput = this.onChangeInput.bind(this);
this.addDoc = this.addDoc.bind(this); this.addDoc = this.addDoc.bind(this);
this.updateDoc = this.updateDoc.bind(this); this.updateDoc = this.updateDoc.bind(this);
this.deleteDoc = this.deleteDoc.bind(this);
this.state = { this.state = {
newWindowOpen: false, newWindowOpen: false,
@ -111,6 +112,21 @@ const PasswordManager = inject("rootStore") ( observer(
} }
deleteDoc(editIndex) {
//Delete doc locally
var newArr = [];
this.state.data.forEach(function(element, index) {
if(index !== editIndex) {
newArr.push(element);
}
});
this.setState({
data: newArr
});
}
onChangeInput(e) { onChangeInput(e) {
//Encryption key and search input //Encryption key and search input
@ -265,7 +281,7 @@ const PasswordManager = inject("rootStore") ( observer(
<div> <div>
<New addDoc={this.addDoc} encryptionkey={this.state.key} open={this.state.newWindowOpen} toggleNewWindow={this.toggleNewWindow} /> <New addDoc={this.addDoc} encryptionkey={this.state.key} open={this.state.newWindowOpen} toggleNewWindow={this.toggleNewWindow} />
<Edit updateDoc={this.updateDoc} data={this.state.data} editIndex={this.state.editIndex} encryptionkey={this.state.key} open={this.state.editWindowOpen} toggleEditWindow={this.toggleEditWindow} /> <Edit updateDoc={this.updateDoc} deleteDoc={this.deleteDoc} data={this.state.data} editIndex={this.state.editIndex} encryptionkey={this.state.key} open={this.state.editWindowOpen} toggleEditWindow={this.toggleEditWindow} />
<Headline black="Password " red="manager" /> <Headline black="Password " red="manager" />