Skip to content

Commit

Permalink
Entity file upload (#791)
Browse files Browse the repository at this point in the history
* chore: restore flatten attributes, PR 465

* feat: display image and file field types in detail view

* feat: display image and file field types in detail view

* feat: file type fields allow ipfs file upload
  • Loading branch information
gabehamilton authored Apr 29, 2024
1 parent 75035fd commit c8ac37a
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 111 deletions.
15 changes: 15 additions & 0 deletions src/Entities/QueryApi/Ipfs.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const ipfsUpload = (f) =>
asyncFetch("https://ipfs.near.social/add", {
method: "POST",
headers: {
Accept: "application/json",
},
body: f,
}).then((res) => res.body.cid);

const ipfsUrl = (cid) => {
const c = typeof cid === "object" ? cid.cid : cid;
return `https://ipfs.near.social/ipfs/${c}`;
};

return { ipfsUpload, ipfsUrl };
16 changes: 14 additions & 2 deletions src/Entities/Template/EntityCreate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ const inputsValidator = (formValues) =>
return !required || typeof formValues[key] === "string";
});

const actionType = data ? (data.accountId === context.accountId ? "Edit" : "Fork") : "Create";
const flattenAttributes = (data) => {
if (!data.attributes) {
return data;
}
const flattened = { ...data };
Object.keys(data.attributes).forEach((key) => {
flattened[key] = data.attributes[key];
});
delete flattened.attributes;
return flattened;
};
const flattenedData = data ? flattenAttributes(data) : data;
const actionType = data ? (flattenedData.accountId === context.accountId ? "Edit" : "Fork") : "Create";

const initialValues = (schema, data) => {
const initial = data ?? {};
Expand Down Expand Up @@ -62,7 +74,7 @@ return (
submitLabel: data ? "Save" : "Launch",
onCancel: onCancel,
cancelLabel: cancelLabel,
externalState: initialValues(schema, data),
externalState: initialValues(schema, flattenedData),
namespace,
entityType,
}}
Expand Down
53 changes: 44 additions & 9 deletions src/Entities/Template/EntityDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ const Properties = styled.div`
`;
const Text = styled.p`
margin: 0;
font-size: 14px;
line-height: 20px;
color: ${(p) => (p.bold ? "#11181C" : "#687076")};
font-weight: ${(p) => (p.bold ? "600" : "400")};
Expand All @@ -132,14 +131,50 @@ const entityProperties = (obj) => {
const attributes = obj.attributes;
return (
<Properties>
{Object.keys(attributes).map((k) => (
<>
<Text bold key={k}>
{capitalize(k)}:
</Text>
<PropValue>{attributes[k]}</PropValue>
</>
))}
{attributes &&
Object.keys(attributes).map((key) => {
const schemaField = schema[key];
const value = attributes[key];
switch (schemaField.type) {
case "image":
return (
<>
<Text bold key={key}>
{capitalize(key)}:
</Text>
<PropValue>
<img className="logo" src={imageUrl} alt={key} />
</PropValue>
</>
);
case "file":
return (
<>
<Text bold key={key}>
{capitalize(key)}:
</Text>
<PropValue>
{value && (
<>
<p>{value?.name}</p>
<p>{value.type}</p>
<p>{value.size} bytes</p>
</>
)}
</PropValue>
</>
);
default:
return (
<>
<Text bold key={key}>
{capitalize(key)}:
</Text>
<PropValue>{value}</PropValue>
</>
);
}
})}
</Properties>
);
};
Expand Down
Loading

0 comments on commit c8ac37a

Please sign in to comment.