Skip to content

Commit

Permalink
[feat] #14 프로젝트 data 필터링해서 assignee 배열 받아오기
Browse files Browse the repository at this point in the history
  • Loading branch information
harin0707 committed May 31, 2024
1 parent 2860ce4 commit 2f21224
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
38 changes: 36 additions & 2 deletions src/components/issue/InfoBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,61 @@ import { useRecoilState } from 'recoil';
import { modalState } from '@/recoil/state';
import SelectAssignee from '../modal/SelectAssignee';
import usePatch from '@/hooks/usePatch';
import { UpdateIssueInfo } from '@/types/type';
import { UpdateIssueInfo, ProjectInfo } from '@/types/type';
import { projectState } from '@/recoil/projectState';
import { contributerId } from '../../recoil/state';
import useFetchProject from '@/hooks/useFetchProject';
interface InputProps {
infoType: string;
data: string;
patchData: UpdateIssueInfo;
isAssigned: number;
}

const fetchProjectData = (data:any):ProjectInfo=>({
projectId: data.projectId,
title: data.title,
adminName: data.adminName,
contributorNames: data.contributorNames

})

const InfoBox: React.FC<InputProps> = ({infoType, data, patchData, isAssigned}) => {
const [isVisible, setVisiable] = useRecoilState(modalState);
const [issueId, setIssueId] = useState<string|null>(null);
const [projectId, setProjectId] = useState<string|null>(null);
const [userId, setUserId] = useState<string|null>(null);

const patchStatus = usePatch('issues/status', issueId ? Number(issueId) : 0)

const [projects, setProjects] = useRecoilState<ProjectInfo[]>(projectState);

const endpointP = '/projects';
const {dataP, loadingP, errorP, refetch} = useFetchProject<ProjectInfo>(endpointP, fetchProjectData, Number(userId));

useEffect(() => {
if (typeof window!== 'undefined'){
setIssueId(localStorage.getItem('issueId'));
setProjectId(localStorage.getItem('projectId'));
setUserId(localStorage.getItem('userId'));
}
}, [setIssueId]);

if (dataP) {
setProjects(dataP);
}

}, [dataP, setProjects, setIssueId, setProjectId]);


const contributerPerPj = projects
? (projects as ProjectInfo[])
.filter((item:ProjectInfo) => item.projectId===Number(projectId))
.map((item:ProjectInfo) => item.contributorNames)
:[0, 1];

console.log(contributerPerPj);


const handleAssignee = ()=>{
setVisiable(true);
}
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/useFetchProject.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState, useEffect, useCallback } from 'react';
import { userIdState } from '@/recoil/userState';
import { useRecoilState } from 'recoil';
import { projectState } from '@/recoil/projectState';

interface FetchResult<T>{
dataP: T[] | null;
Expand All @@ -14,19 +15,22 @@ const [dataP, setData] = useState<T[] | null>(null);
const [loadingP, setLoading] = useState<boolean>(false);
const [errorP, setError] = useState<string | null>(null);
const [userIdT, setUserId] = useRecoilState<number>(userIdState);
const [projectData, setProjectData] = useRecoilState(projectState);

const fetchData = useCallback(async () => {
setLoading(true);
try {
const id = localStorage.getItem('userId');
const url = new URL(`${process.env.NEXT_PUBLIC_API_BASE_URL}${endpoint}?userId=${id}`);

const response = await fetch(url.toString());


if (!response.ok) {
throw new Error('Network response was not ok');
}
const result = await response.json();
setData(result);
setProjectData(result);
setError(null);
console.log(result);

Expand Down
5 changes: 2 additions & 3 deletions src/pages/project/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React, {useEffect, useState} from 'react';
import styled from 'styled-components';
import {useRouter} from 'next/router';
import NewProject from '@/components/modal/NewProject';
import { useRecoilState} from 'recoil';
import { modalState } from '@/recoil/state';
import { useRecoilState } from 'recoil';
import { modalState, contributerName } from '@/recoil/state';
import useFetchData from '@/hooks/useFetchData';
import useFetchProject from '@/hooks/useFetchProject';
import { User, ProjectInfo } from '@/types/type';
Expand Down Expand Up @@ -37,7 +37,6 @@ const Projects = () => {




const handleModal = ()=>{
setVisiable(true);
}
Expand Down
7 changes: 7 additions & 0 deletions src/recoil/projectState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { atom, useRecoilState } from 'recoil';
import { ProjectInfo } from '@/types/type';

export const projectState = atom({
key: 'projectState',
default: [] as ProjectInfo[],
});
5 changes: 5 additions & 0 deletions src/recoil/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export const contributerName = atom<string[]>({
default: []
});

export const contributerNamePerPj = atom<string[]>({
key: 'contributerNamePerPj',
default: []
});

export const contributerId = atom<number[]>({
key: 'contributerId',
default: []
Expand Down

0 comments on commit 2f21224

Please sign in to comment.