Skip to content

Commit

Permalink
feat: Update button label to "Download Trail" in SearchResults component
Browse files Browse the repository at this point in the history
  • Loading branch information
bramses committed Sep 7, 2024
1 parent b9d8490 commit 42f59e1
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 71 deletions.
98 changes: 98 additions & 0 deletions src/components/EditModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import Modal from 'react-modal';

const EditModal = ({
isOpen,
closeModalFn,
data,
metadata,
onSave,
}: {
isOpen: boolean;
closeModalFn: () => void;
data: any;
metadata: any;
onSave: (data: any, metadata: any) => void;
}) => {
return (
<Modal
isOpen={isOpen}
onRequestClose={closeModalFn}
contentLabel="Edit Modal"
ariaHideApp={false}
>
<textarea
id="edit-modal-textarea"
rows={4}
className="my-4 block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500"
>
{data}
</textarea>
{Object.entries(metadata).map(([key, value]) => (
<div key={key} className="my-2">
<label
htmlFor={key}
className="block text-sm font-medium text-gray-700"
>
{key}
</label>
<input
id={key}
type="text"
className="block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500"
defaultValue={String(value)}
/>
</div>
))}
<div className="flex space-x-2">
<button
type="button"
className="mb-2 me-2 w-full rounded-lg border border-gray-300 px-5 py-2.5 text-sm font-medium text-gray-700 hover:bg-gray-800 hover:text-white focus:outline-none focus:ring-4 focus:ring-gray-300"
onClick={() => {
// get data from textarea and inputs
const newData = (
document.getElementById('edit-modal-textarea') as HTMLInputElement
).value;
const newMetadata: any = {};
Object.entries(metadata).forEach(([key]) => {
const input = document.getElementById(key) as HTMLInputElement;
newMetadata[key] = input.value;
});
// if data is empty, do not save
if (!newData) {
return;
}
// if no changes, do not save
if (
newData === data &&
JSON.stringify(newMetadata) === JSON.stringify(metadata)
) {
closeModalFn();
return;
}
// if metadata.alias_ids is not a correct array, do not save
if (
newMetadata.alias_ids &&
!Array.isArray(newMetadata.alias_ids)
) {
return;
}
// send data to parent
onSave(newData, newMetadata);
closeModalFn();
}}
>
Save
</button>
<button
type="button"
className="mb-2 me-2 w-full rounded-lg border border-gray-300 px-5 py-2.5 text-sm font-medium text-gray-700 hover:bg-gray-800 hover:text-white focus:outline-none focus:ring-4 focus:ring-gray-300"
onClick={closeModalFn}
>
Cancel
</button>
</div>
</Modal>
);
};

export default EditModal;
112 changes: 42 additions & 70 deletions src/components/EntryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import {
fetchSearchEntries,
formatDate,
handleAliasAdd,
updateEntry,
} from '../helpers/functions';
import EditModal from './EditModal';
import Loading from './Loading';
import UrlSVG from './UrlSVG';

