Skip to content

Commit

Permalink
chore: reconcile src/ui/services and VITE_API_URI env loading
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieSlome committed Nov 7, 2023
1 parent 25dbeb7 commit 832274d
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 70 deletions.
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_API_URI=http://localhost:8080
1 change: 1 addition & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_API_URI=
28 changes: 28 additions & 0 deletions src/ui/services/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import axios from 'axios';

const baseUrl = import.meta.env.VITE_API_URI
? `${import.meta.env.VITE_API_URI}/api/v1`
: `${location.origin}/api/v1`;

const getAttestationConfig = async (setData) => {
const url = new URL(`${baseUrl}/config/attestation`);
await axios(url.toString()).then((response) => {
setData(response.data.questions);
});
};

const getURLShortener = async (setData) => {
const url = new URL(`${baseUrl}/config/urlShortener`);
await axios(url.toString()).then((response) => {
setData(response.data);
});
};

const getEmailContact = async (setData) => {
const url = new URL(`${baseUrl}/config/contactEmail`);
await axios(url.toString()).then((response) => {
setData(response.data);
});
};

export { getAttestationConfig, getURLShortener, getEmailContact };
75 changes: 42 additions & 33 deletions src/ui/services/git-push.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-disable max-len */
/* eslint-disable require-jsdoc */
import axios from 'axios';
const baseUrl = 'http://localhost:8080/api/v1';

const baseUrl = import.meta.env.VITE_API_URI
? `${import.meta.env.VITE_API_URI}/api/v1`
: `${location.origin}/api/v1`;

const config = {
withCredentials: true,
};

