Skip to content

Instantly share code, notes, and snippets.

@krvajal
Created January 20, 2019 19:40
Show Gist options
  • Save krvajal/d97858aad62dd095d3b3a761110cd329 to your computer and use it in GitHub Desktop.
Save krvajal/d97858aad62dd095d3b3a761110cd329 to your computer and use it in GitHub Desktop.
// V1
export class NotificationsRequests extends SweepbrightCrudRequest {
constructor() {
super();
// @ts-ignore
this.setMiddlewares([parseJson, response => response.data]);
}
getAll = (attributes: { page: number }): Promise<NotificationsResponse> => {
return this.withQueryParameters(attributes).get('notifications');
};
readNotification(notificationId: string): Promise<Notification> {
return this.put(`/notification/${notificationId}`, {
last_read_at: new Date().toISOString(),
});
}
readAllNotifications = () => {
return this.put('notifications', {
last_read_at: new Date().toISOString(),
});
};
}
// V2
export default function NotificationsRequests(httpClient: CrudRequest = new SweepbrightCrudRequest()) {
httpClient.setMiddlewares([parseJson, response => response.data]);
const _userId = () => {
return AuthManager.getUser().id;
};
const getAll = (attributes: { page: number }): Promise<NotificationsResponse> => {
return httpClient.withQueryParameters({ ...attributes, limit: 50 }).get(`users/${_userId()}/notifications`);
};
const readNotification = (notificationId: string): Promise<Notification> => {
return httpClient.put(`users/${_userId()}/notifications/${notificationId}`, {
last_read_at: new Date().toISOString(),
});
};
const readAllNotifications = () => {
return httpClient.put(`users/${_userId()}/notifications/read-all`, {
last_read_at: new Date().toISOString(),
});
};
return {
getAll,
readNotification,
readAllNotifications,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment