Skip to content

Commit

Permalink
Merge pull request #144 from Esri/feat/follow-initiative
Browse files Browse the repository at this point in the history
Feat: new methods for users to follow and unfollow initiatives
  • Loading branch information
john gravois authored Mar 21, 2019
2 parents be6c96a + aeef22e commit c94a025
Show file tree
Hide file tree
Showing 6 changed files with 446 additions and 26 deletions.
36 changes: 18 additions & 18 deletions packages/initiatives/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions packages/initiatives/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"types": "dist/esm/index.d.ts",
"license": "Apache-2.0",
"dependencies": {
"@esri/arcgis-rest-auth": "^1.17.0",
"@esri/arcgis-rest-common-types": "^1.17.0",
"@esri/arcgis-rest-groups": "^1.17.0",
"@esri/arcgis-rest-items": "^1.17.0",
"@esri/arcgis-rest-request": "^1.17.0",
"@esri/arcgis-rest-sharing": "^1.17.0",
"@esri/arcgis-rest-auth": "^1.18.0",
"@esri/arcgis-rest-common-types": "^1.18.0",
"@esri/arcgis-rest-groups": "^1.18.0",
"@esri/arcgis-rest-items": "^1.18.0",
"@esri/arcgis-rest-request": "^1.18.0",
"@esri/arcgis-rest-sharing": "^1.18.0",
"tslib": "^1.9.3"
},
"peerDependencies": {
Expand Down
98 changes: 98 additions & 0 deletions packages/initiatives/src/follow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {
request,
getPortalUrl,
IRequestOptions
} from "@esri/arcgis-rest-request";
import { UserSession, getUserUrl } from "@esri/arcgis-rest-auth";
import { IUser } from "@esri/arcgis-rest-common-types";

export interface IFollowInitiativeRequestOptions extends IRequestOptions {
initiativeId: string;
authentication: UserSession;
}

const getTag = (initiativeId: string) => `hubInitiativeId|${initiativeId}`;

const getUpdateUrl = (session: UserSession) => `${getUserUrl(session)}/update`;

export const currentlyFollowedInitiatives = (user: IUser): string[] =>
user.tags.map(tag => tag.replace(/^hubInitiativeId\|/, ""));

export const isUserFollowing = (user: IUser, initiativeId: string): boolean =>
currentlyFollowedInitiatives(user).indexOf(initiativeId) > -1;

/**
* ```js
* import { followInitiative } from "@esri/hub-initiatives";
* //
* followInitiative({
* initiativeId,
* authentication
* })
* .then(response)
* ```
* Follow an initiative.
*/
export function followInitiative(
requestOptions: IFollowInitiativeRequestOptions
): Promise<{ success: boolean; username: string }> {
// we dont call getUser() because the tags are cached and will be mutating
return request(getUserUrl(requestOptions.authentication), {
authentication: requestOptions.authentication
}).then(user => {
// don't update if already following
if (isUserFollowing(user, requestOptions.initiativeId)) {
return Promise.reject(`user is already following this initiative.`);
}
const tag = getTag(requestOptions.initiativeId);
const tags = JSON.parse(JSON.stringify(user.tags));
tags.push(tag);

return request(getUpdateUrl(requestOptions.authentication), {
params: { tags },
authentication: requestOptions.authentication
});
});
}

/**
* ```js
* import { unfollowInitiative } from "@esri/hub-initiatives";
* //
* unfollowInitiative({
* initiativeId,
* authentication
* })
* .then(response)
* ```
* Follow an initiative.
*/
export function unfollowInitiative(
requestOptions: IFollowInitiativeRequestOptions
): Promise<{ success: boolean; username: string }> {
// we dont call getUser() because the tags are cached and will be mutating
return request(getUserUrl(requestOptions.authentication), {
authentication: requestOptions.authentication
}).then(user => {
// don't update if already following
if (!isUserFollowing(user, requestOptions.initiativeId)) {
return Promise.reject(`user is not following this initiative.`);
}

const tag = getTag(requestOptions.initiativeId);
const tags = JSON.parse(JSON.stringify(user.tags));
// https://stackoverflow.com/questions/9792927/javascript-array-search-and-remove-string
const index = tags.indexOf(tag);
tags.splice(index, 1);

// clear the last tag by passing ",".
if (tags.length === 0) {
tags.push(",");
}

return request(getUpdateUrl(requestOptions.authentication), {
params: { tags },
authentication: requestOptions.authentication
});
});
}
2 changes: 1 addition & 1 deletion packages/initiatives/src/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { IRequestOptions } from "@esri/arcgis-rest-request";

/**
* Create an initiative collaboration or ope data groups
* Create an initiative collaboration or open data group
* Note: This does not ensure a group with the proposed name does not exist. Please use
* `checkGroupExists
*
Expand Down
3 changes: 2 additions & 1 deletion packages/initiatives/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from "./activate";
export * from "./remove";
export * from "./detach-site";
export * from "./search";
// TODO: Move to module in ArcGIS-REST-JS
export * from "./follow";
// TODO: Move to module in ArcGIS REST JS
import geometryService from "./geometry";
export { geometryService };
Loading

0 comments on commit c94a025

Please sign in to comment.