Add org context, and page to submit a new organisation if none exist for a user

This commit is contained in:
Ana Custura 2026-06-02 15:40:46 +01:00
parent 2a399fc397
commit 3bd1a5007a
5 changed files with 151 additions and 10 deletions

View file

@ -9,12 +9,16 @@ import {
import {Breadcrumb, type MenuProps} from 'antd'; import {Breadcrumb, type MenuProps} from 'antd';
import { Layout, Menu, theme } from 'antd'; import { Layout, Menu, theme } from 'antd';
import Home from "./Home.tsx"; import Home from "./Home.tsx";
import { Route, Routes, useNavigate} from "react-router"; import {Route, Routes, useLocation, useNavigate} from "react-router";
import Profile from "./Profile.tsx"; import Profile from "./Profile.tsx";
import {type UserContextType} from './UserContext.tsx'; import {type UserContextType} from './UserContext.tsx';
import {UserContext} from './UserContext.tsx'; import {UserContext} from './UserContext.tsx';
import type {userObject} from "./UserContext.tsx"; import type {userObject} from "./UserContext.tsx";
import {type OrgContextType} from './OrgContext.tsx';
import {OrgContext} from './OrgContext.tsx';
import type {orgObject} from "./OrgContext.tsx";
const { Header, Content, Sider } = Layout; const { Header, Content, Sider } = Layout;
type MenuItem = Required<MenuProps>['items'][number]; type MenuItem = Required<MenuProps>['items'][number];
@ -37,6 +41,7 @@ function getItem(
const App: React.FC = () => { const App: React.FC = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const location = useLocation();
const { const {
token: {colorBgContainer, borderRadiusLG}, token: {colorBgContainer, borderRadiusLG},
} = theme.useToken(); } = theme.useToken();
@ -66,7 +71,23 @@ const App: React.FC = () => {
setCurrentUser: () => {} setCurrentUser: () => {}
} }
const defaultOrg: OrgContextType = {
currentOrg: {
billing_contact: "",
owner_contact: "",
name: "",
status: "",
root_user: "",
security_contact: "",
},
setCurrentOrg: () => {}
}
const [currentUser, setCurrentUser] = useState<userObject>(defaultUser.currentUser); const [currentUser, setCurrentUser] = useState<userObject>(defaultUser.currentUser);
const [currentOrg, setCurrentOrg] = useState<orgObject>(defaultOrg.currentOrg);
// STRILL NEEDS SOMETHING TO get CURRENT USER ORGS and map first one to a CURRENT ORG fetched from the API
// STILL NEEDS THE WRAPPER IN THE RETURN STATEMETNS
/***************************** /*****************************
GETTING CURRENT USER FROM API GETTING CURRENT USER FROM API
*****************************/ *****************************/
@ -76,7 +97,6 @@ const App: React.FC = () => {
(async () => { (async () => {
if (!auth.user) { return } if (!auth.user) { return }
try { try {
console.log(auth.user);
const token = auth.user.access_token; const token = auth.user.access_token;
const response = await fetch("/api/v1/user/self/db", { const response = await fetch("/api/v1/user/self/db", {
headers: { headers: {
@ -90,8 +110,6 @@ const App: React.FC = () => {
})(); })();
}, [auth]); }, [auth]);
// const [current_orgs, set_current_orgs] = useState<Organisation | null>(null);
if (!currentUser) { if (!currentUser) {
return( return(
@ -121,7 +139,7 @@ const App: React.FC = () => {
const onClick: MenuProps['onClick'] = (e) => { const onClick: MenuProps['onClick'] = (e) => {
switch (e.key) { switch (e.key) {
case 'account': case 'profile':
navigate('/profile'); navigate('/profile');
break; break;
case 'logout': case 'logout':
@ -140,7 +158,7 @@ const App: React.FC = () => {
]; ];
const top_nav_items = [ const top_nav_items = [
getItem(currentUser.first_name + " " +currentUser.last_name , 'account1', <UserOutlined />, [getItem('Account details', 'account'), getItem('Log out', 'logout')]), getItem(currentUser.first_name + " " +currentUser.last_name , 'account', <UserOutlined />, [getItem('Account details', 'profile'), getItem('Log out', 'logout')]),
getItem("Organisation Settings", 'orgsettings', <SettingOutlined />), getItem("Organisation Settings", 'orgsettings', <SettingOutlined />),
]; ];
return ( return (
@ -172,7 +190,7 @@ const App: React.FC = () => {
</Sider> </Sider>
<Layout style={{ padding: '0 24px 24px' }}> <Layout style={{ padding: '0 24px 24px' }}>
<Breadcrumb <Breadcrumb
items={[{ title: 'Home' }, { title: 'List' }, { title: 'App' }]} items={[{ title: 'Home' }, { title: location.pathname.substring(1) }]}
style={{ margin: '16px 0' }} style={{ margin: '16px 0' }}
/> />
<Content <Content
@ -187,7 +205,7 @@ const App: React.FC = () => {
<Routes> <Routes>
<Route path="/" element={<Home/>}/> <Route path="/" element={<Home/>}/>
{currentUser && <Route path="/account" element={<Profile/>}/> } {currentUser && <Route path="/profile" element={<Profile/>}/> }
</Routes> </Routes>
</Content> </Content>

85
src/CreateOrg.tsx Normal file
View file

@ -0,0 +1,85 @@
// src/Posts.jsx
import {useUserContext} from './UserContext';
import { Button, Form, Input } from 'antd';
//import React, {useEffect, useState} from "react";
import {useAuth} from "react-oidc-context";
const CreateOrg = () => {
const { currentUser } = useUserContext();
const [form] = Form.useForm();
const auth = useAuth();
async function submitOrgData(values: FormData) {
if (!auth.user) {
return
}
try {
console.log("in here");
const [name, q1, q2, q3] = Object.values(values);
console.log("end here");
const data = {
"name": name,
"intake_questionnaire": {
"question_one": q1,
"question_two": q2,
"question_three": q3,
}
}
console.log(data)
const token = auth.user.access_token;
const requestOptions = {
method: 'POST',
body: JSON.stringify(data),
headers: {Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'}
}
const response = await fetch("/api/v1/org/", requestOptions);
await response.json();
} catch (e) {
console.error(e);
}
}
const onFinish = (values: FormData) => {
console.log(values);
submitOrgData(values)
};
if (!currentUser) {
return <div>Loading...</div>;
}
return (
<>
<Form
layout= "vertical"
form={form}
onFinish={onFinish}
>
<Form.Item name="Organisation name" label="Organisation name" rules={[{ required: true }]}>
<Input />
</Form.Item>
<Form.Item name="Question 1" label="How does your organisation support civil society?">
<Input placeholder="e.g., Our organisation supports various groups such as ..." />
</Form.Item>
<Form.Item name="Question 2" label="Name your first reference">
<Input placeholder="e.g., SR2 Communciations Limited" />
</Form.Item>
<Form.Item name="Question 3" label="Name your second reference">
<Input placeholder="e.g., SR2 Professional Services" />
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">Submit</Button>
</Form.Item>
</Form>
</>
);
};
export default CreateOrg;

25
src/OrgContext.tsx Normal file
View file

@ -0,0 +1,25 @@
import React, { createContext, useContext } from 'react';
interface orgObject {
name: string;
status: "partial",
root_user: "string",
owner_contact: "string",
billing_contact: "string",
security_contact: "string"
}
export interface OrgContextType {
currentOrg: orgObject;
setCurrentOrg: React.Dispatch<React.SetStateAction<orgObject>>;
}
export const OrgContext = createContext<OrgContextType | undefined>(undefined);
export const useOrgContext = () => {
const context: OrgContextType | undefined = useContext(OrgContext);
if (context === undefined) {
throw new Error('Organisation context not found');
}
return context;
};

View file

@ -18,7 +18,7 @@ export const UserContext = createContext<UserContextType | undefined>(undefined)
export const useUserContext = () => { export const useUserContext = () => {
const context: UserContextType | undefined = useContext(UserContext); const context: UserContextType | undefined = useContext(UserContext);
if (context === undefined) { if (context === undefined) {
throw new Error('jhfhgtfhgfghfhgf not found'); throw new Error('User context not found!');
} }
return context; return context;
}; };

View file

@ -1,11 +1,24 @@
import {Breadcrumb, theme} from "antd"; import {Breadcrumb, theme} from "antd";
import React from "react"; import React, {useContext} from "react";
//import {useOrgContext} from './OrgContext';
import CreateOrg from "./CreateOrg.tsx";
import {useUserContext} from "./UserContext.tsx";
const Home: React.FC = () => { const Home: React.FC = () => {
const { const {
token: { colorBgContainer, borderRadiusLG }, token: { colorBgContainer, borderRadiusLG },
} = theme.useToken(); } = theme.useToken();
//const { currentOrg } = useOrgContext();
const { currentUser } = useUserContext();
console.log(currentUser.organisations);
if (currentUser.organisations.length === 0) {
return <>
<h3>Your user has no organisations yet. Create one here.</h3>
<CreateOrg/>
</>
}
return (<> return (<>
<Breadcrumb style={{ margin: '16px 0' }} items={[{ title: 'User' }, { title: 'Bill' }]} /> <Breadcrumb style={{ margin: '16px 0' }} items={[{ title: 'User' }, { title: 'Bill' }]} />
<div <div