-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
186 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
src/pages/ExperiencePoints/ExperiencePointsTable/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
Pagination, | ||
SortTableSelect, | ||
Table, | ||
} from "@appquality/appquality-design-system"; | ||
import { useTranslation } from "react-i18next"; | ||
import { shallowEqual, useSelector } from "react-redux"; | ||
import { updateExperiencePointsPagination } from "src/redux/experiencePoints/actionCreator"; | ||
import { useAppDispatch } from "src/store"; | ||
import { useExperiencePointsColumns } from "../columns"; | ||
import { useResetPaginationOnFilterChange } from "./useResetPaginationOnFilterChange"; | ||
import { useRows } from "./useRows"; | ||
|
||
const ExperiencePointsTable = () => { | ||
const limit = 25; | ||
const { t } = useTranslation(); | ||
const dispatch = useAppDispatch(); | ||
const { rows, loading, totalEntries } = useRows(); | ||
const columns = useExperiencePointsColumns(); | ||
|
||
const setPage = (newPage: number) => { | ||
const newStart = limit * (newPage - 1); | ||
dispatch(updateExperiencePointsPagination(newStart)); | ||
}; | ||
|
||
useResetPaginationOnFilterChange({ setPage }); | ||
|
||
const { order, orderBy, start } = useSelector( | ||
(state: GeneralState) => state.experiencePoints.expList, | ||
shallowEqual | ||
); | ||
|
||
const page = (start ?? 0) / limit + 1; | ||
|
||
return ( | ||
<> | ||
<SortTableSelect | ||
order={order} | ||
orderBy={orderBy} | ||
columns={columns} | ||
label={t("Order By", { context: "Sort Table Select" })} | ||
/> | ||
<Table | ||
dataSource={rows} | ||
columns={columns} | ||
orderBy={orderBy} | ||
order={order} | ||
isLoading={loading} | ||
className="aq-mb-3" | ||
isStriped | ||
/> | ||
<Pagination | ||
onPageChange={setPage} | ||
current={page} | ||
maxPages={Math.ceil(totalEntries / limit)} | ||
mobileText={(current, total) => | ||
t(`Page %current% / %total%`) | ||
.replace("%current%", current.toString()) | ||
.replace("%total%", total ? total.toString() : "0") | ||
} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default ExperiencePointsTable; |
25 changes: 25 additions & 0 deletions
25
src/pages/ExperiencePoints/ExperiencePointsTable/useResetPaginationOnFilterChange.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useEffect } from "react"; | ||
import { shallowEqual, useSelector } from "react-redux"; | ||
|
||
const useResetPaginationOnFilterChange = ({ | ||
setPage, | ||
}: { | ||
setPage: (newPage: number) => void; | ||
}) => { | ||
const { selectedActivity, selectedCampaign, selectedDate, search } = | ||
useSelector((state: GeneralState) => state.experiencePoints, shallowEqual); | ||
|
||
useEffect(() => { | ||
if ( | ||
selectedCampaign || | ||
selectedActivity || | ||
selectedDate || | ||
search || | ||
search === "" | ||
) { | ||
setPage(1); | ||
} | ||
}, [selectedCampaign, selectedActivity, selectedDate, search]); | ||
}; | ||
|
||
export { useResetPaginationOnFilterChange }; |
77 changes: 77 additions & 0 deletions
77
src/pages/ExperiencePoints/ExperiencePointsTable/useRows.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { useTranslation } from "react-i18next"; | ||
import { shallowEqual, useSelector } from "react-redux"; | ||
import { mapActivityName } from "src/redux/experiencePoints/utils"; | ||
import { useGetUsersMeExperienceQuery } from "src/services/tryberApi"; | ||
import dateFormatter from "src/utils/dateFormatter"; | ||
|
||
const useRows = () => { | ||
const limit = 25; | ||
const { t } = useTranslation(); | ||
|
||
const { order, orderBy, start } = useSelector( | ||
(state: GeneralState) => state.experiencePoints.expList, | ||
shallowEqual | ||
); | ||
const { selectedActivity, selectedCampaign, selectedDate, search } = | ||
useSelector((state: GeneralState) => state.experiencePoints, shallowEqual); | ||
|
||
const { | ||
data, | ||
error, | ||
isLoading: loading, | ||
} = useGetUsersMeExperienceQuery({ | ||
limit: limit, | ||
start: start, | ||
order: order, | ||
orderBy: orderBy, | ||
search: search, | ||
searchBy: "note", | ||
filterBy: { | ||
campaign: selectedCampaign?.value, | ||
activity: selectedActivity?.value, | ||
date: selectedDate?.value, | ||
}, | ||
}); | ||
|
||
if (error) { | ||
return { | ||
rows: [], | ||
loading: false, | ||
totalEntries: 0, | ||
}; | ||
} | ||
|
||
const { results, total } = data ?? {}; | ||
|
||
const rows = (results ?? []).map((res) => { | ||
return { | ||
key: res.id, | ||
amount: { | ||
title: `${res.amount > 0 ? `+${res.amount}` : res.amount}pts`, | ||
content: | ||
res.amount === 0 ? ( | ||
<b className="aq-text-primary">{res.amount}pts</b> | ||
) : res.amount > 0 ? ( | ||
<b className="aq-text-success">+{res.amount}pts</b> | ||
) : ( | ||
<b className="aq-text-danger">{res.amount}pts</b> | ||
), | ||
}, | ||
date: dateFormatter(res.date), | ||
activity: mapActivityName(res.activity.id, t), | ||
campaign: | ||
res.campaign.title && res.campaign.id > 0 | ||
? `CP${res.campaign.id}` | ||
: `-`, | ||
note: res.note?.replace(/\\(.)/gm, "$1"), | ||
}; | ||
}); | ||
|
||
return { | ||
rows, | ||
loading, | ||
totalEntries: total ?? 0, | ||
}; | ||
}; | ||
|
||
export { useRows }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.