Skip to content

Commit

Permalink
Merge pull request #25 from VSchool/assets
Browse files Browse the repository at this point in the history
removed update assets component, removed asset from being saved in lo…
  • Loading branch information
tirzah-dev authored Mar 20, 2024
2 parents aa95f49 + 7e1927a commit c4b1628
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 208 deletions.
123 changes: 18 additions & 105 deletions apps/frontend/src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { BrowserRouter } from 'react-router-dom';
import Footer from './components/footer/footer';
import Header from './components/header/header';
import Router from './views/router/router';
import { AssetProvider } from './views/assets/AssetContext';
import './app.module.scss';
// import { UpdateAssets } from './views/assets/UpdateAssets';
import { AssetsInput } from './views/assets/AssetsInput';

// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-explicit-any
export const UserContext = React.createContext({
Expand All @@ -20,38 +23,10 @@ export const UserContext = React.createContext({
},
setUser: (user: any) => {},
});
interface Asset {
id: string;
asset: string;
amount: number;
}

// interface AssetsInputProps {
// saveAssets: (assets: Asset[]) => void; // Adjust the type of saveAssets function
// }

interface AssetContextType {
assets: Asset[];
setAssets: React.Dispatch<React.SetStateAction<Asset[]>>;
addNewAsset: (newAsset: Asset) => void;
saveAssets: (assets: Asset[]) => void;
removeAsset: (asset: Asset) => void;
getAllAssets: () => void;
}

export const AssetContext = React.createContext<AssetContextType>({
assets: [],
setAssets: () => {},
addNewAsset: () => {},
removeAsset: () => {},
// eslint-disable-next-line @typescript-eslint/no-empty-function
saveAssets: () => {},
getAllAssets: () => {},
});

interface AssetProviderProps {
children: React.ReactNode; // Define children prop explicitly
}
export function App() {
const [user, setUser] = React.useState<any>({
firstName: null,
Expand All @@ -64,69 +39,6 @@ export function App() {
name: null,
roles: ['None'],
});
const [assets, setAssets] = useState<Asset[]>([]);

const addNewAsset = (newAsset: Asset) => {
return setAssets((prev: Asset[]) => {
const newAssets: Asset[] = [...prev, newAsset];
setAssets(newAssets);
saveAssets(newAssets);
console.log(newAssets);
return newAssets;
});
};

console.log(assets);

const saveAssets = (assets: Asset[]) => {
try {
localStorage.setItem('assets', JSON.stringify(assets));
} catch (err) {
console.error('error saving to local storage', err);
}
};

const removeAsset = (asset: Asset) =>
setAssets((prev) => {
const newAssets = prev.filter((as) => as.id !== asset.id);
saveAssets(newAssets);
console.log('deleted asset');
return newAssets;
});

const getAllAssets = () => {
try {
const storedAssetsString: string | null = localStorage.getItem('assets');

if (storedAssetsString !== null) {
const storedAssets: Asset[] | Asset = JSON.parse(storedAssetsString);

if (Array.isArray(storedAssets)) {
setAssets(storedAssets);
} else {
setAssets([storedAssets]);
console.log('not saved');
}
}
} catch (err) {
console.error(err);
}
};

console.log(assets);

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

const assetContextValue: AssetContextType = {
assets,
setAssets,
addNewAsset,
saveAssets,
removeAsset,
getAllAssets,
};

useEffect(() => {
const user = localStorage.getItem('user');
Expand Down Expand Up @@ -160,21 +72,22 @@ export function App() {
}, []);

return (
<UserContext.Provider value={{ user, setUser }}>
<AssetContext.Provider value={assetContextValue}>

<BrowserRouter>
<div className="flex flex-col h-screen">
<div style={{ flex: '1 1 0' }}>
<Header></Header>
<Router></Router>
</div>
{/* <Footer></Footer> */}
</div>
</BrowserRouter>
// <UserContext.Provider value={{ user, setUser }}>
// {/* <AssetProvider value={{assets, setAssets, saveAssets, removeAsset, getAllAssets, addNewAsset }} > */}
// {/* <UpdateAssets /> */}

</AssetContext.Provider>
</UserContext.Provider>
<BrowserRouter>
<div className="flex flex-col h-screen">
<div style={{ flex: '1 1 0' }}>
<Header></Header>
<Router></Router>

</div>

</div>
</BrowserRouter>
// {/* </AssetProvider> */}
// </UserContext.Provider>
);
}

Expand Down
136 changes: 93 additions & 43 deletions apps/frontend/src/app/views/assets/AssetContext.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,105 @@
import { createContext, useState, useEffect, useContext } from "react"
import React, { createContext, useState, useEffect, useContext } from "react"
import axios from "axios";

interface Asset {
asset: string,
amount: number
}


interface Asset {
id: string;
asset: string;
amount: number;
}
interface AssetContextType {
assets: Asset[];
setAssets: React.Dispatch<React.SetStateAction<Asset[]>>;


}
addNewAsset: (newAsset: Asset) => void;
saveAssets: (assets: Asset[]) => void;
removeAsset: (asset: Asset) => void;
getAllAssets: () => void;
}

export const AssetContext = createContext<AssetContextType | undefined>(undefined)


export const AssetProvider: React.FC < { children: React.ReactNode }> = ({ children }) => {
const [assets, setAssets] = useState<Asset[]>([])

export const AssetProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [assets, setAssets] = useState<Asset[]>([]);

const addNewAsset = (newAsset: Asset) => {
return setAssets((prev: Asset[]) => {
const newAssets: Asset[] = [...prev, newAsset];
setAssets(newAssets);
saveAssets(newAssets);
console.log(newAssets);
return newAssets;
});
};


// useEffect(() => {
// async function getAssets(){
// try {
// const response = await axios.get<{results: Asset[] }>("https://mocki.io/v1/31ff50e8-8b81-400a-87d1-d7fc37c9005a")
// setAssets(response.data.results)
// console.log(response.data.results)
// } catch (error) {
// console.log(error)

// }
// }
// getAssets()
// }, [])





return (
<AssetContext.Provider value={{ assets, setAssets }}>
{children}
</AssetContext.Provider>
)
}

export const useAssetContext = () => {
const context = useContext(AssetContext)
if(!context) {
throw new Error('useAssetContext must be used within an asset provider')
const saveAssets = (assets: Asset[]) => {
try {
localStorage.setItem('assets', JSON.stringify(assets));
} catch (err) {
console.error('error saving to local storage', err);
}
};

const removeAsset = (asset: Asset) =>
setAssets((prev) => {
const newAssets = prev.filter((as) => as.id !== asset.id);
saveAssets(newAssets);
console.log('deleted asset');
return newAssets;
});

const getAllAssets = () => {
try {
const storedAssetsString: string | null =
localStorage.getItem('assets');

if (storedAssetsString !== null) {
const storedAssets: Asset[] | Asset =
JSON.parse(storedAssetsString);

if (Array.isArray(storedAssets)) {
setAssets(storedAssets);
} else {
setAssets([storedAssets]);
console.log('not saved');
}
}
} catch (err) {
console.error(err);
}
};






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

// type AssetContextType = {
// assets,
// setAssets,
// addNewAsset,
// saveAssets,
// removeAsset,
// getAllAssets,
// };



return (
// <AssetContext.Provider >
// {children}
// </AssetContext.Provider>

<div>
<h1>test</h1>
</div>
)
}
return context
}

19 changes: 12 additions & 7 deletions apps/frontend/src/app/views/assets/AssetList.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
// import { useContext } from 'react';
// import { AssetContext } from './AssetContext';
// import { AssetContext } from './Index';
import { UpdateAssets } from './UpdateAssets';
// import { UpdateAssets } from './UpdateAssets';

const AssetList = (props: any) => {
// const { assets } = useContext(AssetContext);

return (
<ul>
{props.asset.map((asset: any) => (
<UpdateAssets key={props.asset.id} asset={props.asset.asset} />
))}
</ul>
);
// <ul>
// {props.asset.map((asset: any) => (
// // <UpdateAssets key={props.asset.id} asset={props.asset.asset} />
// ))}
// </ul>
// );

<div>
<p>assets</p>
</div>
)
};

export default AssetList;
Loading

0 comments on commit c4b1628

Please sign in to comment.