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 all 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',
},
}
2 changes: 2 additions & 0 deletions test/e2e/mock/survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const template2 = {

export const templateFromSurvey = {
cloneFrom: survey.name,
cloneFromType: 'Survey',
cloneFromLabel: survey.label,
name: 'template_from_survey',
label: 'My Survey',
Expand All @@ -52,6 +53,7 @@ export const templateFromSurvey = {

export const surveyFromTemplate = {
cloneFrom: templateFromSurvey.name,
cloneFromType: 'Template',
cloneFromLabel: templateFromSurvey.label,
name: 'survey_from_template',
label: templateFromSurvey.label,
Expand Down
11 changes: 10 additions & 1 deletion test/e2e/tests/_templateCreate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ import { gotoTemplateCreate } from '../_navigation'
export const createTemplate = (template) => {
gotoTemplateCreate()

const { cloneFrom, cloneFromLabel, label, name } = template
const { cloneFrom, cloneFromType, cloneFromLabel, label, name } = template

test(`Create Template ${name}`, async () => {
await FormUtils.fillInput(TestId.surveyCreate.surveyName, name)

if (cloneFrom) {
// select create type "Clone"
await page.click(getSelector(TestId.surveyCreate.createTypeBtn({ prefix: 'templateCreateType', type: 'clone' })))

// select clone from type (survey or template)
const cloneFromTypeLocator = page.locator(
`.clone-from-type_btn-group .radio-button-group-item[text="${cloneFromType}"]`
)
await cloneFromTypeLocator.waitFor()
await cloneFromTypeLocator.click()

await FormUtils.selectDropdownItem({
testId: TestId.surveyCreate.surveyCloneFrom,
label: `${cloneFrom} - ${cloneFromLabel}`,
Expand Down
8 changes: 6 additions & 2 deletions webapp/components/RadioButtonGroup.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import React, { useCallback } from 'react'
import PropTypes from 'prop-types'
import { FormControlLabel, Radio, RadioGroup } from '@mui/material'
import classNames from 'classnames'

import { useI18n } from '@webapp/store/system'

export const RadioButtonGroup = (props) => {
const { items, onChange: onChangeProp, row = false, value } = props
const { className, items, onChange: onChangeProp, row = false, value } = props

const i18n = useI18n()

const onChange = useCallback((e) => onChangeProp(e.target.value), [onChangeProp])

return (
<RadioGroup onChange={onChange} row={row} value={value}>
<RadioGroup className={classNames('radio-button-group', className)} onChange={onChange} row={row} value={value}>
{items.map((item) => {
const { key, disabled: itemDisabled, label, labelParams } = item
return (
<FormControlLabel
key={key}
className="radio-button-group-item"
value={key}
control={<Radio disabled={itemDisabled} />}
label={i18n.t(label, labelParams)}
Expand All @@ -28,6 +31,7 @@ export const RadioButtonGroup = (props) => {
}

RadioButtonGroup.propTypes = {
className: PropTypes.string,
items: PropTypes.array.isRequired,
onChange: PropTypes.func.isRequired,
row: PropTypes.bool,
Expand Down
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
65 changes: 40 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,26 @@ 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
className="clone-from-type_btn-group"
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 +204,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 +229,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
Loading
Loading