const EntryPage = () => {
// fetch data from the server at id on load
Expand Down Expand Up @@ -59,6 +63,22 @@ const EntryPage = () => {
[key: string]: boolean;
}>({});
const [buildingCollection, setBuildingCollection] = useState(false);
const [isModalOpen, setIsModalOpen] = useState(false);

const openModal = () => setIsModalOpen(true);
const closeModal = () => setIsModalOpen(false);

const handleSave = (newData: any, newMetadata: any) => {
if (!data) return;
setData({
...data,
data: newData,
metadata: newMetadata,
id: data.id,
createdAt: data.createdAt,
});
updateEntry(data.id, newData, newMetadata);
};

function checkEmbeds(
res: { data: any; metadata: any },
Expand Down Expand Up @@ -215,23 +235,31 @@ const EntryPage = () => {
{data ? (
<div>
<p className="text-md my-4 text-gray-500">{data.data}</p>
<h3 className="my-4 text-2xl font-bold">Title</h3>

<EditModal
isOpen={isModalOpen}
closeModalFn={closeModal}
data={data.data}
metadata={data.metadata}
onSave={handleSave}
/>
<Link
href={data.metadata.author}
className=" overflow-auto text-blue-600 hover:underline"
className=" inline-flex items-center overflow-auto font-medium text-blue-600 hover:underline"
target="_blank"
>
{data.metadata.title}
<UrlSVG />
</Link>
<button
onClick={() => toDashboard(data.metadata.title)}
className="mb-2 me-2 mt-4 w-full rounded-lg border border-gray-300 px-5 py-2.5 text-sm font-medium text-gray-700 hover:bg-gray-800 hover:text-white focus:outline-none focus:ring-4 focus:ring-gray-300"
aria-label="Add alias"
type="button"
onClick={openModal}
className="focus:ring-gray-30 my-2 me-2 w-full rounded-lg border border-gray-300 px-5 py-2.5 text-sm font-medium text-gray-700 hover:bg-gray-800 hover:text-white focus:outline-none focus:ring-4"
>
Search for related entries
Edit Entry
</button>
<h3 className="my-4 text-2xl font-bold">Created At</h3>

<h3 className="my-4 text-2xl font-bold">Added to yCb</h3>
<a
href={`/dashboard/garden?date=${new Date(data.createdAt)
.toLocaleDateString()
Expand Down Expand Up @@ -262,7 +290,7 @@ const EntryPage = () => {
</button>
<br />
<span className="text-sm text-gray-500">
Created at: {alias.aliasCreatedAt}
Added to yCb: {alias.aliasCreatedAt}
</span>
</div>
))}
Expand All @@ -271,11 +299,11 @@ const EntryPage = () => {

<h2 className="my-4 text-4xl font-extrabold">Add Comment</h2>
<div className="">
<input
type="text"
<textarea
rows={3}
style={{ fontSize: '17px' }}
className="w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-gray-900 focus:border-blue-500 focus:ring-blue-500"
placeholder="Add an alias..."
placeholder="Add a comment..."
onKeyDown={async (e) => {
if (e.key === 'Enter') {
const aliasInput = document.getElementById(
Expand Down Expand Up @@ -415,7 +443,7 @@ const EntryPage = () => {
</Link>
{/* when was the entry created and updated */}
<div className="text-sm text-gray-500">
Created: {new Date(result.createdAt).toLocaleString()}
Added to yCb: {new Date(result.createdAt).toLocaleString()}
{result.createdAt !== result.updatedAt && (
<>
{' '}
Expand All @@ -432,21 +460,7 @@ const EntryPage = () => {
className="inline-flex items-center font-medium text-blue-600 hover:underline"
>
{toHostname(result.metadata.author)}
<svg
className="ms-2.5 size-3 rtl:rotate-[270deg]"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 18 18"
>
<path
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M15 11v4.833A1.166 1.166 0 0 1 13.833 17H2.167A1.167 1.167 0 0 1 1 15.833V4.167A1.166 1.166 0 0 1 2.167 3h4.618m4.447-2H17v5.768M9.111 8.889l7.778-7.778"
/>
</svg>
<UrlSVG />
</a>
</div>
<button
Expand Down Expand Up @@ -507,49 +521,7 @@ const EntryPage = () => {
</Link>
</div>
) : (
<div className="flex justify-center">
<div
role="status"
className="my-4 max-w-md animate-pulse space-y-4 divide-y divide-gray-200 rounded border border-gray-200 p-4 shadow md:p-6 "
>
<div className="flex items-center justify-between">
<div>
<div className="mb-2.5 h-2.5 w-24 rounded-full bg-gray-300" />
<div className="h-2 w-32 rounded-full bg-gray-200" />
</div>
<div className="h-2.5 w-12 rounded-full bg-gray-300" />
</div>
<div className="flex items-center justify-between pt-4">
<div>
<div className="mb-2.5 h-2.5 w-24 rounded-full bg-gray-300" />
<div className="h-2 w-32 rounded-full bg-gray-200" />
</div>
<div className="h-2.5 w-12 rounded-full bg-gray-300" />
</div>
<div className="flex items-center justify-between pt-4">
<div>
<div className="mb-2.5 h-2.5 w-24 rounded-full bg-gray-300" />
<div className="h-2 w-32 rounded-full bg-gray-200" />
</div>
<div className="h-2.5 w-12 rounded-full bg-gray-300" />
</div>
<div className="flex items-center justify-between pt-4">
<div>
<div className="mb-2.5 h-2.5 w-24 rounded-full bg-gray-300" />
<div className="h-2 w-32 rounded-full bg-gray-200" />
</div>
<div className="h-2.5 w-12 rounded-full bg-gray-300" />
</div>
<div className="flex items-center justify-between pt-4">
<div>
<div className="mb-2.5 h-2.5 w-24 rounded-full bg-gray-300" />
<div className="h-2 w-32 rounded-full bg-gray-200" />
</div>
<div className="h-2.5 w-12 rounded-full bg-gray-300" />
</div>
<span className="sr-only">Loading...</span>
</div>
</div>
<Loading />
);
};

Expand Down
49 changes: 49 additions & 0 deletions src/components/Loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const Loading = () => {
return (
<div className="flex justify-center">
<div
role="status"
className="my-4 max-w-md animate-pulse space-y-4 divide-y divide-gray-200 rounded border border-gray-200 p-4 shadow md:p-6 "
>
<div className="flex items-center justify-between">
<div>
<div className="mb-2.5 h-2.5 w-24 rounded-full bg-gray-300" />
<div className="h-2 w-32 rounded-full bg-gray-200" />
</div>
<div className="h-2.5 w-12 rounded-full bg-gray-300" />
</div>
<div className="flex items-center justify-between pt-4">
<div>
<div className="mb-2.5 h-2.5 w-24 rounded-full bg-gray-300" />
<div className="h-2 w-32 rounded-full bg-gray-200" />
</div>
<div className="h-2.5 w-12 rounded-full bg-gray-300" />
</div>
<div className="flex items-center justify-between pt-4">
<div>
<div className="mb-2.5 h-2.5 w-24 rounded-full bg-gray-300" />
<div className="h-2 w-32 rounded-full bg-gray-200" />
</div>
<div className="h-2.5 w-12 rounded-full bg-gray-300" />
</div>
<div className="flex items-center justify-between pt-4">
<div>
<div className="mb-2.5 h-2.5 w-24 rounded-full bg-gray-300" />
<div className="h-2 w-32 rounded-full bg-gray-200" />
</div>
<div className="h-2.5 w-12 rounded-full bg-gray-300" />
</div>
<div className="flex items-center justify-between pt-4">
<div>
<div className="mb-2.5 h-2.5 w-24 rounded-full bg-gray-300" />
<div className="h-2 w-32 rounded-full bg-gray-200" />
</div>
<div className="h-2.5 w-12 rounded-full bg-gray-300" />
</div>
<span className="sr-only">Loading...</span>
</div>
</div>
);
};

export default Loading;
2 changes: 1 addition & 1 deletion src/components/SearchResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ const SearchResults = () => {
}}
className="mb-2 me-2 mt-4 w-full rounded-lg bg-blue-700 px-5 py-2.5 text-sm font-medium text-white hover:bg-blue-800 focus:outline-none focus:ring-4 focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
>
Download Collection
Download Trail
</button>
)}
{showLoading && <div>Loading...</div>}
Expand Down
21 changes: 21 additions & 0 deletions src/components/UrlSVG.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const UrlSVG = () => {
return (
<svg
className="ms-2.5 size-3 rtl:rotate-[270deg]"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 18 18"
>
<path
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M15 11v4.833A1.166 1.166 0 0 1 13.833 17H2.167A1.167 1.167 0 0 1 1 15.833V4.167A1.166 1.166 0 0 1 2.167 3h4.618m4.447-2H17v5.768M9.111 8.889l7.778-7.778"
/>
</svg>
);
};

export default UrlSVG;

0 comments on commit 42f59e1

Please sign in to comment.