-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectedCoinSlice.ts
78 lines (67 loc) · 1.88 KB
/
selectedCoinSlice.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"use client";
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
type GetCoinDataArgs = {
currency: string;
days: string;
coinId: string;
};
type CoinData = {
id: string;
prices: [number, number][];
total_volumes: [number, number][];
};
type selectedCoinState = {
selectedCoins: CoinData[];
loading: string;
hasError: boolean;
};
const initialState: selectedCoinState = {
selectedCoins: [],
loading: "idle",
hasError: false,
};
export const getCoinDataGraph = createAsyncThunk(
"coinData/getCoinData",
async ({ currency, days, coinId }: GetCoinDataArgs, { rejectWithValue }) => {
try {
const response = await fetch(
`https://api.coingecko.com/api/v3/coins/${coinId}/market_chart?vs_currency=${currency}&days=${days}?x_cg_demo_api_key=${process.env.NEXT_PUBLIC_API_KEY}`
);
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
const { prices, total_volumes } = data;
const item: CoinData = {
id: coinId,
prices,
total_volumes,
};
return item;
} catch (error: any) {
return rejectWithValue(error.message);
}
}
);
const selectedCoinsSlice = createSlice({
name: "selectedCoin",
initialState,
reducers: {},
extraReducers: (builder) => {
builder
.addCase(getCoinDataGraph.pending, (state) => {
state.loading = "pending";
state.hasError = false;
})
.addCase(getCoinDataGraph.fulfilled, (state, action) => {
state.selectedCoins = [action.payload];
state.loading = "fulfilled";
})
.addCase(getCoinDataGraph.rejected, (state, action) => {
state.loading = "rejected";
state.hasError = true;
console.error("API call failed with error:", action.payload);
});
},
});
export default selectedCoinsSlice.reducer;