-
Notifications
You must be signed in to change notification settings - Fork 49
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
Interview import dataset #1188
Draft
jochenklar
wants to merge
25
commits into
interview-copy-dataset
Choose a base branch
from
interview-import-dataset
base: interview-copy-dataset
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Interview import dataset #1188
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a96d85c
Add import function to create set modal on pages
jochenklar 271d1d9
Add separate search endpoint for values and use AsyncSelect in PageHe…
jochenklar 9eda9d9
Fix errors after rebase
jochenklar 30194d2
Add error handling to copy-set action and fix/add tests
jochenklar 80e5ef9
Improve PageHeadFormModal
jochenklar e88a844
Add import function into existing set
jochenklar 4dff50f
Update/fix tests
jochenklar 8f9f3a6
Rename import/copy answers to reuse answers
jochenklar b9a547a
Fix copy_set action and update tests
jochenklar ef84164
Add reuse value modal for questions
jochenklar 6ed31f2
Add reuse value model to checkboxes
jochenklar c8de022
Add project filter to reuse value modal
jochenklar 30e3742
Add useLsState hook and use it to store reuse modal information
jochenklar 40208cd
Fix page component
jochenklar 6c5e2f3
Fix react select error state
jochenklar e4d35a3
Fix reuse modal
jochenklar 9d25784
Add tests for value search endpoint
jochenklar 0a6abff
Fix QuestionSetCopySet component
jochenklar 29dbaa5
Fix Page component
jochenklar 8055cb6
Refactor interview modals
jochenklar 71d4b84
Fix Value model
jochenklar 7984080
Fix typo
jochenklar d88ad3e
Update .gitignore
jochenklar 24f6831
Fix radio input
jochenklar 3966d34
Fix search component
jochenklar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useState } from 'react' | ||
import { isEmpty, isPlainObject, omit as lodashOmit } from 'lodash' | ||
|
||
import { checkStoreId } from '../utils/store' | ||
|
||
const useLsState = (path, initialValue, omit = []) => { | ||
checkStoreId() | ||
|
||
const getLs = (path) => { | ||
const data = JSON.parse(localStorage.getItem(path)) | ||
return isPlainObject(data) ? lodashOmit(data, omit) : data | ||
} | ||
|
||
const setLs = (path, value) => { | ||
const data = isPlainObject(value) ? lodashOmit(value, omit) : value | ||
localStorage.setItem(path, JSON.stringify(data)) | ||
} | ||
|
||
// get the value from the local storage | ||
const lsValue = getLs(path) | ||
|
||
// setup the state with the value from the local storage or the provided initialValue | ||
const [value, setValue] = useState(isEmpty(lsValue) ? initialValue : lsValue) | ||
|
||
return [ | ||
value, | ||
(value) => { | ||
setLs(path, value) | ||
setValue(value) | ||
}, | ||
() => { | ||
if (isPlainObject(initialValue)) { | ||
setValue({ ...initialValue, ...getLs(path) }) | ||
} else { | ||
setValue(initialValue) | ||
} | ||
} | ||
] | ||
} | ||
|
||
export default useLsState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
rdmo/projects/assets/js/interview/components/main/Search.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import AsyncSelect from 'react-select/async' | ||
import { useDebouncedCallback } from 'use-debounce' | ||
import { isEmpty, isNil, pick } from 'lodash' | ||
|
||
import ProjectApi from '../../api/ProjectApi' | ||
import ValueApi from '../../api/ValueApi' | ||
|
||
const Search = ({ attribute, values, setValues, collection = false }) => { | ||
// create a key for the first AsyncSelect, to reset the loaded values when project or snapshot changes | ||
const key = (values.project ? values.project.id : '') + (values.snapshot ? '-all' : '') | ||
|
||
const handleLoadValues = useDebouncedCallback((search, callback) => { | ||
ValueApi.searchValues({ | ||
attribute, | ||
search, | ||
project: values.project ? values.project.id : '', | ||
snapshot: values.snapshot ? 'all' : '', | ||
collection | ||
}).then(response => { | ||
if (collection) { | ||
// if the search component is used from QuestionReuseValues/CheckboxWidget | ||
// the list of values from the server needs to be reduced to show only one entry | ||
// for each set_prefix/set_index combination | ||
callback(response.reduce((collections, value) => { | ||
// look if a value for the same project/snapshot/set_prefix/set_index already exists in values_list | ||
const collection = isNil(collections) ? null : collections.find(v => ( | ||
(v.project == value.project) && | ||
(v.snapshot == value.snapshot) && | ||
(v.set_prefix == value.set_prefix) && | ||
(v.set_index == value.set_index) | ||
)) | ||
if (isNil(collection)) { | ||
// append the value | ||
return [...collections, { ...value, values: [value] }] | ||
} else { | ||
// update the value_title and the values array of the existing value | ||
collection.value_label += '; ' + value.value_label | ||
collection.values.push(value) | ||
return collections | ||
} | ||
}, [])) | ||
} else { | ||
callback(response) | ||
} | ||
}) | ||
}, 500) | ||
|
||
const handleLoadProjects = useDebouncedCallback((search, callback) => { | ||
ProjectApi.fetchProjects({ search }) | ||
.then(response => callback(response.results.map(project => pick(project, 'id', 'title')))) | ||
}, 500) | ||
|
||
return <> | ||
<AsyncSelect | ||
key={key} | ||
classNamePrefix="react-select" | ||
className='react-select' | ||
placeholder={gettext('Search for project or snapshot title, or answer text ...')} | ||
noOptionsMessage={() => gettext( | ||
'No answers match your search.' | ||
)} | ||
loadingMessage={() => gettext('Loading ...')} | ||
options={[]} | ||
value={values.value} | ||
onChange={(value) => setValues({ ...values, value })} | ||
getOptionValue={(value) => value} | ||
getOptionLabel={(value) => value.value_label} | ||
formatOptionLabel={(value) => ( | ||
<div> | ||
{gettext('Project')} <strong>{value.project_label}</strong> | ||
{ | ||
value.snapshot && <> | ||
<span className="mr-5 ml-5">→</span> | ||
{gettext('Snapshot')} <strong>{value.snapshot_label}</strong> | ||
</> | ||
} | ||
<span className="mr-5 ml-5">→</span> | ||
{value.value_label} | ||
</div> | ||
)} | ||
loadOptions={handleLoadValues} | ||
defaultOptions | ||
isClearable | ||
backspaceRemovesValue={true} | ||
/> | ||
|
||
<AsyncSelect | ||
classNamePrefix='react-select' | ||
className='react-select mt-10' | ||
placeholder={gettext('Restrict the search to a particular project ...')} | ||
noOptionsMessage={() => gettext( | ||
'No projects matching your search.' | ||
)} | ||
loadingMessage={() => gettext('Loading ...')} | ||
options={[]} | ||
value={values.project} | ||
onChange={(project) => setValues({ | ||
...values, | ||
value: (isEmpty(project) || project == values.project) ? values.value : '', // reset value | ||
project: project | ||
})} | ||
getOptionValue={(project) => project} | ||
getOptionLabel={(project) => project.title} | ||
loadOptions={handleLoadProjects} | ||
defaultOptions | ||
isClearable | ||
backspaceRemovesValue={true} | ||
/> | ||
|
||
<div className="checkbox"> | ||
<label> | ||
<input | ||
type="checkbox" | ||
checked={values.snapshot} | ||
onChange={() => setValues({ | ||
...values, | ||
value: values.snapshot ? '' : values.value, // reset value | ||
snapshot: !values.snapshot })} | ||
/> | ||
<span>{gettext('Include snapshots in the search')}</span> | ||
</label> | ||
</div> | ||
</> | ||
} | ||
|
||
Search.propTypes = { | ||
attribute: PropTypes.number.isRequired, | ||
values: PropTypes.object.isRequired, | ||
setValues: PropTypes.func.isRequired, | ||
collection: PropTypes.bool | ||
} | ||
|
||
export default Search |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this
localStorage
useLsState
hook just a re-implementation of what was already stored in localStorage before or is it a new state altogether??There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before we had
configActions
which are synced with the local storage. Here I bypass the redux store and just use auseState
hook which is synced to the local storage.