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

fix: dispatch map #310

Merged
merged 1 commit into from
Feb 28, 2024
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
8 changes: 4 additions & 4 deletions src/project/projectSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ export const removeSiteFromAllProjects = createAction<string>(
'project/removeSiteFromAllProjects',
);

const updateDispatchMap = {
const updateDispatchMap = () => ({
project: (project: Project) => updateProjects({ [project.id]: project }),
sites: updateSites,
users: updateUsers,
soilSettings: updateProjectSettings,
};
});

export const fetchProject = createAsyncThunk(
'project/fetchProject',
Expand All @@ -183,12 +183,12 @@ export const fetchProject = createAsyncThunk(

export const fetchProjectsForUser = createAsyncThunk(
'project/fetchProjectsForUser',
dispatchByKeys(projectService.fetchProjectsForUser, {
dispatchByKeys(projectService.fetchProjectsForUser, () => ({
projects: setProjects,
sites: setSites,
users: setUsers,
soilSettings: updateProjectSettings,
}),
})),
);

export const addProject = createAsyncThunk(
Expand Down
4 changes: 2 additions & 2 deletions src/soilId/soilIdSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ export const {

export const fetchSoilDataForUser = createAsyncThunk(
'soilId/fetchSoilDataForUser',
dispatchByKeys(soilIdService.fetchSoilDataForUser, {
dispatchByKeys(soilIdService.fetchSoilDataForUser, () => ({
projects: setProjects,
sites: setSites,
projectSoilSettings: setProjectSettings,
soilData: setSoilData,
users: setUsers,
}),
})),
);

export const updateSoilData = createAsyncThunk(
Expand Down
20 changes: 11 additions & 9 deletions src/store/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type {
SharedDispatch,
SharedState,
} from 'terraso-client-shared/store/store';
import { entries, fromEntries } from 'terraso-client-shared/utils';

export type SerializableSet = Record<string, boolean>;

Expand Down Expand Up @@ -181,18 +182,19 @@ type DispatchMap<Result> = {

export const dispatchByKeys = <T extends object, ThunkArg>(
fetcher: CreateAsyncThunkParams<T, ThunkArg>,
dispatchMap: DispatchMap<T>,
dispatchMapFn: (_: T) => DispatchMap<T>,
): CreateAsyncThunkParams<T, ThunkArg> => {
return async (arg, currentUser, thunkAPI) => {
return Object.fromEntries(
const result = await fetcher(arg, currentUser, thunkAPI);
const dispatchMap = dispatchMapFn(result);

return fromEntries(
await Promise.all(
Object.entries(await fetcher(arg, currentUser, thunkAPI)).map(
async ([key, result]) => {
await thunkAPI.dispatch(dispatchMap[key as keyof T](result));
return [key, result];
},
),
entries<keyof T, T[keyof T]>(result).map(async ([key, result]) => {
await thunkAPI.dispatch(dispatchMap[key](result));
return [key, result];
}),
),
);
) as T;
};
};
10 changes: 6 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

export const fromEntries = <K extends string | number, V>(entries: [K, V][]) =>
Object.fromEntries(entries) as Record<K, V>;
export const fromEntries = <K extends string | number | symbol, V>(
entries: [K, V][],
) => Object.fromEntries(entries) as Record<K, V>;

export const entries = <K extends string | number, V>(object: Record<K, V>) =>
Object.entries(object) as [K, V][];
export const entries = <K extends string | number | symbol, V>(
object: Record<K, V>,
) => Object.entries(object) as [K, V][];

export const filterValues = <T>(obj: Record<any, T>, f: (arg: T) => boolean) =>
Object.values(obj).filter(f);
Expand Down
Loading