From ec4376a401037cc0f94a7aacd2a9a944d7d6bbcb Mon Sep 17 00:00:00 2001 From: Prantosh Biswas Date: Thu, 20 Jul 2023 21:08:13 +0530 Subject: [PATCH 1/3] Fix API endpoint in Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 54e77e2..f8d79a8 100644 --- a/README.md +++ b/README.md @@ -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 From b22ecd503c7c6fff67ce0d5ef6df700e2bd86da5 Mon Sep 17 00:00:00 2001 From: Prantosh Biswas Date: Thu, 20 Jul 2023 20:59:50 +0530 Subject: [PATCH 2/3] Add Contributors in store --- src/app/common.types.ts | 12 ++++++++++++ src/app/store.ts | 2 ++ 2 files changed, 14 insertions(+) diff --git a/src/app/common.types.ts b/src/app/common.types.ts index 551038a..07945fd 100644 --- a/src/app/common.types.ts +++ b/src/app/common.types.ts @@ -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; +} \ No newline at end of file diff --git a/src/app/store.ts b/src/app/store.ts index 7fb3f12..e07bb0b 100644 --- a/src/app/store.ts +++ b/src/app/store.ts @@ -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 }, }); From 35c3e53825fbbfbb99ab366e242e8cba18476482 Mon Sep 17 00:00:00 2001 From: Prantosh Biswas Date: Sat, 22 Jul 2023 21:00:20 +0530 Subject: [PATCH 3/3] Create Contributor Page --- src/common/services/index.ts | 3 +- .../contributors/contributorsSlice.ts | 44 +++++++++++++++++++ src/pages/Contributors.tsx | 18 +++++++- 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/features/contributors/contributorsSlice.ts diff --git a/src/common/services/index.ts b/src/common/services/index.ts index 288edd5..6ef2754 100644 --- a/src/common/services/index.ts +++ b/src/common/services/index.ts @@ -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; diff --git a/src/features/contributors/contributorsSlice.ts b/src/features/contributors/contributorsSlice.ts new file mode 100644 index 0000000..967299e --- /dev/null +++ b/src/features/contributors/contributorsSlice.ts @@ -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; \ No newline at end of file diff --git a/src/pages/Contributors.tsx b/src/pages/Contributors.tsx index 8cdc8bb..b3e0f0b 100644 --- a/src/pages/Contributors.tsx +++ b/src/pages/Contributors.tsx @@ -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 (
- + {contributors.data?.map((contributor) => ( +

{contributor.name}

+ ))}
); };