|
1 | 1 | /* eslint-disable no-template-curly-in-string */ |
2 | | -import { useQuery } from "@apollo/client"; |
| 2 | +import { useMutation, useQuery } from "@apollo/client"; |
3 | 3 | import { gql } from './__generated__/gql'; |
4 | 4 | import React from "react"; |
5 | 5 | import { theme } from "@diamondlightsource/ui-components" |
@@ -27,6 +27,110 @@ query pinInfo ($after: String) { |
27 | 27 | } |
28 | 28 | `); |
29 | 29 |
|
| 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 | + |
30 | 134 | // Displays libraryPins query in table component. The table can load more data if required |
31 | 135 | function DisplayPinInfo(): React.JSX.Element { |
32 | 136 | const { loading, error, data, fetchMore } = useQuery( |
@@ -128,3 +232,6 @@ export default function App(): React.JSX.Element { |
128 | 232 | </ChakraProvider> |
129 | 233 | ); |
130 | 234 | } |
| 235 | + |
| 236 | +export {GET_INFO} |
| 237 | +export { UpdatePinStatus } |
0 commit comments