Skip to content

Commit

Permalink
feat: add dev env
Browse files Browse the repository at this point in the history
  • Loading branch information
yuta-ike committed Nov 17, 2023
1 parent d896e6e commit d928def
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 30 deletions.
25 changes: 17 additions & 8 deletions .env.local.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
NEXT_PUBLIC_APP_URL="http://localhost:3000"

NEXT_PUBLIC_FIREBASE_API_KEY=""
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN="xxx.firebaseapp.com"
NEXT_PUBLIC_FIREBASE_PROJECT_ID=""
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET="xxx.appspot.com"
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=""
NEXT_PUBLIC_FIREBASE_APP_ID=""
# 本番
NEXT_PUBLIC_FIREBASE_API_KEY=
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=
NEXT_PUBLIC_FIREBASE_PROJECT_ID=
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=
NEXT_PUBLIC_FIREBASE_APP_ID=

BASIC_AUTH_USERNAME=""
BASIC_AUTH_PASSWORD=""
# 開発
NEXT_PUBLIC_DEV_FIREBASE_API_KEY=
NEXT_PUBLIC_DEV_FIREBASE_AUTH_DOMAIN=
NEXT_PUBLIC_DEV_FIREBASE_PROJECT_ID=
NEXT_PUBLIC_DEV_FIREBASE_STORAGE_BUCKET=
NEXT_PUBLIC_DEV_FIREBASE_MESSAGING_SENDER_ID=
NEXT_PUBLIC_DEV_FIREBASE_APP_ID=

BASIC_AUTH_USERNAME=
BASIC_AUTH_PASSWORD=
9 changes: 9 additions & 0 deletions src/app/ProdUserList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use client"

import { prodDb } from "@/lib/firebase"

import UserList from "./components/UserList"

export const ProdUserList = () => {
return <UserList db={prodDb} />
}
27 changes: 17 additions & 10 deletions src/app/components/UserList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client"

import {
Firestore,
addDoc,
collection,
deleteDoc,
Expand All @@ -13,9 +14,7 @@ import {
import React, { useEffect, useState } from "react"
import { MdOutlineLocalCafe } from "react-icons/md"
import { fakerJA as faker } from "@faker-js/faker"
import { nanoid } from "nanoid"

import { db } from "@/lib/firebase"
import { customAlphabet, nanoid } from "nanoid"

type User = {
id: string
Expand All @@ -32,12 +31,17 @@ type User = {
homepageLink: string
}

const UserList = () => {
type UserListProps = {
db: Firestore
}

const UserList = ({ db }: UserListProps) => {
const [users, setUsers] = useState<User[]>([])

useEffect(() => {
const unsubscribe = onSnapshot(collection(db, "users"), (querySnapshot) => {
querySnapshot.docChanges().forEach((change) => {
console.log(change)
if (change.type === "added") {
const data = change.doc.data()
setUsers((users) => [...users, { id: change.doc.id, ...data } as User])
Expand All @@ -55,15 +59,17 @@ const UserList = () => {
})

return () => unsubscribe()
}, [])
}, [db])

const kanpai = async (meId: string, targetId: string) => {
console.log(meId, targetId)
const cheerUserIds = users.find((user) => user.id === meId)?.cheerUserIds ?? []
console.log(db, cheerUserIds, targetId)
const targetUser = users.find((user) => user.id === targetId)
if (targetUser == null) {
return
}
await updateDoc(doc(db, "users", meId), {
cheerUserIds: [...cheerUserIds, targetId],
lastCheersUserId: targetId,
cheerUserIds: [...cheerUserIds, targetUser.bleUserId],
lastCheersUserId: targetUser.bleUserId,
})
await addDoc(collection(db, "cheers"), {
fromUserId: meId,
Expand Down Expand Up @@ -118,7 +124,7 @@ const UserList = () => {
}

const [userId, setUserId] = useState("")
const [bleUserId, setBleUserId] = useState("ABCDEF")
// const [bleUserId, setBleUserId] = useState("ABCDEF")
const [name, setName] = useState("ななし")
const [location, setLocation] = useState("関東")
const [profileImageUrl, setProfileImageUrl] = useState(
Expand All @@ -131,6 +137,7 @@ const UserList = () => {

const createUser = async () => {
const docId = userId === "" ? nanoid() : userId
const bleUserId = customAlphabet("1234567890ABCDEF")(6)
await setDoc(doc(db, "users", docId), {
id: docId,
bleUserId,
Expand Down
9 changes: 9 additions & 0 deletions src/app/dev/DevUserList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use client"

import { devDb } from "@/lib/firebase/dev"

import UserList from "../components/UserList"

export const DevUserList = () => {
return <UserList db={devDb} />
}
18 changes: 18 additions & 0 deletions src/app/dev/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DevUserList } from "./DevUserList"

const Dev = () => {
return (
<main>
<div className="m-4">
<DevUserList />
</div>
</main>
)
}

export default Dev

export const metadata = {
title: "Kanpai! Debugger",
description: "Kanpai! Debugger",
}
4 changes: 2 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import UserList from "./components/UserList"
import { ProdUserList } from "./ProdUserList"

const Index = () => {
return (
<main>
<div className="m-4">
<UserList />
<ProdUserList />
</div>
</main>
)
Expand Down
17 changes: 17 additions & 0 deletions src/lib/firebase/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const prodConfig = {
apiKey: process.env["NEXT_PUBLIC_FIREBASE_API_KEY"],
authDomain: process.env["NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN"],
projectId: process.env["NEXT_PUBLIC_FIREBASE_PROJECT_ID"],
storageBucket: process.env["NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET"],
messagingSenderId: process.env["NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID"],
appId: process.env["NEXT_PUBLIC_FIREBASE_APP_ID"],
}

export const devConfig = {
apiKey: process.env["NEXT_PUBLIC_FDEV_IREBASE_API_KEY"],
authDomain: process.env["NEXT_PUBLIC_DEV_FIREBASE_AUTH_DOMAIN"],
projectId: process.env["NEXT_PUBLIC_DEV_FIREBASE_PROJECT_ID"],
storageBucket: process.env["NEXT_PUBLIC_DEV_FIREBASE_STORAGE_BUCKET"],
messagingSenderId: process.env["NEXT_PUBLIC_DEV_FIREBASE_MESSAGING_SENDER_ID"],
appId: process.env["NEXT_PUBLIC_DEV_FIREBASE_APP_ID"],
}
8 changes: 8 additions & 0 deletions src/lib/firebase/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { initializeApp } from "firebase/app"
import { getFirestore } from "firebase/firestore"

import { devConfig } from "./config"

export const devApp = initializeApp(devConfig)

export const devDb = getFirestore(devApp)
13 changes: 3 additions & 10 deletions src/lib/firebase/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { initializeApp } from "firebase/app"
import { getFirestore } from "firebase/firestore"

const firebaseConfig = {
apiKey: process.env["NEXT_PUBLIC_FIREBASE_API_KEY"],
authDomain: process.env["NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN"],
projectId: process.env["NEXT_PUBLIC_FIREBASE_PROJECT_ID"],
storageBucket: process.env["NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET"],
messagingSenderId: process.env["NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID"],
appId: process.env["NEXT_PUBLIC_FIREBASE_APP_ID"],
}
import { prodConfig } from "./config"

export const app = initializeApp(firebaseConfig)
export const prodApp = initializeApp(prodConfig)

export const db = getFirestore(app)
export const prodDb = getFirestore(prodApp)

0 comments on commit d928def

Please sign in to comment.