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: add geolocation hooks and mutations #590

Merged
merged 14 commits into from
Jan 29, 2024
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
"Alexandre Chau"
],
"dependencies": {
"@graasp/sdk": "3.5.0",
"@graasp/translations": "1.22.1",
"@graasp/sdk": "3.6.0",
"@graasp/translations": "github:graasp/graasp-translations#geolocation",
"axios": "0.27.2",
"crypto-js": "4.2.0",
"date-fns": "3.3.1",
"http-status-codes": "2.3.0",
"qs": "6.11.2",
"react-query": "3.39.3",
Expand Down
1 change: 1 addition & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from './search';
export * from './subscription';
export * from './publicProfile';
export * from './shortLink';
export * from './itemGeolocation';
16 changes: 14 additions & 2 deletions src/api/item.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
DiscriminatedItem,
ItemGeolocation,
RecycledItemData,
ResultOf,
UUID,
Expand Down Expand Up @@ -77,11 +78,21 @@ export const getAccessibleItems = async (
export type PostItemPayloadType = Partial<DiscriminatedItem> &
Pick<DiscriminatedItem, 'type' | 'name'> & {
parentId?: UUID;
} & {
geolocation?: Pick<ItemGeolocation, 'lat' | 'lng'>;
};
// payload = {name, type, description, extra}

// payload = {name, type, description, extra, geolocation}
// querystring = {parentId}
export const postItem = async (
{ name, type, description, extra, parentId }: PostItemPayloadType,
{
name,
type,
description,
extra,
parentId,
geolocation,
}: PostItemPayloadType,
{ API_HOST, axios }: PartialQueryConfigForApi,
) =>
verifyAuthentication(() =>
Expand All @@ -91,6 +102,7 @@ export const postItem = async (
type,
description,
extra,
geolocation,
})
.then(({ data }) => data),
);
Expand Down
62 changes: 62 additions & 0 deletions src/api/itemGeolocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Item, ItemGeolocation, UUID } from '@graasp/sdk';

import { PartialQueryConfigForApi } from '../types';
import { verifyAuthentication } from './axios';
import {
buildDeleteItemGeolocationRoute,
buildGetItemGeolocationRoute,
buildGetItemsInMapRoute,
buildPutItemGeolocationRoute,
} from './routes';

// eslint-disable-next-line import/prefer-default-export
export const getItemGeolocation = async (
{ API_HOST, axios }: PartialQueryConfigForApi,
id: UUID,
) =>
axios
.get<ItemGeolocation>(`${API_HOST}/${buildGetItemGeolocationRoute(id)}`)
.then(({ data }) => data);

export const putItemGeolocation = async (
payload: {
itemId: Item['id'];
geolocation: Pick<ItemGeolocation, 'lat' | 'lng'>;
},
{ API_HOST, axios }: PartialQueryConfigForApi,
) =>
verifyAuthentication(() =>
axios
.put<void>(
`${API_HOST}/${buildPutItemGeolocationRoute(payload.itemId)}`,
payload,
)
.then(({ data }) => data),
);

export const getItemsInMap = async (
payload: {
lat1: ItemGeolocation['lat'];
lat2: ItemGeolocation['lat'];
lng1: ItemGeolocation['lng'];
lng2: ItemGeolocation['lng'];
spaenleh marked this conversation as resolved.
Show resolved Hide resolved
},
{ API_HOST, axios }: PartialQueryConfigForApi,
) =>
verifyAuthentication(() =>
axios
.get<ItemGeolocation[]>(`${API_HOST}/${buildGetItemsInMapRoute(payload)}`)
.then(({ data }) => data),
);

