Skip to content
This repository has been archived by the owner on Jul 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #30 from AndresMorelos/update-created-at-date
Browse files Browse the repository at this point in the history
Giving the ability to update create at date.
  • Loading branch information
AndresMorelos authored Mar 21, 2022
2 parents e3e80d1 + f4078d5 commit 5c846fa
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 51 deletions.
71 changes: 71 additions & 0 deletions app/components/form/CreatedAt.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Libraries
import React, { Component } from 'react';
import PropTypes from 'prop-types';

// Custom Components
import { Section } from '../shared/Section';
import CreatedAtPicker from './CreatedAtPicker';

// Animation
import _withFadeInAnimation from '../shared/hoc/_withFadeInAnimation';

// Component
export class CreatedAt extends Component {
constructor(props) {
super(props);
this.state = { created_at: this.props.created_at };
this.updateCustomDate = this.updateCustomDate.bind(this);
this.updatePaymentTerm = this.updatePaymentTerm.bind(this);
this.updateCreateAt = this.updateCreateAt.bind(this);
}

// Handle Clear Form
UNSAFE_componentWillReceiveProps(nextProps) {
const { created_at } = nextProps.created_at;
if (created_at === null) {
this.setState(nextProps.created_at);
}
}

updateCustomDate(created_at) {
this.setState({ created_at }, () => {
this.updateCreateAt(this.state);
});
}

updatePaymentTerm(paymentTerm) {
this.setState({ paymentTerm }, () => {
this.updateCreateAt(this.state);
});
}

updateCreateAt(data) {
this.props.updateFieldData('created_at', data);
}

render() {
const { t } = this.props;
const { created_at } = this.state;
return (
<Section>
<label className="itemLabel">
{t('form:fields:createAtDate:name')}
</label>
<CreatedAtPicker
t={t}
created_at={created_at}
updateCustomDate={this.updateCustomDate}
/>
</Section>
);
}
}

CreatedAt.propTypes = {
created_at: PropTypes.number,
t: PropTypes.func.isRequired,
updateFieldData: PropTypes.func.isRequired,
};

// Export
export default _withFadeInAnimation(CreatedAt);
81 changes: 81 additions & 0 deletions app/components/form/CreatedAtPicker.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Libraries
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';

// React Dates
import 'react-dates/initialize'
import 'react-dates/lib/css/_datepicker.css'
import { SingleDatePicker } from 'react-dates';

// Styles
import styled from 'styled-components';
import _withFadeInAnimation from '../shared/hoc/_withFadeInAnimation';
const Container = styled.div`
display: flex;
margin-bottom: 20px;
`;

// Component
export class CreatedAtPicker extends PureComponent {
constructor(props) {
super(props);
this.state = { focused: false };
this.onFocusChange = this.onFocusChange.bind(this);
this.onDateChange = this.onDateChange.bind(this);
this.clearDate = this.clearDate.bind(this);
}

onFocusChange() {
this.setState({ focused: !this.state.focused });
}

onDateChange(date) {
const created_at = date === null ? null : new Date(date).getTime();
this.props.updateCustomDate(created_at);
}

clearDate() {
this.onDateChange(null);
}

render() {
const { t, created_at } = this.props;
const createdAt = created_at === null ? null : moment(created_at);
return (
<Container>
<SingleDatePicker
id="invoice-createdAt"
placeholder={t('form:fields:createAtDate:placeHolder')}
firstDayOfWeek={1}
withFullScreenPortal
displayFormat="DD/MM/YYYY"
hideKeyboardShortcutsPanel
date={createdAt}
isOutsideRange={() => false}
focused={this.state.focused}
onFocusChange={this.onFocusChange}
onDateChange={newDate => this.onDateChange(newDate)}
/>
{created_at !== null && (
<a className="clearDateBtn active" href="#" onClick={this.clearDate}>
<i className="ion-close-circled" />
</a>
)}
</Container>
);
}
}

CreatedAtPicker.propTypes = {
created_at: PropTypes.number,
t: PropTypes.func.isRequired,
updateCustomDate: PropTypes.func.isRequired,
};

CreatedAtPicker.defaultProps = {
created_at: null,
};

// Export
export default _withFadeInAnimation(CreatedAtPicker);
11 changes: 11 additions & 0 deletions app/containers/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
import * as SettingsActions from '../actions/settings';
import * as FormActions from '../actions/form';
import { getCurrentInvoice } from '../reducers/FormReducer';
import CreatedAt from '../components/form/CreatedAt';

