Skip to content
Merged
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions src/features/projects/api/userAPi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { arrayUnion, doc, updateDoc } from "firebase/firestore";

import type { ApiResMessage } from "@entities/projects/types/firebase";

import { db } from "@shared/firebase/firebase";

/**
* 내가 등록한 프로젝트 등록
* users - myProjects에 ProjectID 넣기
* */
export const updateUserMyProject = async (
uid: string,
projectID: string
): Promise<ApiResMessage> => {
const usersRef = doc(db, "users", uid);

try {
await updateDoc(usersRef, {
myProjects에: arrayUnion(projectID),
});

return {
success: true,
message: "",
};
} catch (err) {
console.log(err);
return {
success: false,
message: "profile update실패. 동작이 반복 될 시 관리자에게 문의 주세요.",
};
}
};
48 changes: 25 additions & 23 deletions src/features/projects/hook/useProjectInsertForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { useState } from "react";

import useProjectInsert from "@features/projects/queries/useProjectInsert";

import { useUserProfile } from "@shared/queries/useUserProfile";
import { useAuthStore } from "@shared/stores/authStore";
import {
ProjectCategory,
RecruitmentStatus,
Workflow,
type ProjectItemInsertReq,
} from "@shared/types/project";
import { ExpectedPeriod } from "@shared/types/schedule";
import { UserExperience } from "@shared/types/user";
import { type User } from "@shared/types/user";

// 이하 InitData 개선 예정
type Setp1Type = Pick<
Expand Down Expand Up @@ -41,18 +43,14 @@ interface InsertFormResult {
}

const useProjectInsertForm = (): InsertFormResult => {
const { mutate: insertItem, isPending } = useProjectInsert();
const user = useAuthStore((state) => state.user);
const { data: userProfile } = useUserProfile(user?.uid || "");
const { mutate: insertProject, isPending } = useProjectInsert();

const [currentStep, setCurrentStep] = useState(1);
// Step1 상태
// const [formStep1, setFormStep1] = useState<Setp1Type>(initForm1);
// Step2 상태
const [formStep2, setFormStep2] = useState<Step2Type>({
teamSize: 0,
expectedPeriod: "",
techStack: [],
positions: [],
});
const [formStep2, setFormStep2] = useState<Step2Type>(initForm2);

const handleChangeStep2 = (field: keyof Step2Type, value: any): void => {
setFormStep2((prev) => ({ ...prev, [field]: value }));
};
Expand All @@ -67,15 +65,19 @@ const useProjectInsertForm = (): InsertFormResult => {
};

const submit = async (): Promise<void> => {
if (!userProfile) return;
if (!window.confirm("등록을 완료 하시겠습니까?")) return;
if (isPending) return;

// lint에러를 피하기 위한...
// 추후에 step3, step4 훅 만들 때 가져다 쓰시라고 미리 만들어놨습니다!
console.log(initForm2, initForm3, initForm4);

// form 검사 추가 바람
insertItem(TestData);
};

// lint에러를 피하기 위한...
// 추후에 step3, step4 훅 만들 때 가져다 쓰시라고 미리 만들어놨습니다!
console.log(initForm2, initForm3, initForm4);
// projects에 insert
insertProject(TestData(userProfile));
};

return {
form: {
Expand Down Expand Up @@ -120,14 +122,14 @@ const initForm4 = {
};

// 테스트용 form 입니다.
const TestData: ProjectItemInsertReq = {
const TestData = (user: User): ProjectItemInsertReq => ({
projectOwner: {
id: "user1234",
name: "홍길동",
userRole: "frontend",
email: "[email protected]",
experience: UserExperience.junior,
avatar: "https://via.placeholder.com/150",
id: user.id,
name: user.name,
userRole: user.userRole,
email: user.email,
experience: user.experience,
avatar: user.avatar,
},
applicants: [],
status: RecruitmentStatus.recruiting,
Expand Down Expand Up @@ -180,4 +182,4 @@ const TestData: ProjectItemInsertReq = {
likedUsers: [],
category: ProjectCategory.webDevelopment,
closedDate: Timestamp.now(),
};
});
13 changes: 10 additions & 3 deletions src/features/projects/queries/useProjectInsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useMutation, type UseMutationResult } from "@tanstack/react-query";
import { useNavigate } from "react-router-dom";

import { insertProjectItem } from "@features/projects/api/projectsApi";
import { updateUserMyProject } from "@features/projects/api/userAPi";

import type { ApiResMessage } from "@entities/projects/types/firebase";

Expand All @@ -23,11 +24,17 @@ const useProjectInsert = (): UseMutationResult<
}
return insertProjectItem(projectItem);
},
onSuccess: (data) => {
onSuccess: async (data) => {
// 게시글 등록 후 본인 게시글로 이동
if (data.success && data.id) {
alert("게시글이 등록 되었습니다.");
Navigate(`/project/${data.id}`);
try {
await updateUserMyProject(user?.uid || "", data.id);

alert("게시글이 등록 되었습니다.");
Navigate(`/project/${data.id}`);
} catch (err) {
console.log("users 업데이트 실패: ", err);
}
}
},
onError: (err) => {
Expand Down
18 changes: 18 additions & 0 deletions src/shared/api/userApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { doc, setDoc, getDoc } from "firebase/firestore";

import { db } from "@shared/firebase/firebase";
import type { User } from "@shared/types/user";

export const saveUser = async (uid: string, userInfo: User): Promise<void> => {
const userDoc = doc(db, "users", uid);
await setDoc(userDoc, userInfo);
};

export const getUser = async (uid: string): Promise<User | null> => {
const userDoc = doc(db, "users", uid);
const userSnap = await getDoc(userDoc);
if (userSnap.exists()) {
return userSnap.data() as User;
}
return null;
};
12 changes: 12 additions & 0 deletions src/shared/queries/useUserProfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useQuery, type UseQueryResult } from "@tanstack/react-query";

import { getUser } from "@shared/api/userApi";
import type { User } from "@shared/types/user";

export function useUserProfile(uid: string): UseQueryResult<User | null> {
return useQuery<User | null>({
queryKey: ["userProfile", uid],
queryFn: () => getUser(uid),
enabled: !!uid, // uid가 있을 때만 쿼리 실행
});
}
1 change: 1 addition & 0 deletions src/shared/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface User {
likeProjects?: string[]; // 좋아요 누른 프로젝트
appliedProjects?: string[]; // 지원한 프로젝트
introduceMyself?: string; // 자기소개
myProjects?: string[]; // 내가 등록한 프로젝트
}

export type UserRole = "frontend" | "backend" | "fullstack" | "designer" | "pm";
Expand Down