Skip to content

Commit

Permalink
Get users folders with single API call
Browse files Browse the repository at this point in the history
  • Loading branch information
saulipurhonen committed Feb 8, 2021
1 parent 766de1a commit 2d7ff8c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
9 changes: 7 additions & 2 deletions src/services/folderAPI.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//@flow
import { create } from "apisauce"

import { errorMonitor } from "./errorMonitor"
// import { errorMonitor } from "./errorMonitor"

const api = create({ baseURL: "/folders" })

api.addMonitor(errorMonitor)
// api.addMonitor(errorMonitor)

const createNewFolder = async (folder: any) => {
return await api.post(null, folder)
Expand All @@ -22,9 +22,14 @@ const deleteFolderById = async (folderId: string) => {
return await api.delete(`/${folderId}`)
}

const getFolders = async () => {
return await api.get()
}

export default {
createNewFolder,
getFolderById,
patchFolderById,
deleteFolderById,
getFolders,
}
35 changes: 15 additions & 20 deletions src/views/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const useStyles = makeStyles(theme => ({
const Home = () => {
const dispatch = useDispatch()
const user = useSelector(state => state.user)
const folderIds = user.folders

const unpublishedFolders = useSelector(state => state.unpublishedFolders)
const publishedFolders = useSelector(state => state.publishedFolders)
Expand All @@ -55,28 +54,24 @@ const Home = () => {
}, [])

useEffect(() => {
if (folderIds) {
const unpublishedArr = []
const publishedArr = []
// Handle fetching details of all folders belong to current user
const fetchFolders = async () => {
for (let i = 0; i < folderIds.length; i += 1) {
const response = await folderAPIService.getFolderById(folderIds[i])
if (response.ok) {
response.data.published ? publishedArr.push(response.data) : unpublishedArr.push(response.data)
} else {
setConnError(true)
setResponseError(response)
setErrorPrefix("Fetching folders error.")
}
}
dispatch(setUnpublishedFolders(unpublishedArr))
dispatch(setPublishedFolders(publishedArr))
let isMounted = true
const getFolders = async () => {
const response = await folderAPIService.getFolders()
if (response.ok && isMounted) {
dispatch(setUnpublishedFolders(response.data.folders.filter(folder => folder.published === false)))
dispatch(setPublishedFolders(response.data.folders.filter(folder => folder.published === true)))
setFetchingFolders(false)
} else {
setConnError(true)
setResponseError(response)
setErrorPrefix("Fetching folders error.")
}
fetchFolders()
}
}, [folderIds?.length])
getFolders()
return () => {
isMounted = false
}
}, [])

// Handle fetching details of all objects (drafts + metadata) of the clicked folder
const handleClickFolder = async (currentFolderId: string, folderType: string) => {
Expand Down

0 comments on commit 2d7ff8c

Please sign in to comment.