export const deleteItemGeolocation = async (
payload: { itemId: UUID },
{ API_HOST, axios }: PartialQueryConfigForApi,
) =>
verifyAuthentication(() =>
axios
.delete<void>(
`${API_HOST}/${buildDeleteItemGeolocationRoute(payload.itemId)}`,
)
.then(({ data }) => data),
);
23 changes: 23 additions & 0 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,25 @@ export const buildPostShortLinkRoute = () => `${SHORT_LINKS_ROUTE}`;
export const buildPatchShortLinkRoute = (alias: string) =>
`${SHORT_LINKS_ROUTE}/${alias}`;

export const buildGetItemGeolocationRoute = (itemId: UUID) =>
`${ITEMS_ROUTE}/${itemId}/geolocation`;
export const buildPutItemGeolocationRoute = (itemId: UUID) =>
`${ITEMS_ROUTE}/${itemId}/geolocation`;
export const buildDeleteItemGeolocationRoute = (itemId: UUID) =>
`${ITEMS_ROUTE}/${itemId}/geolocation`;
export const buildGetItemsInMapRoute = ({
lat1,
lat2,
lng1,
lng2,
}: {
lat1: number;
lat2: number;
lng1: number;
lng2: number;
}) =>
`${ITEMS_ROUTE}/geolocation?lat1=${lat1}&lat2=${lat2}&lng1=${lng1}&lng2=${lng2}`;

export const API_ROUTES = {
APPS_ROUTE,
buildAppListRoute,
Expand Down Expand Up @@ -553,4 +572,8 @@ export const API_ROUTES = {
PUBLIC_PROFILE_ROUTE,
GET_OWN_PROFILE,
buildGetPublicProfileRoute,
buildGetItemsInMapRoute,
buildGetItemGeolocationRoute,
buildDeleteItemGeolocationRoute,
buildPutItemGeolocationRoute,
};
4 changes: 2 additions & 2 deletions src/config/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export class UserIsSignedOut extends Error {
}

export class UndefinedArgument extends Error {
constructor() {
constructor(data?: object) {
super();
this.message = 'UnexpectedInput';
this.message = `UnexpectedInput ${JSON.stringify(data ?? {})}`;
this.name = 'UnexpectedInput';
this.stack = new Error().stack;
}
Expand Down
24 changes: 24 additions & 0 deletions src/config/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,28 @@ export const CURRENT_MEMBER_STORAGE_KEY = [

export const OWN_PUBLIC_PROFILE_KEY = ['own-profile'];
export const buildPublicProfileKey = (memberId: UUID) => ['profile', memberId];

export const itemsWithGeolocationKeys = {
allBounds: [ITEMS_CONTEXT, 'map'],
inBounds: ({
lat1,
lat2,
lng1,
lng2,
}: {
lat1: number;
lat2: number;
lng1: number;
lng2: number;
}) => [...itemsWithGeolocationKeys.allBounds, { lat1, lat2, lng1, lng2 }],
};

export const buildItemGeolocationKey = (itemId?: UUID) => [
pyphilia marked this conversation as resolved.
Show resolved Hide resolved
ITEMS_CONTEXT,
itemId,
'geolocation',
];

export const DATA_KEYS = {
APPS_KEY,
buildItemKey,
Expand Down Expand Up @@ -320,4 +342,6 @@ export const DATA_KEYS = {
buildSearchPublishedItemsKey,
OWN_PUBLIC_PROFILE_KEY,
buildPublicProfileKey,
itemsWithGeolocationKeys,
buildItemGeolocationKey,
};
2 changes: 2 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import configureChatHooks from './chat';
import configureInvitationHooks from './invitation';
import configureItemHooks from './item';
import configureItemFavoriteHooks from './itemFavorite';
import configureItemGeolocationHooks from './itemGeolocation';
import configureItemLikeHooks from './itemLike';
import configureItemLoginHooks from './itemLogin';
import configureItemPublishedHooks from './itemPublish';
Expand Down Expand Up @@ -55,5 +56,6 @@ export default (
...configurePlanHooks(queryConfig),
...configurePublicProfileHooks(queryConfig),
...configureShortLinkHooks(queryConfig),
...configureItemGeolocationHooks(queryConfig),
};
};
Loading