Skip to content

Commit

Permalink
🐛 Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Apr 22, 2024
1 parent 34001d1 commit 6961483
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 deletions.
27 changes: 22 additions & 5 deletions apps/client-ts/src/components/RootLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useProfile from '@/hooks/useProfile';
import useProfileStore from '@/state/profileStore';
import useProjectStore from '@/state/projectStore';
import useProjectsByUser from '@/hooks/useProjectsByUser';
import useProjectsStore, { Project } from '@/state/projectsStore';

const useDeviceSize = () => {

Expand Down Expand Up @@ -54,17 +55,19 @@ export const RootLayout = () => {
const { profile, setProfile } = useProfileStore();

const { idProject, setIdProject } = useProjectStore();

// eslint-disable-next-line react-hooks/rules-of-hooks
const { data: projects, isLoading: isLoadingProjects } = profile ? useProjectsByUser(profile.id_user) : { data: [], isLoading: false };

useEffect(() => {
const { projects, setProjects } = useProjectsStore();

//const { data: projects, isLoading: isLoadingProjects } = useProjectsByUser(user?.user_id!);

/*useEffect(() => {
if(projects && projects[0]){
setIdProject(projects[0].id_project);
}
},[projects, setIdProject])
*/

useEffect(()=> {
useEffect(() => {
if(data){
//console.log("data is "+ JSON.stringify(data));
setProfile({
Expand All @@ -76,6 +79,20 @@ export const RootLayout = () => {
})
}
}, [data, setProfile]);

useEffect(() => {
if(data){
const fetchProjects = async () => {
if (data) {
const response = await fetch(`${config.API_URL}/projects/${data.id_user}`);
const projectsData = await response.json();
setIdProject(projectsData[0]?.id_project); // Assuming you want to set the first project ID
setProjects(projectsData as Project[])
}
};
fetchProjects();
}
}, [data, setIdProject, setProfile, setProjects]);


const handlePageChange = (page: string) => {
Expand Down
25 changes: 11 additions & 14 deletions apps/client-ts/src/components/shared/team-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import * as z from "zod"
import config from "@/lib/config"
import useProjectsStore from "@/state/projectsStore"


const projectFormSchema = z.object({
Expand All @@ -82,25 +83,21 @@ export default function TeamSwitcher({ className }: TeamSwitcherProps) {
open: false,
})

const [isloadingProjects, setIsloadingProjects] = useState<boolean>(true)

//const { data : orgs, isLoading: isloadingOrganisations } = useOrganisations();

const { idProject, setIdProject } = useProjectStore();

const { profile } = useProfileStore();

const { data : projects, isLoading: isloadingProjects } = useProjectsByUser(profile!.id_user);
const { projects } = useProjectsStore();


useEffect(()=>{
if(projects && projects[0]){
setIdProject(projects[0].id_project);
}
//TODO: display connected user
/*if(orgs && orgs[0]){
setOrganisationName(orgs[0].name);
setIdOrg(orgs[0].id_organization);
}*/
},[projects, setIdProject])
if(projects){
setIsloadingProjects(false);
}

const { profile } = useProfileStore();

//const { data: projects, isLoading: isLoadingProjects } = useProjectsByUser(user?.user_id!);

const handleOpenChange = (open: boolean) => {
setShowNewDialog(prevState => ({ ...prevState, open }));
Expand Down
4 changes: 2 additions & 2 deletions apps/client-ts/src/hooks/useProjectsByUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { useQuery } from '@tanstack/react-query';
import { projects as Project } from 'api';


const useProjectsByUser = (userId: string) => {
const useProjectsByUser = (stytchUserId: string) => {
return useQuery({
queryKey: ['projects'],
queryFn: async (): Promise<Project[]> => {
const response = await fetch(`${config.API_URL}/projects/${userId}`);
const response = await fetch(`${config.API_URL}/projects/${stytchUserId}`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
Expand Down
22 changes: 22 additions & 0 deletions apps/client-ts/src/state/projectsStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { create } from 'zustand';

export interface Project {
id_project: string;
name: string;
sync_mode: string;
pull_frequency: string;
redirect_url?: string;
id_user: string;
}

interface ProjectsState {
projects: Project[] | null;
setProjects: (projects: Project[]) => void;
}

const useProjectsStore = create<ProjectsState>()((set) => ({
projects: null,
setProjects: (projects: Project[]) => set({ projects: projects }),
}));

export default useProjectsStore;
9 changes: 7 additions & 2 deletions packages/api/src/@core/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ export class ProjectsService {
}
}

async getProjectsByUser(userId: string) {
async getProjectsByUser(stytchUserId: string) {
try {
const user = await this.prisma.users.findUnique({
where: {
id_stytch: stytchUserId,
},
});
return await this.prisma.projects.findMany({
where: {
id_user: userId,
id_user: user.id_user,
},
});
} catch (error) {
Expand Down

0 comments on commit 6961483

Please sign in to comment.