fix: moved projects from redux to context
This commit is contained in:
@ -1,39 +1,21 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect } from "react";
|
|
||||||
import { useSelector } from "react-redux";
|
|
||||||
import { Group } from "@mantine/core";
|
import { Group } from "@mantine/core";
|
||||||
|
import { useProjectsContext } from "@/app/deals/contexts/ProjectsContext";
|
||||||
import ProjectSelect from "@/components/selects/ProjectSelect/ProjectSelect";
|
import ProjectSelect from "@/components/selects/ProjectSelect/ProjectSelect";
|
||||||
import {
|
|
||||||
selectProject,
|
|
||||||
setProjects,
|
|
||||||
} from "@/lib/features/projects/projectsSlice";
|
|
||||||
import { RootState, useAppDispatch } from "@/lib/store";
|
|
||||||
|
|
||||||
const Header = () => {
|
const Header = () => {
|
||||||
const projectsState = useSelector(
|
const { projects, setSelectedProject, selectedProject } =
|
||||||
(state: RootState) => state.projectsState
|
useProjectsContext();
|
||||||
);
|
|
||||||
const dispatch = useAppDispatch();
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const mockProjects = [
|
|
||||||
{ id: 1, name: "Проект 1" },
|
|
||||||
{ id: 2, name: "Проект 2" },
|
|
||||||
{ id: 3, name: "Проект 3" },
|
|
||||||
];
|
|
||||||
dispatch(setProjects(mockProjects));
|
|
||||||
dispatch(selectProject(mockProjects[0]));
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Group
|
<Group
|
||||||
justify={"flex-end"}
|
justify={"flex-end"}
|
||||||
w={"100%"}>
|
w={"100%"}>
|
||||||
<ProjectSelect
|
<ProjectSelect
|
||||||
data={projectsState.projects}
|
data={projects}
|
||||||
value={projectsState.selectedProject}
|
value={selectedProject}
|
||||||
onChange={value => value && dispatch(selectProject(value))}
|
onChange={value => value && setSelectedProject(value)}
|
||||||
/>
|
/>
|
||||||
</Group>
|
</Group>
|
||||||
);
|
);
|
||||||
|
|||||||
77
src/app/deals/contexts/ProjectsContext.tsx
Normal file
77
src/app/deals/contexts/ProjectsContext.tsx
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import React, {
|
||||||
|
createContext,
|
||||||
|
FC,
|
||||||
|
useContext,
|
||||||
|
useEffect,
|
||||||
|
useState,
|
||||||
|
} from "react";
|
||||||
|
import useProjects from "@/app/deals/hooks/useProjects";
|
||||||
|
import { ProjectSchema } from "@/types/ProjectSchema";
|
||||||
|
|
||||||
|
type ProjectsContextState = {
|
||||||
|
selectedProject: ProjectSchema | null;
|
||||||
|
setSelectedProject: React.Dispatch<
|
||||||
|
React.SetStateAction<ProjectSchema | null>
|
||||||
|
>;
|
||||||
|
projects: ProjectSchema[];
|
||||||
|
};
|
||||||
|
|
||||||
|
const ProjectsContext = createContext<ProjectsContextState | undefined>(
|
||||||
|
undefined
|
||||||
|
);
|
||||||
|
|
||||||
|
const useProjectsContextState = () => {
|
||||||
|
const { projects, refetchProjects } = useProjects();
|
||||||
|
const [selectedProject, setSelectedProject] =
|
||||||
|
useState<ProjectSchema | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (projects.length > 0) {
|
||||||
|
if (selectedProject) {
|
||||||
|
setSelectedProject(
|
||||||
|
projects.find(
|
||||||
|
project => project.id === selectedProject.id
|
||||||
|
) ?? null
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
setSelectedProject(projects[0]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setSelectedProject(null);
|
||||||
|
}
|
||||||
|
}, [projects]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
projects,
|
||||||
|
selectedProject,
|
||||||
|
setSelectedProject,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
type ProjectsContextProviderProps = {
|
||||||
|
children: React.ReactNode;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ProjectsContextProvider: FC<ProjectsContextProviderProps> = ({
|
||||||
|
children,
|
||||||
|
}) => {
|
||||||
|
const state = useProjectsContextState();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ProjectsContext.Provider value={state}>
|
||||||
|
{children}
|
||||||
|
</ProjectsContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useProjectsContext = () => {
|
||||||
|
const context = useContext(ProjectsContext);
|
||||||
|
if (!context) {
|
||||||
|
throw new Error(
|
||||||
|
"useProjectsContext must be used within a ProjectsContextProvider"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
};
|
||||||
25
src/app/deals/hooks/useProjects.ts
Normal file
25
src/app/deals/hooks/useProjects.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { ProjectSchema } from "@/types/ProjectSchema";
|
||||||
|
|
||||||
|
const useProjects = () => {
|
||||||
|
const [projects, setProjects] = useState<ProjectSchema[]>([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const mockProjects = [
|
||||||
|
{ id: 1, name: "Проект 1" },
|
||||||
|
{ id: 2, name: "Проект 2" },
|
||||||
|
{ id: 3, name: "Проект 3" },
|
||||||
|
];
|
||||||
|
setProjects(mockProjects);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const refetchProjects = () => {};
|
||||||
|
|
||||||
|
return {
|
||||||
|
projects,
|
||||||
|
setProjects,
|
||||||
|
refetchProjects,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useProjects;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
import Boards from "@/app/deals/components/Boards/Boards";
|
import Boards from "@/app/deals/components/Boards/Boards";
|
||||||
import Header from "@/app/deals/components/Header/Header";
|
import Header from "@/app/deals/components/Header/Header";
|
||||||
|
import { ProjectsContextProvider } from "@/app/deals/contexts/ProjectsContext";
|
||||||
import PageBlock from "@/components/PageBlock/PageBlock";
|
import PageBlock from "@/components/PageBlock/PageBlock";
|
||||||
import PageContainer from "@/components/PageContainer/PageContainer";
|
import PageContainer from "@/components/PageContainer/PageContainer";
|
||||||
|
|
||||||
@ -7,8 +8,10 @@ export default function DealsPage() {
|
|||||||
return (
|
return (
|
||||||
<PageContainer>
|
<PageContainer>
|
||||||
<PageBlock>
|
<PageBlock>
|
||||||
<Header />
|
<ProjectsContextProvider>
|
||||||
<Boards />
|
<Header />
|
||||||
|
<Boards />
|
||||||
|
</ProjectsContextProvider>
|
||||||
</PageBlock>
|
</PageBlock>
|
||||||
</PageContainer>
|
</PageContainer>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,29 +0,0 @@
|
|||||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
|
||||||
import { ProjectSchema } from "@/types/ProjectSchema";
|
|
||||||
|
|
||||||
interface ProjectsState {
|
|
||||||
projects: ProjectSchema[];
|
|
||||||
selectedProject: ProjectSchema | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const initialState: ProjectsState = {
|
|
||||||
projects: [],
|
|
||||||
selectedProject: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
export const projectsSlice = createSlice({
|
|
||||||
name: "projects",
|
|
||||||
initialState,
|
|
||||||
reducers: {
|
|
||||||
setProjects: (state, action: PayloadAction<ProjectSchema[]>) => {
|
|
||||||
state.projects = action.payload;
|
|
||||||
},
|
|
||||||
selectProject: (state, action: PayloadAction<ProjectSchema>) => {
|
|
||||||
state.selectedProject = action.payload;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
export const { setProjects, selectProject } = projectsSlice.actions;
|
|
||||||
|
|
||||||
export default projectsSlice.reducer;
|
|
||||||
@ -1,10 +1,8 @@
|
|||||||
import { combineReducers } from "@reduxjs/toolkit";
|
import { combineReducers } from "@reduxjs/toolkit";
|
||||||
import authReducer from "@/lib/features/auth/authSlice";
|
import authReducer from "@/lib/features/auth/authSlice";
|
||||||
import projectsReducer from "@/lib/features/projects/projectsSlice";
|
|
||||||
|
|
||||||
const rootReducer = combineReducers({
|
const rootReducer = combineReducers({
|
||||||
auth: authReducer,
|
auth: authReducer,
|
||||||
projectsState: projectsReducer,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default rootReducer;
|
export default rootReducer;
|
||||||
|
|||||||
Reference in New Issue
Block a user