Skip to content

Commit

Permalink
パッケージとコンポーネントの変更を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun-Murakami committed May 4, 2024
1 parent 62cf5eb commit 0cd11bb
Show file tree
Hide file tree
Showing 16 changed files with 775 additions and 120 deletions.
71 changes: 71 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ exports.addUserToTree = functions.https.onCall(async (data: { email: string; tre
return currentMembers;
}
});
const treeMembersRefV2 = admin.database().ref(`trees/${treeId}/membersV2`);
await treeMembersRefV2.transaction((currentMembers: { [key: string]: string | boolean } | null) => {
if (currentMembers === null) {
const newMembers: { [key: string]: string } = {};
newMembers[userId] = email; // 新しいメンバーとして追加
return newMembers;
} else {
if (!Object.prototype.hasOwnProperty.call(currentMembers, userId)) {
currentMembers[userId] = email; // メンバーが存在しなければ追加
}
return currentMembers;
}
});

// users/$userId/treeListにtreeIdを追加
const userTreeListRef = admin.database().ref(`users/${userId}/treeList`);
Expand All @@ -86,6 +99,10 @@ exports.addUserToTree = functions.https.onCall(async (data: { email: string; tre
}
});

// trees/$treeId/timestampに現在の時間を追加
const timestampRef = admin.database().ref(`trees/${treeId}/timestamp`);
await timestampRef.set(Date.now());

return { success: true };
} catch (error) {
console.error('Error adding user to tree:', error);
Expand Down Expand Up @@ -138,6 +155,19 @@ exports.removeUserFromTree = functions.https.onCall(async (data: { treeId: strin
}
}
});
const treeMembersRefV2 = admin.database().ref(`trees/${treeId}/membersV2`);
await treeMembersRefV2.transaction((currentMembers: { [key: string]: string } | null) => {
if (currentMembers === null) {
return currentMembers; // 何もしない
} else {
if (Object.prototype.hasOwnProperty.call(currentMembers, userId)) {
delete currentMembers[userId]; // userIdをキーとして持つプロパティを削除
return currentMembers;
} else {
return currentMembers; // ユーザーが見つからない場合は何もしない
}
}
});

// users/$userId/treeListからtreeIdを削除
const userTreeListRef = admin.database().ref(`users/${userId}/treeList`);
Expand All @@ -155,6 +185,10 @@ exports.removeUserFromTree = functions.https.onCall(async (data: { treeId: strin
}
});

// trees/$treeId/timestampに現在の時間を追加
const timestampRef = admin.database().ref(`trees/${treeId}/timestamp`);
await timestampRef.set(Date.now());

