Skip to content

Commit

Permalink
fix: insert/view job offer
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-eof committed May 1, 2023
1 parent 2bc5d65 commit 08a0711
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default function Main() {
size={'sm'}
mr={4}
leftIcon={<AddIcon />}
onClick={() => navigate('/insertJob')}
onClick={() => navigate('/insertJob/' + JobConst.SCOPE_PRIVATE)}
>
Add Job
</Button>
Expand Down Expand Up @@ -231,8 +231,8 @@ export default function Main() {
/>
}
/>
<Route path="/insertJob" element={<InsertJob />} />
<Route path="/editJob/:id" element={<InsertJob />} />
<Route path="/insertJob/:scope" element={<InsertJob />} />
<Route path="/editJob/:scope/:id" element={<InsertJob />} />
<Route path="/register" element={<Register />} />
<Route path="/authenticate" element={<Login />} />
<Route path="/view/:scope/:id" element={<ViewOfferRequest />} />
Expand Down
54 changes: 30 additions & 24 deletions src/components/job/InsertUpdateJob.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import GenericService from '../../service/GenericService';
import { useNavigate, useParams } from 'react-router-dom';
import Job from '../../dto/Job';
import FileUpload from '../FileUpload';
import JobConst from '../../consts/JobConst';

export default function InserJob() {
const [form, setForm] = useState<Job>({
Expand All @@ -22,21 +23,24 @@ export default function InserJob() {
id: undefined,
});

let { id } = useParams();
let { id, scope } = useParams();
const scopeFromUri = scope || 'public';

const [files, setFiles] = useState(Array<string>);

useEffect(() => {
if (id) {
GenericService.get<Job>('api/v1/job/' + id).then((job) => {
setForm({
title: job.title,
description: job.description,
price: job.price,
type: job.type,
id: job.id,
});
});
GenericService.get<Job>(`api/v1/job/${scopeFromUri}/${id}`).then(
(job) => {
setForm({
title: job.title,
description: job.description,
price: job.price,
type: job.type,
id: job.id,
});
}
);
}
}, []);

Expand All @@ -63,23 +67,25 @@ export default function InserJob() {
};

if (!form.id) {
GenericService.create<Job>('api/v1/job', data).then((data) => {
console.log(data);
if (form.type == 0) {
navigate('/offers');
} else if (form.type == 1) {
navigate('/requests');
GenericService.create<Job>(`api/v1/job/${scopeFromUri}`, data).then(
() => {
if (form.type == JobConst.TYPE_OFFER) {
navigate('/myOffers');
} else if (form.type == JobConst.TYPE_REQUEST) {
navigate('/myRequests');
}
}
});
);
} else {
GenericService.put<Job>('api/v1/job', form.id, data).then((data) => {
console.log(data);
if (form.type == 0) {
navigate('/offers');
} else if (form.type == 1) {
navigate('/requests');
GenericService.put<Job>(`api/v1/job/${scopeFromUri}`, form.id, data).then(
() => {
if (form.type == JobConst.TYPE_OFFER) {
navigate('/myOffers');
} else if (form.type == JobConst.TYPE_REQUEST) {
navigate('/myRequests');
}
}
});
);
}
};

Expand Down
33 changes: 18 additions & 15 deletions src/components/job/ViewOfferRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import UserService from '../../service/UserService';
import Stars from './Stars';
import JobService from '../../service/JobService';
import Title from './Title';
import JobConst from '../../consts/JobConst';

interface Props {}
export default function ViewOfferRequest({}: Props) {
Expand All @@ -36,10 +37,10 @@ export default function ViewOfferRequest({}: Props) {
const navigate = useNavigate();
const [modalImage, setModalImage] = useState<string>();

let { id, scopeFromUrl } = useParams();
const scope = scopeFromUrl || 'public';
let { id, scope } = useParams();
const scopeFromUrl = scope || 'public';
useEffect(() => {
GenericService.get<Job>(`api/v1/job/${scope}/${id}`).then((job) => {
GenericService.get<Job>(`api/v1/job/${scopeFromUrl}/${id}`).then((job) => {
setJob(job);
});
}, []);
Expand All @@ -48,22 +49,22 @@ export default function ViewOfferRequest({}: Props) {
return UserService.isSameUsername(job.author?.username || '');
}

const goToEditOfferRequest = (id: number | undefined) => {
navigate('/editJob/' + id);
const goToEditOfferRequest = (id: number | undefined, scope: string) => {
navigate(`/editJob/${scopeFromUrl}/${id}`);
};

const deleteJobOfferRequest = (jobId: number | undefined, scope: string) => {
const deleteItem = (jobId: number) => {
if (jobId) {
deleteItem(jobId, scope);
GenericService.delete(`api/v1/job/${scopeFromUrl}`, jobId).then((_) => {
if (job && job.type === JobConst.TYPE_OFFER) {
navigate('/myOffers');
} else if (job && job.type === JobConst.TYPE_REQUEST) {
navigate('/myRequests');
}
});
}
};

const deleteItem = (jobId: number, scope: string) => {
GenericService.delete(`api/v1/job/${scope}`, jobId).then((_) => {
navigate('/offers');
});
};

function openModal(image: string) {
setModalImage(image);
onOpen();
Expand Down Expand Up @@ -185,15 +186,17 @@ export default function ViewOfferRequest({}: Props) {
colorScheme="red"
mr={3}
display={job && showUserButtons(job) ? '' : 'none'}
onClick={() => job && deleteJobOfferRequest(job.id, scope)}
onClick={() => job && job.id && deleteItem(job.id)}
>
Delete
</Button>
<Button
variant={'solid'}
colorScheme="green"
display={job && showUserButtons(job) ? '' : 'none'}
onClick={() => job && goToEditOfferRequest(job.id)}
onClick={() =>
job && goToEditOfferRequest(job.id, scopeFromUrl)
}
>
Edit
</Button>
Expand Down

0 comments on commit 08a0711

Please sign in to comment.