const getUser = async (setIsLoading, setData, setAuth, setIsError) => {
const url = new URL(`http://localhost:8080/auth/success`);
const url = new URL(`${location.origin}/api/auth/success`);
await axios(url.toString(), config)
.then((response) => {
const data = response.data;
Expand All @@ -23,8 +24,8 @@ const getUser = async (setIsLoading, setData, setAuth, setIsError) => {
};

const getPush = async (id, setIsLoading, setData, setAuth, setIsError) => {
const url = new URL(`${baseUrl}/push/${id}`);
await axios(url.toString(), config)
const url = `${baseUrl}/push/${id}`;
await axios(url, config)
.then((response) => {
const data = response.data;
data.diff = data.steps.find((x) => x.stepName === 'diff');
Expand Down Expand Up @@ -53,6 +54,7 @@ const getPushes = async (
const url = new URL(`${baseUrl}/push`);
url.search = new URLSearchParams(query);

setIsLoading(true);
await axios(url.toString(), { withCredentials: true })
.then((response) => {
const data = response.data;
Expand All @@ -70,46 +72,53 @@ const getPushes = async (
});
};

const authorisePush = async (id, setAuth, setIsError) => {
const authorisePush = async (id, setMessage, setUserAllowedToApprove, attestation) => {
const url = `${baseUrl}/push/${id}/authorise`;
let errorMsg = '';
let isUserAllowedToApprove = true;
await axios
.post(url, {}, { withCredentials: true })
.then(() => {})
.post(
url,
{
params: {
attestation,
},
},
{ withCredentials: true },
)
.catch((error) => {
if (error.response && error.response.status === 401) {
setAuth(false);
} else {
setIsError(true);
errorMsg = 'You are not authorised to approve...';
isUserAllowedToApprove = false;
}
});
await setMessage(errorMsg);
await setUserAllowedToApprove(isUserAllowedToApprove);
};

const rejectPush = async (id, setAuth, setIsError) => {
const rejectPush = async (id, setMessage, setUserAllowedToReject) => {
const url = `${baseUrl}/push/${id}/reject`;
await axios
.post(url, {}, { withCredentials: true })
.then(() => {})
.catch((error) => {
if (error.response && error.response.status === 401) {
setAuth(false);
} else {
setIsError(true);
}
});
let errorMsg = '';
let isUserAllowedToReject = true;
await axios.post(url, {}, { withCredentials: true }).catch((error) => {
if (error.response && error.response.status === 401) {
errorMsg = 'You are not authorised to reject...';
isUserAllowedToReject = false;
}
});
await setMessage(errorMsg);
await setUserAllowedToReject(isUserAllowedToReject);
};

const cancelPush = async (id, setAuth, setIsError) => {
const url = `${baseUrl}/push/${id}/cancel`;
await axios
.post(url, {}, { withCredentials: true })
.then((response) => {})
.catch((error) => {
if (error.response && error.response.status === 401) {
setAuth(false);
} else {
setIsError(true);
}
});
await axios.post(url, {}, { withCredentials: true }).catch((error) => {
if (error.response && error.response.status === 401) {
setAuth(false);
} else {
setIsError(true);
}
});
};

export { getPush, getPushes, authorisePush, rejectPush, cancelPush, getUser };
82 changes: 66 additions & 16 deletions src/ui/services/repo.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
import axios from 'axios';
const baseUrl = 'http://localhost:8080/api/v1';

const baseUrl = import.meta.env.VITE_API_URI
? `${import.meta.env.VITE_API_URI}/api/v1`
: `${location.origin}/api/v1`;

const config = {
withCredentials: true,
};

const canAddUser = (repoName, user, action) => {
const url = new URL(`${baseUrl}/repo/${repoName}`);
return axios
.get(url.toString(), config)
.then((response) => {
const data = response.data;
if (action === 'authorise') {
return !data.users.canAuthorise.includes(user);
} else {
return !data.users.canPush.includes(user);
}
})
.catch((error) => {
throw error;
});
};

class DupUserValidationError extends Error {
constructor(message) {
super(message);
this.name = 'The user already has this role...';
}
}

const getRepos = async (setIsLoading, setData, setAuth, setIsError, query = {}) => {
const url = new URL(`${baseUrl}/repo`);
url.search = new URLSearchParams(query);

setIsLoading(true);
await axios(url.toString(), config)
.then((response) => {
const data = response.data;
Expand All @@ -28,6 +55,7 @@ const getRepos = async (setIsLoading, setData, setAuth, setIsError, query = {})

const getRepo = async (setIsLoading, setData, setAuth, setIsError, id) => {
const url = new URL(`${baseUrl}/repo/${id}`);
setIsLoading(true);
await axios(url.toString(), config)
.then((response) => {
const data = response.data;
Expand All @@ -45,28 +73,50 @@ const getRepo = async (setIsLoading, setData, setAuth, setIsError, id) => {
});
};

const addUser = async (repoName, user, action) => {
const url = new URL(`${baseUrl}/repo/${repoName}/user/${action}`);
const data = { username: user };
await axios
.patch(url, data, { withCredentials: true })
.then(() => {})
const addRepo = async (onClose, setError, data) => {
const url = new URL(`${baseUrl}/repo`);
axios
.post(url, data, { withCredentials: true })
.then(() => {
onClose();
})
.catch((error) => {
console.log(error.response.data.message);
setError(error.response.data.message);
});
};

const addUser = async (repoName, user, action) => {
const canAdd = await canAddUser(repoName, user, action);
if (canAdd) {
const url = new URL(`${baseUrl}/repo/${repoName}/user/${action}`);
const data = { username: user };
await axios.patch(url, data, { withCredentials: true }).catch((error) => {
console.log(error.response.data.message);
throw error;
});
} else {
console.log('Duplicate user can not be added');
throw new DupUserValidationError();
}
};

const deleteUser = async (user, repoName, action) => {
const url = new URL(`${baseUrl}/repo/${repoName}/user/${action}/${user}`);

await axios
.delete(url, { withCredentials: true })
.then(() => {})
.catch((error) => {
console.log(error.response.data.message);
throw error;
});
await axios.delete(url, { withCredentials: true }).catch((error) => {
console.log(error.response.data.message);
throw error;
});
};

const deleteRepo = async (repoName) => {
const url = new URL(`${baseUrl}/repo/${repoName}/delete`);

await axios.delete(url, { withCredentials: true }).catch((error) => {
console.log(error.response.data.message);
throw error;
});
};

export { addUser, deleteUser, getRepos, getRepo };
export { addUser, deleteUser, getRepos, getRepo, addRepo, deleteRepo };
72 changes: 51 additions & 21 deletions src/ui/services/user.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-disable max-len */
/* eslint-disable require-jsdoc */
import axios from 'axios';
const baseUrl = 'http://localhost:8080';

const baseUrl = import.meta.env.VITE_API_URI
? `${import.meta.env.VITE_API_URI}/api/v1`
: `${location.origin}/api/v1`;

const config = {
withCredentials: true,
};

const getUser = async (setIsLoading, setData, setAuth, setIsError, id = null) => {
let url = `${baseUrl}/auth/profile`;
let url = `${baseUrl}/api/auth/profile`;

if (id) {
url = `${baseUrl}/api/v1/user/${id}`;
Expand All @@ -17,40 +18,69 @@ const getUser = async (setIsLoading, setData, setAuth, setIsError, id = null) =>
console.log(url);

await axios(url, config)
.then((response) => {
const data = response.data;
if (setData) {
setData(data);
}
if (setIsLoading) {
setIsLoading(false);
}
})
.catch((error) => {
if (error.response && error.response.status === 401) {
if (setAuth) {
setAuth(false);
}
} else {
if (setIsError) {
setIsError(true);
}
}
if (setIsLoading) {
setIsLoading(false);
}
});
};

const getUsers = async (setIsLoading, setData, setAuth, setIsError, query = {}) => {
const url = new URL(`${baseUrl}/api/v1/user`);
url.search = new URLSearchParams(query);
setIsLoading(true);
await axios(url.toString(), { withCredentials: true })
.then((response) => {
const data = response.data;
setData(data);
console.log(data);
setIsLoading(false);
})
.catch((error) => {
if (error.response && error.response.status === 401) setAuth(false);
else setIsError(true);
setIsLoading(false);
if (error.response && error.response.status === 401) {
setAuth(false);
} else {
setIsError(true);
}
setIsLoading(false);
});
};

const createUser = async (data) => {
const updateUser = async (data) => {
console.log(data);
const url = new URL(`${baseUrl}/auth/profile`);
await await axios
.post(url, data, { withCredentials: true })
.then(() => {})
.catch((error) => {
console.log(error.response.data.message);
throw error;
});
const url = new URL(`${baseUrl}/api/auth/gitAccount`);
await axios.post(url, data, { withCredentials: true }).catch((error) => {
console.log(error.response.data.message);
throw error;
});
};

const getUsers = async (setIsLoading, setData, setAuth, setIsError, query = {}) => {
const url = new URL(`${baseUrl}/api/v1/user`);
url.search = new URLSearchParams(query);
const getUserLoggedIn = async (setIsLoading, setIsAdmin, setIsError, setAuth) => {
const url = new URL(`${baseUrl}/api/auth/userLoggedIn`);

await axios(url.toString(), { withCredentials: true })
.then((response) => {
const data = response.data;
setData(data);
setIsLoading(false);
setIsAdmin(data.admin);
})
.catch((error) => {
setIsLoading(false);
Expand All @@ -63,4 +93,4 @@ const getUsers = async (setIsLoading, setData, setAuth, setIsError, query = {})
});
};

export { getUser, createUser, getUsers };
export { getUser, getUsers, updateUser, getUserLoggedIn };

0 comments on commit 832274d

Please sign in to comment.