Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Apply filters to Categories as well #30

Merged
merged 1 commit into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!
}
`);