Skip to content

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtyson123 committed Mar 11, 2024
1 parent ce8d63f commit 13ab62d
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 76 deletions.
4 changes: 2 additions & 2 deletions website/src/components/accessibility.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function ToggleAccessibilitySetting({setting, setter}: ToggleAccessibilit
* @returns {boolean} - The accessibility setting
*/
const loader = () => {
let loadedMode = localStorage.getItem(setting)
let loadedMode = sessionStorage.getItem(setting)
let mode = false

// If the mode is loaded as true, otherwise it must be false or incorrect
Expand All @@ -50,7 +50,7 @@ export function ToggleAccessibilitySetting({setting, setter}: ToggleAccessibilit
toggleRef.current!.checked = newMode

// Save the mode
localStorage.setItem(setting, newMode.toString())
sessionStorage.setItem(setting, newMode.toString())

// Update the state
setter(newMode)
Expand Down
8 changes: 4 additions & 4 deletions website/src/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface CacheItem {
}

/**
* Will try to get an item from the localstorage, if the item is expired, it will be removed from the local storage
* Will try to get an item from the sessionStorage, if the item is expired, it will be removed from the local storage
*
* @param {string} id - The key used to store the item in the local storage
*
Expand All @@ -15,7 +15,7 @@ interface CacheItem {
export function getFromCache(id: string){

let item = null;
item = localStorage.getItem(id);
item = sessionStorage.getItem(id);

// Check if the item exists in the local storage
if(item){
Expand All @@ -29,7 +29,7 @@ export function getFromCache(id: string){
console.log("Cache item [" + id + "] has expired");

// If it has expired, remove it from the local storage
localStorage.removeItem(id);
sessionStorage.removeItem(id);

// Return null
return null;
Expand Down Expand Up @@ -60,6 +60,6 @@ export function saveToCache(id: string, data: any){
}

// Save the cache item to the local storage
localStorage.setItem(id, JSON.stringify(cacheItem));
sessionStorage.setItem(id, JSON.stringify(cacheItem));

}
8 changes: 7 additions & 1 deletion website/src/pages/account/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export function AccountPage({dataID}: AccountPageProps){
const signOutUser = async () => {

// Clear the cache
localStorage.clear()
sessionStorage.clear()
await signOut({callbackUrl: "/"})
}

Expand Down Expand Up @@ -486,6 +486,12 @@ export function AccountPage({dataID}: AccountPageProps){
</div>

<div className={styles.actionItem}>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<button onClick={deleteAccount} className={styles.deleteAccountButton}>Delete Account</button>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/account/keys/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default function KeyViewer(){
setLoading("Refreshing...")

// Clear the local cache
localStorage.removeItem("userApiKeysData_"+userApiKeyData.user_id)
sessionStorage.removeItem("userApiKeysData_"+userApiKeyData.user_id)

// Get the key data
await getKeyData(router.query.id as string)
Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/account/keys/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function AccountPage({dataID}: AccountPageProps){
}

// Delete the cached user keys
localStorage.removeItem("userApiKeysData_0")
sessionStorage.removeItem("userApiKeysData_0")

setLoading(false)
// Go to the account page
Expand Down
129 changes: 64 additions & 65 deletions website/src/pages/admin/users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {globalStyles} from "@/lib/global_css";
import { useLogger } from 'next-axiom';
import Table from "@/components/table";
import {Layout} from "@/components/layout";
import {useRouter} from "next/router";
import {getNamesInPreference} from "@/lib/plant_data";

export default function Admin(){
const pageName = "Admin";
Expand All @@ -20,7 +22,7 @@ export default function Admin(){

// Stats
const [users, setUsers] = useState([] as RongoaUser[])
const [sortedField, setSortedField] = useState("id")
const router = useRouter()

// Load the data
const [loadingMessage, setLoadingMessage] = useState("")
Expand All @@ -37,7 +39,7 @@ export default function Admin(){

dataFetch.current = true
fetchData()
}, [session])
})


const fetchData = async () => {
Expand All @@ -52,13 +54,10 @@ export default function Admin(){

// Set the users
let userData = users as RongoaUser[]
for(let i = 0; i < userData.length; i++){
for(let i = 0; i < userData.length; i++)
userData[i].database = userData[i] as any;
}

// Sort the users by id
userData.sort((a, b) => a.database.id - b.database.id)

console.log(userData)
setUsers(userData)
setLoadingMessage("")
}
Expand All @@ -71,6 +70,7 @@ export default function Admin(){
const name = (document.getElementById(`name_${id}`) as HTMLElement).innerText
const email = (document.getElementById(`email_${id}`) as HTMLElement).innerText
const type = (document.getElementById(`type_${id}`) as HTMLInputElement).value
const restricted = (document.getElementById(`restricted_${id}`) as HTMLInputElement).value
let permValue = permissionOptions.indexOf(type)

// Check the if correct type
Expand Down Expand Up @@ -98,7 +98,8 @@ export default function Admin(){
id: id,
name: name,
email: email,
type: permValue
type: permValue,
restricted: restricted === "Yes"
}

await makeRequestWithToken("get", "/api/user/update?adminData=" + JSON.stringify(adminData))
Expand All @@ -110,7 +111,7 @@ export default function Admin(){
sessionStorage.removeItem("user_admin_data")

// Reload the page
window.location.reload()
await router.push("/admin/")
}

const deleteUser = async (id: number) => {
Expand All @@ -125,39 +126,13 @@ export default function Admin(){
sessionStorage.removeItem("user_admin_data")

// Reload the page
window.location.reload()
await router.push("/admin/")
}

const reload = () => {
window.location.reload()
}

useEffect(() => {

// Sort the array based on the field
switch (sortedField) {
case "id":
users.sort((a, b) => a.database.id - b.database.id)
break

case "name":
users.sort((a, b) => a.database.user_name.localeCompare(b.database.user_name))
break

case "email":
users.sort((a, b) => a.database.user_email.localeCompare(b.database.user_email))
break

case "type":
users.sort((a, b) => a.database.user_type - b.database.user_type)
break

case "last_login":
users.sort((a, b) => new Date(a.database.user_last_login).getTime() - new Date(b.database.user_last_login).getTime())
break
}
}, [sortedField]);

return (
<>
<Layout pageName={pageName} loadingMessage={loadingMessage} error={error} loginRequired permissionRequired={"pages:admin:publicAccess"} header={"Users"}>
Expand All @@ -184,38 +159,62 @@ export default function Admin(){

<Section autoPadding>
<Table>
<tr>
<th onClick={() => {setSortedField("id")}}>ID</th>
<th onClick={() => {setSortedField("name")}}>Name</th>
<th onClick={() => {setSortedField("email")}}>Email</th>
<th onClick={() => {setSortedField("type")}}>Type</th>
<th onClick={() => {setSortedField("restricted")}}>Restricted Access</th>
<th onClick={() => {setSortedField("last_login")}}>Last Login</th>
<th>Update</th>
<th>Remove</th>
</tr>

{users.map((user, index) => (
<tr key={index}>
<td> {user.id} </td>
<td><p id={`name_${user.id}`} contentEditable>{user.database.user_name}</p></td>
<td><p id={`email_${user.id}`} contentEditable>{user.database.user_email}</p></td>
<td>
<select id={`type_${user.id}`}
defaultValue={permissionOptions[user.database.user_type]}>
<option value="Admin">Admin</option>
<option value="Editor">Editor</option>
<option value="Member">Member</option>
</select>
</td>
<td> {user.database.user_restricted_access ? "Yes" : "No"} </td>
<td> {new Date(user.database.user_last_login).toLocaleString()} </td>
<td><button onClick={() => {updateUser(user.database.id)}}>Update</button></td>
<td><button onClick={() => {deleteUser(user.database.id)}}>Remove</button></td>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Type</th>
<th>Restricted Access</th>
<th>Last Login</th>
<th>Update</th>
<th>Remove</th>
</tr>
))}
</thead>

<tbody>
{users.map((user, index) => (
<tr key={index}>
<td> {user.id} </td>
<td><p id={`name_${user.id}`} contentEditable>{user.database.user_name}</p></td>
<td><p id={`email_${user.id}`} contentEditable>{user.database.user_email}</p></td>
<td>
<select id={`type_${user.id}`} defaultValue={permissionOptions[user.database.user_type]}>
<option value="Admin">Admin</option>
<option value="Editor">Editor</option>
<option value="Member">Member</option>
</select>
</td>
<td>
<select id={`restricted_${user.id}`} defaultValue={user.database.user_restricted_access ? "Yes" : "No"}>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</td>
<td> {new Date(user.database.user_last_login).toLocaleString()} </td>
<td> <button onClick={() => {updateUser(user.database.id)}}>Update </button> </td>
<td> <button onClick={() => {deleteUser(user.database.id)}}>Remove </button></td>
</tr>
))}
</tbody>
</Table>
</Section>


{/* New User */}
<Section autoPadding>
<div className={globalStyles.gridCentre}>
<div className={globalStyles.container}>
<div className={styles.adminHeaderContainer}>
todo
<h1>New User</h1>
<input placeholder={"Name"} id={"new_name"}/>
<input placeholder={"Email"} id={"new_email"}/>
<button>Create New</button>
</div>
</div>
</div>
</Section>
</Layout>
</>
)
Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/api/user/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default async function handler(
}

let data = JSON.parse(adminData as any);
query = `UPDATE users SET ${tables.user_name} = '${data.name}', ${tables.user_email} = '${data.email}', ${tables.user_type} = '${data.type}' WHERE id = ${data.id}`;
query = `UPDATE users SET ${tables.user_name} = '${data.name}', ${tables.user_email} = '${data.email}', ${tables.user_type} = '${data.type}', ${tables.user_restricted_access} = ${data.restricted} WHERE id = ${data.id}`;
console.log(query);
}

Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/plants/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2431,7 +2431,7 @@ export default function CreatePlant() {
}

// Remove the plant from the local storage
localStorage.removeItem("plant_" + idNum)
sessionStorage.removeItem("plant_" + idNum)

// Add the id to the upload data
console.log("SETTING ID")
Expand Down
1 change: 1 addition & 0 deletions website/src/styles/pages/account/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@

.deleteAccountButton{
background-color: red;
scale: 75%;
}

.signInButton{
Expand Down

0 comments on commit 13ab62d

Please sign in to comment.