Skip to content
This repository was archived by the owner on Jul 23, 2024. It is now read-only.

Commit 108068e

Browse files
committed
created dropdown menu and refetched defualt query after mutation so that page updates instantly
1 parent f62e453 commit 108068e

5 files changed

Lines changed: 130 additions & 80 deletions

File tree

.devcontainer/docker-compose.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ services:
77
dockerfile: Dockerfile.backend
88
volumes:
99
- ..:/workspace:z
10-
command: cargo run --manifest-path /workspace/backend/Cargo.toml --release -p pin_packing serve --database-url postgres://postgres:password@postgres/pin_packing
10+
command: sleep infinity
1111
environment:
1212
OPA_URL: http://opa:8181
1313
DATABASE_URL: postgres://postgres:password@postgres
1414
RABBITMQ_URL: amqp://rabbitmq:password@rabbitmq
15-
ports:
16-
- "8080:80"
1715

1816
frontend:
1917
image: docker.io/library/node:20.6.0-bookworm

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@
33

44
# Developer Tooling
55
.vscode
6-
7-
# Generated Schema
8-
pin_packing.graphql

frontend/src/App.tsx

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-template-curly-in-string */
2-
import { useQuery } from "@apollo/client";
2+
import { useMutation, useQuery } from "@apollo/client";
33
import { gql } from './__generated__/gql';
44
import React from "react";
55
import { theme } from "@diamondlightsource/ui-components"
@@ -27,6 +27,110 @@ query pinInfo ($after: String) {
2727
}
2828
`);
2929

30+
const UPDATE_PIN_STATUS = gql(`
31+
mutation updatePinStatus($barcode: String!, $status: PinStatus!) {
32+
updateLibraryPinStatus(barcode: $barcode, status: $status) {
33+
barcode
34+
status
35+
}
36+
}
37+
`);
38+
39+
const pinFragment = gql(`
40+
fragment pin on LibraryPin {
41+
barcode,
42+
status
43+
}
44+
`)
45+
46+
47+
const libraryPinsFragment = gql(`
48+
fragment pinPage on LibraryPinConnection {
49+
50+
edges {
51+
cursor
52+
node {
53+
...pin
54+
loopSize,
55+
56+
}
57+
}
58+
}
59+
60+
`)
61+
62+
63+
64+
export interface UpdatePinStatusProps {
65+
item: Record<string, any>
66+
}
67+
68+
const UpdatePinStatus = ({item }: UpdatePinStatusProps) => {
69+
70+
const [status, setStatus] = React.useState(item['status']);
71+
72+
const handleStatusChange = (event) => {
73+
setStatus(event.target.value);
74+
};
75+
76+
const textColor = useColorModeValue('#e3e3e3', 'black')
77+
const bgColor = useColorModeValue('#525252', '#c2c2c4')
78+
79+
80+
81+
const [
82+
updateLibraryPinStatus,
83+
{ error: mutationError }
84+
] = useMutation(UPDATE_PIN_STATUS, {
85+
86+
update(cache, {data: {updateLibraryPinStatus}}) {
87+
88+
cache.writeFragment({
89+
fragment: pinFragment,
90+
data: updateLibraryPinStatus,
91+
fragmentName: "pin",
92+
});
93+
94+
const libraryPins = cache.readFragment({
95+
id: "barcode",
96+
fragment: libraryPinsFragment,
97+
fragmentName: 'pinPage'
98+
});
99+
if (libraryPins) {
100+
const newEdges = [...libraryPins.edges, updateLibraryPinStatus]
101+
cache.writeFragment({
102+
id: "barcode",
103+
fragment: libraryPinsFragment,
104+
fragmentName: 'pinPage',
105+
data: { ...libraryPins, edges: newEdges }
106+
});
107+
}
108+
}
109+
})
110+
111+
return (
112+
<div>
113+
<form
114+
onSubmit={e => {
115+
e.preventDefault();
116+
updateLibraryPinStatus({ variables: { barcode: item['barcode'], status: status } });
117+
118+
;
119+
}}
120+
>
121+
<Select value={status} onChange={handleStatusChange} textColor={textColor} bgColor={bgColor}>
122+
<option value='READY'>READY</option>
123+
<option value='OCCUPIED'>OCCUPIED</option>
124+
<option value='DIRTY'>DIRTY</option>
125+
<option value='BROKEN'>BROKEN</option>
126+
</Select>
127+
<button type="submit">Update Status</button>
128+
</form>
129+
{mutationError && <p>Error :( Please try again</p>}
130+
</div>
131+
)
132+
}
133+
30134
// Displays libraryPins query in table component. The table can load more data if required
31135
function DisplayPinInfo(): React.JSX.Element {
32136
const { loading, error, data, fetchMore } = useQuery(
@@ -128,3 +232,6 @@ export default function App(): React.JSX.Element {
128232
</ChakraProvider>
129233
);
130234
}
235+
236+
export {GET_INFO}
237+
export { UpdatePinStatus }

frontend/src/components/Table.tsx

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EditIcon } from "@chakra-ui/icons";
22
import { Box, BoxProps, HStack, Heading, Skeleton, Table, Tbody, Td, Th, Thead, Tr, Text, IconButton } from "@chakra-ui/react";
33
import React, { useCallback, useState } from "react";
4-
import { UpdatePinStatus } from "./UpdatePinStatus";
4+
import { UpdatePinStatus } from "../App";
55

66
export interface TableProps extends Omit<BoxProps, "onClick"> {
77
/** Table data */
@@ -43,17 +43,17 @@ const TableView = ({
4343
const [show, setShow] = useState(true);
4444

4545
return (
46-
<Box overflowY='scroll' {...props}>
46+
<Box overflowY='scroll' {...props} >
4747
{data === null || data.length === 0 ? (
4848
<Heading py={10} w='100%' variant='notFound'>
4949
No {label.toLowerCase()} found
5050
</Heading>
5151
) : (
5252
<Table size='sm' variant={rowVariant}>
53-
<Thead>
54-
<Tr>
53+
<Thead >
54+
<Tr >
5555
{headers.map((header) => (
56-
<Th key={header.label}>{header.label}</Th>
56+
<Th key={header.label} >{header.label} </Th>
5757
))}
5858
</Tr>
5959
</Thead>
@@ -62,26 +62,25 @@ const TableView = ({
6262
{data.map((item, i) => (
6363
<Tr h='2vh' key={i} onClick={handleClick}>
6464
{headers.map((header) => (
65-
<Td data-id={i} key={header.key}>
66-
<HStack spacing={2}>
65+
<Td width='25%' data-id={i} key={header.key}>
66+
<HStack spacing={2} >
6767
<Text>
68-
{item[header.key]}
69-
</Text>
70-
<Text>
71-
{header.key === 'status' ?
72-
<IconButton
73-
aria-label='editButton'
74-
icon={<EditIcon color={'white'}/>}
75-
colorScheme='black'
76-
variant={'unstyled'}
77-
onClick={() => setShow(!show)}
78-
/>
79-
: null}
80-
81-
</Text>
68+
{item[header.key]}
69+
</Text>
70+
<Text>
71+
{header.key === 'status' ?
72+
<IconButton
73+
aria-label='editButton'
74+
icon={<EditIcon color={'white'}/>}
75+
colorScheme='black'
76+
variant={'unstyled'}
77+
onClick={() => setShow(!show)}
78+
/>
79+
: null}
80+
</Text>
8281
</HStack>
8382
<Text>
84-
{header.key === 'status' && show ? <UpdatePinStatus item={item}/> : null}
83+
{header.key === 'status' && show ? <UpdatePinStatus item={item}/> : null}
8584
</Text>
8685
</Td>
8786
))}

frontend/src/components/UpdatePinStatus.tsx

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)