Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable close with auto-ack #520

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/__mocks__/LogionClientMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export class LocRequestState {
getCurrentState() {
return this;
}
isRequester(account: any) {
return false;
}
isOwner(account: any) {
return false;
}
}

export class ClosedCollectionLoc extends LocRequestState {
Expand Down Expand Up @@ -155,11 +161,15 @@ export class OpenLoc extends EditableRequest {
voidLoc: any,
getVerifiedIssuers: () => Promise<VerifiedIssuerWithSelect[]>,
setCollectionFileRestrictedDelivery: (params: any) => Promise<OpenLoc>,
canClose: (autoAck: boolean) => boolean,
canAutoAck: () => boolean,
} = {
close: jest.fn(),
voidLoc: jest.fn(),
getVerifiedIssuers: () => Promise.resolve(issuers),
setCollectionFileRestrictedDelivery: () => Promise.resolve(this),
canClose: () => true,
canAutoAck: () => true,
};
}

Expand Down
7 changes: 6 additions & 1 deletion src/components/toggle/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default function Checkbox(props: Props) {
props.disabled ? "disabled" : undefined,
);
return (
<div role="checkbox" aria-checked={ props.checked } className={ className } onClick={() => !props.disabled && props.setChecked && props.setChecked(!props.checked)}></div>
<div
role="checkbox"
aria-checked={ props.checked }
className={ className }
onClick={() => !props.disabled && props.setChecked && props.setChecked(!props.checked)}
/>
);
}
17 changes: 16 additions & 1 deletion src/loc/CloseLocButton.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
.CloseLocButton .Button.close.btn.btn-primary {
background-image: none;
background-color: #152665;
}
}

.CloseLocButton .toggle-button-container {
display: flex;
}

.CloseLocButton .toggle-container {
display: flex;
padding-right: 20px;
padding-top: 7px;
}

.CloseLocButton .toggle-container p {
padding-right: 20px;
margin: 0;
}
137 changes: 68 additions & 69 deletions src/loc/CloseLocButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import { LocData, ItemStatus, HashString } from "@logion/client";

import { clickByName, expectNoDialogVisible } from '../tests';
Expand All @@ -15,100 +15,83 @@ jest.mock("./LocContext");

