Skip to content

Commit

Permalink
Merge pull request #192 from DarkSmile92/dev
Browse files Browse the repository at this point in the history
Support 2-Decimal Formatting & customizable decimal separator
  • Loading branch information
hql287 authored Feb 11, 2018
2 parents bd47b94 + ae3a05d commit e64f2dd
Show file tree
Hide file tree
Showing 44 changed files with 1,885 additions and 1,309 deletions.
30 changes: 28 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,18 @@ function setInitialValues() {
invoice: {
exportDir: os.homedir(),
template: 'default',
currency: 'USD',
dateFormat: 'MM/DD/YYYY',
tax: {
tin: '123-456-789',
method: 'default',
amount: 0,
},
currency: {
code: 'USD',
placement: 'before',
separator: 'commaDot',
fraction: 2,
},
required_fields: {
invoiceID: false,
dueDate: false,
Expand All @@ -210,11 +215,14 @@ function setInitialValues() {
},
};

// Set initial values conditionally
// Set initial values conditionally work for 2 level depth key only,
// Changing anything deeper would need to be done with migration
for (const key in defaultOptions) {
// Add level 1 key if not exist
if (!appConfig.has(`${key}`)) {
appConfig.set(`${key}`, defaultOptions[key]);
}
// Add level 2 key if not exist
for (const childKey in defaultOptions[key]) {
if (!appConfig.has(`${key}.${childKey}`)) {
appConfig.set(`${key}.${childKey}`, defaultOptions[key][childKey]);
Expand Down Expand Up @@ -268,6 +276,24 @@ function migrateData() {
'test',
]);
},

2: configs => {
// Return current configs if this is the first time install
if ( configs.invoice.currency.placement !== undefined) {
return configs;
}
// Update current configs
return Object.assign({}, configs, {
invoice: Object.assign({}, configs.invoice, {
currency: {
code: configs.invoice.currency,
placement: 'before',
separator: 'commaDot',
fraction: 2,
}
})
});
},
};
// Get the current Config
const configs = appConfig.getAll();
Expand Down
108 changes: 90 additions & 18 deletions app/components/form/Currency.jsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,65 @@
// Libs
import React, { Component } from 'react';
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
const appConfig = require('electron').remote.require('electron-settings');
import currencies from '../../../libs/currencies.json';
import { keys, sortBy, isEqual } from 'lodash';

// Custom Components
import { Section, Header } from '../shared/Section';
import { Row, Field, Part } from '../shared/Part';

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

// Component
export class Currency extends Component {
export class Currency extends PureComponent {
constructor(props) {
super(props);
this.handleInputChange = this.handleInputChange.bind(this);
this.state = props.currency;
this.isSettingsSaved = this.isSettingsSaved.bind(this);
this.saveAsDefault = this.saveAsDefault.bind(this);
this.sortCurrencies = this.sortCurrencies.bind(this);
this.handleInputChange = this.handleInputChange.bind(this);
}

shouldComponentUpdate(nextProps) {
if (this.props.currency !== nextProps.currency) return true;
if (this.props.savedSettings !== nextProps.savedSettings) return true;
return true;
// Handle Form Clear
componentWillReceiveProps(nextProps) {
// Already made changes but not saved
if (this.state !== this.props.savedSettings) {
// Reset to savedSettings if the below confition is met
if (nextProps.currency === nextProps.savedSettings) {
this.setState(nextProps.savedSettings, () => {
this.updateCurrencyState();
});
}
}
}

handleInputChange(event) {
const value = event.target.value;
this.props.updateFieldData('currency', currencies[value]);
const target = event.target;
const name = target.name;
const value = name === 'fraction' ? parseInt(target.value, 10) : target.value;
this.setState({
[name]: value
}, () => {
this.updateCurrencyState();
}
);
}

updateCurrencyState() {
const { updateFieldData } = this.props;
updateFieldData('currency', this.state);
}

isSettingsSaved() {
return isEqual(this.props.currency.code, this.props.savedSettings);
return isEqual(this.props.currency, this.props.savedSettings);
}

saveAsDefault() {
const { updateSavedSettings } = this.props;
updateSavedSettings('currency', this.props.currency.code);
updateSavedSettings('currency', this.state);
}

sortCurrencies() {
Expand Down Expand Up @@ -79,20 +100,71 @@ export class Currency extends Component {
</a>
)}
</Header>
<select
value={this.props.currency.code}
onChange={this.handleInputChange}
>
{this.sortCurrencies()}
</select>
<Part>
<Row>
<Field>
<label className="itemLabel">
{t('form:fields:currency')}
</label>
<select
name="code"
value={this.state.code}
onChange={this.handleInputChange}
>
{this.sortCurrencies()}
</select>
</Field>
<Field>
<label className="itemLabel">{t('settings:fields:currency:separator')}</label>
<select
name="separator"
value={this.state.separator}
onChange={this.handleInputChange}
>
<option value="commaDot">
1,999.000 ({t('settings:fields:currency:commaDot')})
</option>
<option value="dotComma">
1.999,000 ({t('settings:fields:currency:dotComma')})
</option>
<option value="spaceDot">
1 999.000 ({t('settings:fields:currency:spaceDot')})
</option>
</select>
</Field>
</Row>
<Row>
<Field>
<label className="itemLabel">{t('settings:fields:currency:placement')}</label>
<select
name="placement"
value={this.state.placement}
onChange={this.handleInputChange}
>
<option value="before">{t('settings:fields:currency:beforeAmount')}</option>
<option value="after">{t('settings:fields:currency:afterAmount')}</option>
</select>
</Field>
<Field>
<label className="itemLabel">{t('settings:fields:currency:fraction')}</label>
<input
className="form-control"
name="fraction"
type="number"
value={this.state.fraction}
onChange={this.handleInputChange}
/>
</Field>
</Row>
</Part>
</Section>
);
}
}

Currency.propTypes = {
currency: PropTypes.object.isRequired,
savedSettings: PropTypes.string.isRequired,
savedSettings: PropTypes.object.isRequired,
t: PropTypes.func.isRequired,
updateFieldData: PropTypes.func.isRequired,
updateSavedSettings: PropTypes.func.isRequired,
Expand Down
22 changes: 3 additions & 19 deletions app/components/form/RecipientForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,12 @@ import _withFadeInAnimation from '../../components/shared/hoc/_withFadeInAnimati

// Styles
import styled from 'styled-components';
const Form = styled.div`
padding: 20px;
background: #f9fafa;
border-radius: 4px;
margin-bottom: 20px;
border: 1px solid #f2f3f4;
`;

const Row = styled.div`
display: flex;
margin: 0 -15px;
`;

const Field = styled.div`
flex: 1;
margin: 0 15px 20px 15px;
`;
import { Part, Row, Field } from '../shared/Part';

export function RecipientForm({ t, formData, updateRecipientForm }) {
const { fullname, company, email, phone } = formData;
return (
<Form>
<Part>
<Row>
<Field>
<label className="itemLabel">{t('common:fields:fullname')} *</label>
Expand Down Expand Up @@ -69,7 +53,7 @@ export function RecipientForm({ t, formData, updateRecipientForm }) {
/>
</Field>
</Row>
</Form>
</Part>
);
}

Expand Down
38 changes: 6 additions & 32 deletions app/components/form/Tax.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Libraries
import React, { Component } from 'react';
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { isEqual } from 'lodash';
// Custom Components
Expand All @@ -8,31 +8,12 @@ import { Section, Header } from '../shared/Section';
import _withFadeInAnimation from '../shared/hoc/_withFadeInAnimation';
// Styles
import styled from 'styled-components';
import { Part, Row, Field } from '../shared/Part';
const TaxID = styled.div``;
const TaxAmount = styled.div`
flex: 1;
`;

const Form = styled.div`
padding: 20px;
background: #f9fafa;
border-radius: 4px;
margin-bottom: 20px;
border: 1px solid #f2f3f4;
`;

const Row = styled.div`
display: flex;
margin: 0 -15px;
`;

const Field = styled.div`
flex: 1;
margin: 0 15px 20px 15px;
`;
const TaxAmount = styled.div`flex: 1;`;

// Component
export class Tax extends Component {
export class Tax extends PureComponent {
constructor(props) {
super(props);
this.state = props.tax;
Expand All @@ -54,13 +35,6 @@ export class Tax extends Component {
}
}

shouldComponentUpdate(nextProps, nextState) {
if (this.state !== nextState) return true;
if (this.props.tax !== nextProps.tax) return true;
if (this.props.savedSettings !== nextProps.savedSettings) return true;
return false;
}

handleInputChange(event) {
const target = event.target;
const name = target.name;
Expand Down Expand Up @@ -101,7 +75,7 @@ export class Tax extends Component {
</a>
)}
</Header>
<Form>
<Part>
<Row>
<Field>
<label className="itemLabel">{t('form:fields:tax:id')}</label>
Expand Down Expand Up @@ -142,7 +116,7 @@ export class Tax extends Component {
</select>
</Field>
</Row>
</Form>
</Part>
</Section>
);
}
Expand Down
Loading

0 comments on commit e64f2dd

Please sign in to comment.