Skip to content

Commit

Permalink
feat: add PATCH member
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Jun 21, 2021
1 parent d9e460f commit b4226a8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 7 deletions.
18 changes: 16 additions & 2 deletions src/api/member.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { failOnError, DEFAULT_GET } from './utils';
import { failOnError, DEFAULT_GET, DEFAULT_PATCH } from './utils';
import {
buildGetMemberBy,
buildGetMember,
GET_CURRENT_MEMBER_ROUTE,
buildPatchMember,
} from './routes';
import { QueryClientConfig, UUID } from '../types';
import { Member, QueryClientConfig, UUID } from '../types';

export const getMemberBy = async (
{ email }: { email: string },
Expand Down Expand Up @@ -35,3 +36,16 @@ export const getCurrentMember = async ({ API_HOST }: QueryClientConfig) => {

return res.json();
};

export const editMember = async (
payload: { id: UUID; member: Partial<Member> },
{ API_HOST }: QueryClientConfig,
) => {
const { id } = payload;
const res = await fetch(`${API_HOST}/${buildPatchMember(id)}`, {
...DEFAULT_PATCH,
body: JSON.stringify(payload),
}).then(failOnError);

return res.json();
};
1 change: 1 addition & 0 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const MEMBERS_ROUTE = `members`;
export const buildGetMemberBy = (email: string) =>
`${MEMBERS_ROUTE}?email=${email}`;
export const buildGetMember = (id: UUID) => `${MEMBERS_ROUTE}/${id}`;
export const buildPatchMember = (id: UUID) => `${MEMBERS_ROUTE}/${id}`;
export const buildUploadFilesRoute = (parentId: UUID) =>
parentId
? `${ITEMS_ROUTE}/upload?parentId=${parentId}`
Expand Down
1 change: 1 addition & 0 deletions src/config/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ export const MUTATION_KEYS = {
DELETE_ITEM_TAG: 'deleteItemTag',
POST_ITEM_TAG: 'postItemTags',
PUT_ITEM_LOGIN: 'putItemLogin',
EDIT_MEMBER: 'editMember',
};
45 changes: 41 additions & 4 deletions src/mutations/member.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { QueryClient } from 'react-query';
import { Map, Record } from 'immutable';
import * as Api from '../api';
import { signOutRoutine } from '../routines';
import { editMemberRoutine, signOutRoutine } from '../routines';
import { CURRENT_MEMBER_KEY, MUTATION_KEYS } from '../config/keys';
import { QueryClientConfig } from '../types';
import { Member, QueryClientConfig } from '../types';

export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => {
const { notifier } = queryConfig;
Expand All @@ -14,13 +15,13 @@ export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => {
await queryClient.cancelQueries(CURRENT_MEMBER_KEY);

// Snapshot the previous value
const previousItems = queryClient.getQueryData(CURRENT_MEMBER_KEY);
const previousMember = queryClient.getQueryData(CURRENT_MEMBER_KEY);

// Optimistically update to the new value
queryClient.setQueryData(CURRENT_MEMBER_KEY, null);

// Return a context object with the snapshotted value
return { previousItems };
return { previousMember };
},
onSuccess: () => {
notifier?.({ type: signOutRoutine.SUCCESS });
Expand All @@ -35,4 +36,40 @@ export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => {
queryClient.resetQueries();
},
});

// suppose you can only edit yourself
queryClient.setMutationDefaults(MUTATION_KEYS.EDIT_MEMBER, {
mutationFn: (payload) =>
Api.editMember(payload, queryConfig).then((member) => Map(member)),
onMutate: async (payload) => {
// Cancel any outgoing refetches (so they don't overwrite our optimistic update)
await queryClient.cancelQueries(CURRENT_MEMBER_KEY);

// Snapshot the previous value
const previousMember = queryClient.getQueryData(
CURRENT_MEMBER_KEY,
) as Record<Member>;

// Optimistically update to the new value
queryClient.setQueryData(
CURRENT_MEMBER_KEY,
previousMember.merge(payload),
);

// Return a context object with the snapshotted value
return { previousMember };
},
onSuccess: () => {
notifier?.({ type: editMemberRoutine.SUCCESS });
},
// If the mutation fails, use the context returned from onMutate to roll back
onError: (error) => {
notifier?.({ type: editMemberRoutine.FAILURE, payload: { error } });
},
// Always refetch after error or success:
onSettled: () => {
// invalidate all queries
queryClient.invalidateQueries(CURRENT_MEMBER_KEY);
},
});
};
2 changes: 1 addition & 1 deletion src/routines/member.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createRoutine from './utils';

// eslint-disable-next-line import/prefer-default-export
export const signOutRoutine = createRoutine('SIGN_OUT');
export const editMemberRoutine = createRoutine('EDIT_MEMBER');
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export type Item = {
extra: {};
};

export type Member = {
id: UUID;
name: string;
email: string;
extra: {};
};

export type ExtendedItem = Item & {
parentId: UUID;
};
Expand Down

0 comments on commit b4226a8

Please sign in to comment.