Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #5

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .yarn/install-state.gz
Binary file not shown.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
"dependencies": {
"@wix/app-management": "^1.0.30",
"@wix/dashboard": "^1.3.21",
"@wix/dashboard-react": "^1.0.20",
"@wix/data": "^1.0.144",
"@wix/design-system": "^1.0.0",
"@wix/ecom": "^1.0.711",
"@wix/essentials": "^0.1.5",
"@wix/patterns": "^1.71.0",
"@wix/sdk-react": "^0.5.7",
"@wix/wix-ui-icons-common": "^3.0.0",
"react-to-webcomponent": "^2.0.0"
},
Expand Down
35 changes: 0 additions & 35 deletions src/backend/api/checkout/api.ts

This file was deleted.

37 changes: 37 additions & 0 deletions src/backend/api/jewels/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
DataItem,
getDataFromCollection,
upsertDataToCollection,
} from '../../database';
import { JEWELRY_COLLECTION_ID } from '../../consts';
import { Jewel } from '../../../types';

export async function GET(req: Request) {
const url = new URL(req.url);
if (url.searchParams.get('id')) {
console.log('has id');
}
const jewelsCollection = await getDataFromCollection({
dataCollectionId: JEWELRY_COLLECTION_ID,
});

return new Response(JSON.stringify(jewelsCollection.items));
}

export async function POST(req: Request) {
const { jewel } = (await req.json()) as { jewel: Jewel };

try {
await upsertDataToCollection({
dataCollectionId: JEWELRY_COLLECTION_ID,
item: {
_id: jewel.id,
data: jewel,
},
});

return new Response('Success');
} catch (error) {
return new Response('Failed', { status: 500 });
}
}
38 changes: 0 additions & 38 deletions src/backend/api/settings/api.ts

This file was deleted.

12 changes: 1 addition & 11 deletions src/backend/consts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
import type { Settings } from "../types";

// Update according to your app's needed collections
export const SETTINGS_COLLECTION_ID = 'carbon-offset-settings';
export const CHECKOUT_COLLECTION_ID = 'carbon-offset-checkout';
export const DEFAULT_SETTING: Settings = {
title: 'Make it carbon neutral',
amount: 2,
color: '#000000',
iconColor: '#000000',
};
export const JEWELRY_COLLECTION_ID = 'Jewelry';
55 changes: 32 additions & 23 deletions src/backend/database.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import { items } from '@wix/data';
import { auth } from '@wix/essentials';

import { Jewel } from '../types';

// Exposing utility functions over Wix Data APIs for easier usage and replacement of database

type DataItem = {
export type DataItem = {
_id?: string;
data: Record<string, any>;
data: Jewel;
};

export const getDataFromCollection = async ({
dataCollectionId
}: { dataCollectionId: string }) => {
const data = await auth.elevate(items.queryDataItems)({
dataCollectionId,
}).find();
dataCollectionId,
}: {
dataCollectionId: string;
}) => {
const data = await items
.queryDataItems({
dataCollectionId,
})
.find();

return data;
};

export const safelyGetItemFromCollection = async ({
dataCollectionId,
itemId
}: { dataCollectionId: string; itemId: string }) => {
itemId,
}: {
dataCollectionId: string;
itemId: string;
}) => {
try {
const { data } = await auth.elevate(items.getDataItem)(
itemId,
{ dataCollectionId },
);

const { data } = await items.getDataItem(itemId, { dataCollectionId });
return data;
} catch (error) {
// Wix data's "getDataItem" API throws exception when item with id does not exist
Expand All @@ -36,31 +40,36 @@ export const safelyGetItemFromCollection = async ({

export const upsertDataToCollection = async ({
dataCollectionId,
item
}: { dataCollectionId: string; item: DataItem }) => {
item,
}: {
dataCollectionId: string;
item: DataItem;
}) => {
const collection = await getDataFromCollection({ dataCollectionId });
const existsInCollection = item._id && collection.items.find(existingItem => existingItem._id === item._id);
const existsInCollection =
item._id &&
collection.items.find((existingItem) => existingItem._id === item._id);

if (item._id && existsInCollection) {
await auth.elevate(items.updateDataItem)(item._id, {
await items.updateDataItem(item._id, {
dataCollectionId,
dataItem: {
data: {
_id: item._id,
...item.data
...item.data,
},
},
});
} else {
await auth.elevate(items.insertDataItem)({
await items.insertDataItem({
dataCollectionId,
dataItem: {
_id: item._id ?? undefined,
data: {
_id: item._id ?? undefined,
...item.data
...item.data,
},
},
});
};
}
};
38 changes: 7 additions & 31 deletions src/backend/events/installation/event.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,25 @@
import { auth } from '@wix/essentials';
import { collections } from '@wix/data';
import { appInstances } from '@wix/app-management';
import { CHECKOUT_COLLECTION_ID, SETTINGS_COLLECTION_ID } from '../../consts';
import { JEWELRY_COLLECTION_ID } from '../../consts';
import { Jewel } from '../../../types';

appInstances.onAppInstanceInstalled(() => {
auth.elevate(collections.createDataCollection)({
_id: SETTINGS_COLLECTION_ID,
displayName: "Carbon Offset Settings",
_id: JEWELRY_COLLECTION_ID,
displayName: 'Jewelry',
fields: [
{ key: 'title', type: collections.Type.TEXT },
{ key: 'amount', type: collections.Type.NUMBER },
{ key: 'color', type: collections.Type.TEXT },
{ key: 'iconColor', type: collections.Type.TEXT },
{ key: 'jewelry', type: collections.Type.TEXT },
],
permissions: {
// Make sure to change the permissions according to the actual usage of your collection
insert: collections.Role.ANYONE,
read: collections.Role.ANYONE,
remove: collections.Role.ANYONE,
update: collections.Role.ANYONE,
},
// Plugin for single item collection
plugins: [{
type: collections.PluginType.SINGLE_ITEM,
singleItemOptions: {
singleItemId: "SETTINGS"
},
}],
});

auth.elevate(collections.createDataCollection)({
_id: CHECKOUT_COLLECTION_ID,
displayName: "Carbon Offset Checkout",
fields: [
// In this case, checkoutId is stored as an "added" field that is not neccessarry
// the actual _id for each item of this collection will be purchaseFlowId for easy fetching
{ key: 'checkoutId', type: collections.Type.TEXT },
{ key: 'shouldAdd', type: collections.Type.BOOLEAN },
],
permissions: {
// Make sure to change the permissions according to the actual usage of your collection
//TODO: Make sure to change the permissions according to the actual usage of your collection
insert: collections.Role.ANYONE,
read: collections.Role.ANYONE,
remove: collections.Role.ANYONE,
update: collections.Role.ANYONE,
},
});
//TODO: Add initial data to the collection
});
94 changes: 0 additions & 94 deletions src/components/carbon-offset.tsx

This file was deleted.

Loading