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

Commit

Permalink
created UpdatePinStatus mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
kaimsfd committed Dec 4, 2023
1 parent c0377bc commit f62e453
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 56 deletions.
55 changes: 2 additions & 53 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-template-curly-in-string */
import { useMutation, useQuery } from "@apollo/client";
import { useQuery } from "@apollo/client";
import { gql } from './__generated__/gql';
import React from "react";
import { theme } from "@diamondlightsource/ui-components"
Expand Down Expand Up @@ -27,57 +27,6 @@ query pinInfo ($after: String) {
}
`);

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

function UpdatePin() {
const { loading, error, data } = useQuery(
GET_INFO,
{
notifyOnNetworkStatusChange: true,
});
const [
updateLibraryPinStatus,
{ loading: mutationLoading, error: mutationError }
] = useMutation(UPDATE_PIN_STATUS);

if (loading) return <p>Loading...</p>;
if (error) return <p>Error :(</p>;

return data.libraryPins.edges.map((edge) => {
let input;

return (
<div >
<p></p>
<form
onSubmit={e => {
e.preventDefault();
updateLibraryPinStatus({ variables: { barcode: edge.node.barcode, status: input.value } });

input.value = "";
}}
>
<input
ref={node => {
input = node;
}}
/>
<button type="submit">Update Todo</button>
</form>
{mutationLoading && <p>Loading...</p>}
{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 Down Expand Up @@ -175,7 +124,7 @@ export default function App(): React.JSX.Element {
return (
<ChakraProvider theme={theme}>
<DisplayPinInfo />
<UpdatePin/>
{/* <UpdatePin/> */}
</ChakraProvider>
);
}
21 changes: 18 additions & 3 deletions frontend/src/components/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EditIcon } from "@chakra-ui/icons";
import { Box, BoxProps, HStack, Heading, Skeleton, Spacer, Stack, Table, Tbody, Td, Th, Thead, Tr, Text } from "@chakra-ui/react";
import React, { useCallback } from "react";
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 "./UpdatePinStatus";

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

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

return (
<Box overflowY='scroll' {...props}>
{data === null || data.length === 0 ? (
Expand All @@ -65,9 +68,21 @@ const TableView = ({
{item[header.key]}
</Text>
<Text>
{header.key === 'status' ? <EditIcon/> : []}
{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
51 changes: 51 additions & 0 deletions frontend/src/components/UpdatePinStatus.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useMutation } from "@apollo/client";
import React from "react";
import { gql } from "../__generated__/gql";


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

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

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

const [
updateLibraryPinStatus,
{ loading: mutationLoading, error: mutationError }
] = useMutation(UPDATE_PIN_STATUS);

let input;

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

input.value = "";
}}
>
<input
ref={node => {
input = node;
}}
/>
<button type="submit">Update Todo</button>
</form>
{mutationLoading && <p>Loading...</p>}
{mutationError && <p>Error :( Please try again</p>}
</div>
)
}

export { UpdatePinStatus }

0 comments on commit f62e453

Please sign in to comment.