Skip to content

Commit

Permalink
Add filter to aplu
Browse files Browse the repository at this point in the history
  • Loading branch information
pacoccino committed Mar 1, 2022
1 parent dca1220 commit a3456aa
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 46 deletions.
2 changes: 1 addition & 1 deletion api/src/graphql/aplu.sdl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const schema = gql`
type Query {
arbo: JSONObject! @requireAuth
arbo(filter: ImageFilters): JSONObject! @requireAuth
}
type ArboResponse {
Expand Down
23 changes: 21 additions & 2 deletions api/src/services/aplu/aplu.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { arbo } from './aplu'
import { ArboPath, ArboDate } from 'types/graphql'
import moment from 'moment'

type NewArbo = {
path: string | number
Expand Down Expand Up @@ -47,7 +48,7 @@ describe('aplu', () => {

expect(rootO.children[2001].path).toEqual(2001)
expect(rootO.children[2001].date).toEqual(
new Date(2001, 0, 1).toISOString()
moment.utc([2001, 0, 1]).toISOString()
)
expect(rootO.children[2001].count).toEqual(3)
expect(Object.keys(rootO.children[2001].children).length).toEqual(2)
Expand All @@ -56,11 +57,29 @@ describe('aplu', () => {
expect(rootO.children[2001].children[1].count).toEqual(1)
expect(rootO.children[2001].children[1].children[2].count).toEqual(1)
expect(rootO.children[2001].children[1].children[2].date).toEqual(
new Date(2001, 1, 2).toISOString()
moment.utc([2001, 1, 2]).toISOString()
)
expect(rootO.children[2002].count).toEqual(1)
expect(rootO.children[2002].children[0].count).toEqual(1)
expect(rootO.children[2004].count).toEqual(2)
expect(rootO.children[2004].children[2].count).toEqual(2)
})

scenario('arbo with filter', async () => {
const filter = {
dateRange: {
from: moment.utc([2001, 0, 1]).toISOString(),
to: moment.utc([2002, 0, 1]).toISOString(),
},
}
const root = await arbo({ filter })
const rootO = arboToObject<ArboDate>(root.arboDate)

expect(rootO.count).toEqual(3)
expect(Object.keys(rootO.children).length).toEqual(1)
expect(Object.keys(rootO.children[2001].children).length).toEqual(2)
expect(rootO.children[2001].count).toEqual(3)
expect(rootO.children[2001].children[0].count).toEqual(2)
expect(rootO.children[2001].children[1].count).toEqual(1)
})
})
30 changes: 22 additions & 8 deletions api/src/services/aplu/aplu.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import type { ArboResponse, ArboPath, ArboDate } from 'types/graphql'
import { db } from 'src/lib/db'
import type {
ArboResponse,
ArboPath,
ArboDate,
QueryarboArgs,
} from 'types/graphql'
import { images } from 'src/services/images'
import _ from 'lodash'
import S3Path from 'src/lib/files/S3Path'
import moment from 'moment'

export const arbo = async (): Promise<ArboResponse> => {
// Advanced Picture Look Up

export const arbo = async ({
filter,
}: QueryarboArgs = {}): Promise<ArboResponse> => {
const arboPath: ArboPath = {
path: '/',
count: 0,
Expand All @@ -17,12 +26,17 @@ export const arbo = async (): Promise<ArboResponse> => {
children: [],
}

const allImages = await db.image.findMany({
select: {
path: true,
dateTaken: true,
const allImages = await images(
{
filter,
},
})
{
select: {
path: true,
dateTaken: true,
},
}
)
arboPath.count = allImages.length
arboDate.count = allImages.length

Expand Down
18 changes: 11 additions & 7 deletions api/src/services/images/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ export const Image = {
db.image.findUnique({ where: { id: root.id } }).tagsOnImages(),
}

export const images = ({
filter,
take,
skip,
sorting,
cursor,
}: QueryimagesArgs): Promise<PImage[]> => {
interface ImagesOpts {
select?: Prisma.ImageSelect
}

export const images = (
{ filter, take, skip, sorting, cursor }: QueryimagesArgs,
opts: ImagesOpts = {}
): Promise<PImage[]> => {
const query: Prisma.ImageFindManyArgs = {
orderBy: new Array<Prisma.ImageOrderByWithRelationInput>(),
}
Expand All @@ -46,6 +47,9 @@ export const images = ({
if (skip !== undefined) {
query.skip = skip
}
if (opts.select) {
query.select = opts.select
}

if (cursor) {
query.cursor = {
Expand Down
43 changes: 19 additions & 24 deletions web/src/components/Filter/Arbo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export const ArboPath = () => {
const { filter, setPath } = useFilterContext()
const { apluQuery } = useApluContext()

if (apluQuery.loading) {
if (!apluQuery.previousData && apluQuery.loading) {
return <DefaultSpinner />
}
const arbo = apluQuery.data.arbo.arboPath
const arbo = (apluQuery.data || apluQuery.previousData).arbo.arboPath

const selectPath = (paths) => {
const pathToSelect = paths.slice(1).join('/')
Expand All @@ -31,26 +31,21 @@ export const ArboPath = () => {
const selectedPath = ['/'].concat(S3Path.splitPath(filter.path || ''))

return (
<Box>
<Text align="center" mb={1}>
Folders
</Text>
<Box
maxHeight="300px"
overflowY="scroll"
px={1}
borderWidth={1}
borderColor="gray.600"
bg="gray.600"
borderRadius="md"
>
<Tree
tree={arbo}
selectedPath={selectedPath}
onSelect={selectPath}
formatPath={formatPath}
/>
</Box>
<Box
maxHeight="300px"
overflowY="scroll"
px={1}
borderWidth={1}
borderColor="gray.600"
bg="gray.800"
borderRadius="md"
>
<Tree
tree={arbo}
selectedPath={selectedPath}
onSelect={selectPath}
formatPath={formatPath}
/>
</Box>
)
}
Expand Down Expand Up @@ -82,10 +77,10 @@ export const ArboDate = () => {
return [0]
}, [dateRange])

if (apluQuery.loading) {
if (!apluQuery.previousData && apluQuery.loading) {
return <DefaultSpinner />
}
const arbo = apluQuery.data.arbo.arboDate
const arbo = (apluQuery.data || apluQuery.previousData).arbo.arboDate

const selectDate = (paths, tree) => {
if (paths.length === 1) {
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/Filter/PathPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export const PathPanel = () => {
return (
<>
<ArboPath />
<Box mt={2} />
<PathSearch />
{/*<Box mt={2} />*/}
{/*<PathSearch />*/}
</>
)
}
12 changes: 10 additions & 2 deletions web/src/contexts/aplu.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useContext } from 'react'
import { useQuery } from '@redwoodjs/web'
import { ArboDate, ArboPath } from 'api/types/graphql'
import { useFilterContext } from 'src/contexts/filter'

export const QUERY = gql`
query APLU {
arbo
query APLU($filter: ImageFilters) {
arbo(filter: $filter)
}
`

Expand All @@ -19,11 +20,18 @@ export const ApluContext = React.createContext<ApluContextType>({
})

export const ApluContextProvider = ({ children }) => {
const { filter } = useFilterContext()

const apluQuery = useQuery(QUERY, {
fetchPolicy: 'cache-first',
notifyOnNetworkStatusChange: true,
variables: {
filter,
},
})

console.log(apluQuery)

return (
<ApluContext.Provider
value={{
Expand Down

0 comments on commit a3456aa

Please sign in to comment.