Skip to content

Commit

Permalink
Merge pull request #1 from elliotBraem/Megha-Dev-19-ft-announcement
Browse files Browse the repository at this point in the history
sets up for contract, adds admin check
  • Loading branch information
elliotBraem authored Dec 26, 2023
2 parents 97d9a82 + 7feb37b commit 5f687fc
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 179 deletions.
56 changes: 10 additions & 46 deletions src/core/adapter/devhub-contract.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,52 +112,6 @@ function getAllCommunitiesMetadata() {
);
}

function getAvailableAddons() {
return [
{
id: "wiki",
title: "Wiki",
description: "Create a wiki for your community",
view_widget: "${REPL_DEVHUB}/widget/devhub.entity.addon.wiki.Viewer",
configurator_widget:
"${REPL_DEVHUB}/widget/devhub.entity.addon.wiki.Configurator",
},
{
id: "telegram",
title: "Telegram",
description: "Connect your telegram",
view_widget: "${REPL_DEVHUB}/widget/devhub.entity.addon.telegram.Viewer",
configurator_widget:
"${REPL_DEVHUB}/widget/devhub.entity.addon.telegram.Configurator",
},
{
id: "github",
title: "Github",
description: "Connect your github",
view_widget: "${REPL_DEVHUB}/widget/devhub.entity.addon.github.Viewer",
configurator_widget:
"${REPL_DEVHUB}/widget/devhub.entity.addon.github.Configurator",
},
{
id: "kanban",
title: "Kanban",
description: "Connect your github kanban board",
view_widget: "${REPL_DEVHUB}/widget/devhub.entity.addon.kanban.Viewer",
configurator_widget:
"${REPL_DEVHUB}/widget/devhub.entity.addon.kanban.Configurator",
},
{
id: "blog",
title: "Blog",
description: "Create a blog for your community",
view_widget: "${REPL_DEVHUB}/widget/devhub.entity.addon.blog.Viewer",
configurator_widget:
"${REPL_DEVHUB}/widget/devhub.entity.addon.blog.Configurator",
},
];
// return Near.view("${REPL_DEVHUB_CONTRACT}", "get_available_addons") ?? null;
}

