Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored membership #29

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 107 additions & 103 deletions admin/src/Membership/MemberBoxSpans.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useEffect, useState } from "react";
import "react-day-picker/lib/style.css";
import { Link } from "react-router-dom";
import CollectionTable from "../Components/CollectionTable";
Expand All @@ -11,113 +11,117 @@ import { get } from "../gateway";
import { confirmModal } from "../message";
import MembershipPeriodsInput from "./MembershipPeriodsInput";

class MemberBoxSpans extends React.Component {
constructor(props) {
super(props);
this.collection = new Collection({
type: Span,
url: `/membership/member/${props.match.params.member_id}/spans`,
pageSize: 0,
includeDeleted: true,
});
this.state = { items: [], pending_labaccess_days: "?" };
this.pending_actions = get({
url: `/membership/member/${props.match.params.member_id}/pending_actions`,
}).then((r) => {
const sum_pending_labaccess_days = r.data.reduce((acc, value) => {
if (value.action.action === ADD_LABACCESS_DAYS)
return acc + value.action.value;
return acc;
}, 0);
this.setState({
pending_labaccess_days: sum_pending_labaccess_days,
const MemberBoxSpans = (props) => {
const collection = new Collection({
type: Span,
url: `/membership/member/${props.match.params.member_id}/spans`,
pageSize: 0,
includeDeleted: true,
});

const [pendingLabAccessDays, setPendingLabAccessDays] = useState("?");

useEffect(() => {
// Fetch pending actions
const fetchPendingActions = async () => {
const response = await get({
url: `/membership/member/${props.match.params.member_id}/pending_actions`,
});
});
}
const sumPendingLabAccessDays = response.data.reduce(
(acc, value) => {
if (value.action.action === ADD_LABACCESS_DAYS)
return acc + value.action.value;
return acc;
},
0,
);
setPendingLabAccessDays(sumPendingLabAccessDays);
};

fetchPendingActions();

componentDidMount() {
this.unsubscribe = this.collection.subscribe(({ items }) => {
this.setState({ items });
// Subscribe to collection
const unsubscribe = collection.subscribe(() => {
// No need to handle the subscription data since it's unused
});
}

componentWillUnmount() {
this.unsubscribe();
}
// Cleanup on component unmount
return () => {
unsubscribe();
};
}, [props.match.params.member_id]);

render() {
const deleteItem = (item) =>
confirmModal(item.deleteConfirmMessage())
.then(() => item.del())
.then(
() => this.collection.fetch(),
() => null,
);
const deleteItem = (item) =>
confirmModal(item.deleteConfirmMessage())
.then(() => item.del())
.then(
() => collection.fetch(),
() => null,
);

return (
<div className="uk-margin-top">
<h2>Medlemsperioder</h2>
<p>
<b>{this.state.pending_labaccess_days}</b> dagar labaccess
kommer läggas till vid en nyckelsynkronisering.
</p>
<hr />
<MembershipPeriodsInput
spans={this.collection}
member_id={this.props.match.params.member_id}
/>
<h2>Spans</h2>
<hr />
<CollectionTable
collection={this.collection}
columns={[
{ title: "#", sort: "span_id" },
{ title: "Typ", sort: "type" },
{ title: "Skapad", sort: "created_at" },
{ title: "" },
{ title: "Raderad", sort: "deleted_at" },
{ title: "Start", sort: "startdate" },
{ title: "Slut", sort: "enddate" },
]}
rowComponent={({ item }) => (
<tr>
<td>
<Link to={"/membership/spans/" + item.id}>
{item.id}
</Link>
</td>
<td>
<Link to={"/membership/spans/" + item.id}>
{item.type}
</Link>
</td>
<td>
<DateTimeShow date={item.created_at} />
</td>
<td>{item.creation_reason}</td>
<td>
<DateTimeShow date={item.deleted_at} />
</td>
<td>
<DateShow date={item.startdate} />
</td>
<td>
<DateShow date={item.enddate} />
</td>
<td>
<a
onClick={() => deleteItem(item)}
className="removebutton"
>
<i className="uk-icon-trash" />
</a>
</td>
</tr>
)}
/>
</div>
);
}
}
return (
<div className="uk-margin-top">
<h2>Medlemsperioder</h2>
<p>
<b>{pendingLabAccessDays}</b> dagar labaccess kommer läggas till
vid en nyckelsynkronisering.
</p>
<hr />
<MembershipPeriodsInput
spans={collection}
member_id={props.match.params.member_id}
/>
<h2>Spans</h2>
<hr />
<CollectionTable
collection={collection}
columns={[
{ title: "#", sort: "span_id" },
{ title: "Typ", sort: "type" },
{ title: "Skapad", sort: "created_at" },
{ title: "" },
{ title: "Raderad", sort: "deleted_at" },
{ title: "Start", sort: "startdate" },
{ title: "Slut", sort: "enddate" },
]}
rowComponent={({ item }) => (
<tr>
<td>
<Link to={"/membership/spans/" + item.id}>
{item.id}
</Link>
</td>
<td>
<Link to={"/membership/spans/" + item.id}>
{item.type}
</Link>
</td>
<td>
<DateTimeShow date={item.created_at} />
</td>
<td>{item.creation_reason}</td>
<td>
<DateTimeShow date={item.deleted_at} />
</td>
<td>
<DateShow date={item.startdate} />
</td>
<td>
<DateShow date={item.enddate} />
</td>
<td>
<a
onClick={() => deleteItem(item)}
className="removebutton"
>
<i className="uk-icon-trash" />
</a>
</td>
</tr>
)}
/>
</div>
);
};

export default MemberBoxSpans;
74 changes: 33 additions & 41 deletions admin/src/Membership/MemberExport.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import React from "react";
import React, { useState } from "react";
import { get } from "../gateway";

class MemberExport extends React.Component {
constructor(props) {
super(props);
this.state = {
csv_content: null,
state: "none",
};
}
function MemberExport() {
const [csvContent, setCsvContent] = useState(null);
const [state, setState] = useState("none");

exportMembers() {
this.setState({ state: "loading" });
// Function to export members
const exportMembers = () => {
setState("loading");

get({ url: "/membership/member/all_with_membership" }).then((data) => {
const members = data.data;
Expand Down Expand Up @@ -46,38 +42,34 @@ class MemberExport extends React.Component {
]);
}

this.setState({
state: "loaded",
csv_content: rows.map((r) => r.join(",")).join("\n"),
});
setState("loaded");
setCsvContent(rows.map((r) => r.join(",")).join("\n"));
});
}
};

render() {
return (
<div>
<h2>Exportera medlemslista</h2>
{this.state.csv_content && (
<textarea
readOnly
className="uk-width-1-1"
value={this.state.csv_content}
rows={50}
></textarea>
)}
{!this.state.csv_content && (
<a
className="uk-button uk-button-primary"
role="button"
onClick={() => this.exportMembers()}
>
Exportera alla aktiva medlemmar som CSV
{this.state.state === "loading" ? "..." : ""}
</a>
)}
</div>
);
}
return (
<div>
<h2>Exportera medlemslista</h2>
{csvContent && (
<textarea
readOnly
className="uk-width-1-1"
value={csvContent}
rows={50}
></textarea>
)}
{!csvContent && (
<a
className="uk-button uk-button-primary"
role="button"
onClick={exportMembers}
>
Exportera alla aktiva medlemmar som CSV
{state === "loading" ? "..." : ""}
</a>
)}
</div>
);
}

export default MemberExport;
Loading