From 770e5bd823c39ef6c340fe95461b1a920728e4e9 Mon Sep 17 00:00:00 2001 From: arch-spatula Date: Sun, 8 Jan 2023 16:02:11 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=A7=88=EC=9D=B4=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=80=20=ED=86=B5=EC=8B=A0=20#1=20#2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 본안 약만 볼 수 있게 통신을 통신 기능을 추가했습니다. 임시로 로그인, 회원가입, uid 조회 코드를 작성했습니다. --- src/screen/MyPage.jsx | 170 +++++++++++++++++++++++++++++++++++++++++ src/shared/firebase.js | 2 + 2 files changed, 172 insertions(+) diff --git a/src/screen/MyPage.jsx b/src/screen/MyPage.jsx index e69de29..36f1699 100644 --- a/src/screen/MyPage.jsx +++ b/src/screen/MyPage.jsx @@ -0,0 +1,170 @@ +import styled from '@emotion/native'; +import { Text, View } from 'react-native'; +import { useQuery, useMutation } from 'react-query'; +import { + getDocs, + addDoc, + deleteDoc, + query, + collection, + orderBy, + where, + doc, +} from 'firebase/firestore'; +import { + createUserWithEmailAndPassword, + signInWithEmailAndPassword, +} from 'firebase/auth'; +import { dbService, authService } from '../shared/firebase'; +import { getAuth } from 'firebase/auth'; +import { useState } from 'react'; +import { v4 as uuid } from 'uuid'; + +const adminInfo = { + id: 'catchPillAdmin@email.com', + pw: '123456', +}; + +const MyPage = () => { + // 회원가입입니다. + const handlesSignUp = () => { + createUserWithEmailAndPassword(authService, adminInfo.id, adminInfo.pw) + .then((userData) => console.log(userData.user)) + .catch((error) => { + const errorMessage = error.message; + console.log('회원가입 실패', errorMessage); + }); + }; + + // 로그인입니다. + const handleLogin = () => { + signInWithEmailAndPassword(authService, adminInfo.id, adminInfo.pw) + .then(() => { + console.log('로그인 성공'); + }) + .catch((error) => { + console.log('로그인 실패', error.errorMessage); + }); + }; + + // TODO: 파이어 베이스 초대 받으면 문서 이름 알아내기 + // const q = query(collection(dbService, ''), orderBy('')); + + const newPill = { + id: uuid(), + userId: uid, + pillName: '아침약', + time: '08:00', + isTaken: false, + }; + + // /pill + const addPill = (newPill) => { + const selectedDoc = query(collection(dbService, 'pill')); + addDoc(selectedDoc, newPill) + .then(() => { + console.log('추가 성공'); + }) + .catch((error) => console.log('error', error.errorMessage)); + }; + // TODO: 쿼리키 통일하기 + // UID를 조회합니다. + const getUID = () => { + return getAuth(); + }; + const { data: uid } = useQuery('uid', getUID, { + select: (data) => { + return data.currentUser.uid; + }, + }); + console.log('getUid', uid); + // read users pill + const getUsersPillList = (uid) => { + const selectedDocs = query( + collection(dbService, 'pill'), + where('userId', '==', uid), + ); + return getDocs(selectedDocs); + }; + + // 의존적 쿼리입니다. 사용자의 UID를 얻은 후에 통신이 가능합니다. + const { isLoading, isError, error, data } = useQuery( + 'pill-list', + () => getUsersPillList(uid), + { + enabled: !!uid, + }, + ); + const pillList = data?.docs.map((doc) => ({ ...doc.data(), id: doc.id })); + const renderPillList = pillList?.map((pillListItem) => ( + + {pillListItem.pillName} + {pillListItem.time} + + )); + + // 에러 핸들링: 통신 실패의 경우 보여주는 화면입니다. + if (isError) { + return ( + + 나의 약관리 + + 에러 + {error.errorMessage} + + + ); + } + + // 통신 중 + if (isLoading) { + return ( + + 나의 약관리 + + Loading... + + + ); + } + + return ( + + 나의 약관리 + {/* 목록을 통신으로 얻어오기 */} + {/* TODO: flatList 컴포넌트로 리팩토링하기 */} + console.log('add Pill')}> + 새로운 약 추가하기 + + signInWithEmailAndPassword()}> + 임시 회원가입 + + addPill(newPill)}> + 약추가 + + {/* 약추가 */} + {renderPillList} + {/* TODO: 하단 tabs */} + + ); +}; + +const MyPageContainer = styled.SafeAreaView` + flex: 1; +`; + +const PageTitle = styled.Text` + font-size: 36px; + font-weight: 700; + line-height: 40px; + margin: 20px 16px; +`; + +const AddPill = styled.TouchableOpacity` + background-color: #f8f8f8; + margin: 8px 16px; + height: 80px; + border-radius: 16px; +`; + +export default MyPage; diff --git a/src/shared/firebase.js b/src/shared/firebase.js index abd5f3c..01365eb 100644 --- a/src/shared/firebase.js +++ b/src/shared/firebase.js @@ -1,4 +1,5 @@ import { initializeApp } from 'firebase/app'; +import { getAuth } from 'firebase/auth'; import { getFirestore } from 'firebase/firestore'; import { CONFIG_KEY } from './config'; @@ -24,3 +25,4 @@ const firebaseConfig = { // Initialize Firebase export const app = initializeApp(firebaseConfig); export const dbService = getFirestore(app); +export const authService = getAuth(app);