Skip to content

Commit

Permalink
Merge pull request #26 from irfanullahjan/more-ux-and-ui-improvements
Browse files Browse the repository at this point in the history
More ux and UI improvements
  • Loading branch information
irfanullahjan committed Sep 11, 2021
2 parents 64ca2f6 + 24db98a commit 6b59c63
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 52 deletions.
64 changes: 34 additions & 30 deletions client/src/common/components/PropertyForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,43 @@ export function PropertyForm(props: Props) {
? new Date(propertyData.dateAvailable).toISOString().slice(0, 10)
: '',
},
onSubmit: values => {
fetch(`/api/properties${propertyId ? `/${propertyId}` : ''}`, {
method: propertyId ? 'PUT' : 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
...values,
installments: values.offer === 'sale' ? values.installments : false,
dateAvailable: new Date(values.dateAvailable),
userId: user.id,
}),
})
.then(res => {
formik.setSubmitting(false);
if (res.status === 200 || res.status === 204) {
router.push('/');
setFormFeedback({
accent: 'success',
message:
'Property saved successfully. Redirecting you to home page.',
});
} else {
throw res;
}
})
.catch(err => {
onSubmit: async values => {
setFormFeedback(undefined);
try {
const res = await fetch(
`/api/properties${propertyId ? `/${propertyId}` : ''}`,
{
method: propertyId ? 'PUT' : 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
...values,
installments:
values.offer === 'sale' ? values.installments : false,
dateAvailable: new Date(values.dateAvailable),
userId: user.id,
}),
},
);

if (res.status === 200 || res.status === 204) {
setFormFeedback({
accent: 'danger',
message: 'Signup failed due to a network or server issue.',
accent: 'success',
message:
'Property saved successfully. Redirecting you to your properties.',
});
console.error(err);
setTimeout(() => router.push(`/user/${user.id}`), 1000);
} else {
throw res;
}
} catch (err) {
setFormFeedback({
accent: 'danger',
message: 'Signup failed due to a network or server issue.',
});
console.error(err);
}
},
validationSchema,
});
Expand Down
36 changes: 32 additions & 4 deletions client/src/common/components/lib/PropertiesGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Link from 'next/link';
import { SessionContext } from '../../../pages/_app';
import {
Badge,
Button,
Card,
CardBody,
CardImg,
Expand All @@ -10,14 +12,33 @@ import {
Col,
Row,
} from 'reactstrap';
import { mutate } from 'swr';
import { useContext } from 'react';

type Props = {
properties: { [key: string]: any }[];
editable?: boolean;
mutateUrl?: string | null;
};

export function PropertiesGrid(props: Props) {
const { properties, editable } = props;
const { user } = useContext(SessionContext);
const { properties, editable, mutateUrl } = props;

const handleDelete = (id: number) => {
if (confirm('Are you sure you wnat to delete this property?')) {
fetch(`/api/properties/${id}`, { method: 'DELETE' })
.then(res => {
if (res.status === 204) {
if (mutateUrl) mutate(mutateUrl);
} else {
alert('Deleting property failed.');
}
})
.catch(() => alert('Error making network call.'));
}
};

return (
<Row className="my-4">
{properties.map((property, i) => (
Expand Down Expand Up @@ -49,9 +70,16 @@ export function PropertiesGrid(props: Props) {
<a className="btn btn-primary mr-2">View</a>
</Link>
{editable && (
<Link href={`/${property.id}/edit`} passHref>
<a className="btn btn-secondary">Edit</a>
</Link>
<>
<Link href={`/${property.id}/edit`} passHref>
<a className="btn btn-secondary mr-2">Edit</a>
</Link>
<Button
color="danger"
onClick={() => handleDelete(property.id)}>
Delete
</Button>
</>
)}
</CardBody>
</Card>
Expand Down
27 changes: 23 additions & 4 deletions client/src/pages/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Spinner } from 'components/lib/Spinner';
import { useRouter } from 'next/dist/client/router';
import { SessionContext } from '../_app';
import { useContext } from 'react';
import { Badge, Table } from 'reactstrap';
import { Badge, Button, Table } from 'reactstrap';
import { getAsString } from 'utils/getAsString';
import Link from 'next/link';
import useSWR from 'swr';
Expand Down Expand Up @@ -30,6 +30,20 @@ export default function ViewProperty() {
if (error)
return <Error statusCode={error.status} title={error.statusText} />;

const handleDelete = () => {
if (confirm('Are you sure you wnat to delete this property?')) {
fetch(`/api/properties/${getAsString(id)}`, { method: 'DELETE' })
.then(res => {
if (res.status === 204) {
router.push(`/user/${user.id}`);
} else {
alert('Deleting property failed.');
}
})
.catch(() => alert('Error making network call.'));
}
};

return (
<>
<h1>View Property</h1>
Expand Down Expand Up @@ -119,9 +133,14 @@ export default function ViewProperty() {
</Table>
{user &&
(user.id === property?.userId ? (
<Link href={`/${property.id}/edit`} passHref>
<a className="btn btn-primary">Edit</a>
</Link>
<>
<Link href={`/${property.id}/edit`} passHref>
<a className="btn btn-primary mr-3">Edit</a>
</Link>
<Button color="danger" onClick={handleDelete}>
Delete
</Button>
</>
) : (
<Link href={`/${property?.id}/report`} passHref>
<a className="btn btn-danger">Report</a>
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/[id]/report.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ export default function ReportProperty() {
});
const json = await res.json();
if (json?.id) {
router.push('/');
setFormFeedback({
accent: 'success',
message:
'Reported the property successfully. Redirecting you to home page.',
});
setTimeout(() => router.push('/'), 1000);
} else {
throw res;
}
Expand Down
12 changes: 11 additions & 1 deletion client/src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default function Login() {
}>();

const router = useRouter();
if (user) router.push('/');

const formik = useFormik<{
email: string;
Expand All @@ -26,6 +25,7 @@ export default function Login() {
password: '',
},
onSubmit: async values => {
setFormFeedback(undefined);
try {
const res = await fetch('/api/user/login', {
method: 'POST',
Expand Down Expand Up @@ -72,6 +72,16 @@ export default function Login() {
},
});

if (user) {
if (!formFeedback) {
setFormFeedback({
accent: 'info',
message: 'You are already logged in. Redirecting you to home page.',
});
}
setTimeout(() => router.push('/'), 1000);
}

return (
<>
<h1>Login</h1>
Expand Down
3 changes: 3 additions & 0 deletions client/src/pages/reports/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export default function ReportedProperty() {
/>
);

if (error)
return <Error statusCode={error.status} title={error.statusText} />;

const handleDelete = async () => {
if (confirm('Are you sure you want to delete this property?')) {
const res = await fetch(`/api/properties/${id}`, {
Expand Down
13 changes: 11 additions & 2 deletions client/src/pages/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default function Signup() {
verifyPassword: '',
},
onSubmit: async values => {
setFormFeedback(undefined);
try {
const formData = {
...values,
Expand All @@ -41,11 +42,11 @@ export default function Signup() {
body: JSON.stringify(formData),
});
if (res.status === 200) {
router.push('/login');
setFormFeedback({
accent: 'success',
message: 'Signup successful. Redirecting you to login page.',
});
setTimeout(() => router.push('/login'), 1000);
} else if (res.status === 409) {
setFormFeedback({
accent: 'danger',
Expand Down Expand Up @@ -84,7 +85,15 @@ export default function Signup() {
},
});

if (user) router.push('/');
if (user) {
if (!formFeedback) {
setFormFeedback({
accent: 'info',
message: 'You are already logged in. Redirecting you to home page.',
});
}
setTimeout(() => router.push('/'), 1000);
}

return (
<>
Expand Down
16 changes: 6 additions & 10 deletions client/src/pages/user/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ export default function UserByIdProperties() {
const router = useRouter();
const { id } = router.query;

const {
data: properties,
error,
isValidating,
} = useSWR(
id
? `/api/properties?filter[where][userId]=${id}&filter[include][]=user`
: null,
fetcher,
);
const fetchUrl = id
? `/api/properties?filter[where][userId]=${id}&filter[include][]=user&filter[order][]=createStamp DESC`
: null;

const { data: properties, error, isValidating } = useSWR(fetchUrl, fetcher);

if (error)
return <Error statusCode={error.status} title={error.statusText} />;
Expand All @@ -41,6 +36,7 @@ export default function UserByIdProperties() {
<PropertiesGrid
properties={properties}
editable={id === user?.id}
mutateUrl={fetchUrl}
/>
) : (
<p>This user has no properties.</p>
Expand Down

1 comment on commit 6b59c63

@vercel
Copy link

@vercel vercel bot commented on 6b59c63 Sep 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.