// Component
class Form extends PureComponent {
Expand All @@ -52,6 +53,7 @@ class Form extends PureComponent {
} = boundFormActionCreators;
// Form Value
const {
created_at,
dueDate,
currency,
discount,
Expand All @@ -62,6 +64,7 @@ class Form extends PureComponent {
settings,
savedSettings,
} = currentInvoice;

const { required_fields, open, editMode } = settings;
// Translation
const { t } = this.props;
Expand Down Expand Up @@ -104,6 +107,13 @@ class Form extends PureComponent {
)}
<Recipient />
<ItemsList />
{editMode.active && (
<CreatedAt
t={t}
created_at={created_at}
updateFieldData={updateFieldData}
/>
)}
{required_fields.dueDate && (
<DueDate
t={t}
Expand Down Expand Up @@ -178,6 +188,7 @@ Form.propTypes = {
note: PropTypes.object.isRequired,
payment: PropTypes.object.isRequired,
settings: PropTypes.object.isRequired,
created_at: PropTypes.number,
}).isRequired,
};

Expand Down
45 changes: 31 additions & 14 deletions app/helpers/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isEmpty, pick, includes } from 'lodash';
import { v4 as uuidv4 } from 'uuid';
import i18n from '../../i18n/i18n';
import { getInvoiceValue } from './invoice';
import { encrypt } from './encryption'
import { encrypt } from './encryption';
const appConfig = require('@electron/remote').require('electron-settings');
const openDialog = require('../renderers/dialog');

Expand Down Expand Up @@ -45,6 +45,7 @@ function getInvoiceData(formData, secretKey) {
note,
payment,
settings,
created_at,
} = formData;
// Required fields
const { editMode, required_fields } = settings;
Expand All @@ -54,7 +55,8 @@ function getInvoiceData(formData, secretKey) {
if (recipient.newRecipient) {
// Add id & created_at so the invoice records will remembers
invoiceData.recipient = {
...recipient.new, _id: uuidv4(),
...recipient.new,
_id: uuidv4(),
created_at: Date.now(),
};
} else {
Expand Down Expand Up @@ -86,35 +88,44 @@ function getInvoiceData(formData, secretKey) {
const invoice = {
_id: editMode.active ? editMode.data._id : uuidv4(),
_rev: editMode.active ? editMode.data._rev : null,
};

let createdAtToUpdate = Date.now();

if (editMode.active) {
createdAtToUpdate =
created_at.created_at !== editMode.data.created_at
? created_at.created_at
: editMode.data.created_at;
}

const content = encrypt({
docs: {
...invoiceData, // Metadata
created_at: editMode.active ? editMode.data.created_at : Date.now(),
created_at: createdAtToUpdate,
updated_at: Date.now(),
status: editMode.active ? editMode.data.status : 'pending',
// Alway calculate subtotal & grandTotal
subtotal: getInvoiceValue(invoiceData).subtotal,
grandTotal: getInvoiceValue(invoiceData).grandTotal,
},
secretKey
})
secretKey,
});

invoice.content = content;

const newRecipient = {
_id: invoiceData.recipient._id
}
_id: invoiceData.recipient._id,
};

delete invoiceData.recipient._id;

const recipientContent = encrypt({
docs: {
...invoiceData.recipient
...invoiceData.recipient,
},
secretKey
})
secretKey,
});

newRecipient.content = recipientContent;

Expand Down Expand Up @@ -151,7 +162,8 @@ function validateRecipient(recipient) {
return false;
}
// Is email address valid?
const regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
const regex =
/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!regex.test(recipient.new.email)) {
openDialog({
type: 'warning',
Expand Down Expand Up @@ -302,7 +314,6 @@ function validatePayment(isRequired, payment) {
return true;
}


function validateInvoiceID(isRequired, invoiceID) {
if (isRequired) {
if (!invoiceID || invoiceID === '') {
Expand All @@ -321,7 +332,7 @@ function validateInvoiceID(isRequired, invoiceID) {
// SET RECIPIENT INFORMATION IN EDIT MODE
function setEditRecipient(allContacts, currentContact) {
if (allContacts.length) {
const contactIDs = allContacts.map(contact => contact._id);
const contactIDs = allContacts.map((contact) => contact._id);
if (includes(contactIDs, currentContact._id)) {
return {
newRecipient: false,
Expand All @@ -331,7 +342,13 @@ function setEditRecipient(allContacts, currentContact) {
}
return {
newRecipient: true,
new: pick(currentContact, ['fullname', 'company', 'phone', 'email', 'address']),
new: pick(currentContact, [
'fullname',
'company',
'phone',
'email',
'address',
]),
};
}

Expand Down
Loading

0 comments on commit 5c846fa

Please sign in to comment.