Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit 48ed281

Browse files
authored
feat: add geolocation hooks and mutations (#590)
1 parent bc2d44b commit 48ed281

19 files changed

+769
-24
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
"Alexandre Chau"
1818
],
1919
"dependencies": {
20-
"@graasp/sdk": "3.5.0",
21-
"@graasp/translations": "1.22.1",
20+
"@graasp/sdk": "3.6.0",
21+
"@graasp/translations": "github:graasp/graasp-translations#geolocation",
2222
"axios": "0.27.2",
2323
"crypto-js": "4.2.0",
24+
"date-fns": "3.3.1",
2425
"http-status-codes": "2.3.0",
2526
"qs": "6.11.2",
2627
"react-query": "3.39.3",

src/api/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ export * from './search';
2121
export * from './subscription';
2222
export * from './publicProfile';
2323
export * from './shortLink';
24+
export * from './itemGeolocation';

src/api/item.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
DiscriminatedItem,
3+
ItemGeolocation,
34
RecycledItemData,
45
ResultOf,
56
UUID,
@@ -77,11 +78,21 @@ export const getAccessibleItems = async (
7778
export type PostItemPayloadType = Partial<DiscriminatedItem> &
7879
Pick<DiscriminatedItem, 'type' | 'name'> & {
7980
parentId?: UUID;
81+
} & {
82+
geolocation?: Pick<ItemGeolocation, 'lat' | 'lng'>;
8083
};
81-
// payload = {name, type, description, extra}
84+
85+
// payload = {name, type, description, extra, geolocation}
8286
// querystring = {parentId}
8387
export const postItem = async (
84-
{ name, type, description, extra, parentId }: PostItemPayloadType,
88+
{
89+
name,
90+
type,
91+
description,
92+
extra,
93+
parentId,
94+
geolocation,
95+
}: PostItemPayloadType,
8596
{ API_HOST, axios }: PartialQueryConfigForApi,
8697
) =>
8798
verifyAuthentication(() =>
@@ -91,6 +102,7 @@ export const postItem = async (
91102
type,
92103
description,
93104
extra,
105+
geolocation,
94106
})
95107
.then(({ data }) => data),
96108
);

src/api/itemGeolocation.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { Item, ItemGeolocation, UUID } from '@graasp/sdk';
2+
3+
import { PartialQueryConfigForApi } from '../types';
4+
import { verifyAuthentication } from './axios';
5+
import {
6+
buildDeleteItemGeolocationRoute,
7+
buildGetItemGeolocationRoute,
8+
buildGetItemsInMapRoute,
9+
buildPutItemGeolocationRoute,
10+
} from './routes';
11+
12+
// eslint-disable-next-line import/prefer-default-export
13+
export const getItemGeolocation = async (
14+
{ API_HOST, axios }: PartialQueryConfigForApi,
15+
id: UUID,
16+
) =>
17+
axios
18+
.get<ItemGeolocation>(`${API_HOST}/${buildGetItemGeolocationRoute(id)}`)
19+
.then(({ data }) => data);
20+
21+
export const putItemGeolocation = async (
22+
payload: {
23+
itemId: Item['id'];
24+
geolocation: Pick<ItemGeolocation, 'lat' | 'lng'>;
25+
},
26+
{ API_HOST, axios }: PartialQueryConfigForApi,
27+
) =>
28+
verifyAuthentication(() =>
29+
axios
30+
.put<void>(
31+
`${API_HOST}/${buildPutItemGeolocationRoute(payload.itemId)}`,
32+
payload,
33+
)
34+
.then(({ data }) => data),
35+
);
36+
37+
export const getItemsInMap = async (
38+
payload: {
39+
lat1: ItemGeolocation['lat'];
40+
lat2: ItemGeolocation['lat'];
41+
lng1: ItemGeolocation['lng'];
42+
lng2: ItemGeolocation['lng'];
43+
},
44+
{ API_HOST, axios }: PartialQueryConfigForApi,
45+
) =>
46+
verifyAuthentication(() =>
47+
axios
48+
.get<ItemGeolocation[]>(`${API_HOST}/${buildGetItemsInMapRoute(payload)}`)
49+
.then(({ data }) => data),
50+
);
51+
52+
export const deleteItemGeolocation = async (
53+
payload: { itemId: UUID },
54+
{ API_HOST, axios }: PartialQueryConfigForApi,
55+
) =>
56+
verifyAuthentication(() =>
57+
axios
58+
.delete<void>(
59+
`${API_HOST}/${buildDeleteItemGeolocationRoute(payload.itemId)}`,
60+
)
61+
.then(({ data }) => data),
62+
);

src/api/routes.ts

+23
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,25 @@ export const buildPostShortLinkRoute = () => `${SHORT_LINKS_ROUTE}`;
441441
export const buildPatchShortLinkRoute = (alias: string) =>
442442
`${SHORT_LINKS_ROUTE}/${alias}`;
443443

444+
export const buildGetItemGeolocationRoute = (itemId: UUID) =>
445+
`${ITEMS_ROUTE}/${itemId}/geolocation`;
446+
export const buildPutItemGeolocationRoute = (itemId: UUID) =>
447+
`${ITEMS_ROUTE}/${itemId}/geolocation`;
448+
export const buildDeleteItemGeolocationRoute = (itemId: UUID) =>
449+
`${ITEMS_ROUTE}/${itemId}/geolocation`;
450+
export const buildGetItemsInMapRoute = ({
451+
lat1,
452+
lat2,
453+
lng1,
454+
lng2,
455+
}: {
456+
lat1: number;
457+
lat2: number;
458+
lng1: number;
459+
lng2: number;
460+
}) =>
461+
`${ITEMS_ROUTE}/geolocation?lat1=${lat1}&lat2=${lat2}&lng1=${lng1}&lng2=${lng2}`;
462+
444463
export const API_ROUTES = {
445464
APPS_ROUTE,
446465
buildAppListRoute,
@@ -553,4 +572,8 @@ export const API_ROUTES = {
553572
PUBLIC_PROFILE_ROUTE,
554573
GET_OWN_PROFILE,
555574
buildGetPublicProfileRoute,
575+
buildGetItemsInMapRoute,
576+
buildGetItemGeolocationRoute,
577+
buildDeleteItemGeolocationRoute,
578+
buildPutItemGeolocationRoute,
556579
};

src/config/errors.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export class UserIsSignedOut extends Error {
1111
}
1212

1313
export class UndefinedArgument extends Error {
14-
constructor() {
14+
constructor(data?: object) {
1515
super();
16-
this.message = 'UnexpectedInput';
16+
this.message = `UnexpectedInput ${JSON.stringify(data ?? {})}`;
1717
this.name = 'UnexpectedInput';
1818
this.stack = new Error().stack;
1919
}

src/config/keys.ts

+24
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,28 @@ export const CURRENT_MEMBER_STORAGE_KEY = [
279279

280280
export const OWN_PUBLIC_PROFILE_KEY = ['own-profile'];
281281
export const buildPublicProfileKey = (memberId: UUID) => ['profile', memberId];
282+
283+
export const itemsWithGeolocationKeys = {
284+
allBounds: [ITEMS_CONTEXT, 'map'],
285+
inBounds: ({
286+
lat1,
287+
lat2,
288+
lng1,
289+
lng2,
290+
}: {
291+
lat1: number;
292+
lat2: number;
293+
lng1: number;
294+
lng2: number;
295+
}) => [...itemsWithGeolocationKeys.allBounds, { lat1, lat2, lng1, lng2 }],
296+
};
297+
298+
export const buildItemGeolocationKey = (itemId?: UUID) => [
299+
ITEMS_CONTEXT,
300+
itemId,
301+
'geolocation',
302+
];
303+
282304
export const DATA_KEYS = {
283305
APPS_KEY,
284306
buildItemKey,
@@ -320,4 +342,6 @@ export const DATA_KEYS = {
320342
buildSearchPublishedItemsKey,
321343
OWN_PUBLIC_PROFILE_KEY,
322344
buildPublicProfileKey,
345+
itemsWithGeolocationKeys,
346+
buildItemGeolocationKey,
323347
};

src/hooks/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import configureChatHooks from './chat';
88
import configureInvitationHooks from './invitation';
99
import configureItemHooks from './item';
1010
import configureItemFavoriteHooks from './itemFavorite';
11+
import configureItemGeolocationHooks from './itemGeolocation';
1112
import configureItemLikeHooks from './itemLike';
1213
import configureItemLoginHooks from './itemLogin';
1314
import configureItemPublishedHooks from './itemPublish';
@@ -55,5 +56,6 @@ export default (
5556
...configurePlanHooks(queryConfig),
5657
...configurePublicProfileHooks(queryConfig),
5758
...configureShortLinkHooks(queryConfig),
59+
...configureItemGeolocationHooks(queryConfig),
5860
};
5961
};

0 commit comments

Comments
 (0)