Skip to content

Commit

Permalink
Make bdb paginated
Browse files Browse the repository at this point in the history
  • Loading branch information
Bestem0r committed Nov 13, 2024
1 parent 4c1ddb8 commit df24a0f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
10 changes: 8 additions & 2 deletions app/actions/CompanyActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,21 @@ export const fetchAll = ({ fetchMore }: { fetchMore: boolean }) => {
});
};

export function fetchAllAdmin(semesterId: EntityId) {
export function fetchAllAdmin(semesterId: EntityId, fetchMore: boolean) {
return callAPI<AdminListCompany[]>({
types: Company.FETCH,
endpoint: `/bdb/?semester_id=${semesterId}`,
endpoint: `/bdb/`,
schema: [companySchema],
meta: {
errorMessage: 'Henting av bedrifter feilet',
},
propagateError: true,
query: {
semester_id: semesterId,
},
pagination: {
fetchNext: fetchMore,
},
});
}

Expand Down
6 changes: 4 additions & 2 deletions app/reducers/companies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ const companiesSlice = createSlice({

export default companiesSlice.reducer;

export const { selectAll: selectAllCompanies, selectById: selectCompanyById } =
legoAdapter.getSelectors((state: RootState) => state.companies);
export const {
selectAllPaginated: selectAllCompanies,
selectById: selectCompanyById,
} = legoAdapter.getSelectors((state: RootState) => state.companies);

export type TransformedSemesterStatus = Overwrite<
SemesterStatus,
Expand Down
30 changes: 25 additions & 5 deletions app/routes/bdb/components/BdbPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { SelectInput } from 'app/components/Form';
import Table from 'app/components/Table';
import { selectTransformedAdminCompanies } from 'app/reducers/companies';
import { selectAllCompanySemesters } from 'app/reducers/companySemesters';
import { selectPaginationNext } from 'app/reducers/selectors';
import { useAppDispatch, useAppSelector } from 'app/store/hooks';
import { EntityType } from 'app/store/models/entities';
import { guardLogin } from 'app/utils/replaceUnlessLoggedIn';
import useQuery from 'app/utils/useQuery';
import {
Expand Down Expand Up @@ -36,9 +38,7 @@ const companiesDefaultQuery = {
const BdbPage = () => {
const { query, setQuery } = useQuery(companiesDefaultQuery);

const companies = useAppSelector(selectTransformedAdminCompanies);
const companySemesters = useAppSelector(selectAllCompanySemesters);
const fetching = useAppSelector((state) => state.companies.fetching);

const resolveCurrentSemester = (
slug: string | undefined,
Expand All @@ -57,6 +57,23 @@ const BdbPage = () => {
[companySemesters, query.semester],
);

console.log(currentCompanySemester);
const { pagination } = useAppSelector(
selectPaginationNext({
endpoint: '/bdb/',
entity: EntityType.Companies,
query: {
semester_id: currentCompanySemester?.id,
},
}),
);

const companies = useAppSelector((state) =>
selectTransformedAdminCompanies(state, { pagination }),
);

const fetching = useAppSelector((state) => state.companies.fetching);

const dispatch = useAppDispatch();
usePreparedEffect(
'fetchBdb',
Expand All @@ -72,9 +89,9 @@ const BdbPage = () => {
query.semester,
companySemesters,
);
return dispatch(fetchAllAdmin(semester!.id));
return dispatch(fetchAllAdmin(semester!.id, false));
}
return dispatch(fetchAllAdmin(currentCompanySemester!.id));
return dispatch(fetchAllAdmin(currentCompanySemester!.id, false));
},
[query.semester, currentCompanySemester, companySemesters],
);
Expand Down Expand Up @@ -193,7 +210,10 @@ const BdbPage = () => {
filters={query}
onChange={setQuery}
loading={fetching}
hasMore={false}
onLoad={() => {
dispatch(fetchAllAdmin(currentCompanySemester?.id, true));
}}
hasMore={pagination.hasMore}
/>
</Page>
);
Expand Down

0 comments on commit df24a0f

Please sign in to comment.