diff --git a/assets/js/api.js b/assets/js/api.js index 36ea32214..232f6b1e6 100644 --- a/assets/js/api.js +++ b/assets/js/api.js @@ -685,8 +685,25 @@ export const refreshDispositionHistoryLink = (projectId, surveyId) => { export const triggerRespondentsResultFile = (projectId, surveyId, q) => apiPostJSON(`projects/${projectId}/surveys/${surveyId}/respondents/results?${ +const downloadGeneratedFile = (sourceUrl) => + apiFetch(sourceUrl, null, null).then((response) => { + response.json().then((data) => { + window.open(data.url) + }) + } + ) + +export const downloadRespondentsResultsFile = (projectId, surveyId, q) => + downloadGeneratedFile(`projects/${projectId}/surveys/${surveyId}/respondents/results_csv?${ (q && `&q=${encodeURIComponent(q)}`) || "" - }`, null, null) + }`) +export const downloadRespondentsDispositionHistoryFile = (projectId, surveyId) => + downloadGeneratedFile(`projects/${projectId}/surveys/${surveyId}/respondents/disposition_history`) +export const downloadRespondentsIncentivesFile = (projectId, surveyId) => + downloadGeneratedFile(`projects/${projectId}/surveys/${surveyId}/respondents/incentives`) +export const downloadRespondentsInteractionsFile = (projectId, surveyId) => + downloadGeneratedFile(`projects/${projectId}/surveys/${surveyId}/respondents/interactions`) + export const triggerRespondentsDispositionHistoryCSV = (projectId, surveyId) => apiPostJSON(`projects/${projectId}/surveys/${surveyId}/respondents/disposition_history`, null, null) export const triggerRespondentsIncentivesCSV = (projectId, surveyId) => diff --git a/assets/js/components/respondents/RespondentIndex.jsx b/assets/js/components/respondents/RespondentIndex.jsx index 6411d04df..c689e1bfb 100644 --- a/assets/js/components/respondents/RespondentIndex.jsx +++ b/assets/js/components/respondents/RespondentIndex.jsx @@ -121,25 +121,25 @@ class RespondentIndex extends Component { this.fetchRespondents(pageNumber - 1) } - downloadCSV(applyUserFilter = false) { + downloadResultsCSV(applyUserFilter = false) { const { projectId, surveyId, filter } = this.props const q = (applyUserFilter && filter) || null - api.triggerRespondentsResultFile(projectId, surveyId, q) + api.downloadRespondentsResultsFile(projectId, surveyId, q) } downloadDispositionHistoryCSV() { const { projectId, surveyId } = this.props - api.triggerRespondentsDispositionHistoryCSV(projectId, surveyId) + api.downloadRespondentsDispositionHistoryFile(projectId, surveyId) } downloadIncentivesCSV() { const { projectId, surveyId } = this.props - api.triggerRespondentsIncentivesCSV(projectId, surveyId) + api.downloadRespondentsIncentivesFile(projectId, surveyId) } downloadInteractionsCSV() { const { projectId, surveyId } = this.props - api.triggerRespondentsInteractionsCSV(projectId, surveyId) + api.downloadRespondentsInteractionsFile(projectId, surveyId) } sortBy(name) { @@ -343,7 +343,7 @@ class RespondentIndex extends Component { { totalCount, filter } ), downloadLink: null, - onDownload: () => this.downloadCSV(true), + onDownload: () => this.downloadResultsCSV(true), } break case "results": @@ -358,7 +358,7 @@ class RespondentIndex extends Component { this.refreshResultsLink, "resultsLink" ), - onDownload: () => this.downloadCSV(), + onDownload: () => this.downloadResultsCSV(), } break case "disposition-history": @@ -418,7 +418,7 @@ class RespondentIndex extends Component { const downloadButton = (
- + get_app @@ -481,7 +481,7 @@ class RespondentIndex extends Component { const ownerOrAdmin = userLevel == "owner" || userLevel == "admin" return ( - +
{t("Download CSV")}

{t("Choose the data you want to download")}

diff --git a/lib/ask_web/controllers/respondent_controller.ex b/lib/ask_web/controllers/respondent_controller.ex index 2a38f1551..7926cc177 100644 --- a/lib/ask_web/controllers/respondent_controller.ex +++ b/lib/ask_web/controllers/respondent_controller.ex @@ -729,6 +729,30 @@ defmodule AskWeb.RespondentController do conn end + # FIXME: this function should return the proper file URL/path + defp generated_file_url(%{id: survey_id}, {:results, _filter}), do: "/?some-thingy" + defp generated_file_url(%{id: survey_id}, :disposition_history), do: "#dispositon_history" + defp generated_file_url(%{id: survey_id}, :incentives), do: "#incentives-#{survey_id}" + defp generated_file_url(%{id: survey_id}, :interactions), do: "#interactions-#{survey_id}" + + defp file_redirection(conn, survey, file_type) do + file_url = generated_file_url(survey, file_type) + + render(conn, "file-redirect.json", + file_url: file_url + ) + end + + def results_csv(conn, %{"project_id" => project_id, "survey_id" => survey_id} = params) do + project = load_project(conn, project_id) + survey = load_survey(project, survey_id) + + filter = RespondentsFilter.parse(Map.get(params, "q", "")) + filter = add_params_to_filter(filter, params) + + file_redirection(conn, survey, {:results, filter}) + end + def trigger_results(conn, %{"project_id" => project_id, "survey_id" => survey_id} = params) do project = load_project(conn, project_id) survey = load_survey(project, survey_id) @@ -766,6 +790,15 @@ defmodule AskWeb.RespondentController do filter end + def generate_disposition_history(conn, %{"project_id" => project_id, "survey_id" => survey_id}) do + project = load_project(conn, project_id) + survey = load_survey(project, survey_id) + + SurveyResults.generate_disposition_history_file(survey.id) + + conn |> render("ok.json") + end + def disposition_history(conn, %{"project_id" => project_id, "survey_id" => survey_id}) do project = load_project(conn, project_id) survey = load_survey(project, survey_id) @@ -774,8 +807,7 @@ defmodule AskWeb.RespondentController do # and add another log when actually downloading? ActivityLog.download(project, conn, survey, "disposition_history") |> Repo.insert() - SurveyResults.generate_disposition_history_file(survey_id) - conn |> send_resp(200, "OK") + file_redirection(conn, survey, :disposition_history) end def incentives(conn, %{"project_id" => project_id, "survey_id" => survey_id}) do @@ -793,6 +825,24 @@ defmodule AskWeb.RespondentController do # and add another log when actually downloading? ActivityLog.download(project, conn, survey, "incentives") |> Repo.insert() + file_redirection(conn, survey, :incentives) + end + + def generate_incentives(conn, %{"project_id" => project_id, "survey_id" => survey_id}) do + project = + conn + |> load_project_for_owner(project_id) + + survey = + project + |> assoc(:surveys) + |> where([s], s.incentives_enabled) + |> Repo.get!(survey_id) + + # TODO: We just change this for "trigger generation" + # and add another log when actually downloading? + ActivityLog.download(project, conn, survey, "incentives") |> Repo.insert() + SurveyResults.generate_incentives_file(survey_id) conn |> send_resp(200, "OK") end @@ -805,6 +855,17 @@ defmodule AskWeb.RespondentController do # and add another log when actually downloading? ActivityLog.download(project, conn, survey, "interactions") |> Repo.insert() + file_redirection(conn, survey, :interactions) + end + + def generate_interactions(conn, %{"project_id" => project_id, "survey_id" => survey_id}) do + project = load_project_for_owner(conn, project_id) + survey = load_survey(project, survey_id) + + # TODO: We just change this for "trigger generation" + # and add another log when actually downloading? + ActivityLog.download(project, conn, survey, "interactions") |> Repo.insert() + SurveyResults.generate_interactions_file(survey_id) conn |> send_resp(200, "OK") end diff --git a/lib/ask_web/router.ex b/lib/ask_web/router.ex index 4e21cb474..5813b3530 100644 --- a/lib/ask_web/router.ex +++ b/lib/ask_web/router.ex @@ -189,16 +189,19 @@ defmodule AskWeb.Router do pipe_through :api get "/results", RespondentController, :results, as: :get_respondents_results - + get "/results_csv", RespondentController, :results_csv post "/results", RespondentController, :generate_results, as: :respondents_results + get "/disposition_history", RespondentController, :disposition_history, as: :respondents_disposition_history post "/disposition_history", RespondentController, :generate_disposition_history, - as: :respondents_disposition_history + as: :generate_disposition_history - post "/incentives", RespondentController, :generate_incentives, as: :respondents_incentives + get "/incentives", RespondentController, :incentives, as: :respondents_incentives + post "/incentives", RespondentController, :generate_incentives, as: :generate_respondents_incentives + get "/interactions", RespondentController, :interactions, as: :respondents_interactions post "/interactions", RespondentController, :generate_interactions, - as: :respondents_interactions + as: :generate_respondents_interactions end end end diff --git a/lib/ask_web/views/respondent_view.ex b/lib/ask_web/views/respondent_view.ex index 8bae12d80..1b1985e1f 100644 --- a/lib/ask_web/views/respondent_view.ex +++ b/lib/ask_web/views/respondent_view.ex @@ -119,6 +119,10 @@ defmodule AskWeb.RespondentView do %{data: %{}} end + def render("file-redirect.json", %{file_url: url}) do + %{url: url} + end + def render("respondent.json", %{ respondent: respondent, partial_relevant_enabled: partial_relevant_enabled @@ -269,6 +273,8 @@ defmodule AskWeb.RespondentView do percent: percentage } end + + def render("ok.json", _), do: %{status: :ok} defp render_index_meta(%{respondents_count: respondents_count, index_fields: index_fields}), do: %{