Skip to content

Commit

Permalink
Merge pull request #132 from dhirajudhani/feat/128-wrap-api-calls-in-…
Browse files Browse the repository at this point in the history
…try-catch

feat:#128 wrapped all the api calls into try catch block
  • Loading branch information
shivaypiece authored Jul 8, 2024
2 parents 2371adb + 4ef260f commit 01c5677
Showing 1 changed file with 65 additions and 58 deletions.
123 changes: 65 additions & 58 deletions src/app/components/Asset/Asset.tsx
Original file line number Diff line number Diff line change
@@ -1,86 +1,93 @@
import * as Pieces from "@pieces.app/pieces-os-client";
import {SeededAsset, SeedTypeEnum} from "@pieces.app/pieces-os-client";
import {Application} from "@pieces.app/pieces-os-client";

import { SeededAsset, SeedTypeEnum } from "@pieces.app/pieces-os-client";
import { Application } from "@pieces.app/pieces-os-client";

type LocalAsset = {
name: string,
id: string,
classification: Pieces.ClassificationSpecificEnum,
}
name: string,
id: string,
classification: Pieces.ClassificationSpecificEnum,
}

//==============================[/create]==================================//
export function createAsset(applicationData: Application, data: string, name: string) {

export async function createAsset(applicationData: Application, data: string, name: string) {
let _seededAsset: SeededAsset = {
application: applicationData,
format: {
fragment: {
string: { raw: data },
},
application: applicationData,
format: {
fragment: {
string: { raw: data },
},
metadata: {
name: name
}
},
metadata: {
name: name
}
}

// create your seed
let _seed: Pieces.Seed = {
asset: _seededAsset,
type: SeedTypeEnum.Asset
asset: _seededAsset,
type: SeedTypeEnum.Asset
}

console.log("seed:", _seed)

// make your api call.
new Pieces.AssetsApi().assetsCreateNewAsset({seed: _seed}).then(_a => {
console.log("well howdy", _a);
})

try {
const _a = await new Pieces.AssetsApi().assetsCreateNewAsset({ seed: _seed });
console.log("well howdy", _a);
} catch (error) {
console.error("Error creating asset:", error);
}
}
//==============================[.end /create]==================================//

export function deleteAsset(_id: String,setArray:Function){

const newAssetsList : Array<LocalAsset> = [];

new Pieces.AssetsApi().assetsSnapshot({}).then(_assetList => {
for (let i = 0; i < _assetList.iterable.length; i++) {
if (_assetList.iterable[i].id == _id) {
new Pieces.AssetsApi().assetsDeleteAsset({asset: _assetList.iterable[i].id }).then(()=>console.log(_id));
}
else{
newAssetsList.push( {
id: _assetList.iterable[i].id,
name: _assetList.iterable[i].name,
classification: _assetList.iterable[i].original.reference.classification.specific
})
}
export async function deleteAsset(_id: String, setArray: Function) {
const newAssetsList: Array<LocalAsset> = [];
try {
const _assetList = await new Pieces.AssetsApi().assetsSnapshot({});
for (let i = 0; i < _assetList.iterable.length; i++) {
if (_assetList.iterable[i].id == _id) {
try {
await new Pieces.AssetsApi().assetsDeleteAsset({ asset: _assetList.iterable[i].id });
console.log(_id);
} catch (error) {
console.error("Error deleting asset:", error);
}
} else {
newAssetsList.push({
id: _assetList.iterable[i].id,
name: _assetList.iterable[i].name,
classification: _assetList.iterable[i].original.reference.classification.specific
});
}
window.alert("selected snippet got deleted");
setArray(newAssetsList);
})
}
window.alert("selected snippet got deleted");
setArray(newAssetsList);
} catch (error) {
console.error("Error fetching asset list:", error);
}
}

// used to rename an asset, takes in an _id and _name that comes from the input fields on
// NameInput + DataInput fields.
//
// this uses your asset snapshot to get your list of snippets, then() to get the snippet list back,
// then use the _id to select the snippet from the list of all snippets.
export function renameAsset(_name: string, _id: String){

new Pieces.AssetsApi().assetsSnapshot({}).then(_assetList => {
for (let i = 0; i < _assetList.iterable.length; i++) {
if (_assetList.iterable[i].id == _id) {

let _asset = _assetList.iterable[i];

_asset.name = _name;

new Pieces.AssetApi().assetUpdate({asset: _asset}).then(_updated => {
console.log("updated:", _updated);
})
}
export async function renameAsset(_name: string, _id: String) {
try {
const _assetList = await new Pieces.AssetsApi().assetsSnapshot({});
for (let i = 0; i < _assetList.iterable.length; i++) {
if (_assetList.iterable[i].id == _id) {
let _asset = _assetList.iterable[i];
_asset.name = _name;
try {
const _updated = await new Pieces.AssetApi().assetUpdate({ asset: _asset });
console.log("updated:", _updated);
} catch (error) {
console.error("Error updating asset:", error);
}
}
})
}
}
} catch (error) {
console.error("Error fetching asset list:", error);
}
}

0 comments on commit 01c5677

Please sign in to comment.