aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/utils/Fetcher.jsx250
-rw-r--r--src/utils/Helpers.jsx7
-rw-r--r--src/utils/TokenManager.jsx65
3 files changed, 322 insertions, 0 deletions
diff --git a/src/utils/Fetcher.jsx b/src/utils/Fetcher.jsx
new file mode 100644
index 0000000..6444b34
--- /dev/null
+++ b/src/utils/Fetcher.jsx
@@ -0,0 +1,250 @@
+import { API_URL } from '../Config'
+import { Fetch, HEADERS } from './TokenManager'
+
+const createChore = userID => {
+ return Fetch(`${API_URL}/chores/`, {
+ method: 'POST',
+ headers: HEADERS(),
+ body: JSON.stringify({
+ createdBy: Number(userID),
+ }),
+ }).then(response => response.json())
+}
+
+const signUp = (username, password, displayName, email) => {
+ return fetch(`${API_URL}/auth/`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({ username, password, displayName, email }),
+ })
+}
+
+const login = (username, password) => {
+ return fetch(`${API_URL}/auth/login`, {
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ method: 'POST',
+ body: JSON.stringify({ username, password }),
+ })
+}
+
+const GetAllUsers = () => {
+ return fetch(`${API_URL}/users/`, {
+ method: 'GET',
+ headers: HEADERS(),
+ })
+}
+
+const GetChores = () => {
+ return Fetch(`${API_URL}/chores/`, {
+ method: 'GET',
+ headers: HEADERS(),
+ })
+}
+
+const GetChoreByID = id => {
+ return Fetch(`${API_URL}/chores/${id}`, {
+ method: 'GET',
+ headers: HEADERS(),
+ })
+}
+const CreateChore = chore => {
+ return Fetch(`${API_URL}/chores/`, {
+ method: 'POST',
+ headers: HEADERS(),
+ body: JSON.stringify(chore),
+ })
+}
+
+const DeleteChore = id => {
+ return Fetch(`${API_URL}/chores/${id}`, {
+ method: 'DELETE',
+ headers: HEADERS(),
+ })
+}
+
+const SaveChore = chore => {
+ console.log('chore', chore)
+ return Fetch(`${API_URL}/chores/`, {
+ method: 'PUT',
+ headers: HEADERS(),
+ body: JSON.stringify(chore),
+ })
+}
+const GetChoreHistory = choreId => {
+ return Fetch(`${API_URL}/chores/${choreId}/history`, {
+ method: 'GET',
+ headers: HEADERS(),
+ })
+}
+
+const GetAllCircleMembers = () => {
+ return Fetch(`${API_URL}/circles/members`, {
+ method: 'GET',
+ headers: HEADERS(),
+ })
+}
+
+const GetUserProfile = () => {
+ return Fetch(`${API_URL}/users/profile`, {
+ method: 'GET',
+ headers: HEADERS(),
+ })
+}
+
+const GetUserCircle = () => {
+ return Fetch(`${API_URL}/circles/`, {
+ method: 'GET',
+ headers: HEADERS(),
+ })
+}
+
+const JoinCircle = inviteCode => {
+ return Fetch(`${API_URL}/circles/join?invite_code=${inviteCode}`, {
+ method: 'POST',
+ headers: HEADERS(),
+ })
+}
+
+const GetCircleMemberRequests = () => {
+ return Fetch(`${API_URL}/circles/members/requests`, {
+ method: 'GET',
+ headers: HEADERS(),
+ })
+}
+
+const AcceptCircleMemberRequest = id => {
+ return Fetch(`${API_URL}/circles/members/requests/accept?requestId=${id}`, {
+ method: 'PUT',
+ headers: HEADERS(),
+ })
+}
+
+const LeaveCircle = id => {
+ return Fetch(`${API_URL}/circles/leave?circle_id=${id}`, {
+ method: 'DELETE',
+ headers: HEADERS(),
+ })
+}
+
+const DeleteCircleMember = (circleID, memberID) => {
+ return Fetch(
+ `${API_URL}/circles/${circleID}/members/delete?member_id=${memberID}`,
+ {
+ method: 'DELETE',
+ headers: HEADERS(),
+ },
+ )
+}
+
+const UpdateUserDetails = userDetails => {
+ return Fetch(`${API_URL}/users`, {
+ method: 'PUT',
+ headers: HEADERS(),
+ body: JSON.stringify(userDetails),
+ })
+}
+
+const GetSubscriptionSession = () => {
+ return Fetch(API_URL + `/payments/create-subscription`, {
+ method: 'GET',
+ headers: HEADERS(),
+ })
+}
+
+const CancelSubscription = () => {
+ return Fetch(API_URL + `/payments/cancel-subscription`, {
+ method: 'POST',
+ headers: HEADERS(),
+ })
+}
+
+const GetThings = () => {
+ return Fetch(`${API_URL}/things`, {
+ method: 'GET',
+ headers: HEADERS(),
+ })
+}
+const CreateThing = thing => {
+ return Fetch(`${API_URL}/things`, {
+ method: 'POST',
+ headers: HEADERS(),
+ body: JSON.stringify(thing),
+ })
+}
+
+const SaveThing = thing => {
+ return Fetch(`${API_URL}/things`, {
+ method: 'PUT',
+ headers: HEADERS(),
+ body: JSON.stringify(thing),
+ })
+}
+
+const UpdateThingState = thing => {
+ return Fetch(`${API_URL}/things/${thing.id}/state?value=${thing.state}`, {
+ method: 'PUT',
+ headers: HEADERS(),
+ })
+}
+const DeleteThing = id => {
+ return Fetch(`${API_URL}/things/${id}`, {
+ method: 'DELETE',
+ headers: HEADERS(),
+ })
+}
+
+const CreateLongLiveToken = name => {
+ return Fetch(`${API_URL}/users/tokens`, {
+ method: 'POST',
+ headers: HEADERS(),
+ body: JSON.stringify({ name }),
+ })
+}
+const DeleteLongLiveToken = id => {
+ return Fetch(`${API_URL}/users/tokens/${id}`, {
+ method: 'DELETE',
+ headers: HEADERS(),
+ })
+}
+
+const GetLongLiveTokens = () => {
+ return Fetch(`${API_URL}/users/tokens`, {
+ method: 'GET',
+ headers: HEADERS(),
+ })
+}
+export {
+ AcceptCircleMemberRequest,
+ CancelSubscription,
+ createChore,
+ CreateChore,
+ CreateLongLiveToken,
+ CreateThing,
+ DeleteChore,
+ DeleteCircleMember,
+ DeleteLongLiveToken,
+ DeleteThing,
+ GetAllCircleMembers,
+ GetAllUsers,
+ GetChoreByID,
+ GetChoreHistory,
+ GetChores,
+ GetCircleMemberRequests,
+ GetLongLiveTokens,
+ GetSubscriptionSession,
+ GetThings,
+ GetUserCircle,
+ GetUserProfile,
+ JoinCircle,
+ LeaveCircle,
+ login,
+ SaveChore,
+ SaveThing,
+ signUp,
+ UpdateThingState,
+ UpdateUserDetails,
+}
diff --git a/src/utils/Helpers.jsx b/src/utils/Helpers.jsx
new file mode 100644
index 0000000..17c0351
--- /dev/null
+++ b/src/utils/Helpers.jsx
@@ -0,0 +1,7 @@
+import moment from 'moment'
+
+const isPlusAccount = userProfile => {
+ return userProfile?.expiration && moment(userProfile?.expiration).isAfter()
+}
+
+export { isPlusAccount }
diff --git a/src/utils/TokenManager.jsx b/src/utils/TokenManager.jsx
new file mode 100644
index 0000000..35472e4
--- /dev/null
+++ b/src/utils/TokenManager.jsx
@@ -0,0 +1,65 @@
+import Cookies from 'js-cookie'
+import { API_URL } from '../Config'
+export function Fetch(url, options) {
+ if (!isTokenValid()) {
+ console.log('FETCH: Token is not valid')
+ console.log(localStorage.getItem('ca_token'))
+ // store current location in cookie
+ Cookies.set('ca_redirect', window.location.pathname)
+ // Assuming you have a function isTokenValid() that checks token validity
+ window.location.href = '/login' // Redirect to login page
+ // return Promise.reject("Token is not valid");
+ }
+ if (!options) {
+ options = {}
+ }
+ options.headers = { ...options.headers, ...HEADERS() }
+
+ return fetch(url, options)
+}
+
+export const HEADERS = () => {
+ return {
+ 'Content-Type': 'application/json',
+ Authorization: 'Bearer ' + localStorage.getItem('ca_token'),
+ }
+}
+
+export const isTokenValid = () => {
+ const expiration = localStorage.getItem('ca_expiration')
+ const token = localStorage.getItem('ca_token')
+
+ if (localStorage.getItem('ca_token')) {
+ const now = new Date()
+ const expire = new Date(expiration)
+ if (now < expire) {
+ if (now.getTime() + 24 * 60 * 60 * 1000 > expire.getTime()) {
+ refreshAccessToken()
+ }
+
+ return true
+ } else {
+ localStorage.removeItem('ca_token')
+ localStorage.removeItem('ca_expiration')
+ }
+ return false
+ }
+}
+
+export const refreshAccessToken = () => {
+ fetch(API_URL + '/auth/refresh', {
+ method: 'GET',
+ headers: HEADERS(),
+ }).then(res => {
+ if (res.status === 200) {
+ res.json().then(data => {
+ localStorage.setItem('ca_token', data.token)
+ localStorage.setItem('ca_expiration', data.expire)
+ })
+ } else {
+ return res.json().then(error => {
+ console.log(error)
+ })
+ }
+ })
+}