Skip to content

Commit

Permalink
feat: Apply filters to Categories as well (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
deniaz authored Apr 4, 2020
1 parent 72a313c commit 6f1cfe7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 30 deletions.
1 change: 0 additions & 1 deletion .prettierrc

This file was deleted.

3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('@smartive/prettier-config'),
}
8 changes: 5 additions & 3 deletions src/compositions/search-with-results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ const getVendors = `query Vendors($service: [Service!], $zip: Int, $tenants: [Te
order
contact
}
categories(filter:{service: $service, tenants: $tenants, zip: $zip})
total
}`;

type Props = {
query: ParsedUrlQuery;
categories?: string[];
selectedCategory?: string;
};

export const SearchWithResults: FC<Props> = ({ query, categories, selectedCategory }) => {
export const SearchWithResults: FC<Props> = ({ query, selectedCategory }) => {
const tenants = query['tenants'] && query['tenants'];

const [service, setService] = useState<'DELIVERY' | 'TAKEAWAY'>('TAKEAWAY');
Expand All @@ -64,7 +64,7 @@ export const SearchWithResults: FC<Props> = ({ query, categories, selectedCatego
}
}, [postcode]);

const { pages, isLoadingMore, isReachingEnd, loadMore } = useSWRPages(
const { pages, isLoadingMore, pageSWRs, isReachingEnd, loadMore } = useSWRPages(
'results',
({ offset, withSWR }) => {
const variables = useMemo(() => {
Expand Down Expand Up @@ -113,6 +113,8 @@ export const SearchWithResults: FC<Props> = ({ query, categories, selectedCatego
[zip, service, tenants, categoryFilter]
);

const [page] = pageSWRs;
const categories = page?.data?.categories;
return (
<>
<Search onChange={(zip) => setZip(zip)} onToggle={(service) => setService(service)} service={service} zip={zip} />
Expand Down
11 changes: 3 additions & 8 deletions src/pages/iframe-suche.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { SearchWithResults } from '../compositions/search-with-results';
import { getClient } from '../services/mongo';

export async function getServerSideProps(context) {
const client = await getClient();
const categories = await client.db('shops').collection('shops').distinct('categories');
client.close();

return {
props: { query: context.query, categories },
props: { query: context.query },
};
}

export default ({ query, categories }) => {
return <SearchWithResults categories={categories} query={query} />;
export default ({ query }) => {
return <SearchWithResults query={query} />;
};
15 changes: 2 additions & 13 deletions src/pages/suche.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
import { useRouter } from 'next/router';
import { SearchWithResults } from '../compositions/search-with-results';
import { Stacked } from '../layout/stacked';
import { getClient } from '../services/mongo';

export async function getServerSideProps() {
const client = await getClient();
const categories = await client.db('shops').collection('shops').distinct('categories');
client.close();

return {
props: { categories },
};
}

export default ({ categories }) => {
export default () => {
const { query } = useRouter();
return (
<Stacked title="Anbieter und Angebote - Emma bringts!">
<SearchWithResults categories={categories} query={query} />
<SearchWithResults query={query} />
</Stacked>
);
};
8 changes: 6 additions & 2 deletions src/resolvers/vendors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export const vendors = {
}));
},
total: async (_args, { client }: Context): Promise<number> => client.db('shops').collection('shops').countDocuments(),
categories: async (_args, { client }: Context): Promise<string[]> =>
client.db('shops').collection('shops').distinct('categories'),
categories: async ({ filter = {} }: VendorInput, { client }: Context): Promise<string[]> => {
const query = await buildQuery(filter);
const categories = await client.db('shops').collection('shops').distinct('categories', query);

return categories;
},
};
4 changes: 1 addition & 3 deletions src/schemas/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,13 @@ export const schema = buildSchema(`
contact: [String]!
}
type Mutation {
createVendor(vendor: VendorInput!): Vendor!
}
type Query {
vendors(filter: VendorFilterInput, skip: Int, limit: Int): [Vendor]!
categories: [String]!
categories(filter: VendorFilterInput): [String]!
total: Int!
}
`);

1 comment on commit 6f1cfe7

@vercel
Copy link

@vercel vercel bot commented on 6f1cfe7 Apr 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.