Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ npm install
Create a `.env.local` file in the root directory and add your API endpoint URL:

```sh
VITE_API_URL=http://localhost:3000/api/v1
VITE_API_URL=http://localhost:3000/api/v1/
```

### 💻 Usage
Expand Down
12 changes: 12 additions & 0 deletions src/app/common.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ export interface Snippet {
updated_at: string;
user_id: number;
}

export interface Contributor {
id: number;
email: string;
name: string;
bio: string;
avatar_url: string;
admin: boolean;
code_snippets_counter: number;
created_at: string;
updated_at: string;
}
2 changes: 2 additions & 0 deletions src/app/store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { configureStore } from '@reduxjs/toolkit';
import snippetReducer from '../features/snippets/snippetsSlice';
import contributorReducer from '../features/contributors/contributorsSlice'

const store = configureStore({
reducer: {
snippets: snippetReducer,
contributors: contributorReducer
},
});

Expand Down
3 changes: 2 additions & 1 deletion src/common/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const API_ENDPOINT = import.meta.env.VITE_API_URL;
// 'http://localhost:3000/api/v1/'

const getAllSnippets = () => axios.get(API_ENDPOINT + 'code_snippets');
const getAllContributors = () => axios.get(API_ENDPOINT + 'contributors');

const ApiService = { getAllSnippets };
const ApiService = { getAllSnippets, getAllContributors };

export default ApiService;
44 changes: 44 additions & 0 deletions src/features/contributors/contributorsSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import ApiService from "../../common/services";
import { Status } from "../../common/constants";
import { Contributor } from "../../app/common.types";

interface ContributorState {
status: Status;
data: Contributor[];
error: string;
}

const initialState: ContributorState = {
status: Status.Idle,
data: [],
error: ''
}

const contributorSlice = createSlice({
name: 'contributor',
initialState,
reducers: {
setContributors(state, action){
state.data = action.payload;
}
}
})

export const fetchContributors = createAsyncThunk('contributors/get', async () => {
try {
const response = await ApiService.getAllContributors();
const data = response.data;
console.log("Fetched contributors data:", data);
return data;
} catch (error) {
console.error("Error fetching contributors:", error);
throw error;
}
});




export const {setContributors} = contributorSlice.actions;
export default contributorSlice.reducer;
18 changes: 16 additions & 2 deletions src/pages/Contributors.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import NotFound from './NotFound';
import { useEffect } from "react";
import { useAppDispatch, useAppSelector } from "../app/hooks";
import { fetchContributors } from "../features/contributors/contributorsSlice";

const Contributors = () => {
const contributors = useAppSelector((state) => state.contributors);
const dispatch = useAppDispatch();

useEffect(() => {
console.log("Dispatching fetchContributors action...");
dispatch(fetchContributors());
}, [dispatch]);

console.log("Contributors state:", contributors);

return (
<div>
<NotFound />
{contributors.data?.map((contributor) => (
<h1 key={contributor.id}>{contributor.name}</h1>
))}
</div>
);
};
Expand Down