-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into tsq-refactor-assets
- Loading branch information
Showing
6 changed files
with
229 additions
and
1 deletion.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/apiQueries/useUpdateOrgPaymentCancellationPeriodDays.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { API_URL } from "constants/settings"; | ||
import { fetchApi } from "helpers/fetchApi"; | ||
import { AppError } from "types"; | ||
|
||
export const useUpdateOrgPaymentCancellationPeriodDays = () => { | ||
const mutation = useMutation({ | ||
mutationFn: (cancellationPeriod: number) => { | ||
const formData = new FormData(); | ||
|
||
formData.append( | ||
"data", | ||
`{"payment_cancellation_period_days": ${cancellationPeriod}}`, | ||
); | ||
|
||
return fetchApi( | ||
`${API_URL}/organization`, | ||
{ | ||
method: "PATCH", | ||
body: formData, | ||
}, | ||
{ omitContentType: true }, | ||
); | ||
}, | ||
cacheTime: 0, | ||
}); | ||
|
||
return { | ||
...mutation, | ||
error: mutation.error as AppError, | ||
data: mutation.data as { message: string }, | ||
mutateAsync: async (cancellationPeriod: number) => { | ||
try { | ||
await mutation.mutateAsync(cancellationPeriod); | ||
} catch (e) { | ||
// do nothing | ||
} | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { useEffect, useState } from "react"; | ||
import { | ||
Button, | ||
Card, | ||
Input, | ||
Notification, | ||
Toggle, | ||
Loader, | ||
} from "@stellar/design-system"; | ||
import { useDispatch } from "react-redux"; | ||
|
||
import { DropdownMenu } from "components/DropdownMenu"; | ||
import { MoreMenuButton } from "components/MoreMenuButton"; | ||
|
||
import { useUpdateOrgPaymentCancellationPeriodDays } from "apiQueries/useUpdateOrgPaymentCancellationPeriodDays"; | ||
import { useRedux } from "hooks/useRedux"; | ||
import { AppDispatch } from "store"; | ||
import { getOrgInfoAction } from "store/ducks/organization"; | ||
|
||
export const SettingsEnablePaymentCancellation = () => { | ||
const { organization } = useRedux("organization"); | ||
|
||
const [paymentCancellationPeriodDays, setPaymentCancellationPeriodDays] = | ||
useState(0); | ||
const [isEditMode, setIsEditMode] = useState(false); | ||
|
||
const dispatch: AppDispatch = useDispatch(); | ||
|
||
const { mutateAsync, isLoading, error, isSuccess } = | ||
useUpdateOrgPaymentCancellationPeriodDays(); | ||
|
||
useEffect(() => { | ||
setPaymentCancellationPeriodDays( | ||
organization.data.paymentCancellationPeriodDays, | ||
); | ||
}, [organization.data.paymentCancellationPeriodDays]); | ||
|
||
useEffect(() => { | ||
if (isSuccess) { | ||
dispatch(getOrgInfoAction()); | ||
setIsEditMode(false); | ||
} | ||
}, [dispatch, isSuccess]); | ||
|
||
const handleToggleChange = () => { | ||
// Default period is 5 days | ||
mutateAsync(organization.data.paymentCancellationPeriodDays === 0 ? 5 : 0); | ||
}; | ||
|
||
const handlePaymentCancellationSubmit = ( | ||
event: React.FormEvent<HTMLFormElement>, | ||
) => { | ||
event.preventDefault(); | ||
mutateAsync(paymentCancellationPeriodDays); | ||
}; | ||
|
||
const handlePaymentCancellationReset = ( | ||
event: React.FormEvent<HTMLFormElement>, | ||
) => { | ||
event.preventDefault(); | ||
setIsEditMode(false); | ||
setPaymentCancellationPeriodDays( | ||
organization.data.paymentCancellationPeriodDays, | ||
); | ||
}; | ||
|
||
const renderContent = () => { | ||
return ( | ||
<div className="SdpSettings"> | ||
<div className="SdpSettings__row"> | ||
<div className="SdpSettings__item"> | ||
<label | ||
className="SdpSettings__label" | ||
htmlFor="payment-cancellation" | ||
> | ||
Enable automatic payments cancellation | ||
</label> | ||
<div className="Toggle__wrapper"> | ||
{isLoading ? <Loader size="1rem" /> : null} | ||
<Toggle | ||
id="payment-cancellation" | ||
checked={Boolean( | ||
organization.data.paymentCancellationPeriodDays, | ||
)} | ||
onChange={handleToggleChange} | ||
disabled={isLoading} | ||
/> | ||
</div> | ||
</div> | ||
<div className="Note"> | ||
Select this option to automatically cancel pending payments after a | ||
certain time period. Uncompleted payments will not be made once they | ||
are canceled, even if the receiver tries to claim funds. Completed | ||
payments are always final. | ||
</div> | ||
</div> | ||
|
||
{organization.data.paymentCancellationPeriodDays ? ( | ||
<div className="SdpSettings__row"> | ||
<form | ||
className="SdpSettings__form" | ||
onSubmit={handlePaymentCancellationSubmit} | ||
onReset={handlePaymentCancellationReset} | ||
> | ||
<div className="SdpSettings__form__row"> | ||
<Input | ||
fieldSize="sm" | ||
id="payment-cancellation-period" | ||
label="Payments Cancellation Period (days)" | ||
type="number" | ||
value={paymentCancellationPeriodDays} | ||
onChange={(e) => | ||
setPaymentCancellationPeriodDays(Number(e.target.value)) | ||
} | ||
disabled={!isEditMode} | ||
error={ | ||
paymentCancellationPeriodDays === 0 | ||
? "Cancellation period cannot be 0" | ||
: "" | ||
} | ||
/> | ||
{!isEditMode ? ( | ||
<DropdownMenu triggerEl={<MoreMenuButton />}> | ||
<DropdownMenu.Item onClick={() => setIsEditMode(true)}> | ||
Edit | ||
</DropdownMenu.Item> | ||
</DropdownMenu> | ||
) : null} | ||
</div> | ||
{isEditMode ? ( | ||
<div className="SdpSettings__form__buttons"> | ||
<Button | ||
variant="secondary" | ||
size="xs" | ||
type="reset" | ||
isLoading={isLoading} | ||
> | ||
Cancel | ||
</Button> | ||
<Button | ||
variant="primary" | ||
size="xs" | ||
type="submit" | ||
isLoading={isLoading} | ||
disabled={ | ||
!paymentCancellationPeriodDays || | ||
paymentCancellationPeriodDays === | ||
organization.data.paymentCancellationPeriodDays | ||
} | ||
> | ||
Update | ||
</Button> | ||
</div> | ||
) : null} | ||
</form> | ||
</div> | ||
) : null} | ||
</div> | ||
); | ||
}; | ||
|
||
return ( | ||
<> | ||
{error ? ( | ||
<Notification variant="error" title="Error"> | ||
{error.message} | ||
</Notification> | ||
) : null} | ||
|
||
<Card> | ||
<div className="CardStack__card">{renderContent()}</div> | ||
</Card> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters