Skip to content

Commit

Permalink
fix(export): users list dynamic based on mindate (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristan-gueguen authored Oct 23, 2024
1 parent afd683f commit 6caf75d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
15 changes: 15 additions & 0 deletions common/utils/apiQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,21 @@ export const ADMIN_WORK_DAYS_QUERY = gql`
}
`;

export const ADMIN_USERS_SINCE_DATE = gql`
query adminCompanies($id: Int!, $activityAfter: Date, $companyIds: [Int]) {
user(id: $id) {
adminedCompanies(companyIds: $companyIds) {
id
users(fromDate: $activityAfter) {
id
firstName
lastName
}
}
}
}
`;

export const GET_EMPLOYMENT_QUERY = gql`
query getInvitation($token: String!) {
employment(token: $token) {
Expand Down
11 changes: 10 additions & 1 deletion web/admin/modals/C1BExportModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { TeamFilter } from "../components/TeamFilter";
import { EmployeeFilter } from "../components/EmployeeFilter";
import { CompanyFilter } from "../components/CompanyFilter";
import Modal, { modalStyles } from "../../common/Modal";
import { syncUsers } from "./ExcelExportModal";

export default function C1BExportModal({
open,
Expand All @@ -31,7 +32,8 @@ export default function C1BExportModal({
initialTeams = [],
defaultMinDate = null,
defaultMaxDate = null,
defaultCompany
defaultCompany,
getUsersSinceDate
}) {
const MAX_RANGE_DAYS = 60;
const today = new Date();
Expand All @@ -50,6 +52,13 @@ export default function C1BExportModal({
const [teams, setTeams] = React.useState(initialTeams);
const [employeeVersion, setEmployeeVersion] = React.useState(true);

React.useEffect(async () => {
if (minDate < defaultMinDate) {
const newUsers = await getUsersSinceDate(minDate);
syncUsers(setUsers, newUsers);
}
}, [minDate]);

const invalidDateRange = (minDate, maxDate) =>
maxDate &&
minDate &&
Expand Down
19 changes: 18 additions & 1 deletion web/admin/modals/ExcelExportModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ import Modal, { modalStyles } from "../../common/Modal";
import { TeamFilter } from "../components/TeamFilter";
import { EmployeeFilter } from "../components/EmployeeFilter";

export const syncUsers = (setUsers, newUsers) => {
setUsers(currentUsers => [
...currentUsers,
...newUsers.filter(
newUser =>
!currentUsers.map(currentUser => currentUser.id).includes(newUser.id)
)
]);
};
export default function ExcelExportModal({
open,
handleClose,
Expand All @@ -30,7 +39,8 @@ export default function ExcelExportModal({
initialUsers,
initialTeams,
defaultMinDate = null,
defaultMaxDate = null
defaultMaxDate = null,
getUsersSinceDate
}) {
const api = useApi();
const alerts = useSnackbarAlerts();
Expand All @@ -45,6 +55,13 @@ export default function ExcelExportModal({

const [isEnabledDownload, setIsEnabledDownload] = React.useState(true);

React.useEffect(async () => {
if (minDate < defaultMinDate) {
const newUsers = await getUsersSinceDate(minDate);
syncUsers(setUsers, newUsers);
}
}, [minDate]);

React.useEffect(() => setIsEnabledDownload(true), [
minDate,
maxDate,
Expand Down
8 changes: 6 additions & 2 deletions web/admin/panels/Activities.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { LogHolidayButton } from "../../common/LogHolidayButton";
import { LogHolidayForm } from "../../common/LogHolidayForm";
import { graphQLErrorMatchesCode } from "common/utils/errors";
import { usePageTitle } from "../../common/UsePageTitle";
import { useGetUsersSinceDate } from "../../common/hooks/useGetUsersSinceDate";

const useStyles = makeStyles(theme => ({
filterGrid: {
Expand Down Expand Up @@ -165,6 +166,7 @@ function ActivitiesPanel() {
const api = useApi();
const history = useHistory();
const { trackEvent } = useMatomo();
const { getUsersSinceDate } = useGetUsersSinceDate();

const [users, setUsers] = React.useState(adminStore.activitiesFilters.users);
const [teams, setTeams] = React.useState(adminStore.activitiesFilters.teams);
Expand Down Expand Up @@ -392,7 +394,8 @@ function ActivitiesPanel() {
defaultMinDate: minDate ? new Date(minDate) : null,
defaultMaxDate: maxDate
? new Date(maxDate)
: startOfDayAsDate(new Date())
: startOfDayAsDate(new Date()),
getUsersSinceDate
});
}}
>
Expand All @@ -413,7 +416,8 @@ function ActivitiesPanel() {
defaultMinDate: minDate ? new Date(minDate) : null,
defaultMaxDate: maxDate
? new Date(maxDate)
: startOfDayAsDate(new Date())
: startOfDayAsDate(new Date()),
getUsersSinceDate
});
}}
>
Expand Down
36 changes: 36 additions & 0 deletions web/common/hooks/useGetUsersSinceDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from "react";
import { ADMIN_USERS_SINCE_DATE } from "common/utils/apiQueries";
import { useApi } from "common/utils/api";
import { useAdminStore } from "../../admin/store/store";
import { isoFormatLocalDate } from "common/utils/time";
import { useSnackbarAlerts } from "../Snackbar";
import { formatApiError } from "common/utils/errors";

export const useGetUsersSinceDate = () => {
const api = useApi();
const alerts = useSnackbarAlerts();
const adminStore = useAdminStore();
const earliestDate = React.useRef(null);
const memo = React.useRef(null);

const getUsersSinceDate = async fromDate => {
if (!earliestDate.current || fromDate < earliestDate.current) {
try {
const payload = await api.graphQlQuery(ADMIN_USERS_SINCE_DATE, {
id: adminStore.userId,
activityAfter: isoFormatLocalDate(fromDate),
companyIds: [adminStore.companyId]
});
const users = payload?.data?.user?.adminedCompanies[0]?.users;
earliestDate.current = fromDate;
memo.current = users;
return users;
} catch (err) {
alerts.error(formatApiError(err), "get_users_since_date", 6000);
}
}
return memo.current;
};

return { getUsersSinceDate };
};

0 comments on commit 6caf75d

Please sign in to comment.