Skip to content

Commit

Permalink
refactor: move lastSyncedValue to mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Sep 25, 2022
1 parent d6a0776 commit 9813abf
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"classnames": "^2.3.1",
"expr-eval": "^2.0.2",
"final-form": "^4.20.6",
"final-form-set-field-data": "^1.0.2",
"history": "^5.1.0",
"html-react-parser": "^1.4.8",
"idb-keyval": "^6.1.0",
Expand Down
2 changes: 2 additions & 0 deletions src/data-workspace/data-entry-cell/data-entry-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const DataEntryField = React.memo(function DataEntryField({
const [syncStatus, setSyncStatus] = useState({
syncing: false,
synced: false,
lastSyncedValue: undefined,
})
const { locked } = useLockedContext()

Expand All @@ -38,6 +39,7 @@ export const DataEntryField = React.memo(function DataEntryField({
dataElement={de}
categoryOptionCombo={coc}
setSyncStatus={setSyncStatus}
syncStatus={syncStatus}
disabled={disabled}
locked={locked}
/>
Expand Down
4 changes: 4 additions & 0 deletions src/data-workspace/data-entry-cell/entry-field-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function EntryFieldInput({
dataElement: de,
categoryOptionCombo: coc,
setSyncStatus,
syncStatus,
disabled,
locked,
}) {
Expand Down Expand Up @@ -102,6 +103,7 @@ export function EntryFieldInput({
disabled,
locked,
setSyncStatus,
syncStatus,
onFocus,
onKeyDown,
}),
Expand All @@ -112,6 +114,7 @@ export function EntryFieldInput({
disabled,
locked,
setSyncStatus,
syncStatus,
onFocus,
onKeyDown,
]
Expand All @@ -136,4 +139,5 @@ EntryFieldInput.propTypes = {
fieldname: PropTypes.string,
locked: PropTypes.bool,
setSyncStatus: PropTypes.func,
syncStatus: PropTypes.object,
}
24 changes: 19 additions & 5 deletions src/data-workspace/data-entry-cell/inner-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export function InnerWrapper({
fieldname,
deId,
cocId,
<<<<<<< HEAD
syncStatus,
=======
>>>>>>> refactor: move lastSyncedValue to mutatior
}) {
const hasComment = useValueStore((state) =>
state.hasComment({
Expand All @@ -61,9 +64,17 @@ export function InnerWrapper({
const { item } = useHighlightedFieldIdContext()
const highlighted = item && deId === item.de.id && cocId === item.coc.id
const {
meta: { invalid, active },
} = useField(fieldname, { subscription: { invalid: true, active: true } })

input: { value },
meta: { invalid, active, data },
} = useField(fieldname, {
subscription: {
value: true,
invalid: true,
active: true,
data: true,
},
})
const synced = data.lastSyncedValue === value
// Detect if this field is sending data
const dataValueParams = useDataValueParams({ deId, cocId })
const activeMutations = useIsMutating({
Expand All @@ -74,7 +85,7 @@ export function InnerWrapper({
// see https://dhis2.atlassian.net/browse/TECH-1316
const cellStateClassName = invalid
? styles.invalid
: activeMutations === 0 && syncStatus.synced
: activeMutations === 0 && synced
? styles.synced
: null

Expand All @@ -90,7 +101,7 @@ export function InnerWrapper({
{children}
<SyncStatusIndicator
isLoading={activeMutations > 0}
isSynced={syncStatus.synced}
isSynced={synced}
/>
<CommentIndicator hasComment={hasComment} />
</div>
Expand All @@ -103,8 +114,11 @@ InnerWrapper.propTypes = {
disabled: PropTypes.bool,
fieldname: PropTypes.string,
locked: PropTypes.bool,
<<<<<<< HEAD
syncStatus: PropTypes.shape({
synced: PropTypes.bool,
syncing: PropTypes.bool,
}),
=======
>>>>>>> refactor: move lastSyncedValue to mutatior
}
2 changes: 2 additions & 0 deletions src/data-workspace/final-form-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import setFieldData from 'final-form-set-field-data'
import PropTypes from 'prop-types'
import React, { useState } from 'react'
import { Form } from 'react-final-form'
Expand Down Expand Up @@ -37,6 +38,7 @@ export function FinalFormWrapper({ children, dataValueSet }) {
initialValues={initialValues}
subscriptions={subscriptions}
keepDirtyOnReinitialize
mutators={{ setFieldData }}
>
{() => children}
</Form>
Expand Down
9 changes: 4 additions & 5 deletions src/data-workspace/inputs/boolean-radios.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import i18n from '@dhis2/d2-i18n'
import { Button, Radio } from '@dhis2/ui'
import cx from 'classnames'
import React, { useState } from 'react'
import React from 'react'
import { useField } from 'react-final-form'
import { useSetDataValueMutation } from '../../shared/index.js'
import styles from './inputs.module.css'
Expand All @@ -20,6 +20,7 @@ export const BooleanRadios = ({
disabled,
locked,
setSyncStatus,
syncStatus,
onKeyDown,
onFocus,
}) => {
Expand Down Expand Up @@ -59,7 +60,6 @@ export const BooleanRadios = ({
input: { value: fieldvalue },
meta,
} = useField(fieldname)
const [lastSyncedValue, setLastSyncedValue] = useState(fieldvalue)

const { mutate } = useSetDataValueMutation({ deId, cocId })
const syncData = (value) => {
Expand All @@ -69,8 +69,7 @@ export const BooleanRadios = ({
{ value: value || '' },
{
onSuccess: () => {
setLastSyncedValue(value)
setSyncStatus({ syncing: false, synced: true })
setSyncStatus({ synced: true, lastSyncedValue: value })
},
}
)
Expand All @@ -82,7 +81,7 @@ export const BooleanRadios = ({
const handleChange = (value) => {
const { valid } = meta
// If this value has changed, sync it to server if valid
if (valid && value !== lastSyncedValue) {
if (valid && value !== syncStatus.lastSyncedValue) {
syncData(value)
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/data-workspace/inputs/file-inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ export const FileResourceInput = ({
input.onChange({ name: newFile.name, size: newFile.size })
input.onBlur()
if (newFile instanceof File) {
setSyncStatus({ syncing: true, synced: false })
uploadFile(
{ file: newFile },
{
onSuccess: () => {
setSyncStatus({ syncing: false, synced: true })
setSyncStatus({ synced: true })
},
}
)
Expand All @@ -83,10 +82,9 @@ export const FileResourceInput = ({
const handleDelete = () => {
input.onChange('')
input.onBlur()
setSyncStatus({ syncing: true, synced: false })
deleteFile(null, {
onSuccess: () => {
setSyncStatus({ syncing: false, synced: true })
setSyncStatus({ synced: true })
},
})
}
Expand Down
17 changes: 9 additions & 8 deletions src/data-workspace/inputs/generic-input.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cx from 'classnames'
import PropTypes from 'prop-types'
import React, { useState } from 'react'
import { useField } from 'react-final-form'
import React from 'react'
import { useField, useForm } from 'react-final-form'
import {
NUMBER_TYPES,
VALUE_TYPES,
Expand All @@ -25,7 +25,6 @@ export const GenericInput = ({
fieldname,
deId,
cocId,
setSyncStatus,
valueType,
onKeyDown,
onFocus,
Expand All @@ -46,16 +45,17 @@ export const GenericInput = ({
return value.trim()
}
}
const form = useForm()
const { input, meta } = useField(fieldname, {
validate: validateByValueTypeWithLimits(valueType, limits),
subscription: { value: true, dirty: true, valid: true },
subscription: { value: true, dirty: true, valid: true, data: true },
format: formatValue,
formatOnBlur: true,
// This is require to ensure form is validated on first page load
// this is because the validate prop doesn't rerender when limits change
data: limits,
})
const [lastSyncedValue, setLastSyncedValue] = useState(input.value)

const { mutate } = useSetDataValueMutation({ deId, cocId })
const syncData = (value) => {
// todo: Here's where an error state could be set: ('onError')
Expand All @@ -64,8 +64,9 @@ export const GenericInput = ({
{ value: value || '' },
{
onSuccess: () => {
setLastSyncedValue(value)
setSyncStatus({ syncing: false, synced: true })
form.mutators.setFieldData(fieldname, {
lastSyncedValue: value,
})
},
}
)
Expand All @@ -75,7 +76,7 @@ export const GenericInput = ({
const { value } = input
const formattedValue = formatValue(value)
const { valid } = meta
if (valid && formattedValue !== lastSyncedValue) {
if (valid && formattedValue !== meta.data.lastSyncedValue) {
syncData(formattedValue)
}
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8446,6 +8446,11 @@ filter-obj@^1.1.0:
resolved "https://registry.yarnpkg.com/filter-obj/-/filter-obj-1.1.0.tgz#9b311112bc6c6127a16e016c6c5d7f19e0805c5b"
integrity sha512-8rXg1ZnX7xzy2NGDVkBVaAy+lSlPNwad13BtgSlLuxfIslyt5Vg64U7tFcCt4WS1R0hvtnQybT/IyCkGZ3DpXQ==

final-form-set-field-data@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/final-form-set-field-data/-/final-form-set-field-data-1.0.2.tgz#ce19095af5d607148c1e6ce3403d75d40223d848"
integrity sha512-gAnENimyQ5GW3OEGca5pbwm4lYshW2orzfBlPUYqzcm7ZxkQrVO8FqCAgEcCM+Rq9U1OU0q+D+UkqETvvDY6jw==

final-form@^4.20.2, final-form@^4.20.6:
version "4.20.7"
resolved "https://registry.yarnpkg.com/final-form/-/final-form-4.20.7.tgz#e7e2eb5fd952951d4fe6153d46043da2d68b207e"
Expand Down

0 comments on commit 9813abf

Please sign in to comment.