Skip to content

Commit

Permalink
fix edit job dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
simke9445 committed Mar 19, 2024
1 parent dc10508 commit f65ae42
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

.description_inner
height: 100%

> *
height: 100%
align-items: flex-start !important
padding: 0 !important

.textarea_label
margin-top: 40px
margin-right: 16px

.description_input
height: 180px
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Button, Text } from 'components/primitives';
import { DialogProps, useDialog } from '@terra-money/apps/hooks';
import { useEditJobForm } from './useEditJobForm';
import { isMatch, isEmpty } from 'lodash';
import { isMatch } from 'lodash';
import { useMemo } from 'react';
import { InputAdornment } from '@mui/material';
import { TextInput } from 'components/primitives/text-input';
import { FormControl } from 'components/form-control/FormControl';
import { Form } from 'components/form/Form';
import { Dialog, DialogBody, DialogFooter, DialogHeader } from 'components/dialog';
import { AmountInput } from '../../inputs/AmountInput';
import { microfy } from '@terra-money/apps/libs/formatting';
import { Job } from 'types/job';
import { useEditJobTx } from 'tx';
import { useNativeToken } from 'hooks/useNativeToken';

import styles from './EditJobDialog.module.sass';

export type EditJobDialogProps = {
job: Job;
Expand All @@ -23,21 +22,9 @@ export const EditJobDialog = (props: DialogProps<EditJobDialogProps>) => {

const [txResult, editJobTx] = useEditJobTx();

const [input, { name, nameError, reward, rewardError, rewardValid, submitDisabled, balance, balanceLoading }] =
useEditJobForm(job);

const updatedJob = useMemo(
() => ({
...job,
name,
reward,
}),
[job, name, reward]
);

const jobModified = useMemo(() => !isMatch(updatedJob, job), [updatedJob, job]);
const [input, { name, nameError, submitDisabled, description, descriptionError }] = useEditJobForm(job);

const nativeToken = useNativeToken();
const jobModified = useMemo(() => !isMatch(job, { name, description }), [name, description, job]);

return (
<Dialog>
Expand All @@ -63,20 +50,29 @@ export const EditJobDialog = (props: DialogProps<EditJobDialogProps>) => {
}}
/>
</FormControl>
<AmountInput
label="Increase reward by"
value={reward}
onChange={(value) =>
input({
reward: value.target.value,
})
}
balance={balance}
balanceLoading={balanceLoading}
error={rewardError}
token={nativeToken}
valid={rewardValid}
/>
<FormControl label="Description" className={styles.description_input}>
<TextInput
placeholder="Type a comprehensive description of the job. Your precise details will help us tailor AI assistance."
margin="none"
className={styles.description_inner}
multiline={true}
value={description}
onChange={(value) => {
input({ description: value.target.value });
}}
helperText={descriptionError}
error={descriptionError !== undefined}
InputProps={{
endAdornment: (
<InputAdornment position="end">
{description && description.length > 0 && (
<Text className={styles.textarea_label} variant="label">{`${description?.length ?? 0}/200`}</Text>
)}
</InputAdornment>
),
}}
/>
</FormControl>
</DialogBody>
<DialogFooter>
<Button
Expand All @@ -87,8 +83,8 @@ export const EditJobDialog = (props: DialogProps<EditJobDialogProps>) => {
if (jobModified) {
const resp = await editJobTx({
name,
description,
jobId: job.info.id,
reward: !isEmpty(reward) ? microfy(reward, nativeToken.decimals) : undefined,
});

if (resp.code !== 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,82 +1,30 @@
import {
FormFunction,
FormInitializer,
FormModifier,
LocalWallet,
useForm,
useLocalWallet,
} from '@terra-money/apps/hooks';
import { microfy } from '@terra-money/apps/libs/formatting';
import { fetchTokenBalance } from '@terra-money/apps/queries';
import { Token, u } from '@terra-money/apps/types';
import Big from 'big.js';
import { useNativeToken } from 'hooks/useNativeToken';
import { FormFunction, useForm } from '@terra-money/apps/hooks';
import { useMemo } from 'react';
import { Job } from 'types/job';

interface EditJobInput {
name: string;
reward: string;
description: string;
}

interface EditJobState extends EditJobInput {
nameError?: string;
rewardError?: string;
descriptionError?: string;
nameValid?: boolean;
rewardValid?: boolean;
descriptionValid?: boolean;
submitDisabled: boolean;
balance: u<Big>;
balanceLoading: boolean;
}

const dispatchBalance = async (dispatch: FormModifier<EditJobState>, localWallet: LocalWallet, token: Token) => {
dispatch({
reward: '',
balance: Big(0) as u<Big>,
balanceLoading: true,
});

try {
const balance = await fetchTokenBalance(localWallet.lcd, token, localWallet.walletAddress);

dispatch({
reward: '',
balance: balance as u<Big>,
balanceLoading: false,
});
} catch {
dispatch({
reward: '',
balance: Big(0) as u<Big>,
balanceLoading: false,
});
}
};

export const useEditJobForm = (job: Job) => {
const initialValue = useMemo<EditJobState>(
() => ({
name: job.info.name,
reward: job.info.reward,
description: job.info.description,
submitDisabled: true,
balance: Big(0) as u<Big>,
balanceLoading: false,
}),
[job]
);

const wallet = useLocalWallet();

const nativeToken = useNativeToken();

const initializer: FormInitializer<EditJobState> = async (_, dispatch) => {
if (wallet.connectedWallet === undefined) {
throw Error('The wallet is not connected');
}

dispatchBalance(dispatch, wallet, nativeToken);
};

const form: FormFunction<EditJobInput, EditJobState> = async (input, getState, dispatch) => {
const state = {
...getState(),
Expand All @@ -85,24 +33,20 @@ export const useEditJobForm = (job: Job) => {

const nameError = state.name.length > 140 ? 'The name can not exceed the maximum of 140 characters' : undefined;

const uReward = input.reward ? microfy(input.reward, nativeToken.decimals) : Big(0);

const rewardError = uReward.gt(state.balance) ? 'The amount can not exceed the maximum balance' : undefined;

const rewardValid = uReward.gt(0) && rewardError === undefined;
const descriptionError =
state.description.length > 200 ? 'The description can not exceed the maximum of 200 characters' : undefined;

const submitDisabled = Boolean(
state.name === undefined || state.name === null || state.name.length < 1 || nameError
state.name === undefined || state.name === null || state.name.length < 1 || nameError || descriptionError
);

dispatch({
...state,
nameError,
rewardError,
rewardValid,
descriptionError,
submitDisabled,
});
};

return useForm<EditJobInput, EditJobState>(form, initialValue, initializer);
return useForm<EditJobInput, EditJobState>(form, initialValue);
};
10 changes: 3 additions & 7 deletions apps/warp-protocol/src/tx/useEditJobTx.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import { useTx } from '@terra-money/apps/libs/transactions';
import { u } from '@terra-money/apps/types';
import Big from 'big.js';
import { useWarpSdk } from '@terra-money/apps/hooks';
import { TX_KEY } from './txKey';
import { warp_resolver } from '@terra-money/warp-sdk';

interface EditJobTx {
name?: string;
reward?: u<Big>;
description?: string;
jobId: string;
condition?: warp_resolver.Condition;
}

export const useEditJobTx = () => {
const sdk = useWarpSdk();

return useTx<EditJobTx>(
async (options) => {
const { wallet, jobId, name, reward } = options;
const { wallet, jobId, name, description } = options;

return sdk.tx.updateJob(wallet.walletAddress, {
id: jobId,
name,
...(reward && { added_reward: reward.toString() }),
description,
});
},
{
Expand Down

0 comments on commit f65ae42

Please sign in to comment.