Skip to content

Commit

Permalink
Fix loading active surveys was triggered on component creation and no…
Browse files Browse the repository at this point in the history
…t before modal display
  • Loading branch information
ismaelbej committed Nov 19, 2024
1 parent 086f315 commit 1f38a00
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 81 deletions.
58 changes: 57 additions & 1 deletion assets/js/components/channels/ChannelIndex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,28 @@ import { Preloader } from "react-materialize"
import { config } from "../../config"
import { translate } from "react-i18next"
import ProviderModal from "./ProviderModal"
import * as api from "../../api"

type State = {
modalLoading: boolean,
modalSurveys: Array<Object>,
modalProvider: ?string,
modalIndex: ?number,
}

class ChannelIndex extends Component<any, State> {
state : State

constructor(props) {
super(props)
this.state = {
modalLoading: false,
modalSurveys: [],
modalProvider: null,
modalIndex: null,
}
}

class ChannelIndex extends Component<any> {
componentDidMount() {
this.props.actions.fetchChannels()
}
Expand All @@ -37,6 +57,31 @@ class ChannelIndex extends Component<any> {
toggleProvider(provider, index, checked) {
if (checked) {
$(`#${provider}Modal-${index}`).modal("open")
this.setState({
modalLoading: true,
modalSurveys: [],
modalProvider: provider,
modalIndex: index,
})
const { baseUrl } = config[provider][index]
api.fetchActiveSurveys(provider, baseUrl)
.then((response) => {
const surveys = response || []
this.setState({
modalLoading: false,
modalSurveys: surveys,
modalProvider: provider,
modalIndex: index,
})
})
.catch(() => {
this.setState({
modalLoading: false,
modalSurveys: [],
modalProvider: provider,
modalIndex: index,
})
})
} else {
this.props.authActions.toggleAuthorization(provider, index)
}
Expand Down Expand Up @@ -88,6 +133,13 @@ class ChannelIndex extends Component<any> {
router,
} = this.props

const {
modalLoading,
modalSurveys,
modalProvider,
modalIndex,
} = this.state;

if (!channels) {
return (
<div>
Expand Down Expand Up @@ -124,6 +176,8 @@ class ChannelIndex extends Component<any> {
}

const providerModal = (provider, index, friendlyName, multiple) => {
const loading = provider === modalProvider && index === modalIndex ? modalLoading : false
const surveys = provider === modalProvider && index === modalIndex ? modalSurveys : []
return (
<ProviderModal
key={`${provider}-${index}`}
Expand All @@ -132,6 +186,8 @@ class ChannelIndex extends Component<any> {
friendlyName={friendlyName}
multiple={multiple}
onConfirm={() => this.deleteProvider(provider, index)}
loading={loading}
surveys={surveys}
/>
)
}
Expand Down
125 changes: 45 additions & 80 deletions assets/js/components/channels/ProviderModal.jsx
Original file line number Diff line number Diff line change
@@ -1,95 +1,60 @@
import React, { Component, PropTypes } from "react"
import React, { 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>
export const ProviderModal = ({
t,
provider,
index,
friendlyName,
multiple,
onConfirm,
loading,
surveys,
}) => {
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("Searching active 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>
</ConfirmationModal>
)
}
</div>
</ConfirmationModal>
)
}

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

export default translate()(ProviderModal)
1 change: 1 addition & 0 deletions locales/template/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@
"Sat": "",
"Saving...": "",
"Scheduled for {{dateString}}": "",
"Searching active surveys...": "",
"Secondary color": "",
"Section <i>{{oldSectionTitle}}</i> of <i>{{questionnaireName}}</i> renamed to <i>{{newSectionTitle}}</i>": "",
"Select a channel...": "",
Expand Down

0 comments on commit 1f38a00

Please sign in to comment.