Skip to content

Commit

Permalink
fix: fix server fetching of material list in product form
Browse files Browse the repository at this point in the history
  • Loading branch information
mnindrazaka committed Oct 28, 2024
1 parent a4bb61a commit d6f1be4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
34 changes: 29 additions & 5 deletions libs/ui/src/app/ProductCreateScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// eslint-disable-next-line @nx/enforce-module-boundaries
import { categoryList, categoryListQueryKey } from '../../../api-contract/src';
import {
categoryList,
categoryListQueryKey,
materialList,
materialListQueryKey,
} from '../../../api-contract/src';
import {
OpenAPICategoryRepository,
OpenAPIMaterialRepository,
Expand All @@ -20,10 +25,29 @@ import {

export async function getProductCreateScreenDehydratedState(): Promise<DehydratedState> {
const queryClient = new QueryClient();
await queryClient.prefetchQuery({
queryKey: categoryListQueryKey(),
queryFn: () => categoryList(),
});
await Promise.all([
queryClient.prefetchQuery({
queryKey: categoryListQueryKey(),
queryFn: () => categoryList(),
}),
queryClient.prefetchQuery({
queryKey: materialListQueryKey({
limit: 8,
skip: 0,
order: 'desc',
sortBy: 'created_at',
query: '',
}),
queryFn: () =>
materialList({
limit: 8,
skip: 0,
order: 'desc',
sortBy: 'created_at',
query: '',
}),
}),
]);

return dehydrate(queryClient);
}
Expand Down
19 changes: 19 additions & 0 deletions libs/ui/src/app/ProductUpdateScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { createParam } from 'solito';
import {
categoryList,
categoryListQueryKey,
materialList,
materialListQueryKey,
productFindById,
productFindByIdQueryKey,
} from '../../../api-contract/src';
Expand Down Expand Up @@ -38,6 +40,23 @@ export async function getProductUpdateScreenDehydratedState(
queryKey: categoryListQueryKey(),
queryFn: () => categoryList(),
}),
queryClient.prefetchQuery({
queryKey: materialListQueryKey({
limit: 8,
skip: 0,
order: 'desc',
sortBy: 'created_at',
query: '',
}),
queryFn: () =>
materialList({
limit: 8,
skip: 0,
order: 'desc',
sortBy: 'created_at',
query: '',
}),
}),
]);

return dehydrate(queryClient);
Expand Down
7 changes: 0 additions & 7 deletions libs/ui/src/data/openApi/material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,6 @@ export class OpenAPIMaterialRepository implements MaterialRepository {
this.client.removeQueries({ queryKey: materialListQueryKey(params) });

return {
type: res?.data ? 'loaded' : 'idle',
page: 1,
itemPerPage: 8,
query: '',
sortBy: 'created_at',
orderBy: 'desc',
errorMessage: null,
materials: res?.data.map(transformers.material) ?? [],
totalItem: res?.meta.total ?? 0,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const Context = createContext<ContextValue>(null);

export const useMaterialListController = () => {
const materialListController = useContext(Context);
console.log('materialListController', materialListController);
if (materialListController === null) {
throw new Error('useMaterialListController is called outside provider');
}
Expand All @@ -30,6 +29,5 @@ export const MaterialListProvider = ({
usecase,
}: MaterialListProviderProps) => {
const controller = useController(usecase);
console.log('controller', controller);
return <Context.Provider value={controller}>{children}</Context.Provider>;
};

0 comments on commit d6f1be4

Please sign in to comment.