Skip to content

Commit

Permalink
feat: extract job recreation state logic to separate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
awgaan authored and varl committed Mar 23, 2020
1 parent 5826a2d commit 11d698d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 120 deletions.
58 changes: 21 additions & 37 deletions src/pages/DataImport/DataImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import PropTypes from 'prop-types'
import { useConfig } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'

// import s from './DataImport.module.css'
import { dataImportPage as p } from '../../utils/pages'
import { uploadFile } from '../../utils/helper'
import { getPrevJobDetails, uploadFile } from '../../utils/helper'
import { testIds } from '../../utils/testIds'
import { helpText } from '../../utils/text'
import {
Expand Down Expand Up @@ -35,45 +34,30 @@ import { ImportButtonStrip } from '../../components/ImportButtonStrip'
import { FormAlerts } from '../../components/FormAlerts'
import { TaskContext, getNewestTask } from '../../contexts/'

const createInitialState = prevJobDetails => ({
file: prevJobDetails.file,
format: prevJobDetails.format || defaultFormatOption,
strategy: prevJobDetails.strategy || defaultStrategyOption,
firstRowIsHeader: !!prevJobDetails.firstRowIsHeader,
preheatCache: !!prevJobDetails.preheatCache,
skipAudit: !!prevJobDetails.skipAudit,
dataElementIdScheme:
prevJobDetails.dataElementIdScheme || defaultDataElementIdSchemeOption,
orgUnitIdScheme:
prevJobDetails.orgUnitIdScheme || defaultOrgUnitIdSchemeOption,
idScheme: prevJobDetails.idScheme || defaultIdSchemeOption,
skipExistingCheck: !!prevJobDetails.skipExistingCheck,
})

const DataImport = ({ query }) => {
const {
tasks: { data: dataTasks },
addTask,
} = useContext(TaskContext)

// recreating a previously run job
let prevJobDetails = undefined
if (query && query.id) {
const job = dataTasks[query.id]
if (job) {
prevJobDetails = job.jobDetails
}
}

const initialState = {
file: prevJobDetails ? prevJobDetails.file : undefined,
format: prevJobDetails ? prevJobDetails.format : defaultFormatOption,
strategy: prevJobDetails
? prevJobDetails.strategy
: defaultStrategyOption,
firstRowIsHeader: prevJobDetails
? prevJobDetails.firstRowIsHeader
: false,
preheatCache: prevJobDetails ? prevJobDetails.preheatCache : false,
skipAudit: prevJobDetails ? prevJobDetails.skipAudit : false,
dataElementIdScheme: prevJobDetails
? prevJobDetails.dataElementIdScheme
: defaultDataElementIdSchemeOption,
orgUnitIdScheme: prevJobDetails
? prevJobDetails.orgUnitIdScheme
: defaultOrgUnitIdSchemeOption,
idScheme: prevJobDetails
? prevJobDetails.idScheme
: defaultIdSchemeOption,
skipExistingCheck: prevJobDetails
? prevJobDetails.skipExistingCheck
: false,
}
const prevJobDetails = getPrevJobDetails(query, dataTasks)
const initialState = createInitialState(prevJobDetails)

const [progress, setProgress] = useState(0)
const [file, setFile] = useState(initialState.file)
Expand All @@ -98,7 +82,7 @@ const DataImport = ({ query }) => {
const [showFullSummaryTask, setShowFullSummaryTask] = useState(false)
const { baseUrl } = useConfig()

const onSubmit = ({ dryRun }) => {
const onImport = ({ dryRun }) => {
// validate
const alerts = []
const timestamp = new Date().getTime()
Expand All @@ -121,6 +105,7 @@ const DataImport = ({ query }) => {
const apiBaseUrl = `${baseUrl}/api/`
const endpoint = 'dataValueSets.json'
const params = [
'async=true',
`dryRun=${dryRun}`,
`strategy=${strategy.value}`,
`preheatCache=${preheatCache}`,
Expand All @@ -130,7 +115,6 @@ const DataImport = ({ query }) => {
`idScheme=${idScheme.value}`,
`skipExistingCheck=${skipExistingCheck}`,
`format=${format.value}`,
'async=true',
format.value == 'csv' ? `firstRowIsHeader=${firstRowIsHeader}` : '',
]
.filter(s => s != '')
Expand Down Expand Up @@ -249,7 +233,7 @@ const DataImport = ({ query }) => {
/>
</MoreOptions>
<ImportButtonStrip
onSubmit={onSubmit}
onImport={onImport}
dryRunDataTest={testIds.DataImport.dryRun}
importDataTest={testIds.DataImport.submit}
dataTest={testIds.DataImport.ImportButtonStrip}
Expand Down
35 changes: 13 additions & 22 deletions src/pages/EventImport/EventImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import PropTypes from 'prop-types'
import { useConfig } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'

// import s from './EventImport.module.css';
import { eventImportPage as p } from '../../utils/pages'
import { uploadFile } from '../../utils/helper'
import { getPrevJobDetails, uploadFile } from '../../utils/helper'
import { testIds } from '../../utils/testIds'
import {
formatOptions,
Expand All @@ -22,31 +21,23 @@ import { ImportButtonStrip } from '../../components/ImportButtonStrip'
import { FormAlerts } from '../../components/FormAlerts'
import { TaskContext, getNewestTask } from '../../contexts/'

const createInitialState = prevJobDetails => ({
file: prevJobDetails.file,
format: prevJobDetails.format || defaultFormatOption,
orgUnitIdScheme:
prevJobDetails.orgUnitIdScheme || defaultOrgUnitIdSchemeOption,
eventIdScheme: prevJobDetails.eventIdScheme || defaultEventIdSchemeOption,
})

const EventImport = ({ query }) => {
const {
tasks: { event: eventTasks },
addTask,
} = useContext(TaskContext)

// recreating a previously run job
let prevJobDetails = undefined
if (query && query.id) {
const job = eventTasks[query.id]
if (job) {
prevJobDetails = job.jobDetails
}
}

const initialState = {
file: prevJobDetails ? prevJobDetails.file : undefined,
format: prevJobDetails ? prevJobDetails.format : defaultFormatOption,
orgUnitIdScheme: prevJobDetails
? prevJobDetails.orgUnitIdScheme
: defaultOrgUnitIdSchemeOption,
eventIdScheme: prevJobDetails
? prevJobDetails.eventIdScheme
: defaultEventIdSchemeOption,
}
const prevJobDetails = getPrevJobDetails(query, eventTasks)
const initialState = createInitialState(prevJobDetails)

const [progress, setProgress] = useState(0)
const [file, setFile] = useState(initialState.file)
Expand All @@ -61,7 +52,7 @@ const EventImport = ({ query }) => {
const [showFullSummaryTask, setShowFullSummaryTask] = useState(false)
const { baseUrl } = useConfig()

const onSubmit = ({ dryRun }) => {
const onImport = ({ dryRun }) => {
// validate
const alerts = []
const timestamp = new Date().getTime()
Expand Down Expand Up @@ -151,7 +142,7 @@ const EventImport = ({ query }) => {
/>
</MoreOptions>
<ImportButtonStrip
onSubmit={onSubmit}
onImport={onImport}
dryRunDataTest={testIds.DataImport.dryRun}
importDataTest={testIds.DataImport.submit}
dataTest={testIds.DataImport.ImportButtonStrip}
Expand Down
24 changes: 9 additions & 15 deletions src/pages/GMLImport/GMLImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,36 @@ import PropTypes from 'prop-types'
import { useConfig } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'

// import s from './GMLImport.module.css';
import { gmlImportPage as p } from '../../utils/pages'
import { uploadFile } from '../../utils/helper'
import { getPrevJobDetails, uploadFile } from '../../utils/helper'
import { testIds } from '../../utils/testIds'
import { Page } from '../../components/Page'
import { FileUpload } from '../../components/FileUpload'
import { ImportButtonStrip } from '../../components/ImportButtonStrip'
import { FormAlerts } from '../../components/FormAlerts'
import { TaskContext, getNewestTask } from '../../contexts/'

const createInitialState = prevJobDetails => ({
file: prevJobDetails.file,
})

const GMLImport = ({ query }) => {
const {
tasks: { gml: gmlTasks },
addTask,
} = useContext(TaskContext)

// recreating a previously run job
let prevJobDetails = undefined
if (query && query.id) {
const job = gmlTasks[query.id]
if (job) {
prevJobDetails = job.jobDetails
}
}

const initialState = {
file: prevJobDetails ? prevJobDetails.file : undefined,
}
const prevJobDetails = getPrevJobDetails(query, gmlTasks)
const initialState = createInitialState(prevJobDetails)

const [progress, setProgress] = useState(0)
const [file, setFile] = useState(initialState.file)
const [alerts, setAlerts] = useState([])
const [showFullSummaryTask, setShowFullSummaryTask] = useState(false)
const { baseUrl } = useConfig()

const onSubmit = ({ dryRun }) => {
const onImport = ({ dryRun }) => {
// validate
const alerts = []
const timestamp = new Date().getTime()
Expand Down Expand Up @@ -98,7 +92,7 @@ const GMLImport = ({ query }) => {
dataTest={testIds.GMLImport.FileUpload}
/>
<ImportButtonStrip
onSubmit={onSubmit}
onImport={onImport}
dryRunDataTest={testIds.DataImport.dryRun}
importDataTest={testIds.DataImport.submit}
dataTest={testIds.DataImport.ImportButtonStrip}
Expand Down
71 changes: 25 additions & 46 deletions src/pages/MetadataImport/MetadataImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import PropTypes from 'prop-types'
import { useConfig } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'

// import s from './MetadataImport.module.css';
import { metadataImportPage as p } from '../../utils/pages'
import { uploadFile } from '../../utils/helper'
import { getPrevJobDetails, uploadFile } from '../../utils/helper'
import { testIds } from '../../utils/testIds'
import {
formatOptions,
Expand Down Expand Up @@ -37,55 +36,35 @@ import { ImportButtonStrip } from '../../components/ImportButtonStrip'
import { FormAlerts } from '../../components/FormAlerts'
import { TaskContext, getNewestTask } from '../../contexts/'

const createInitialState = prevJobDetails => ({
file: prevJobDetails.file,
format: prevJobDetails.format || defaultFormatOption,
identifier: prevJobDetails.identifier || defaultIdentifierOption,
importReportMode:
prevJobDetails.importReportMode || defaultImportReportModeOption,
preheatMode: prevJobDetails.preheatMode || defaultPreheatModeOption,
importStrategy:
prevJobDetails.importStrategy || defaultImportStrategyOption,
firstRowIsHeader: !!prevJobDetails.firstRowIsHeader,
atomicMode: prevJobDetails.atomicMode || defaultAtomicModeOption,
mergeMode: prevJobDetails.mergeMode || defaultMergeModeOption,
flushMode: prevJobDetails.flushMode || defaultFlushModeOption,
inclusionStrategy:
prevJobDetails.inclusionStrategy || defaultInclusionStrategyOption,
skipSharing: !!prevJobDetails.skipSharing,
skipValidation: !!prevJobDetails.skipValidation,
isAsync: !prevJobDetails.isAsync,
})

const MetadataImport = ({ query }) => {
const {
tasks: { metadata: metadataTasks },
addTask,
} = useContext(TaskContext)

// recreating a previously run job
let prevJobDetails = undefined
if (query && query.id) {
const job = metadataTasks[query.id]
if (job) {
prevJobDetails = job.jobDetails
}
}

const initialState = {
file: prevJobDetails ? prevJobDetails.file : undefined,
format: prevJobDetails ? prevJobDetails.format : defaultFormatOption,
identifier: prevJobDetails
? prevJobDetails.identifier
: defaultIdentifierOption,
importReportMode: prevJobDetails
? prevJobDetails.importReportMode
: defaultImportReportModeOption,
preheatMode: prevJobDetails
? prevJobDetails.preheatMode
: defaultPreheatModeOption,
importStrategy: prevJobDetails
? prevJobDetails.importStrategy
: defaultImportStrategyOption,
firstRowIsHeader: prevJobDetails
? prevJobDetails.firstRowIsHeader
: false,
atomicMode: prevJobDetails
? prevJobDetails.atomicMode
: defaultAtomicModeOption,
mergeMode: prevJobDetails
? prevJobDetails.mergeMode
: defaultMergeModeOption,
flushMode: prevJobDetails
? prevJobDetails.flushMode
: defaultFlushModeOption,
inclusionStrategy: prevJobDetails
? prevJobDetails.inclusionStrategy
: defaultInclusionStrategyOption,
skipSharing: prevJobDetails ? prevJobDetails.skipSharing : false,
skipValidation: prevJobDetails ? prevJobDetails.skipValidation : false,
isAsync: prevJobDetails ? prevJobDetails.isAsync : true,
}
const prevJobDetails = getPrevJobDetails(query, metadataTasks)
const initialState = createInitialState(prevJobDetails)

const [progress, setProgress] = useState(0)
const [file, setFile] = useState(initialState.file)
Expand Down Expand Up @@ -117,7 +96,7 @@ const MetadataImport = ({ query }) => {
const [showFullSummaryTask, setShowFullSummaryTask] = useState(false)
const { baseUrl } = useConfig()

const onSubmit = ({ dryRun }) => {
const onImport = ({ dryRun }) => {
// validate
const alerts = []
const timestamp = new Date().getTime()
Expand Down Expand Up @@ -332,7 +311,7 @@ const MetadataImport = ({ query }) => {
/>
</MoreOptions>
<ImportButtonStrip
onSubmit={onSubmit}
onImport={onImport}
dryRunDataTest={testIds.DataImport.dryRun}
importDataTest={testIds.DataImport.submit}
dataTest={testIds.DataImport.ImportButtonStrip}
Expand Down
10 changes: 10 additions & 0 deletions src/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,20 @@ const locationAssign = url => {
}
}

const getPrevJobDetails = (query, tasks) => {
if (!query || !query.id) return {}

const job = tasks[query.id]
if (!job) return {}

return job.jobDetails
}

export {
createBlob,
downloadBlob,
fetchAttributes,
getPrevJobDetails,
locationAssign,
jsDateToISO8601,
jsDateToString,
Expand Down

0 comments on commit 11d698d

Please sign in to comment.