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

feat!: set default available from to next month #1775

Merged
merged 2 commits into from
Nov 23, 2024
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 cypress/e2e/transaction-import.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('Transaction Import', () => {

// Now at the last transaction, import this too
cy.contains('5 of 5')
cy.getInputFor('Available From').should('have.value', '2023-06-01')
cy.getInputFor('Available From').should('have.value', '2023-07-01')
cy.get('button').contains('Import').click()

// Now back at the second transactions since we skipped it
Expand Down
11 changes: 7 additions & 4 deletions cypress/e2e/transactions.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import {
createTransactions,
} from '../support/setup'
import { Budget, Account, Envelope } from '../../src/types'
import { dateFromIsoString } from '../../src/lib/dates'
import { dateFromIsoString, setToFirstOfNextMonth } from '../../src/lib/dates'

describe('Transactions', () => {
const date = new Date()

// Get the current month in YYYY-MM-01 format
const currentMonth = `${new Date(Date.now())
.toISOString()
.substring(0, 7)}-01`
const currentMonth = `${date.toISOString().substring(0, 7)}-01`

beforeEach(() => {
// prepare a budget with two internal & one external accounts
Expand Down Expand Up @@ -112,6 +110,11 @@ describe('Transactions', () => {
cy.getAutocompleteFor('Destination').type('Bank ac')
cy.contains('Bank account').click()

cy.getInputFor('Available From').should(
'have.value',
setToFirstOfNextMonth(dateFromIsoString(date.toISOString()))
)

cy.getInputFor('Available From').type(currentMonth)
cy.getInputFor('Available From').should('have.value', currentMonth)

Expand Down
34 changes: 28 additions & 6 deletions src/components/TransactionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
dateToIsoString,
monthYearFromDate,
setToFirstOfTheMonth,
setToFirstOfNextMonth,
} from '../lib/dates'
import { safeName } from '../lib/name-helper'
import {
Expand Down Expand Up @@ -117,6 +118,18 @@ const TransactionForm = ({ budget, setNotification }: Props) => {
setTransaction({ ...transaction, [key]: value })
}

const updateValues = (
values: { key: keyof UnpersistedTransaction; value: any }[]
) => {
const newTransaction = { ...transaction }
values.forEach(
(value: { key: keyof UnpersistedTransaction; value: any }) => {
newTransaction[value.key] = value.value
}
)
setTransaction(newTransaction)
}

const createNewResources = async () => {
const promises = []
let { sourceAccountId, destinationAccountId } = transaction
Expand Down Expand Up @@ -394,7 +407,15 @@ const TransactionForm = ({ budget, setNotification }: Props) => {
onChange={e => {
// value is empty string for invalid dates (e.g. when prefixing month with 0 while typing) – we want to ignore that and keep the previous input
if (e.target.value) {
updateValue('date', dateToIsoString(e.target.value))
updateValues([
{ key: 'date', value: dateToIsoString(e.target.value) },
{
key: 'availableFrom',
value: dateToIsoString(
setToFirstOfNextMonth(e.target.value)
),
},
])
}
}}
options={{ disabled: transaction.reconciled || false }}
Expand All @@ -412,11 +433,12 @@ const TransactionForm = ({ budget, setNotification }: Props) => {
}`}
value={(isSupported.inputTypeMonth()
? (date: string) => monthYearFromDate(new Date(date))
: (date: string) =>
setToFirstOfTheMonth(dateFromIsoString(date)))(
transaction.availableFrom ||
transaction.date ||
new Date().toISOString()
: (date: string) => date)(
dateFromIsoString(transaction.availableFrom || '') ||
setToFirstOfNextMonth(
dateFromIsoString(transaction.date || '') ||
new Date().toISOString()
)
)}
onChange={e => {
// value is empty string for invalid dates (e.g. when prefixing month with 0 while typing) – we want to ignore that and keep the previous input
Expand Down
3 changes: 2 additions & 1 deletion src/components/TransactionImport/Result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
dateToIsoString,
monthYearFromDate,
setToFirstOfTheMonth,
setToFirstOfNextMonth,
} from '../../lib/dates'
import { api } from '../../lib/api/base'
import { safeName } from '../../lib/name-helper'
Expand Down Expand Up @@ -436,7 +437,7 @@ const Result = (props: Props) => {
value={(isSupported.inputTypeMonth()
? (date: string) => monthYearFromDate(new Date(date))
: (date: string) =>
setToFirstOfTheMonth(dateFromIsoString(date)))(
setToFirstOfNextMonth(dateFromIsoString(date)))(
currentTransaction().availableFrom ||
currentTransaction().date ||
new Date().toISOString()
Expand Down
10 changes: 10 additions & 0 deletions src/lib/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@ const setToFirstOfTheMonth = (date: string) => {
return `${year}-${month}-01`
}

const setToFirstOfNextMonth = (date: string) => {
const d = new Date(date)
d.setDate(1)
d.setMonth(d.getMonth() + 1)

// Return date in YYYY-MM-DD format
return dateFromIsoString(d.toISOString())
}

export {
dateFromIsoString,
dateToIsoString,
monthYearFromDate,
dateFromMonthYear,
setToFirstOfTheMonth,
setToFirstOfNextMonth,
translatedMonthFormat,
shortTranslatedMonthFormat,
}
Loading