Skip to content

Commit

Permalink
added organization and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cophilot committed Dec 19, 2023
1 parent 31e650c commit eea664e
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 117 deletions.
2 changes: 1 addition & 1 deletion scripts/create_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import requests
url = get_server_url()
print('Creating tables on ' + url +'...')
url += '/dev/createTables'
url += '/dev/createTables/public'
response = requests.post(url)
print(response.text)
except:
Expand Down
56 changes: 31 additions & 25 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { UserProvider } from './providers/UserProvider';
import Settings from './components/Settings/Settings';
import { NotificationProvider } from './providers/NotificationProvider';
import CreateOrg from './components/CreateOrg/CreateOrg';
import { OrganizationProvider } from './providers/OrganizationProvider';

// The main component of the application
function App() {
Expand All @@ -17,31 +18,36 @@ function App() {
<NotificationProvider>
<StorageProvider>
<ThemeProvider>
<UserProvider>
<BrowserRouter>
<Routes>
<Route path="/" Component={Home} />
<Route path="/login" Component={Login} />
<Route
path="/organization/create"
Component={CreateOrg}
/>
<Route
path="/register"
Component={Register}
/>
<Route
path="/settings"
Component={Settings}
/>
<Route
path="/profile"
Component={Profile}
/>
<Route path="*" Component={Home} />
</Routes>
</BrowserRouter>
</UserProvider>
<OrganizationProvider>
<UserProvider>
<BrowserRouter>
<Routes>
<Route path="/" Component={Home} />
<Route
path="/login"
Component={Login}
/>
<Route
path="/organization/create"
Component={CreateOrg}
/>
<Route
path="/register"
Component={Register}
/>
<Route
path="/settings"
Component={Settings}
/>
<Route
path="/profile"
Component={Profile}
/>
<Route path="*" Component={Home} />
</Routes>
</BrowserRouter>
</UserProvider>
</OrganizationProvider>
</ThemeProvider>
</StorageProvider>
</NotificationProvider>
Expand Down
2 changes: 0 additions & 2 deletions src/components/FileUpload/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ function FileUpload() {

const handleUpload = () => {
if (files.length === 0) return;
console.log(files);
APIService.upload(files).then((res) => {
showNotification('File upload', res);

setFiles([]);
});
};
Expand Down
19 changes: 17 additions & 2 deletions src/components/Profile/Profile.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,25 @@
font-weight: bold;
margin-bottom: 20px;
}
.orgBtns {
.orgs {
display: flex;
flex-direction: column;
.create {
max-width: 150px;
}
}
.orgItem {
display: flex;
flex-direction: row;
align-items: center;
p {
font-size: 20px;
line-height: 20px;
font-weight: bold;
min-width: 200px;
}
* {
margin-right: 10px;
margin-right: 15px;
}
}
}
102 changes: 47 additions & 55 deletions src/components/Profile/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Link, useNavigate } from 'react-router-dom';
import APIService from '../../utils/ApiService';
import Navbar from '../Navbar/Navbar';
import './Profile.scss';
import { useEffect, useState } from 'react';
Expand All @@ -10,6 +9,11 @@ import {
} from '../../providers/UserProvider';
import MyFiles from '../MyFiles/MyFiles';
import { useShowChoiceNotification } from '../../providers/NotificationProvider';
import Organization from '../../utils/Organization';
import {
useGetOrganizations,
useUpdateOrganizations,
} from '../../providers/OrganizationProvider';

/**
* The profile page component
Expand All @@ -23,27 +27,17 @@ function Profile() {
const logOut = useLogOut();

const [username] = useState(getUserName());
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [fileNames, setFileNames] = useState<string[]>([]);
const [organizationName, setOrganizationName] = useState<string>('');

const getFiles = () => {
APIService.getFileNames(username).then((res) => {
setFileNames(res);
});
};

const getOrganizationName = () => {
APIService.getOrganization(username).then((res) => {
setOrganizationName(res);
});
};
const getOrganizations = useGetOrganizations();
const updateOrganizations = useUpdateOrganizations();

useEffect(() => {
if (!isLoggedIn) {
navigate('/');
}
getFiles();
getOrganizationName();
updateOrganizations();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isLoggedIn, username]);

Expand All @@ -56,52 +50,50 @@ function Profile() {
<span className="db">{username.slice(-2)}</span>
</h1>
<h2>
<span className="db">My</span>Organization
<span className="db">My</span>Organizations
</h2>
{organizationName !== '' ? (
<p>
<b>{organizationName}</b>
</p>
) : (
{getOrganizations().length === 0 && (
<p>
<i>You are not a member of any organization.</i>
</p>
)}

<div className="orgBtns">
{organizationName !== '' ? (
<>
{/* //TODO */}
<button className="btn">View</button>
<button className="btn">Add Member</button>
<button
className="btn"
onClick={() => {
showChoice(
'Leave Organization',
'Are you sure you want to leave ' +
organizationName +
'?',
() => {
// TODO: Leave organization
console.log('Leave');
},
() => {},
'Leave',
'Cancel'
);
}}
>
Leave
</button>
</>
) : (
<>
<Link className="btn" to="/organization/create">
Create New
</Link>
</>
)}
<div className="orgs">
{getOrganizations().length > 0 &&
getOrganizations().map((org: Organization) => (
<li key={org.id} className="orgItem">
<p key={org.id + 'Name'}>{org.name}</p>
<button className="btn">View</button>
<button className="btn">Add Member</button>
<button
className="btn"
onClick={() => {
showChoice(
'Leave Organization',
'Are you sure you want to leave ' +
org.name +
'?',
() => {
// TODO: Leave organization
console.log('Leave');
},
() => {},
'Leave',
'Cancel'
);
}}
>
Leave
</button>
</li>
))}
<Link
className="btn create"
to="/organization/create"
style={{ marginTop: '25px' }}
>
Create New
</Link>
</div>
<h2>
<span className="db">My</span>Files
Expand Down
82 changes: 82 additions & 0 deletions src/providers/OrganizationProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* eslint-disable react-refresh/only-export-components */
import React, { ReactNode, useEffect } from 'react';
import Organization from '../utils/Organization';
import APIService from '../utils/ApiService';