function getCommunityAddons({ handle }) {
return Near.view("${REPL_DEVHUB_CONTRACT}", "get_community_addons", {
handle,
Expand Down Expand Up @@ -193,6 +147,15 @@ function getPostsByLabel({ label }) {
);
}

function addCommunityAnnouncement({ handle, data }) {
return (
Near.call("${REPL_DEVHUB_CONTRACT}", "add_community_announcement", {
handle,
data,
}) ?? null
);
}

function useQuery(name, params) {
const initialState = { data: null, error: null, isLoading: true };

Expand Down Expand Up @@ -246,5 +209,6 @@ return {
getPost,
getPostsByAuthor,
getPostsByLabel,
addCommunityAnnouncement,
useQuery,
};
43 changes: 18 additions & 25 deletions src/devhub/entity/community/Announcements.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { handle } = props;
const { Feed } = VM.require("devs.near/widget/Module.Feed");
const { getCommunity } = VM.require(
const { getCommunity, addCommunityAnnouncement } = VM.require(
"${REPL_DEVHUB}/widget/core.adapter.devhub-contract"
);

Feed = Feed || (() => <></>);
getCommunity = getCommunity || (() => <></>);
addCommunityAnnouncement = addCommunityAnnouncement || (() => <></>);

const communityData = getCommunity({ handle });

Expand Down Expand Up @@ -56,17 +58,20 @@ return (
<Container className="d-flex gap-3 m-3 pl-2">
<MainContent className="max-width-100">
<div className="d-flex flex-column gap-4">
{context.accountId && (
<div className="card p-3">
<Widget
src={"${REPL_DEVHUB}/widget/devhub.entity.community.Compose"}
props={{
optimisticUpdateFn: () => console.log("commit"),
clearOptimisticUpdateFn: () => console.log("clear"),
}}
/>
</div>
)}
{context.accountId &&
(communityData?.admins ?? []).includes(context.accountId) && (
<div className="card p-3">
<Widget
src={"${REPL_DEVHUB}/widget/devhub.entity.community.Compose"}
props={{
optimisticUpdateFn: () => console.log("commit"),
clearOptimisticUpdateFn: () => console.log("clear"),
onSubmit: (v) =>
addCommunityAnnouncement({ handle, data: v }),
}}
/>
</div>
)}
<div className="d-flex flex-wrap justify-content-between">
<h5>Announcements</h5>
<div className="d-flex align-items-center gap-2">
Expand All @@ -93,19 +98,7 @@ return (
options: {
limit: 10,
order: "desc",
accountId: communityData.accountId,
},
cacheOptions: {
ignoreCache: true,
},
},
{
action: "repost",
key: "main",
options: {
limit: 10,
order: "desc",
accountId: communityData.accountId,
accountId: [`${handle}.communities.devhub.near`],
},
cacheOptions: {
ignoreCache: true,
Expand Down
18 changes: 10 additions & 8 deletions src/devhub/entity/community/Compose.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ function onPostClick() {
}
}

const handleSubmit = () => {
const data = composeData();
onSubmit(data);
onCancel();
};

function onCommit() {
State.update({
image: {},
Expand Down Expand Up @@ -461,17 +467,13 @@ return (
)}
</button>

<CommitButton
disabled={!state.text}
force
data={composeData}
onCancel={onCancel}
onClick={onPostClick}
onCommit={onCommit}
<button
disabled={!state.text && !state.image}
onClick={handleSubmit}
className="commit-post-button"
>
Post
</CommitButton>
</button>
</Actions>
</Wrapper>
);
1 change: 1 addition & 0 deletions src/devhub/entity/post/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ function defaultRenderItem(postId, additionalProps) {
}
getPostIds(tag);
},
transactionHashes: props.transactionHashes,
}}
/>
</div>
Expand Down
62 changes: 2 additions & 60 deletions src/devhub/entity/post/Post.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -452,32 +452,6 @@ const buttonsFooter = props.isPreview ? null : (
</div>
);

const CreatorWidget = (postType) => {
return (
<div
class={`collapse ${
draftState?.parent_post_id == postId && draftState?.postType == postType
? "show"
: ""
}`}
id={`collapse${postType}Creator${postId}`}
data-bs-parent={`#accordion${postId}`}
>
<Widget
src={"${REPL_DEVHUB}/widget/devhub.entity.post.PostEditor"}
props={{
postType,
onDraftStateChange,
draftState:
draftState?.parent_post_id == postId ? draftState : undefined,
parentId: postId,
mode: "Create",
}}
/>
</div>
);
};

const tokenMapping = {
NEAR: "NEAR",
USDT: {
Expand Down Expand Up @@ -516,40 +490,6 @@ function tokenResolver(token) {
}
}

const EditorWidget = (postType) => {
return (
<div
class={`collapse ${
draftState?.edit_post_id == postId && draftState?.postType == postType
? "show"
: ""
}`}
id={`collapse${postType}Editor${postId}`}
data-bs-parent={`#accordion${postId}`}
>
<Widget
src={"${REPL_DEVHUB}/widget/devhub.entity.post.PostEditor"}
props={{
postType,
postId,
mode: "Edit",
author_id: post.author_id,
labels: post.snapshot.labels,
name: post.snapshot.name,
description: post.snapshot.description,
amount: post.snapshot.amount,
token: tokenResolver(post.snapshot.sponsorship_token),
supervisor: post.snapshot.supervisor,
githubLink: post.snapshot.github_link,
onDraftStateChange,
draftState:
draftState?.edit_post_id == postId ? draftState : undefined,
}}
/>
</div>
);
};

const isDraft =
(draftState?.parent_post_id === postId &&
draftState?.postType === state.postType) ||
Expand Down Expand Up @@ -598,6 +538,7 @@ function Editor() {
parentId: postId,
mode: "Create",
toggleEditor: toggleEditor,
transactionHashes: props.transactionHashes,
}}
/>
</>
Expand All @@ -623,6 +564,7 @@ function Editor() {
draftState:
draftState?.edit_post_id == postId ? draftState : undefined,
toggleEditor: toggleEditor,
transactionHashes: props.transactionHashes,
}}
/>
</>
Expand Down
59 changes: 46 additions & 13 deletions src/devhub/entity/post/PostEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ const cleanDescription = (description) => {
: description;
};

const postType = props.postType ?? "Sponsorship";
const parentId = props.parentId ?? null;
const postId = props.postId ?? null;
const mode = props.mode ?? "Create";
const toggleEditor = props.toggleEditor;
const referralLabels = props.referral ? [`referral:${props.referral}`] : [];
const labelStrings = (props.labels ?? []).concat(referralLabels);

const labels = labelStrings.map((s) => {
return { name: s };
});

initState({
seekingFunding: props.seekingFunding ?? false,
author_id: context.accountId,
Expand Down Expand Up @@ -42,6 +54,40 @@ const AutoComplete = styled.div`
}
`;

if (props.transactionHashes) {
const transaction = useCache(
() =>
asyncFetch("https://rpc.mainnet.near.org", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: "dontcare",
method: "tx",
params: [props.transactionHashes, context.accountId],
}),
}).then((res) => res),
props.transactionHashes + context.accountId,
{ subscribe: false }
);

if (transaction !== null) {
const transaction_method_name =
transaction?.body?.result?.transaction?.actions[0].FunctionCall
.method_name;

const is_edit_or_add_post_transaction =
transaction_method_name == "add_post" ||
transaction_method_name == "edit_post";

if (is_edit_or_add_post_transaction) {
props.onDraftStateChange(null);
}
}
}

function textareaInputHandler(value) {
const words = value.split(/\s+/);
const allMentiones = words
Expand Down Expand Up @@ -85,19 +131,6 @@ function autoCompleteAccountId(id) {

/* END_INCLUDE: "core/lib/autocomplete" */

const postType = props.postType ?? "Sponsorship";
const parentId = props.parentId ?? null;
const postId = props.postId ?? null;
const mode = props.mode ?? "Create";
const toggleEditor = props.toggleEditor;

const referralLabels = props.referral ? [`referral:${props.referral}`] : [];
const labelStrings = (props.labels ?? []).concat(referralLabels);

const labels = labelStrings.map((s) => {
return { name: s };
});

if (!state.draftStateApplied && props.draftState) {
State.update({ ...props.draftState, draftStateApplied: true });
}
Expand Down
27 changes: 0 additions & 27 deletions src/devhub/entity/post/draft.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
const DRAFT_STATE_STORAGE_KEY = "POST_DRAFT_STATE";
let is_edit_or_add_post_transaction = false;
let transaction_method_name;

if (props.transactionHashes) {
const transaction = fetch("https://rpc.mainnet.near.org", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: "dontcare",
method: "tx",
params: [props.transactionHashes, context.accountId],
}),
});
transaction_method_name =
transaction?.body?.result?.transaction?.actions[0].FunctionCall.method_name;

is_edit_or_add_post_transaction =
transaction_method_name == "add_post" ||
transaction_method_name == "edit_post";

if (is_edit_or_add_post_transaction) {
Storage.privateSet(DRAFT_STATE_STORAGE_KEY, undefined);
}
}

const onDraftStateChange = (draftState) =>
Storage.privateSet(DRAFT_STATE_STORAGE_KEY, JSON.stringify(draftState));
Expand Down

0 comments on commit 5f687fc

Please sign in to comment.