return { success: true };
} catch (error) {
console.error('Error removing user from tree:', error);
Expand Down Expand Up @@ -187,10 +221,26 @@ exports.createNewTree = functions.https.onCall(async (data: { items: ServerTreeI
const db = admin.database();
const treesRef = db.ref('trees');
const newTreeRef = treesRef.push();
const emails = await Promise.all(
Object.keys(members).map(async (uid) => {
const userRecord = await admin.auth().getUser(uid);
return userRecord.email;
})
);
const membersV2: { [key: string]: string } = {};
Object.keys(members).forEach((uid, index) => {
const email = emails[index];
if (email) {
membersV2[uid] = email;
} else {
throw new Error('Email not found for UID: ' + uid);
}
});
await newTreeRef.set({
items: items,
name: name,
members: members,
membersV2: membersV2,
});
return newTreeRef.key;
} catch (error) {
Expand All @@ -217,4 +267,25 @@ exports.copyFileInStorage = functions.https.onCall(async (data: { sourcePath: st
console.error('Error copying file:', error);
throw new functions.https.HttpsError('unknown', 'Failed to copy file');
}
});

// 指定されたuidリストを指定されたタイムスタンプで更新
exports.updateTimestamps = functions.https.onCall(async (data: { uids: string[], timestamp: number }) => {
const { uids, timestamp } = data;
if (!uids || !Array.isArray(uids) || !timestamp) {
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with valid "uids" and "timestamp".');
}
try {
const db = admin.database();
await Promise.all(
uids.map(async (uid) => {
const userRef = db.ref(`users/${uid}`);
await userRef.update({ timestamp: timestamp });
})
);
return { success: true };
} catch (error) {
console.error('Error updating timestamps:', error);
throw new functions.https.HttpsError('unknown', 'Failed to update timestamps');
}
});
21 changes: 19 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"@fontsource/m-plus-1p": "^5.0.18",
"@mui/icons-material": "^5.15.15",
"@mui/material": "^5.15.15",
"dexie": "^4.0.4",
"dexie-react-hooks": "^1.1.7",
"firebase": "^10.6.0",
"immer": "^10.0.4",
"lodash": "^4.17.21",
Expand Down
4 changes: 2 additions & 2 deletions src/components/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ export function HomePage() {
Appleでログイン
</Button>
<Button
onClick={() => {
onClick={async () => {
setIsOffline(true);
setIsLoggedIn(true);
handleCreateOfflineTree();
await handleCreateOfflineTree();
}}
variant='contained'
sx={{ textTransform: 'none' }}
Expand Down
4 changes: 2 additions & 2 deletions src/components/SortableList/SortableList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const SortableList: FC<SortableListProps> = ({ handleListClick, setDrawer
onDragStart={(event) => {
setActiveId(event.active.id as number);
}}
onDragEnd={(event) => {
onDragEnd={async (event) => {
setActiveId(null);
const { active, over } = event;
if (over == null || active.id === over.id) {
Expand All @@ -45,7 +45,7 @@ export const SortableList: FC<SortableListProps> = ({ handleListClick, setDrawer
const newIndex = treesList.findIndex((item) => item.id === over.id);
const newItems = arrayMove(treesList, oldIndex, newIndex);
setTreesList(newItems);
saveTreesListDb(newItems);
await saveTreesListDb(newItems);
}}
>
<SortableContext items={treesList}>
Expand Down
1 change: 1 addition & 0 deletions src/components/SortableTree/SortableTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export function SortableTree({ collapsible, indicator = false, indentationWidth

// コンポーネントのアンマウント時にイベントリスナーを削除
return () => window.removeEventListener('resize', handleResize);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const measuring = {
Expand Down
4 changes: 2 additions & 2 deletions src/components/TreeSettingsAccordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ export function TreeSettingsAccordion() {
onClick={(e) => e.stopPropagation()}
onCompositionStart={() => setIsComposing(true)}
onCompositionEnd={() => setIsComposing(false)}
onKeyDown={(e) => {
onKeyDown={async (e) => {
if (e.key === 'Enter' && !isComposing) {
e.preventDefault(); // エンターキーのデフォルト動作を防ぐ
if (editedTreeName && editedTreeName !== '') {
handleTreeNameSubmit(editedTreeName);
await handleTreeNameSubmit(editedTreeName);
}
setIsAccordionExpanded(false);
}
Expand Down
12 changes: 6 additions & 6 deletions src/hooks/useAppStateManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,29 @@ export const useAppStateManagement = () => {
};

// ダークモードと完了済みアイテムの非表示設定を保存 ------------------------------------------------
const saveAppSettingsDb = (darkModeNew: boolean, hideDoneItemsNew: boolean) => {
const saveAppSettingsDb = async (darkModeNew: boolean, hideDoneItemsNew: boolean) => {
const db = getDatabase();
if (!uid || !db) {
return;
}
try {
const userSettingsRef = ref(db, `users/${uid}/settings`);
saveTimeStampDb();
set(userSettingsRef, { darkMode: darkModeNew, hideDoneItems: hideDoneItemsNew });
await saveTimeStampDb();
await set(userSettingsRef, { darkMode: darkModeNew, hideDoneItems: hideDoneItemsNew });
} catch (error) {
handleError(error);
}
}

// クイックメモをデータベースに保存する関数 ---------------------------------------------------------------------------
const saveQuickMemoDb = (quickMemoText: string) => {
const saveQuickMemoDb = async (quickMemoText: string) => {
if (!uid) {
return;
}
try {
const quickMemoRef = ref(getDatabase(), `users/${uid}/quickMemo`);
saveTimeStampDb();
set(quickMemoRef, quickMemoText);
await saveTimeStampDb();
await set(quickMemoRef, quickMemoText);
} catch (error) {
handleError('クイックメモの変更をデータベースに保存できませんでした。\n\n' + error);
}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useAttachedFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const useAttachedFile = () => {
const newItems: TreeItem[] = JSON.parse(JSON.stringify(items));
const updatedItems = await deleteAttachedFile(newItems, fileName);
setItems(updatedItems);
saveItemsDb(updatedItems, currentTree!);
await saveItemsDb(updatedItems, currentTree!);
}
}
}
Expand Down
Loading

0 comments on commit 0cd11bb

Please sign in to comment.