Skip to content

Commit

Permalink
Surveys list: added checkbox to show only own surveys (#3549)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefano Ricci <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 30, 2024
1 parent ef2c645 commit 7d6544c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions core/i18n/resources/en/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ Merge cannot be performed.`,
languages: 'Languages',
nodes: 'Nodes',
noSurveysMatchingFilter: 'No surveys matching the specified filter',
onlyOwn: 'Only own surveys',
records: 'Records',
recordsCreatedWithMoreApps: 'Records created with more apps:',
},
Expand Down
6 changes: 4 additions & 2 deletions server/modules/survey/api/surveyApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const init = (app) => {
sortBy,
sortOrder,
includeCounts = false,
onlyOwn = false,
} = Request.getParams(req)

const list = await SurveyService.fetchUserSurveysInfo({
Expand All @@ -89,6 +90,7 @@ export const init = (app) => {
sortBy,
sortOrder,
includeCounts,
onlyOwn,
})

res.json({ list })
Expand All @@ -100,9 +102,9 @@ export const init = (app) => {
app.get('/surveys/count', AuthMiddleware.requireLoggedInUser, async (req, res, next) => {
try {
const user = Request.getUser(req)
const { draft = true, template = false, search = null, lang = null } = Request.getParams(req)
const { draft = true, template = false, search = null, lang = null, onlyOwn = false } = Request.getParams(req)

const count = await SurveyService.countUserSurveys({ user, draft, template, search, lang })
const count = await SurveyService.countUserSurveys({ user, draft, template, search, lang, onlyOwn })

res.json({ count })
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions server/modules/survey/manager/surveyManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ export const fetchUserSurveysInfo = async (
sortOrder,
includeCounts = false,
includeOwnerEmailAddress = false,
onlyOwn = false,
},
client = db
) => {
Expand All @@ -324,6 +325,7 @@ export const fetchUserSurveysInfo = async (
sortBy,
sortOrder,
includeOwnerEmailAddress,
onlyOwn,
})
).map(assocSurveyInfo)

Expand Down
6 changes: 4 additions & 2 deletions server/modules/survey/repository/surveyRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const _getSurveysSelectQuery = ({
sortBy = Survey.sortableKeys.dateModified,
sortOrder = 'DESC',
includeOwnerEmailAddress = false,
onlyOwn = false,
}) => {
const checkAccess = (!template || draft) && !User.isSystemAdmin(user)
const propsCol = draft ? '(s.props || s.props_draft)' : 's.props'
Expand Down Expand Up @@ -152,6 +153,7 @@ const _getSurveysSelectQuery = ({
-- if draft is false, fetch only published surveys
${draft ? '' : `s.props <> '{}'::jsonb AND `}
${_getSelectWhereCondition({ draft, search })}
${onlyOwn ? ' AND s.owner_uuid = $/userUuid/' : ''}
ORDER BY ${sortByField} ${sortOrder}
LIMIT ${limit === null ? 'ALL' : limit}
OFFSET ${offset}
Expand Down Expand Up @@ -194,15 +196,15 @@ export const fetchUserSurveys = async (
}

export const countUserSurveys = async (
{ user, draft = false, template = false, search: searchParam = null, lang = null },
{ user, draft = false, template = false, search: searchParam = null, lang = null, onlyOwn = false },
client = db
) => {
const search = StringUtils.isNotBlank(searchParam) ? `%${searchParam.toLowerCase()}%` : null

return client.one(
`
SELECT COUNT(*)
FROM (${_getSurveysSelectQuery({ user, draft, template, lang, search })}) AS s`,
FROM (${_getSurveysSelectQuery({ user, draft, template, lang, search, onlyOwn })}) AS s`,
{ search, template, userUuid: User.getUuid(user) },
(row) => Number(row.count)
)
Expand Down
12 changes: 9 additions & 3 deletions webapp/components/survey/Surveys/HeaderLeft/HeaderLeft.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React from 'react'
import PropTypes from 'prop-types'

import { TextInput } from '@webapp/components/form'
import { Checkbox, TextInput } from '@webapp/components/form'
import { ButtonDownload } from '@webapp/components/buttons'
import { useAuthCanExportSurveysList } from '@webapp/store/user/hooks'
import { useAuthCanExportSurveysList, useUserIsSystemAdmin } from '@webapp/store/user/hooks'

const HeaderLeft = (props) => {
const { handleSearch, search, totalCount } = props
const { handleSearch, onlyOwn = false, search, setOnlyOwn = null, totalCount } = props

const isSystemAdmin = useUserIsSystemAdmin()
const canExportSurveys = useAuthCanExportSurveysList()

if (!totalCount) return null
Expand All @@ -22,14 +23,19 @@ const HeaderLeft = (props) => {
}}
placeholder="surveysView.filterPlaceholder"
/>
{isSystemAdmin && setOnlyOwn && (
<Checkbox checked={onlyOwn} label="surveysView.onlyOwn" onChange={() => setOnlyOwn(!onlyOwn)} />
)}
{canExportSurveys && <ButtonDownload href="/api/surveys/export" label="common.csvExport" />}
</>
)
}

HeaderLeft.propTypes = {
handleSearch: PropTypes.func.isRequired,
onlyOwn: PropTypes.bool,
search: PropTypes.string.isRequired,
setOnlyOwn: PropTypes.func,
totalCount: PropTypes.number.isRequired,
}

Expand Down
7 changes: 5 additions & 2 deletions webapp/components/survey/Surveys/Surveys.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { appModuleUri, homeModules } from '@webapp/app/appModules'

import { useBrowserLanguageCode, useOnUpdate } from '@webapp/components/hooks'
import { SurveyActions, useSurveyInfo } from '@webapp/store/survey'
import { useUser } from '@webapp/store/user'
import { useUser, useUserIsSystemAdmin } from '@webapp/store/user'

import Table from '@webapp/components/Table'
import { TableCellFiles } from '@webapp/components/Table/TableCellFiles'
Expand All @@ -29,12 +29,14 @@ const Surveys = (props) => {
const user = useUser()
const surveyInfo = useSurveyInfo()
const lang = useBrowserLanguageCode()
const isSystemAdmin = useUserIsSystemAdmin()

/**
* Parameter passed to table rest params
* (used to reload table data on survey publish).
*/
const [requestedAt, setRequestedAt] = useState(Date.now())
const [onlyOwn, setOnlyOwn] = useState(isSystemAdmin)

// Redirect to dashboard on survey change
useOnUpdate(() => {
Expand Down Expand Up @@ -174,13 +176,14 @@ const Surveys = (props) => {
className="surveys"
columns={columns}
headerLeftComponent={HeaderLeft}
headerProps={{ onlyOwn, setOnlyOwn }}
isRowActive={isRowActive}
keyExtractor={({ item }) => Survey.getId(item)}
module={module}
moduleApiUri={moduleApiUri}
noItemsLabelForSearchKey="surveysView.noSurveysMatchingFilter"
onRowClick={onRowClick}
restParams={{ lang, template, requestedAt, includeCounts: true }}
restParams={{ lang, template, requestedAt, includeCounts: true, onlyOwn }}
visibleColumnsSelectionEnabled
/>
)
Expand Down

0 comments on commit 7d6544c

Please sign in to comment.