Skip to content

Commit

Permalink
Work on profile setup and db script
Browse files Browse the repository at this point in the history
  • Loading branch information
Not-A-Normal-Robot committed May 31, 2024
1 parent 27aeb3f commit 9a8581f
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 17 deletions.
4 changes: 3 additions & 1 deletion auth/profile-setup.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ <h1 class="center-text">Set up your profile</h1>
<menu class="pfp">
<img alt="Profile picture preview" class="" id="pfp-preview">
<input type="file" id="pfp-input" name="pfp">
<button type="button" onclick="document.getElementById('pfp-input').click();" class="browse">Browse...</button>
</menu>
<br>
<label for="username">
Expand All @@ -60,9 +61,10 @@ <h1 class="center-text">Set up your profile</h1>
<label for="bio">Bio</label>
<textarea class="" id="bio" name="bio" maxlength="250" placeholder="Write some text about yourself"></textarea>
<span class="char-count" for="bio"></span>
<button type="submit">
<button type="submit" id="submit-button">
Confirm
</button>
<span id="progress"></span>
</form>
</section>
</div>
Expand Down
28 changes: 28 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ form#profile-setup {
form#profile-setup menu.pfp {
display: flex;
flex-direction: row;
flex-wrap: wrap;
padding-left: 0;
gap: 0.5em;
justify-content: space-evenly;
align-items: center;
Expand All @@ -383,6 +385,32 @@ form#profile-setup menu.pfp img {
height: 10em;
border: 1px solid lime;
}
form#profile-setup menu.pfp input[type="file"] {
display: none;
}
form#profile-setup menu.pfp button.browse {
padding: 0.5em;
box-shadow: 0 0 1em 0.2em rgba(0,255,0,0.1);
border: 0.125em solid lime;
border-radius: 0.5em;
background-color: rgba(0,100,0,0.5);
color: white;
transition: all 250ms;
font-family: techmino-proportional, sans-serif;
font-size: 1em;
cursor: pointer;
transition: all 250ms;
}
form#profile-setup menu.pfp button.browse:hover {
background-color: rgba(100,100,0,0.8);
border-color: yellow;
box-shadow: 0 0 1em 0.2em rgba(255,255,0,0.3);
}
form#profile-setup menu.pfp button.browse:focus {
background-color: rgba(100,60,0,0.8);
border-color: orange;
box-shadow: 0 0 3em 0.3em rgba(255,165,0,0.3);
}

form#profile-setup input[type="text"] {
padding: 0.5em;
Expand Down
121 changes: 119 additions & 2 deletions js/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ export default SUPABASE;
window.SUPABASE = SUPABASE;

// #region Type definitions
// Also includes: a peek into the database's structure!
/**
* @typedef {Object} User
* @typedef {Object} Profile
* @property {string} id
* @property {string} username
* @property {AccountState} account_state
Expand All @@ -25,6 +26,7 @@ window.SUPABASE = SUPABASE;
* @property {string} submitted_by
* @property {SubmissionValidity} validity
* @property {string?} proof
* @property {string | undefined} replay_data - Base64 encoded replay data, may be undefined if not requested
*/

