Skip to content

Contributors Page UI #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
@@ -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
12 changes: 12 additions & 0 deletions src/app/common.types.ts
Original file line number Diff line number Diff line change
@@ -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
},
});

3 changes: 2 additions & 1 deletion src/common/services/index.ts
Original file line number Diff line number Diff line change
@@ -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>
);
};