Skip to content

Commit

Permalink
maybe fix gql typings
Browse files Browse the repository at this point in the history
  • Loading branch information
a-type committed Aug 9, 2024
1 parent 474ae59 commit 5b63113
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
48 changes: 47 additions & 1 deletion apps/wish-wash/web/src/components/items/ItemEditDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ import { preventDefault } from '@a-type/utils';
import { ImageUploader } from '@a-type/ui/components/imageUploader';
import { ItemExpirationEditor } from './ItemExpirationEditor.jsx';
import { NumberStepper } from '@a-type/ui/components/numberStepper';
import { ReactNode } from 'react';
import { ReactNode, useCallback } from 'react';
import { Icon } from '@a-type/ui/components/icon';
import { clsx } from '@a-type/ui';
import { withClassName } from '@a-type/ui/hooks';
import { ScrollArea } from '@a-type/ui/components/scrollArea/ScrollArea';
import {
graphql,
useCanSync,
useLazyQuery,
useMutation,
} from '@biscuits/client';

export interface ItemEditDialogProps {
list: List;
Expand Down Expand Up @@ -222,9 +228,45 @@ function CountField({ item }: { item: Item }) {
);
}

const scanPage = graphql(`
query ScanStorePage($url: String!) {
storePageScan(input: { url: $url }) {
currency
price
productName
scanner
}
}
`);

function SingleLinkField({ item }: { item: Item }) {
const { links } = hooks.useWatch(item);
hooks.useWatch(links);
const firstLink = links.get(0) ?? null;

const subscribed = useCanSync();
const [doScan, { loading: scanning }] = useLazyQuery(scanPage);
const maybeScanPage = useCallback(async () => {
// don't scan if data already exists
if (item.get('description')) {
return;
}

const result = await doScan({
variables: {
url: firstLink,
},
});
if (result.data?.storePageScan) {
const scan = result.data.storePageScan;
console.log('scan result', scan);
item.update({
description: scan.productName ?? undefined,
});
} else {
console.error('scan failed', result.error);
}
}, [firstLink, item]);

return (
<>
Expand All @@ -234,8 +276,12 @@ function SingleLinkField({ item }: { item: Item }) {
value={links.get(0) || ''}
type="url"
onChange={(e) => links.set(0, e.currentTarget.value)}
onBlur={maybeScanPage}
autoSelect
/>
{scanning && (
<span className="text-xs text-gray-5">Scanning page...</span>
)}
</>
);
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"kill-port": "2.0.1",
"prettier": "3.2.5",
"turbo": "^1.9.3",
"typescript": "^5.4.5"
"typescript": "^5.4.5",
"gql.tada": "1.8.5"
}
}
Loading

0 comments on commit 5b63113

Please sign in to comment.