Skip to content

Commit

Permalink
Merge pull request #118
Browse files Browse the repository at this point in the history
ELVIS-1166--Users-bulk-delete
  • Loading branch information
Dim145 authored Nov 21, 2024
2 parents be493c3 + df0d958 commit ee8e7d5
Showing 1 changed file with 92 additions and 34 deletions.
126 changes: 92 additions & 34 deletions frontend/components/UserList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import _ from "lodash";
const moment = require("moment");
require("moment/locale/fr");

import { csrfToken } from "./utils";
import { makeDebounce } from "../tools/inputs";
import {csrfToken} from "./utils";
import {makeDebounce} from "../tools/inputs";
import ReactTableFullScreen from "./ReactTableFullScreen";
import * as api from "../tools/api";
import swal from "sweetalert2";
Expand Down Expand Up @@ -72,7 +72,7 @@ class UserList extends React.Component {
}

fetchData(state, instance) {
this.setState({ loading: true, filter: state });
this.setState({loading: true, filter: state});

debounce(() => {
requestData(
Expand Down Expand Up @@ -156,20 +156,16 @@ class UserList extends React.Component {
});
}

sendConfirmationMail()
{
sendConfirmationMail() {
api.set()
.success((datas) => {
if(!datas || datas.length === 0)
{
if (!datas || datas.length === 0) {
swal({
title: `Tous les utilisateurs ${this.state.selected.length > 0 ? "sélectionnés" : ""} ont déjà confirmé leur compte`,
type: "warning",
confirmButtonText: "Ok"
});
}
else
{
} else {
swal({
title: `Les utilisateurs suivants ont reçu un mail de confirmation :`,
html: "<ul>" + datas.map(d => `<li>${d}</li>`).join("") + "</ul>",
Expand All @@ -188,8 +184,60 @@ class UserList extends React.Component {
.post('/users/resend_confirmation', {ids: this.state.selected.length > 0 ? this.state.selected : this.state.data.map(d => d.id)});
}

handleDeleteUser = () => {
const selectedUserIds = this.state.selected;
let successCount = 0;
let errorCount = 0;
const isSingleUser = selectedUserIds.length === 1;

swal({
title: `Supprimer ${isSingleUser ? "l'utilisateur sélectionné" : "les utilisateurs sélectionnés"}`,
html: `<h4>Cela supprimera ${isSingleUser ? "l'utilisateur, ses liens familiaux ainsi que son adhésion" : "les utilisateurs, leurs liens familiaux ainsi que leurs adhésions"}. Êtes-vous sûr ?</h4></br>
<p>Les utilisateurs associés seront détachés, et un email leur sera envoyé pour créer un mot de passe (si leur email est différent de celui de ce compte).</p>`,
type: "warning",
showCancelButton: true,
confirmButtonText: "Oui, supprimer",
cancelButtonText: "Annuler"
}).then((result) => {
if (result.value) {
Promise.all(
selectedUserIds.map((id) =>
api.set()
.success(() => { successCount += 1; })
.error(() => { errorCount += 1; })
.del(`/destroy/User/${id}`)
)
).then(() => {
const successMessage = successCount === 1 ?
"1 utilisateur supprimé avec succès." :
`${successCount} utilisateurs supprimés avec succès.`;
const errorMessage = errorCount > 0 ?
`<p>${errorCount} erreur(s) rencontrée(s).</p>` :
"";

swal({
title: "Suppression terminée",
html: `<p>${successMessage}</p>${errorMessage}`,
type: successCount > 0 ? "success" : "error",
confirmButtonText: "Ok"
});
this.fetchData(this.state.filter);
this.setState({ selected: [] });
}).catch(() => {
swal({
title: "Erreur",
text: "Une erreur est survenue lors de la suppression de masse.",
type: "error",
confirmButtonText: "Ok"
});
});
}
});
};


render() {
const { data, pages, loading } = this.state;
const {data, pages, loading} = this.state;

const columns = [
{
Expand Down Expand Up @@ -289,10 +337,10 @@ class UserList extends React.Component {
},
sortable: false,
filterable: !this.props.nofilter,
Filter: ({ filter, onChange }) => (
Filter: ({filter, onChange}) => (
<select
onChange={event => onChange(event.target.value)}
style={{ width: "100%" }}
style={{width: "100%"}}
value={filter ? filter.value : "all"}
>
<option value="all">Tous les utilisateurs</option>
Expand All @@ -309,10 +357,10 @@ class UserList extends React.Component {
Header: "Type de compte",
sortable: false,
filterable: true,
Filter: ({ filter, onChange }) => (
Filter: ({filter, onChange}) => (
<select
onChange={event => onChange(event.target.value)}
style={{ width: "100%" }}
style={{width: "100%"}}
value={filter ? filter.value : "all"}
>
<option value="">Tous les comptes</option>
Expand Down Expand Up @@ -364,7 +412,7 @@ class UserList extends React.Component {
);
}

return <p />;
return <p/>;
},
filterable: false,
},
Expand All @@ -386,7 +434,7 @@ class UserList extends React.Component {
href={`/planning/${props.original.planning.id}`}
className="btn btn-xs btn-primary m-b-sm"
>
<i className="fas fa-calendar" />
<i className="fas fa-calendar"/>
&nbsp; Planning
</a>
) : null}
Expand All @@ -401,16 +449,16 @@ class UserList extends React.Component {
{
anyActive(props.original.adhesions)
|| props.original['any_users_self_is_paying_for?'] ? (
<a
href={`/payments/summary/${props.original.id}`}
className="btn btn-xs btn-primary m-r-sm m-b-sm"
>
<i className="fas fa-euro-sign" />
&nbsp; Paiements
</a>
) : (
""
)}
<a
href={`/payments/summary/${props.original.id}`}
className="btn btn-xs btn-primary m-r-sm m-b-sm"
>
<i className="fas fa-euro-sign"/>
&nbsp; Paiements
</a>
) : (
""
)}
</div>
</div>
);
Expand All @@ -429,25 +477,35 @@ class UserList extends React.Component {
className="btn btn-primary m-r"
onClick={() => this.onCsvExport()}
>
<i className="fas fa-upload m-r-sm" />
<i className="fas fa-upload m-r-sm"/>
Exporter en CSV
</button>

<a className="btn btn-primary m-r" href="/scripts/merge_users">
Fusionner des doublons
</a>

<button className="btn btn-primary m-r" href="/users/new" onClick={() => this.setState({showAttachAccountModal: true})}>
<button className="btn btn-primary m-r" href="/users/new"
onClick={() => this.setState({showAttachAccountModal: true})}>
<i className="fas fa-bezier-curve"></i>&nbsp;
Rattacher des utilisateurs
</button>


<button
data-tippy-content="Envoyer le mail de confirmation"
className="btn btn-warning" onClick={this.sendConfirmationMail}>
<i className="fas fa-envelope" />
className="btn btn-warning m-r" onClick={this.sendConfirmationMail}>
<i className="fas fa-envelope"/>
</button>

{this.state.selected.length > 0 ? (
<button
data-tippy-content="Supprimer l'utilisateur sélectionné"
className={"btn btn-danger m-r"} onClick={this.handleDeleteUser}>
<i className="fas fa-trash"/>
</button>
) : null}

</div>

<ReactTableFullScreen
Expand All @@ -459,9 +517,9 @@ class UserList extends React.Component {
loading={loading}
onFetchData={this.fetchData}
columns={columns}
defaultSorted={[{ id: "adherent_number", desc: true }]}
defaultSorted={[{id: "adherent_number", desc: true}]}
filterable
defaultFiltered={[{ id: "role", value: this.props.filter }]}
defaultFiltered={[{id: "role", value: this.props.filter}]}
defaultFilterMethod={(filter, row) => {
if (row[filter.id] != null) {
return row[filter.id]
Expand Down Expand Up @@ -492,7 +550,7 @@ class UserList extends React.Component {
<AttachAccount onSucess={() => {
this.setState({showAttachAccountModal: false});
this.fetchData(this.state.filter)
}} />
}}/>
</Modal>
</div>
);
Expand Down

0 comments on commit ee8e7d5

Please sign in to comment.