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

Survey clone: divide surveys and templates; sort by name #3613

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
7 changes: 5 additions & 2 deletions core/i18n/i18nFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export const Trans = i18nTrans

const defaultLanguage = 'en'

const namespaces = ['common', 'appErrors', 'jobs', 'surveyCreate']
const defaultNamespace = 'common'

const createParams = (lang) => ({
fallbackLng: defaultLanguage,
debug: ProcessUtils.isEnvDevelopment,
Expand All @@ -18,8 +21,8 @@ const createParams = (lang) => ({
nsMode: 'default', // Set it to fallback to let passed namespaces to translated hoc act as fallbacks
},
lng: lang,
ns: ['common', 'appErrors', 'jobs'],
defaultNS: 'common',
ns: namespaces,
defaultNS: defaultNamespace,
resources: {
en: enTranslation,
},
Expand Down
24 changes: 1 addition & 23 deletions core/i18n/resources/en/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ Do you want to proceed?`,
boolean: 'Boolean',
code: 'Code',
coordinate: 'Coordinate',
geo: 'Geospatial',
taxon: 'Taxon',
file: 'File',
entity: 'Entity',
Expand Down Expand Up @@ -479,29 +480,6 @@ $t(common.cantUndoWarning)`,
active: '$t(common.active)',
activate: 'Activate',
},
surveyCreate: {
createSurvey: 'Create Survey',
createTemplate: 'Create Template',
newSurvey: 'New Survey',
newSurveyFromScratch: 'New Survey From Scratch',
newTemplate: 'New Template',
newTemplateFromScratch: 'New Template From Scratch',
source: {
label: 'Source',
arena: 'Arena (.zip)',
collect: 'Collect (.collect, .collect-backup, .collect-data)',
},
startImport: 'Start import',
survey: 'Survey',
survey_other: 'Surveys',
template: 'Template',
template_other: 'Templates',
error: 'Error creating new survey',
errorMaxSurveysCountExceeded: `Error creating survey; please check that the maximum number of surveys that you can creeate ({{maxSurveysCount}}) has not been exceeded.`,
options: {
includeData: 'Include data',
},
},
collectImportReport: {
excludeResolvedItems: 'Exclude resolved items',
expression: 'Expression',
Expand Down
2 changes: 2 additions & 0 deletions core/i18n/resources/en/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import activityLog from './activityLog'
import appErrors from './appErrors'
import common from './common'
import jobs from './jobs'
import surveyCreate from './surveyCreate'

export default {
activityLog,
appErrors,
common,
jobs,
surveyCreate,
}
27 changes: 27 additions & 0 deletions core/i18n/resources/en/surveyCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default {
cloneFromType: {
survey: 'Survey',
template: 'Template',
},
createSurvey: 'Create Survey',
createTemplate: 'Create Template',
newSurvey: 'New Survey',
newSurveyFromScratch: 'New Survey From Scratch',
newTemplate: 'New Template',
newTemplateFromScratch: 'New Template From Scratch',
source: {
label: 'Source',
arena: 'Arena (.zip)',
collect: 'Collect (.collect, .collect-backup, .collect-data)',
},
startImport: 'Start import',
survey: 'Survey',
survey_other: 'Surveys',
template: 'Template',
template_other: 'Templates',
error: 'Error creating new survey',
errorMaxSurveysCountExceeded: `Error creating survey; please check that the maximum number of surveys that you can creeate ({{maxSurveysCount}}) has not been exceeded.`,
options: {
includeData: 'Include data',
},
}
4 changes: 3 additions & 1 deletion webapp/components/form/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const Dropdown = (props) => {
itemLabel = 'label',
itemValue = 'value',
items: itemsProp,
loading: loadingProp = false,
menuPlacement = 'auto',
menuPosition = 'fixed',
multiple = false,
Expand Down Expand Up @@ -116,7 +117,7 @@ const Dropdown = (props) => {
inputId={inputId}
isClearable={clearable && !readOnly}
isDisabled={disabled}
isLoading={loading}
isLoading={loading || loadingProp}
isMulti={multiple}
isSearchable={searchable && !readOnly}
onChange={onChange}
Expand Down Expand Up @@ -147,6 +148,7 @@ Dropdown.propTypes = {
itemLabel: PropTypes.oneOfType([PropTypes.string, PropTypes.func]), // item label function or property name
itemValue: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
items: PropTypes.oneOfType([PropTypes.array, PropTypes.func]).isRequired,
loading: PropTypes.bool,
menuPlacement: PropTypes.oneOf(['auto', 'top', 'bottom']),
menuPosition: PropTypes.oneOf(['absolute', 'fixed']),
multiple: PropTypes.bool,
Expand Down
2 changes: 1 addition & 1 deletion webapp/components/form/Dropdown/useDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ export const useDropdown = ({

const itemToOption = useCallback(
(item) => ({
...item,
description: getOptionDescription(item),
icon: getOptionIcon(item),
label: getOptionLabel(item),
value: getOptionValue(item),
...(item.options ? { options: item.options } : {}),
}),
[getOptionDescription, getOptionIcon, getOptionLabel, getOptionValue]
)
Expand Down
64 changes: 39 additions & 25 deletions webapp/components/survey/SurveyCreate/SurveyCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,28 @@ import LanguageDropdown from '@webapp/components/form/languageDropdown'
import { useOnUpdate } from '@webapp/components/hooks'
import { Checkbox, Dropdown } from '@webapp/components/form'
import { Button, Dropzone, ProgressBar, RadioButtonGroup } from '@webapp/components'
import { SurveyType } from '@webapp/model'

import { createTypes, importSources, useCreateSurvey } from './store'
import { SurveyDropdown } from '../SurveyDropdown'

const fileMaxSizeDefault = 1000 // 1GB
const fileMaxSizeSystemAdmin = 2000 // 2GB

const importSourceButtonGroupItems = Object.values(importSources).map((key) => ({
key,
label: `surveyCreate:source.${key}`,
}))

const cloneFromTypeButtonGroupItems = Object.values(SurveyType)
.reverse()
.map((key) => ({
key,
label: `surveyCreate:cloneFromType.${key}`,
}))

const SurveyCreate = (props) => {
const { showImport = true, submitButtonLabel = 'homeView.surveyCreate.createSurvey', template = false } = props
const { showImport = true, submitButtonLabel = 'surveyCreate:createSurvey', template = false } = props

const surveyInfo = useSurveyInfo()
const i18n = useI18n()
Expand All @@ -52,6 +65,7 @@ const SurveyCreate = (props) => {
source,
validation,
cloneFrom,
cloneFromType,
cloneFromCycle,
options,
file,
Expand All @@ -77,9 +91,7 @@ const SurveyCreate = (props) => {
items={[
{
key: createTypes.fromScratch,
label: template
? 'homeView.surveyCreate.newTemplateFromScratch'
: 'homeView.surveyCreate.newSurveyFromScratch',
label: template ? 'surveyCreate:newTemplateFromScratch' : 'surveyCreate:newSurveyFromScratch',
},
{
key: createTypes.clone,
Expand Down Expand Up @@ -128,15 +140,25 @@ const SurveyCreate = (props) => {

{createType === createTypes.clone && (
<>
<FormItem label="common.cloneFrom">
<SurveyDropdown
selection={cloneFrom?.value}
onChange={(value) => {
const cycles = value?.cycles || []
const lastCycleKey = cycles[cycles.length - 1]
onUpdate({ name: 'cloneFrom', value }, { name: 'cloneFromCycle', value: lastCycleKey })
}}
/>
<FormItem className="clone-from">
<fieldset>
<legend>{i18n.t('common.cloneFrom')}</legend>
<RadioButtonGroup
items={cloneFromTypeButtonGroupItems}
onChange={(value) => onUpdate({ name: 'cloneFromType', value })}
row
value={cloneFromType}
/>
<SurveyDropdown
onChange={(value) => {
const cycles = value?.cycles || []
const lastCycleKey = cycles[cycles.length - 1]
onUpdate({ name: 'cloneFrom', value }, { name: 'cloneFromCycle', value: lastCycleKey })
}}
selection={cloneFrom?.value}
type={cloneFromType}
/>
</fieldset>
</FormItem>
{cloneFrom?.cycles?.length > 1 && (
<FormItem label="common.cycle">
Expand Down Expand Up @@ -181,22 +203,14 @@ const SurveyCreate = (props) => {
<Checkbox
id={TestId.surveyCreate.optionIncludeDataCheckbox}
checked={options['includeData']}
label={`homeView.surveyCreate.options.includeData`}
label={`surveyCreate:options.includeData`}
onChange={(value) => onOptionChange({ key: 'includeData', value })}
/>
</div>
</fieldset>
</div>
<FormItem label="homeView.surveyCreate.source.label">
<RadioButtonGroup
items={Object.values(importSources).map((key) => ({
key,
label: `homeView.surveyCreate.source.${key}`,
}))}
onChange={onSourceChange}
row
value={source}
/>
<FormItem label="surveyCreate:source.label">
<RadioButtonGroup items={importSourceButtonGroupItems} onChange={onSourceChange} row value={source} />
</FormItem>
<div className="row">
<Dropzone
Expand All @@ -214,7 +228,7 @@ const SurveyCreate = (props) => {
<Button
className="btn-primary"
disabled={!file || uploading}
label={'homeView.surveyCreate.startImport'}
label="surveyCreate:startImport"
onClick={onImport}
testId={TestId.surveyCreate.startImportBtn}
/>
Expand Down
4 changes: 4 additions & 0 deletions webapp/components/survey/SurveyCreate/SurveyCreate.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@
.progress-bar-with-label {
flex-direction: column;
}

.clone-from {
height: auto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ const sendSurveyCreateRequest = async ({ dispatch, newSurvey, user }) => {
errorParams = null
if (e.status === StatusCodes.UNAUTHORIZED) {
const maxSurveysCount = Authorizer.getMaxSurveysUserCanCreate(user)
errorKey = 'homeView.surveyCreate.errorMaxSurveysCountExceeded'
errorKey = 'surveyCreate:errorMaxSurveysCountExceeded'
errorParams = { maxSurveysCount }
} else {
errorKey = 'homeView.surveyCreate.error'
errorKey = 'surveyCreate:error'
}
dispatch(NotificationActions.notifyError({ key: errorKey, params: errorParams }))
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useState } from 'react'

import { Objects } from '@openforis/arena-core'

import { SurveyType } from '@webapp/model'

import { useActions } from './actions'
import { importSources } from './importSources'

Expand All @@ -17,6 +19,7 @@ const initialState = {
label: '',
lang: 'en',
cloneFrom: null,
cloneFromType: SurveyType.template,
cloneFromCycle: null,
template: false,
options: { includeData: false },
Expand Down
16 changes: 10 additions & 6 deletions webapp/components/survey/SurveyDropdown/SurveyDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@ import { useI18n } from '@webapp/store/system'
import { TestId } from '@webapp/utils/testId'

import { Dropdown } from '@webapp/components/form'
import { SurveyType } from '@webapp/model'

import { useSurveyDropdownOptions } from './useSurveyDropdownOptions'

const SurveyDropdown = (props) => {
const { selection = null, onChange } = props
const { options } = useSurveyDropdownOptions()
const { onChange, selection = null, type = SurveyType.survey } = props
const { loading, options } = useSurveyDropdownOptions({ type })

const allOptions = options.reduce((optionsAcc, optionGroup) => [...optionsAcc, ...optionGroup.options], [])
const selectedOption = allOptions.find((option) => option.value === selection)
const selectedOption = options.find((option) => option.value === selection)

const i18n = useI18n()

return (
<Dropdown
className="survey-dropdown"
items={options}
loading={loading}
onChange={(item) => onChange(item)}
placeholder={i18n.t('common.select')}
renderOptionLabel={({ data }) => {
renderOptionLabel={(params) => {
const { data } = params
const { surveyName, surveyLabel } = data
return (
<div className="dropdown-option__label">
Expand All @@ -45,8 +48,9 @@ const SurveyDropdown = (props) => {
}

SurveyDropdown.propTypes = {
onChange: PropTypes.func.isRequired,
selection: PropTypes.string,
onChange: PropTypes.func,
type: PropTypes.oneOf(Object.values(SurveyType)),
}

export { SurveyDropdown }
2 changes: 0 additions & 2 deletions webapp/components/survey/SurveyDropdown/SurveyDropdown.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
.survey-dropdown {
width: 30rem;

.survey-name {
font-weight: bold;
}
Expand Down
Loading
Loading