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

Commit

Permalink
created dropdown menu and refetched defualt query after mutation so t…
Browse files Browse the repository at this point in the history
…hat page updates instantly
  • Loading branch information
kaimsfd committed Dec 5, 2023
1 parent 926aaf3 commit 50b3615
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 772 deletions.
4 changes: 1 addition & 3 deletions .devcontainer/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ services:
dockerfile: Dockerfile.backend
volumes:
- ..:/workspace:z
command: cargo run --manifest-path /workspace/backend/Cargo.toml --release -p pin_packing serve --database-url postgres://postgres:password@postgres/pin_packing
command: sleep infinity
environment:
OPA_URL: http://opa:8181
DATABASE_URL: postgres://postgres:password@postgres
RABBITMQ_URL: amqp://rabbitmq:password@rabbitmq
ports:
- "8080:80"

frontend:
image: docker.io/library/node:20.6.0-bookworm
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@

# Developer Tooling
.vscode

# Generated Schema
pin_packing.graphql
8 changes: 2 additions & 6 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@
"build": "node scripts/build.js",
"test": "node scripts/test.js",
"format": "prettier --write **/*.js",
"eslint-lint": "node scripts/lint.js",
"compile": "graphql-codegen",
"watch": "graphql-codegen -w"
"eslint-lint": "node scripts/lint.js"
},
"eslintConfig": {
"extends": [
Expand Down Expand Up @@ -92,9 +90,7 @@
"graphql": "^16.3.0",
"http-proxy-middleware": "^2.0.6",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-helmet": "^6.1.0",
"react-toggle-dark-mode": "^1.1.1"
"react-dom": "^18.2.0"
},
"jest": {
"roots": [
Expand Down
108 changes: 99 additions & 9 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable no-template-curly-in-string */
import { useQuery } from "@apollo/client";
import { useMutation, useQuery } from "@apollo/client";
import { gql } from './__generated__/gql';
import React from "react";
import { theme } from "@diamondlightsource/ui-components"
import { ChakraProvider, Alert, AlertIcon, AlertTitle, AlertDescription, Button, HStack } from "@chakra-ui/react";
import { ChakraProvider, Alert, AlertIcon, AlertTitle, AlertDescription, Button, HStack, Select } from "@chakra-ui/react";
import { PaginationTable } from "./components/PaginationTable";
import { PinStatus } from "./__generated__/graphql";

const GET_INFO = gql(`
query pinInfo ($after: String) {
Expand All @@ -27,6 +27,98 @@ query pinInfo ($after: String) {
}
`);

const UPDATE_PIN_STATUS = gql(`
mutation updatePinStatus($barcode: String!, $status: PinStatus!) {
updateLibraryPinStatus(barcode: $barcode, status: $status) {
barcode
status
}
}
`);

const PIN_FRAGMENT = gql(`
fragment pin on LibraryPin {
barcode,
status
}
`)


const LIBRARY_PINS_FRAGMENT = gql(`
fragment pinPage on LibraryPinConnection {
edges {
cursor
node {
...pin
loopSize,
}
}
}
`)

export interface UpdatePinStatusProps {
item: Record<string, any>
}

const UpdatePinStatus = ({item }: UpdatePinStatusProps) => {

const [status, setStatus] = React.useState(item['status']);

const handleStatusChange = (event) => {
setStatus(event.target.value);
};
const [
updateLibraryPinStatus,
{ error: mutationError }
] = useMutation(UPDATE_PIN_STATUS, {

update(cache, {data: {updateLibraryPinStatus}}) {

cache.writeFragment({
fragment: PIN_FRAGMENT,
data: updateLibraryPinStatus,
fragmentName: "pin",
});

const libraryPins = cache.readFragment({
id: "barcode",
fragment: LIBRARY_PINS_FRAGMENT,
fragmentName: 'pinPage'
});
if (libraryPins) {
const newEdges = [...libraryPins.edges, updateLibraryPinStatus]
cache.writeFragment({
id: "barcode",
fragment: LIBRARY_PINS_FRAGMENT,
fragmentName: 'pinPage',
data: { ...libraryPins, edges: newEdges }
});
}
}
})

return (
<div>
<form
onSubmit={e => {
e.preventDefault();
updateLibraryPinStatus({ variables: { barcode: item['barcode'], status: status } });

;
}}
>
<Select value={status} onChange={handleStatusChange}>
{Object.values(PinStatus).map((status) => (
<option value={status}>{status}</option>))}
</Select>
<button type="submit">Update Status</button>
</form>
{mutationError && <p>Error :( Please try again</p>}
</div>
)
}

// Displays libraryPins query in table component. The table can load more data if required
function DisplayPinInfo(): React.JSX.Element {
const { loading, error, data, fetchMore } = useQuery(
Expand All @@ -35,10 +127,7 @@ function DisplayPinInfo(): React.JSX.Element {
notifyOnNetworkStatusChange: true,
});

const { toggleColorMode } = useColorMode()

var loadingRows = loading ? 2 : 0
const bgColour = useColorModeValue('white', 'black')

if (error) return (
<Alert status='error'>
Expand Down Expand Up @@ -104,7 +193,7 @@ function DisplayPinInfo(): React.JSX.Element {
rowVariant={"diamondStriped"}
/>
<HStack justify='center' width='100%'>
<Button marginTop={'10px'}
<Button marginTop={'1em'}
colorScheme='teal'
variant='outline'
onClick={loadMore}
Expand All @@ -115,7 +204,6 @@ function DisplayPinInfo(): React.JSX.Element {
Load More
</Button>
</HStack>
</div>
</>
);
}
Expand All @@ -124,7 +212,9 @@ export default function App(): React.JSX.Element {
return (
<ChakraProvider theme={theme}>
<DisplayPinInfo />
{/* <UpdatePin/> */}
</ChakraProvider>
);
}

export {GET_INFO}
export { UpdatePinStatus }
24 changes: 0 additions & 24 deletions frontend/src/components/DarkModeButton.tsx

This file was deleted.

27 changes: 25 additions & 2 deletions frontend/src/components/PaginationTable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Box, BoxProps, Heading, Skeleton, Table, Tbody, Td, Th, Thead, Tr } from "@chakra-ui/react";
import React, { useCallback } from "react";
import { EditIcon } from "@chakra-ui/icons";
import { Box, BoxProps, HStack, Heading, Skeleton, Table, Tbody, Td, Th, Thead, Tr, Text, IconButton } from "@chakra-ui/react";
import React, { useCallback, useState } from "react";
import { UpdatePinStatus } from "../App";

export interface TableProps extends Omit<BoxProps, "onClick"> {
/** Table data */
Expand Down Expand Up @@ -38,6 +40,8 @@ export const PaginationTable = ({
[data, onClick],
);

const [show, setShow] = useState(true);

return (
<Box overflowY='scroll' {...props}>
{data === null || data.length === 0 ? (
Expand All @@ -58,7 +62,26 @@ export const PaginationTable = ({
<Tr h='2vh' key={i} onClick={handleClick}>
{headers.map((header) => (
<Td data-id={i} key={header.key}>
<HStack spacing={2}>
<Text>
{item[header.key]}
</Text>
<Text>
{header.key === 'status' ?
<IconButton
aria-label='editButton'
icon={<EditIcon color={'white'}/>}
colorScheme='black'
variant={'unstyled'}
onClick={() => setShow(!show)}
/>
: null}

</Text>
</HStack>
<Text>
{header.key === 'status' && show ? <UpdatePinStatus item={item}/> : null}
</Text>
</Td>
))}
</Tr>
Expand Down
108 changes: 0 additions & 108 deletions frontend/src/components/Table.tsx

This file was deleted.

Loading

0 comments on commit 50b3615

Please sign in to comment.