/**
Expand Down Expand Up @@ -72,6 +74,12 @@ export const TABLES = {
}

// #region Profile table functions
/**
* @param {number} limit
* @param {number} offset
* @returns {Promise<Profile[]>}
* @throws {Error}
*/
export async function getAllProfiles(limit = 10, offset = 0) {
const { data, error } = await SUPABASE.from(TABLES.PROFILES)
.select('*')
Expand All @@ -80,6 +88,12 @@ export async function getAllProfiles(limit = 10, offset = 0) {
if(error) throw new Error(error.message);
return data;
}

/**
* @param {string} id
* @returns {Promise<Profile>}
* @throws {Error}
*/
export async function getProfileById(id) {
const { data, error } = await SUPABASE.from(TABLES.PROFILES)
.select(1)
Expand All @@ -88,6 +102,12 @@ export async function getProfileById(id) {
if(error) throw new Error(error.message);
return data[0];
}

/**
* @param {string} username
* @returns {Promise<Profile>}
* @throws {Error}
*/
export async function getProfileByUsername(username) {
const { data, error } = await SUPABASE.from(TABLES.PROFILES)
.select(1)
Expand All @@ -97,6 +117,12 @@ export async function getProfileByUsername(username) {
return data[0];
}

/**
* @param {string} id
* @param {Partial<Profile>} newProfileData
* @returns {Promise<Profile>}
* @throws {Error}
*/
export async function editProfile(id, newProfileData) {
const { data, error } = await SUPABASE.from(TABLES.PROFILES)
.update(newProfileData)
Expand All @@ -107,6 +133,11 @@ export async function editProfile(id, newProfileData) {
}

// Scary!
/**
* @param {string} id
* @returns {Promise<string>}
* @throws {Error}
*/
export async function deleteAccount() {
const { error } = await SUPABASE.rpc('delete_account');
if(error) throw new Error(error.message);
Expand All @@ -115,6 +146,13 @@ export async function deleteAccount() {
// #endregion

// #region Submission table functions
/**
* @param {string} gameMode
* @param {number} limit
* @param {number} offset
* @returns {Promise<Submission[]>}
* @throws {Error}
*/
export async function getSubmissionsByGameMode(gameMode, limit = 10, offset = 0) {
const { data, error } = await SUPABASE.from(TABLES.SUBMISSIONS)
.select('*')
Expand All @@ -124,6 +162,12 @@ export async function getSubmissionsByGameMode(gameMode, limit = 10, offset = 0)
if(error) throw new Error(error.message);
return data;
}

/**
* @param {string} id
* @returns {Promise<Submission>}
* @throws {Error}
*/
export async function getSubmissionById(id) {
const { data, error } = await SUPABASE.from(TABLES.SUBMISSIONS)
.select(1)
Expand All @@ -132,6 +176,12 @@ export async function getSubmissionById(id) {
if(error) throw new Error(error.message);
return data[0];
}

/**
* @param {string} id
* @returns {Promise<Submission>}
* @throws {Error}
*/
export async function getSubmissionWithReplayById(id) {
const { data, error } = await SUPABASE.from(TABLES.SUBMISSIONS)
.select('*, replays(replay_data)')
Expand All @@ -140,6 +190,14 @@ export async function getSubmissionWithReplayById(id) {
if(error) throw new Error(error.message);
return data[0];
}

/**
* @param {string} userId
* @param {number} limit
* @param {number} offset
* @returns {Promise<Submission[]>}
* @throws {Error}
*/
export async function getSubmissionsByUserId(userId, limit = 10, offset = 0) {
const { data, error } = await SUPABASE.from(TABLES.SUBMISSIONS)
.select('*')
Expand All @@ -149,6 +207,14 @@ export async function getSubmissionsByUserId(userId, limit = 10, offset = 0) {
if(error) throw new Error(error.message);
return data;
}

/**
* @param {string} userId
* @param {number} limit
* @param {number} offset
* @returns {Promise<Submission[]>}
* @throws {Error}
*/
export async function getSubmissionsWithReplaysByUserId(userId, limit = 10, offset = 0) {
const { data, error } = await SUPABASE.from(TABLES.SUBMISSIONS)
.select('*, replays(replay_data)')
Expand All @@ -158,13 +224,24 @@ export async function getSubmissionsWithReplaysByUserId(userId, limit = 10, offs
return data;
}

/**
* @param {Submission} submissionData
* @returns {Promise<Submission>}
* @throws {Error}
*/
export async function createSubmission(submissionData) {
const { data, error } = await SUPABASE.from(TABLES.SUBMISSIONS)
.insert(submissionData);

if(error) throw new Error(error.message);
return data;
}

/**
* @param {string} id
* @param {Partial<Submission>} newSubmissionData
* @returns {Promise<Submission>}
*/
export async function editSubmission(id, newSubmissionData) {
const { data, error } = await SUPABASE.from(TABLES.SUBMISSIONS)
.update(newSubmissionData)
Expand All @@ -173,6 +250,12 @@ export async function editSubmission(id, newSubmissionData) {
if(error) throw new Error(error.message);
return data;
}

/**
* @param {string} id
* @returns {Promise<Submission>}
* @throws {Error}
*/
export async function deleteSubmission(id) {
const { data, error } = await SUPABASE.from(TABLES.SUBMISSIONS)
.delete()
Expand All @@ -184,6 +267,11 @@ export async function deleteSubmission(id) {
// #endregion

// #region Replay table functions
/**
* @param {string} submissionId
* @returns {Promise<ReplayData>}
* @throws {Error}
*/
export async function getReplayDataBySubmissionId(submissionId) {
const { data, error } = await SUPABASE.from(TABLES.REPLAYS)
.select(1)
Expand All @@ -192,13 +280,25 @@ export async function getReplayDataBySubmissionId(submissionId) {
if(error) throw new Error(error.message);
return data[0];
}

/**
* @param {string} submission_id
* @param {string} replayData
* @returns {Promise<ReplayData>}
*/
export async function createReplay(submission_id, replayData) {
const { data, error } = await SUPABASE.from(TABLES.REPLAYS)
.insert({ submission_id: submission_id, replay_data: replayData });

if(error) throw new Error(error.message);
return data;
}

/**
* @param {string} submissionId
* @returns {Promise<ReplayData>}
* @throws {Error}
*/
export async function deleteReplay(submissionId) {
const { data, error } = await SUPABASE.from(TABLES.REPLAYS)
.delete()
Expand All @@ -208,9 +308,17 @@ export async function deleteReplay(submissionId) {
return data;
}
// #endregion
// #region Download all data
// #region Download all (table) data
// This is an open-source project, why not let users backup everything in case something terrible happens?
// Note: profile pictures will not be scraped, as they are stored in a different location.
// Besides, it'd probably take up a lot of space.

/**
* @returns {Promise<{
* profile: Profile,
* submissions: Submission[]
* }>}
*/
export async function getAllDataBySelf() {
const { data, error } = await SUPABASE.auth.getUser();
if(error) throw new Error(error.message);
Expand All @@ -223,6 +331,15 @@ export async function getAllDataBySelf() {
submissions: submissions
}
}

/**
*
* @returns {Promise<{
* profiles: Profile[],
* submissions: Submission[],
* replays: ReplayData[]
* }>}
*/
export async function getAllData() {
const profiles = await getAllProfiles();
const submissions = await SUPABASE.from(TABLES.SUBMISSIONS).select('*');
Expand Down
Loading

0 comments on commit 9a8581f

Please sign in to comment.