diff --git a/cypress/e2e/transactions.cy.ts b/cypress/e2e/transactions.cy.ts index 3036665e..127a5f91 100644 --- a/cypress/e2e/transactions.cy.ts +++ b/cypress/e2e/transactions.cy.ts @@ -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() diff --git a/src/components/TransactionForm.tsx b/src/components/TransactionForm.tsx index 2ae2b831..af1737e5 100644 --- a/src/components/TransactionForm.tsx +++ b/src/components/TransactionForm.tsx @@ -8,6 +8,7 @@ import { dateFromIsoString, dateToIsoString, monthYearFromDate, + setToFirstOfTheMonth, setToFirstOfNextMonth, } from '../lib/dates' import { safeName } from '../lib/name-helper' @@ -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 @@ -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 }} @@ -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)) ) } }} diff --git a/src/components/TransactionImport/Result.tsx b/src/components/TransactionImport/Result.tsx index 8c0b81d6..50e46731 100644 --- a/src/components/TransactionImport/Result.tsx +++ b/src/components/TransactionImport/Result.tsx @@ -21,6 +21,7 @@ import { dateFromIsoString, dateToIsoString, monthYearFromDate, + setToFirstOfTheMonth, setToFirstOfNextMonth, } from '../../lib/dates' import { api } from '../../lib/api/base' @@ -446,7 +447,7 @@ const Result = (props: Props) => { if (e.target.value) { updateValue( 'availableFrom', - dateToIsoString(e.target.value) + dateToIsoString(setToFirstOfTheMonth(e.target.value)) ) } }} diff --git a/src/lib/dates.ts b/src/lib/dates.ts index 4e037e48..19c06a8d 100644 --- a/src/lib/dates.ts +++ b/src/lib/dates.ts @@ -23,13 +23,18 @@ 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 { @@ -37,6 +42,7 @@ export { dateToIsoString, monthYearFromDate, dateFromMonthYear, + setToFirstOfTheMonth, setToFirstOfNextMonth, translatedMonthFormat, shortTranslatedMonthFormat,