|
| 1 | +import { getSmartAccountWalletClient } from '@/lib/smart-account'; |
| 2 | +import { type GeoSmartAccount, Graph, type Op } from '@graphprotocol/grc-20'; |
| 3 | +import { publishOps, useHypergraphSpace } from '@graphprotocol/hypergraph-react'; |
| 4 | +import { useState } from 'react'; |
| 5 | +import { Button } from './ui/button'; |
| 6 | +import { Card, CardContent } from './ui/card'; |
| 7 | + |
| 8 | +const createPropertiesAndTypesEvent = async ({ |
| 9 | + smartAccountWalletClient, |
| 10 | + space, |
| 11 | +}: { smartAccountWalletClient: GeoSmartAccount; space: string }) => { |
| 12 | + const ops: Array<Op> = []; |
| 13 | + const { id: salaryPropertyId, ops: createSalaryPropertyOps } = Graph.createProperty({ |
| 14 | + dataType: 'NUMBER', |
| 15 | + name: 'Salary', |
| 16 | + }); |
| 17 | + ops.push(...createSalaryPropertyOps); |
| 18 | + |
| 19 | + const { id: jobOfferTypeId, ops: createJobOfferTypeOps } = Graph.createType({ |
| 20 | + name: 'Job Offer', |
| 21 | + properties: [salaryPropertyId], |
| 22 | + }); |
| 23 | + ops.push(...createJobOfferTypeOps); |
| 24 | + |
| 25 | + const { id: jobOffersRelationTypeId, ops: createJobOffersRelationTypeOps } = Graph.createProperty({ |
| 26 | + dataType: 'RELATION', |
| 27 | + name: 'Job Offer', |
| 28 | + relationValueTypes: [jobOfferTypeId], |
| 29 | + }); |
| 30 | + ops.push(...createJobOffersRelationTypeOps); |
| 31 | + |
| 32 | + const { id: companyTypeId, ops: createCompanyTypeOps } = Graph.createType({ |
| 33 | + name: 'Company', |
| 34 | + properties: [salaryPropertyId, jobOffersRelationTypeId], |
| 35 | + }); |
| 36 | + ops.push(...createCompanyTypeOps); |
| 37 | + |
| 38 | + const { id: sponsorsRelationTypeId, ops: createSponsorsRelationTypeOps } = Graph.createProperty({ |
| 39 | + dataType: 'RELATION', |
| 40 | + name: 'Sponsor', |
| 41 | + relationValueTypes: [companyTypeId], |
| 42 | + }); |
| 43 | + ops.push(...createSponsorsRelationTypeOps); |
| 44 | + |
| 45 | + const { id: eventTypeId, ops: createEventTypeOps } = Graph.createType({ |
| 46 | + name: 'Event', |
| 47 | + properties: [sponsorsRelationTypeId], |
| 48 | + }); |
| 49 | + ops.push(...createEventTypeOps); |
| 50 | + |
| 51 | + const result = await publishOps({ |
| 52 | + ops, |
| 53 | + walletClient: smartAccountWalletClient, |
| 54 | + space, |
| 55 | + name: 'Create properties and types', |
| 56 | + network: 'TESTNET', |
| 57 | + }); |
| 58 | + return { |
| 59 | + result, |
| 60 | + eventTypeId, |
| 61 | + companyTypeId, |
| 62 | + jobOfferTypeId, |
| 63 | + salaryPropertyId, |
| 64 | + jobOffersRelationTypeId, |
| 65 | + sponsorsRelationTypeId, |
| 66 | + }; |
| 67 | +}; |
| 68 | + |
| 69 | +export const CreatePropertiesAndTypesEvent = () => { |
| 70 | + const [mapping, setMapping] = useState<string>(''); |
| 71 | + const space = useHypergraphSpace(); |
| 72 | + |
| 73 | + return ( |
| 74 | + <div> |
| 75 | + {mapping && ( |
| 76 | + <Card> |
| 77 | + <CardContent> |
| 78 | + <pre>{mapping}</pre> |
| 79 | + </CardContent> |
| 80 | + </Card> |
| 81 | + )} |
| 82 | + <Button |
| 83 | + onClick={async () => { |
| 84 | + const smartAccountWalletClient = await getSmartAccountWalletClient(); |
| 85 | + if (!smartAccountWalletClient) { |
| 86 | + throw new Error('Missing smartAccountWalletClient'); |
| 87 | + } |
| 88 | + const { |
| 89 | + eventTypeId, |
| 90 | + companyTypeId, |
| 91 | + jobOfferTypeId, |
| 92 | + salaryPropertyId, |
| 93 | + jobOffersRelationTypeId, |
| 94 | + sponsorsRelationTypeId, |
| 95 | + } = await createPropertiesAndTypesEvent({ |
| 96 | + // @ts-expect-error TODO: in the future we probably only only use one smart account wallet client |
| 97 | + smartAccountWalletClient, |
| 98 | + space, |
| 99 | + }); |
| 100 | + |
| 101 | + const newMapping = `Event: { |
| 102 | + typeIds: [Id.Id('${eventTypeId}')], |
| 103 | + properties: { |
| 104 | + name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'), |
| 105 | + }, |
| 106 | + relations: { |
| 107 | + sponsors: Id.Id('${sponsorsRelationTypeId}'), |
| 108 | + }, |
| 109 | +}, |
| 110 | +Company: { |
| 111 | + typeIds: [Id.Id('${companyTypeId}')], |
| 112 | + properties: { |
| 113 | + name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'), |
| 114 | + jobOffers: Id.Id('${jobOffersRelationTypeId}'), |
| 115 | + }, |
| 116 | +}, |
| 117 | +JobOffer: { |
| 118 | + typeIds: [Id.Id('${jobOfferTypeId}')], |
| 119 | + properties: { |
| 120 | + name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'), |
| 121 | + salary: Id.Id('${salaryPropertyId}'), |
| 122 | + }, |
| 123 | +}, |
| 124 | +`; |
| 125 | + setMapping(newMapping); |
| 126 | + }} |
| 127 | + > |
| 128 | + Create properties and types for Event, Company and JobOffer |
| 129 | + </Button> |
| 130 | + </div> |
| 131 | + ); |
| 132 | +}; |
0 commit comments