Skip to content
Snippets Groups Projects
Commit 417bc880 authored by Nishita Dash's avatar Nishita Dash
Browse files

Update useFolder.js

parent c603721b
No related branches found
No related tags found
No related merge requests found
import { useState,useReducer, useEffect } from "react"; import { useState,useReducer, useEffect } from "react";
import { database } from '../fire.js' import { database } from '../fire.js'
import firebase from 'firebase' import firebase from 'firebase'
import Loader from "react-loader-spinner";
export const ROOT_FOLDER = {name: 'Root', id : null , path : [] }; export const ROOT_FOLDER = {name: 'Root', id : null , path : [] };
...@@ -14,6 +12,7 @@ export function useFolder( folderId = null, folder= null) { ...@@ -14,6 +12,7 @@ export function useFolder( folderId = null, folder= null) {
SELECT_FOLDER : 'select-folder', SELECT_FOLDER : 'select-folder',
UPDATE_FOLDER : 'update-folder', UPDATE_FOLDER : 'update-folder',
SET_CHILD_FOLDERS : 'set_child_folders', SET_CHILD_FOLDERS : 'set_child_folders',
SET_CHILD_FILES: "set-child-files",
} }
function reducer( state, { type,payload } ){ function reducer( state, { type,payload } ){
...@@ -38,6 +37,11 @@ export function useFolder( folderId = null, folder= null) { ...@@ -38,6 +37,11 @@ export function useFolder( folderId = null, folder= null) {
...state, ...state,
childFolders : payload.childFolders, childFolders : payload.childFolders,
}; };
case ACTIONS.SET_CHILD_FILES:
return {
...state,
childFiles: payload.childFiles,
};
default: default:
return state; return state;
...@@ -85,15 +89,6 @@ export function useFolder( folderId = null, folder= null) { ...@@ -85,15 +89,6 @@ export function useFolder( folderId = null, folder= null) {
useEffect( () => { useEffect( () => {
if (!firebase.auth().currentUser) {
return <><div className='centered'><Loader
type="Puff"
color="#00BFFF"
height={100}
width={100}
timeout={3000} //3 secs
/></div></>
}
return database.folders return database.folders
.where("parentId", "==" ,folderId) .where("parentId", "==" ,folderId)
.where("userId","==", firebase.auth().currentUser.uid) .where("userId","==", firebase.auth().currentUser.uid)
...@@ -105,6 +100,18 @@ export function useFolder( folderId = null, folder= null) { ...@@ -105,6 +100,18 @@ export function useFolder( folderId = null, folder= null) {
}) })
}) })
},[folderId]) },[folderId])
useEffect(() => {
return (
database.files
.where("folderId", "==", folderId)
.where("userId", "==", firebase.auth().currentUser.uid)
.onSnapshot(snapshot => {
dispatch({
type: ACTIONS.SET_CHILD_FILES,
payload: { childFiles: snapshot.docs.map(database.formatDoc) },
})
})
)
}, [folderId])
return state; return state;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment