Skip to content

Commit

Permalink
fixup! feat!: set default available from to next month
Browse files Browse the repository at this point in the history
  • Loading branch information
morremeyer committed Nov 23, 2024
1 parent b4cfbfc commit 52df7a4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
5 changes: 4 additions & 1 deletion cypress/e2e/transactions.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ describe('Transactions', () => {

cy.getInputFor('Available From').should(
'have.value',
setToFirstOfNextMonth(date.toISOString().split('T')[0])
setToFirstOfNextMonth(dateFromIsoString(date.toISOString()))
)

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

cy.getAutocompleteFor('Envelope').type('Onl')
cy.contains('Only one').click()

Expand Down
36 changes: 29 additions & 7 deletions src/components/TransactionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
dateFromIsoString,
dateToIsoString,
monthYearFromDate,
setToFirstOfTheMonth,
setToFirstOfNextMonth,
} from '../lib/dates'
import { safeName } from '../lib/name-helper'
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,18 +433,19 @@ const TransactionForm = ({ budget, setNotification }: Props) => {
}`}
value={(isSupported.inputTypeMonth()
? (date: string) => monthYearFromDate(new Date(date))
: (date: string) =>
setToFirstOfNextMonth(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
if (e.target.value) {
updateValue(
'availableFrom',
dateToIsoString(e.target.value)
dateToIsoString(setToFirstOfTheMonth(e.target.value))
)
}
}}
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 @@ -21,6 +21,7 @@ import {
dateFromIsoString,
dateToIsoString,
monthYearFromDate,
setToFirstOfTheMonth,
setToFirstOfNextMonth,
} from '../../lib/dates'
import { api } from '../../lib/api/base'
Expand Down Expand Up @@ -446,7 +447,7 @@ const Result = (props: Props) => {
if (e.target.value) {
updateValue(
'availableFrom',
dateToIsoString(e.target.value)
dateToIsoString(setToFirstOfTheMonth(e.target.value))
)
}
}}
Expand Down
10 changes: 8 additions & 2 deletions src/lib/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,26 @@ const dateFromMonthYear = (date: string) => {
return new Date(`${month}/15/${year}`)
}

const setToFirstOfTheMonth = (date: string) => {
const [year, month] = date.split('-')
return `${year}-${month}-01`
}

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

// Return date part of ISO string
return d.toISOString().split('T')[0]
// Return date in YYYY-MM-DD format
return dateFromIsoString(d.toISOString())
}

export {
dateFromIsoString,
dateToIsoString,
monthYearFromDate,
dateFromMonthYear,
setToFirstOfTheMonth,
setToFirstOfNextMonth,
translatedMonthFormat,
shortTranslatedMonthFormat,
Expand Down

0 comments on commit 52df7a4

Please sign in to comment.