const OrganizationContext = React.createContext({
getOrganizations: (): Organization[] => {
return [];
},
updateOrganizations: () => {},
});

/**
* A hook to get the organizations from the OrganizationProvider
* @returns A function that returns an array of organizations
*/
export function useGetOrganizations() {
const context = React.useContext(OrganizationContext);
if (!context) {
throw new Error(
'useGetOrganizations must be used within a OrganizationProvider'
);
}
return context.getOrganizations;
}

/**
* A hook to update the organizations in the OrganizationProvider
* @returns A function that updates the organizations in the OrganizationProvider
*/
export function useUpdateOrganizations() {
const context = React.useContext(OrganizationContext);
if (!context) {
throw new Error(
'useUpdateOrganizations must be used within a OrganizationProvider'
);
}
return context.updateOrganizations;
}

interface Props {
children: ReactNode;
}

/**
* A provider that provides the organizations to the application
*/
export function OrganizationProvider({ children }: Props) {
const [organizations, setOrganizations] = React.useState<Organization[]>(
[]
);

useEffect(() => {
updateOrganizations();
}, []);

const getOrganizations = () => {
return organizations;
};

const updateOrganizations = () => {
APIService.getOrganizationNames().then((response) => {
if (!response) {
setOrganizations([]);
return;
}
response = response.filter((org) => org.id > 0);
setOrganizations(response);
});
};

return (
<OrganizationContext.Provider
value={{
getOrganizations: getOrganizations,
updateOrganizations: updateOrganizations,
}}
>
{children}
</OrganizationContext.Provider>
);
}
Loading

0 comments on commit eea664e

Please sign in to comment.