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

Refactor AppRoute and related components to use redux hooks #4348

Merged
merged 3 commits into from
Nov 30, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 12 additions & 15 deletions app/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@ type Props = {
connectedHistory: History & { listenObject: boolean };
};

const Root = (props: Props) => {
const { store, connectedHistory, ...restProps } = props;
return (
<HelmetProvider>
<Provider store={store}>
<ThemeContextListener />
<ErrorBoundary openReportDialog>
<Router history={connectedHistory}>
<RouteConfig {...restProps} />
</Router>
</ErrorBoundary>
</Provider>
</HelmetProvider>
);
};
const Root = ({ store, connectedHistory }: Props) => (
<HelmetProvider>
<Provider store={store}>
<ThemeContextListener />
<ErrorBoundary openReportDialog>
<Router history={connectedHistory}>
<RouteConfig />
</Router>
</ErrorBoundary>
</Provider>
</HelmetProvider>
);

export default Root;
7 changes: 3 additions & 4 deletions app/actions/FeedActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { feedActivitySchema } from 'app/reducers';
import { feedIdByUserId } from 'app/reducers/feeds';
import { Feed } from './ActionTypes';
import callAPI from './callAPI';
import type { Thunk } from 'app/types';

export function fetchUserFeed(userId: string): Thunk<any> {
export function fetchUserFeed(userId: string) {
return callAPI({
types: Feed.FETCH,
endpoint: `/feed-user/${userId}/`,
Expand All @@ -14,7 +13,7 @@ export function fetchUserFeed(userId: string): Thunk<any> {
},
});
}
export function fetchPersonalFeed(): Thunk<any> {
export function fetchPersonalFeed() {
return callAPI({
types: Feed.FETCH,
endpoint: '/feed-personal/',
Expand All @@ -24,7 +23,7 @@ export function fetchPersonalFeed(): Thunk<any> {
},
});
}
export function fetchNotificationFeed(): Thunk<any> {
export function fetchNotificationFeed() {
return callAPI({
types: Feed.FETCH,
endpoint: '/feed-notifications/',
Expand Down
87 changes: 35 additions & 52 deletions app/actions/GroupActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ import { push } from 'redux-first-history';
import callAPI from 'app/actions/callAPI';
import { groupSchema, membershipSchema } from 'app/reducers';
import { Group, Membership } from './ActionTypes';
import type { AppDispatch } from 'app/store/createStore';
import type { ID } from 'app/store/models';
import type MembershipType from 'app/store/models/Membership';
import type { Thunk } from 'app/types';
import type { RoleType } from 'app/utils/constants';

export type AddMemberArgs = {
groupId: ID;
userId: ID;
role: RoleType;
};
export function addMember({
groupId,
userId,
role,
}: AddMemberArgs): Thunk<any> {
export function addMember({ groupId, userId, role }: AddMemberArgs) {
return callAPI({
types: Membership.CREATE,
endpoint: `/groups/${groupId}/memberships/`,
Expand All @@ -33,7 +29,7 @@ export function addMember({
},
});
}
export function removeMember(membership: MembershipType): Thunk<any> {
export function removeMember(membership: MembershipType) {
return callAPI({
types: Membership.REMOVE,
endpoint: `/groups/${membership.abakusGroup}/memberships/${membership.id}/`,
Expand All @@ -46,18 +42,18 @@ export function removeMember(membership: MembershipType): Thunk<any> {
},
});
}
export function fetchGroup(groupId: ID): Thunk<any> {
export function fetchGroup(groupId: ID, { propagateError = true } = {}) {
return callAPI({
types: Group.FETCH,
endpoint: `/groups/${groupId}/`,
schema: groupSchema,
meta: {
errorMessage: 'Henting av gruppe feilet',
},
propagateError: true,
propagateError,
});
}
export function fetchAll(): Thunk<any> {
export function fetchAll() {
return callAPI({
types: Group.FETCH,
endpoint: '/groups/',
Expand All @@ -68,7 +64,7 @@ export function fetchAll(): Thunk<any> {
propagateError: true,
});
}
export function fetchAllWithType(type: string): Thunk<any> {
export function fetchAllWithType(type: string) {
return callAPI({
types: Group.FETCH,
endpoint: `/groups/?type=${type}`,
Expand All @@ -79,8 +75,8 @@ export function fetchAllWithType(type: string): Thunk<any> {
propagateError: true,
});
}
export function editGroup(group: Record<string, any>): Thunk<any> {
return (dispatch) =>
export function editGroup(group: Record<string, any>) {
return (dispatch: AppDispatch) =>
dispatch(
callAPI({
types: Group.UPDATE,
Expand Down Expand Up @@ -110,8 +106,8 @@ export function joinGroup(
groupId: number,
user: Record<string, any>,
role = 'member'
): Thunk<any> {
return (dispatch) =>
) {
return (dispatch: AppDispatch) =>
dispatch(
callAPI({
types: Membership.JOIN_GROUP,
Expand All @@ -134,11 +130,8 @@ export function joinGroup(
return dispatch(fetchMemberships(groupId));
});
}
export function leaveGroup(
membership: Record<string, any>,
groupId: number
): Thunk<any> {
return (dispatch) => {
export function leaveGroup(membership: Record<string, any>, groupId: number) {
return (dispatch: AppDispatch) => {
return dispatch(
callAPI({
types: Membership.LEAVE_GROUP,
Expand All @@ -157,11 +150,8 @@ export function leaveGroup(
});
};
}
export function fetchAllMemberships(
groupId: number,
descendants = false
): Thunk<any> {
return (dispatch) => {
export function fetchAllMemberships(groupId: number, descendants = false) {
return (dispatch: AppDispatch) => {
return dispatch(
fetchMembershipsPagination({
descendants,
Expand All @@ -178,7 +168,7 @@ export function fetchMemberships(
groupId: number,
descendants = false,
query: Record<string, any> = {}
): Thunk<any> {
) {
return fetchMembershipsPagination({
groupId,
descendants,
Expand All @@ -196,28 +186,24 @@ export function fetchMembershipsPagination({
next: boolean;
descendants: boolean;
query?: Record<string, string | number | boolean>;
}): Thunk<any> {
return (dispatch) => {
return dispatch(
callAPI({
types: Group.MEMBERSHIP_FETCH,
endpoint: `/groups/${groupId}/memberships/`,
schema: [membershipSchema],
pagination: {
fetchNext: next,
},
query: { ...query, descendants },
meta: {
groupId: groupId,
errorMessage: 'Henting av medlemmene for gruppen feilet',
},
propagateError: true,
})
);
};
}) {
return callAPI({
types: Group.MEMBERSHIP_FETCH,
endpoint: `/groups/${groupId}/memberships/`,
schema: [membershipSchema],
pagination: {
fetchNext: next,
},
query: { ...query, descendants },
meta: {
groupId: groupId,
errorMessage: 'Henting av medlemmene for gruppen feilet',
},
propagateError: true,
});
}
export function createGroup(group: Record<string, any>): Thunk<any> {
return (dispatch) => {
export function createGroup(group: Record<string, any>) {
return (dispatch: AppDispatch) => {
const { name, description, text, logo, type, showBadge, active } = group;
return dispatch(
callAPI({
Expand Down Expand Up @@ -256,11 +242,8 @@ export function createGroup(group: Record<string, any>): Thunk<any> {
});
};
}
export function removeGroup(
id: string,
group: Record<string, any>
): Thunk<any> {
return (dispatch) =>
export function removeGroup(id: string, group: Record<string, any>) {
return (dispatch: AppDispatch) =>
dispatch(
callAPI({
types: Group.REMOVE,
Expand Down
10 changes: 4 additions & 6 deletions app/actions/SearchActions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { createAction } from '@reduxjs/toolkit';
import callAPI from 'app/actions/callAPI';
import { selectAutocomplete } from 'app/reducers/search';
import { Search } from './ActionTypes';
import type { Action, Thunk } from 'app/types';
import type { Thunk } from 'app/types';

export const toggleSearch = createAction(Search.TOGGLE_OPEN);

export function toggleSearch(): Action {
return {
type: Search.TOGGLE_OPEN,
};
}
export function autocomplete(
query: string,
filter?: Array<string>
Expand Down
12 changes: 12 additions & 0 deletions app/actions/UserActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,15 @@ export function resetPassword({
},
});
}

export function updateUserTheme(username: string, theme: 'light' | 'dark') {
return updateUser(
{
username,
selectedTheme: theme,
},
{
noRedirect: true,
}
);
}