describe("CloseLocButton", () => {

it("does not close with draft items", async () => {
setLocItems([ metadataItem("DRAFT") ]);
let called = false;
const closeLocMock = async (params: any) => {
called = true;
params.callback(mockSubmittableResult(true));
return params.locState;
};
const locState = new OpenLoc();
locState.data = () => ({
locType: "Transaction",
status: "OPEN",
} as LocData);
locState.legalOfficer.close = closeLocMock;
setLocState(locState);

render(<CloseLocButton />);
beforeEach(() => {
closeCalled = false;
closeLocMock = successCloseLocMock;
});

it("does not close when not closeable", async () => {
renderGivenLoc(false, false);

await clickByName(/Close LOC/);

await expectNoDialogVisible();
expect(called).toBe(false);
expect(closeCalled).toBe(false);
})

it("closes with all items recorded", async () => {
setLocItems([ metadataItem("ACKNOWLEDGED") ]);
const locState = new OpenLoc();
locState.data = () => ({
locType: "Transaction",
status: "OPEN",
} as LocData);
let called = false;
const closeLocMock = async (params: any) => {
called = true;
params.callback(mockSubmittableResult(true));
return params.locState;
};
locState.legalOfficer.close = closeLocMock;
setLocState(locState);

render(<CloseLocButton />);
it("closes when closeable", async () => {
renderGivenLoc(true, false);

await clickByName(/Close LOC/);
await clickByName("Proceed");

await expectNoDialogVisible();
expect(called).toBe(true);
expect(closeCalled).toBe(true);
})

it("does not close on cancel", async () => {
setLocItems([ metadataItem("ACKNOWLEDGED") ]);
const locState = new OpenLoc();
locState.data = () => ({
locType: "Transaction",
status: "OPEN",
} as LocData);
let called = false;
const closeLocMock = async (params: any) => {
called = true;
params.callback(mockSubmittableResult(true));
return params.locState;
};
locState.legalOfficer.close = closeLocMock;
setLocState(locState);

render(<CloseLocButton />);
it("does not close on cancel and closeable", async () => {
renderGivenLoc(true, false);

await clickByName(/Close LOC/);
await clickByName("Cancel");

await expectNoDialogVisible();
expect(called).toBe(false);
expect(closeCalled).toBe(false);
})

it("shows message on error", async () => {
setLocItems([ metadataItem("ACKNOWLEDGED") ]);
const locState = new OpenLoc();
locState.data = () => ({
locType: "Transaction",
status: "OPEN",
} as LocData);
const closeLocMock = async (params: any) => {
params.callback(mockSubmittableResult(false, "Failed", true));
throw new Error();
};
locState.legalOfficer.close = closeLocMock;
setLocState(locState);

render(<CloseLocButton />);
closeLocMock = failureCloseLocMock;
renderGivenLoc(true, false);

await clickByName(/Close LOC/);
await clickByName("Proceed");

await clickByName("OK");
await expectNoDialogVisible();
})

it("enables auto-ack toggle", async () => {
renderGivenLoc(false, true);

const ackAllToggle = screen.getByRole("checkbox");

expect(ackAllToggle).not.toHaveClass("disabled");
})

it("disables auto-ack toggle", async () => {
renderGivenLoc(true, false);

const ackAllToggle = screen.getByRole("checkbox");

expect(ackAllToggle).toHaveClass("disabled");
})
})

function renderGivenLoc(canClose: boolean, canAck: boolean) {
setLocItems([ metadataItem(canClose ? "ACKNOWLEDGED" : "PUBLISHED") ]);
const locState = new OpenLoc();
locState.data = () => ({
locType: "Transaction",
status: "OPEN",
} as LocData);
locState.legalOfficer.close = closeLocMock;
locState.legalOfficer.canClose = () => canClose;
locState.legalOfficer.canAutoAck = () => canAck;
setLocState(locState);

render(<CloseLocButton />);
}

function metadataItem(status: ItemStatus): LocItem {
return new MetadataItem(
{
Expand All @@ -125,3 +108,19 @@ function metadataItem(status: ItemStatus): LocItem {
}
);
}

let closeCalled = false;

const successCloseLocMock = async (params: any) => {
closeCalled = true;
params.callback(mockSubmittableResult(true));
return params.locState;
};

const failureCloseLocMock = async (params: any) => {
closeCalled = true;
params.callback(mockSubmittableResult(false, "Failed", true));
throw new Error();
};

let closeLocMock = successCloseLocMock;
55 changes: 39 additions & 16 deletions src/loc/CloseLocButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useNavigate } from 'react-router-dom';
import { ProtectionRequest, OpenLoc } from "@logion/client";
import { Col, Row } from "react-bootstrap";
Expand All @@ -20,6 +20,7 @@ import { signAndSend } from "../logion-chain/Signature";
import ClientExtrinsicSubmitter, { Call, CallCallback } from "src/ClientExtrinsicSubmitter";

import './CloseLocButton.css';
import Checkbox from "src/components/toggle/Checkbox";

enum CloseStatus {
NONE,
Expand All @@ -45,11 +46,11 @@ export default function CloseLocButton(props: Props) {
const navigate = useNavigate();
const { accounts, axiosFactory, api, signer } = useLogionChain();
const { refreshRequests } = useLegalOfficerContext();
const { mutateLocState, locItems, loc } = useLocContext();
const { mutateLocState,locItems, loc, locState } = useLocContext();
const [ closeState, setCloseState ] = useState<CloseState>({ status: CloseStatus.NONE });
const [ call, setCall ] = useState<Call>();
const [ disabled, setDisabled ] = useState<boolean>(false);
const [ signAndSubmitVouch, setSignAndSubmitVouch ] = useState<SignAndSubmit>(null);
const [ autoAck, setAutoAck ] = useState(false);

useEffect(() => {
if (closeState.status === CloseStatus.CLOSE_PENDING) {
Expand All @@ -58,6 +59,7 @@ export default function CloseLocButton(props: Props) {
mutateLocState(async current => {
if(signer && current instanceof OpenLoc) {
return current.legalOfficer.close({
autoAck,
signer,
callback,
});
Expand All @@ -67,15 +69,15 @@ export default function CloseLocButton(props: Props) {
});
setCall(() => call);
}
}, [ mutateLocState, closeState, setCloseState, signer ]);
}, [ mutateLocState, closeState, setCloseState, signer, autoAck ]);

useEffect(() => {
if (locItems.findIndex(locItem => locItem.status !== "ACKNOWLEDGED") < 0) {
setDisabled(false)
const canClose = useMemo(() => {
if(locState instanceof OpenLoc) {
return locState.legalOfficer.canClose(autoAck);
} else {
setDisabled(true)
return false;
}
}, [ locItems, setDisabled ]);
}, [ locState, autoAck ]);

const alreadyVouched = useCallback(async (lost: string, rescuer: string, currentAddress: string) => {
const activeRecovery = await api!.queries.getActiveRecovery(
Expand Down Expand Up @@ -137,6 +139,14 @@ export default function CloseLocButton(props: Props) {
}
}, [ closeState, setCloseState, accounts, axiosFactory, loc, navigate, props.protectionRequest, refreshRequests ]);

const canAutoAck = useMemo(() => {
if(locState instanceof OpenLoc) {
return locItems.length > 0 && locState.legalOfficer.canAutoAck();
} else {
return false;
}
}, [ locItems, locState ]);

if(!loc) {
return null;
}
Expand Down Expand Up @@ -165,13 +175,26 @@ export default function CloseLocButton(props: Props) {
<div className="CloseLocButton">
{
loc.status === "OPEN" &&
<Button
onClick={ () => setCloseState({ status: firstStatus }) }
className="close"
disabled={ disabled }
>
<Icon icon={{ id: iconId }} height="19px" /><span className="text">{ closeButtonText }</span>
</Button>
<div className="toggle-button-container">
<div className="toggle-container">
<p>Acknowledge all?</p>
<Checkbox
skin="Toggle black"
checked={ autoAck }
setChecked={ (value) => setAutoAck(value) }
disabled={ !canAutoAck }
/>
</div>
<div className="button-container">
<Button
onClick={ () => setCloseState({ status: firstStatus }) }
className="close"
disabled={ !canClose }
>
<Icon icon={{ id: iconId }} height="19px" /><span className="text">{ closeButtonText }</span>
</Button>
</div>
</div>
}
<ProcessStep
active={ closeState.status === CloseStatus.ACCEPT }
Expand Down
2 changes: 1 addition & 1 deletion src/loc/LocDetailsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export function LocDetailsTabContent(props: ContentProps) {
{
viewer === "LegalOfficer" &&
<>
<Col className="close-button-container" xxl={ 3 } xl={ 4 }>
<Col className="close-button-container" xxl={ 5 } xl={ 6 }>
{
!loc.voidInfo &&
<CloseLocButton protectionRequest={ protectionRequest } />
Expand Down
Loading
Loading