Skip to content

Commit c4d8ca1

Browse files
authored
Merge pull request #909 from dacadeorg/feat/add-loader-to-bounties-fetch
feat: add loader when fetching bounties
2 parents 66801e1 + a533625 commit c4d8ca1

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/components/list/Bounty.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import Bounty from "@/components/cards/Bounty";
33
import { Bounty as BountyType } from "@/types/bounty";
44
import { ReactElement } from "react";
55
import { Referral as ReferralType } from "@/types/community";
6+
import Loader from "../ui/Loader";
7+
import { IRootState } from "@/store";
8+
import { useSelector } from "@/hooks/useTypedSelector";
69

710
/**
811
* BountyList component props
@@ -28,9 +31,10 @@ interface BountyListProps {
2831
* @returns {ReactElement}
2932
*/
3033
export default function BountyList({ bounties = [], referrals = [] }: BountyListProps): ReactElement {
34+
const loading = useSelector((state: IRootState) => state.bounties.isLoading)
3135
return (
3236
<div>
33-
{bounties && (
37+
{bounties && !loading ? (
3438
<div className="relative w-full px-0 mb-10 space-y-0 overflow-hidden divide-y divide-gray-200 bg-gray-50 rounded-3xl lg:max-w-2xl">
3539
{referrals.map((referral) => (
3640
<Referral referral={referral} key={referral.name} />
@@ -39,7 +43,9 @@ export default function BountyList({ bounties = [], referrals = [] }: BountyList
3943
<Bounty bounty={bounty} key={bounty.id} />
4044
))}
4145
</div>
42-
)}
46+
) : <div className="h-24 sm:h-48 grid place-items-center">
47+
<Loader />
48+
</div>}
4349
</div>
4450
);
4551
}

src/store/feature/bouties.slice.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import { IRootState } from "..";
55
interface InitialState {
66
bountiesList: Bounty[];
77
filteredBountyList: Bounty[];
8+
isLoading: boolean;
89
}
910

1011
const initialState: InitialState = {
1112
bountiesList: [],
1213
filteredBountyList: [],
14+
isLoading: false
1315
};
1416

1517
/**
@@ -28,10 +30,13 @@ const bountiesSlice = createSlice({
2830
setFilteredBountiesList: (state, action: PayloadAction<Bounty[]>) => {
2931
state.filteredBountyList = action.payload;
3032
},
33+
setLoading: (state, action) => {
34+
state.isLoading = action.payload;
35+
}
3136
},
3237
});
3338

34-
export const { setBountiesList, setFilteredBountiesList } = bountiesSlice.actions;
39+
export const { setBountiesList, setFilteredBountiesList, setLoading } = bountiesSlice.actions;
3540

3641
/**
3742
* Find bouties by slug

src/store/services/bounties.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import baseQuery from "@/config/baseQuery";
22
import { createApi } from "@reduxjs/toolkit/dist/query";
3-
import { setBountiesList } from "../feature/bouties.slice";
3+
import { setBountiesList, setLoading } from "../feature/bouties.slice";
44
import { HYDRATE } from "next-redux-wrapper";
55
import { setBusy, setError } from "../feature/index.slice";
66

@@ -23,9 +23,12 @@ const bountiesService = createApi({
2323
getBounties: builder.query({
2424
query: () => "bounties",
2525
onQueryStarted: async (slug, { dispatch, queryFulfilled }) => {
26+
dispatch(setLoading(true))
2627
try {
2728
const { data } = await queryFulfilled;
28-
dispatch(setBountiesList(data));
29+
if (data) dispatch(setBountiesList(data));
30+
dispatch(setLoading(false))
31+
return data
2932
} catch (error) {
3033
dispatch(setBusy(false));
3134
dispatch(setError(error));

0 commit comments

Comments
 (0)