Skip to content

Commit

Permalink
Display active surveys
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelbej committed Nov 13, 2024
1 parent a8a65aa commit 6802740
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 11 deletions.
6 changes: 6 additions & 0 deletions assets/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ export const newWave = (projectId, panelSurveyId) => {
return apiPostJSON(`projects/${projectId}/panel_surveys/${panelSurveyId}/new_wave`, surveySchema)
}

export const fetchActiveSurveys = (provider, baseUrl) => {
return apiFetchJSON(
`surveys/active_channel/${provider}?base_url=${encodeURIComponent(baseUrl)}`,
)
}

export const fetchTimezones = () => {
return apiFetchJSONWithCallback(`timezones`, null, {}, (json, schema) => {
return () => {
Expand Down
17 changes: 6 additions & 11 deletions assets/js/components/channels/ChannelIndex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import {
UntitledIfEmpty,
SortableHeader,
Modal,
ConfirmationModal,
PagingFooter,
channelFriendlyName,
} from "../ui"
import { Preloader } from "react-materialize"
import { config } from "../../config"
import { translate } from "react-i18next"
import ProviderModal from "./ProviderModal"

class ChannelIndex extends Component<any> {
componentDidMount() {
Expand Down Expand Up @@ -124,19 +124,14 @@ class ChannelIndex extends Component<any> {
}

const providerModal = (provider, index, friendlyName, multiple) => {
let name = `${provider[0].toUpperCase()}${provider.slice(1)}`
if (multiple) name = `${name} (${friendlyName})`

return (
<ConfirmationModal
<ProviderModal
key={`${provider}-${index}`}
modalId={`${provider}Modal-${index}`}
modalText={t("Do you want to delete the channels provided by {{name}}?", { name })}
header={t("Turn off {{name}}", { name })}
confirmationText={t("Yes")}
provider={provider}
index={index}
friendlyName={friendlyName}
multiple={multiple}
onConfirm={() => this.deleteProvider(provider, index)}
style={{ maxWidth: "600px" }}
showCancel
/>
)
}
Expand Down
95 changes: 95 additions & 0 deletions assets/js/components/channels/ProviderModal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import React, { Component, PropTypes } from "react"
import { translate } from "react-i18next"
import * as api from "../../api"
import { ConfirmationModal } from "../ui"
import { config } from "../../config"

export class ProviderModal extends Component {
constructor(props) {
super(props)
this.state = {
loading: false,
surveys: [],
}
}

componentDidMount() {
const { provider, index } = this.props
const { baseUrl } = config[provider][index]
this.setState({
loading: true,
surveys: [],
})
api.fetchActiveSurveys(provider, baseUrl)
.then((response) => {
const surveys = response || []
this.setState({
loading: false,
surveys,
})
})
.catch(() => {
this.setState({
loading: false,
surveys: [],
})
})
}

render() {
const {
t,
provider,
index,
friendlyName,
multiple,
onConfirm,
} = this.props

const { loading, surveys } = this.state

let name = `${provider[0].toUpperCase()}${provider.slice(1)}`
if (multiple) name = `${name} (${friendlyName})`

return (
<ConfirmationModal
modalId={`${provider}Modal-${index}`}
header={t("Turn off {{name}}", { name })}
confirmationText={t("Yes")}
onConfirm={onConfirm}
style={{ maxWidth: "600px" }}
showCancel
>
<div>
<p>{t("Do you want to delete the channels provided by {{name}}?", { name })}</p>

<div className="provider-surveys">
{loading ? <span>{t("Loading surveys...")}</span> :
surveys.length == 0 ? <span>{t("No active surveys")}</span> :
<div>
<span>{t("Active surveys")}</span>
<ul>
{surveys.map((survey) => (
<li key={`survey-${survey.id}`}>
<span>{survey.name}</span>
</li>
))}
</ul>
</div>}
</div>
</div>
</ConfirmationModal>
)
}
}

ProviderModal.propTypes = {
t: PropTypes.func,
provider: PropTypes.any,
index: PropTypes.number,
friendlyName: PropTypes.string,
multiple: PropTypes.bool,
onConfirm: PropTypes.func,
}

export default translate()(ProviderModal)
11 changes: 11 additions & 0 deletions assets/vendor/css/materialize/components/_global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -790,3 +790,14 @@ td, th{
cursor: default !important;
}
}

.provider-surveys {
ul {
padding-left: 1rem;
list-style-type: disc;

li {
list-style-type: disc;
}
}
}
15 changes: 15 additions & 0 deletions lib/ask/survey.ex
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,21 @@ defmodule Ask.Survey do
%{survey | down_channels: down_channels}
end

def with_active_channels(provider, base_url) do
query =
from s in Survey,
where: s.state == :running,
join: group in RespondentGroup,
on: s.id == group.survey_id,
join: rgc in RespondentGroupChannel,
on: group.id == rgc.respondent_group_id,
join: c in Channel,
on: rgc.channel_id == c.id and c.provider == ^provider and c.base_url == ^base_url,
select: s

query |> Repo.all()
end

def stats(survey) do
respondents_by_disposition = survey |> RespondentStats.respondents_by_disposition()

Expand Down
6 changes: 6 additions & 0 deletions lib/ask_web/controllers/survey_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,12 @@ defmodule AskWeb.SurveyController do
end
end

def active(conn, params = %{"provider" => provider, "base_url" => base_url}) do
surveys = Survey.with_active_channels(provider, base_url)

render(conn, "index.json", surveys: surveys)
end

defp load_survey(project, survey_id) do
project
|> assoc(:surveys)
Expand Down
2 changes: 2 additions & 0 deletions lib/ask_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ defmodule AskWeb.Router do
get "/get_invite_by_email_and_project", InviteController, :get_by_email_and_project
get "/settings", UserController, :settings, as: :settings
post "/update_settings", UserController, :update_settings, as: :update_settings

get "/surveys/active_channel/:provider", SurveyController, :active, as: :active
end
end

Expand Down
2 changes: 2 additions & 0 deletions locales/template/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"Accepts refusals": "",
"Action": "",
"Active": "",
"Active surveys": "",
"Activity": "",
"Actual success rate": "",
"Actual success rate value throughout the survey's life": "",
Expand Down Expand Up @@ -288,6 +289,7 @@
"Named survey as <i>{{newSurveyName}}</i>": "",
"New questionnaire": "",
"Next step": "",
"No active surveys": "",
"No cutoff": "",
"No fallback": "",
"No folder": "",
Expand Down

0 comments on commit 6802740

Please sign in to comment.