diff --git a/server/src/websocket_server/server.rs b/server/src/websocket_server/server.rs index 4c13aadee..28b5832a5 100644 --- a/server/src/websocket_server/server.rs +++ b/server/src/websocket_server/server.rs @@ -103,7 +103,7 @@ pub struct CreateCommunity { } #[derive(Serialize, Deserialize)] -pub struct CreateCommunityResponse { +pub struct CommunityResponse { op: String, community: CommunityView } @@ -244,6 +244,7 @@ pub struct EditPost { #[derive(Serialize, Deserialize)] pub struct EditCommunity { edit_id: i32, + name: String, title: String, description: Option, category_id: i32, @@ -431,6 +432,10 @@ impl Handler for ChatServer { let edit_post: EditPost = serde_json::from_str(&data.to_string()).unwrap(); edit_post.perform(self, msg.id) }, + UserOperation::EditCommunity => { + let edit_community: EditCommunity = serde_json::from_str(&data.to_string()).unwrap(); + edit_community.perform(self, msg.id) + }, _ => { let e = ErrorMessage { op: "Unknown".to_string(), @@ -597,7 +602,7 @@ impl Perform for CreateCommunity { let community_view = CommunityView::read(&conn, inserted_community.id).unwrap(); serde_json::to_string( - &CreateCommunityResponse { + &CommunityResponse { op: self.op_type().to_string(), community: community_view } @@ -796,7 +801,6 @@ impl Perform for GetCommunity { } }; - let moderators = match CommunityModeratorView::for_community(&conn, self.id) { Ok(moderators) => moderators, Err(_e) => { @@ -1187,6 +1191,68 @@ impl Perform for EditPost { post_out } } + +impl Perform for EditCommunity { + fn op_type(&self) -> UserOperation { + UserOperation::EditCommunity + } + + fn perform(&self, chat: &mut ChatServer, addr: usize) -> String { + + let conn = establish_connection(); + + let claims = match Claims::decode(&self.auth) { + Ok(claims) => claims.claims, + Err(_e) => { + return self.error("Not logged in."); + } + }; + + let user_id = claims.id; + + let community_form = CommunityForm { + name: self.name.to_owned(), + title: self.title.to_owned(), + description: self.description.to_owned(), + category_id: self.category_id.to_owned(), + creator_id: user_id, + updated: Some(naive_now()) + }; + + let _updated_community = match Community::update(&conn, self.edit_id, &community_form) { + Ok(community) => community, + Err(_e) => { + return self.error("Couldn't update Community"); + } + }; + + let community_view = CommunityView::read(&conn, self.edit_id).unwrap(); + + // Do the subscriber stuff here + // let mut community_sent = post_view.clone(); + // community_sent.my_vote = None; + + let community_out = serde_json::to_string( + &CommunityResponse { + op: self.op_type().to_string(), + community: community_view + } + ) + .unwrap(); + + // let post_sent_out = serde_json::to_string( + // &PostResponse { + // op: self.op_type().to_string(), + // post: post_sent + // } + // ) + // .unwrap(); + + chat.send_room_message(self.edit_id, &community_out, addr); + + community_out + } +} // impl Handler for ChatServer { // type Result = MessageResult; @@ -1315,7 +1381,7 @@ impl Perform for EditPost { // MessageResult( // Ok( -// CreateCommunityResponse { +// CommunityResponse { // op: UserOperation::CreateCommunity.to_string(), // community: community // } diff --git a/ui/src/components/community-form.tsx b/ui/src/components/community-form.tsx new file mode 100644 index 000000000..a8ea7b118 --- /dev/null +++ b/ui/src/components/community-form.tsx @@ -0,0 +1,155 @@ +import { Component, linkEvent } from 'inferno'; +import { Subscription } from "rxjs"; +import { retryWhen, delay, take } from 'rxjs/operators'; +import { CommunityForm as CommunityFormI, UserOperation, Category, ListCategoriesResponse, CommunityResponse } from '../interfaces'; +import { WebSocketService, UserService } from '../services'; +import { msgOp } from '../utils'; + +import { Community } from '../interfaces'; + +interface CommunityFormProps { + community?: Community; // If a community is given, that means this is an edit + onCancel?(); + onCreate?(id: number); + onEdit?(community: Community); +} + +interface CommunityFormState { + communityForm: CommunityFormI; + categories: Array; +} + +export class CommunityForm extends Component { + private subscription: Subscription; + + private emptyState: CommunityFormState = { + communityForm: { + name: null, + title: null, + category_id: null + }, + categories: [] + } + + constructor(props, context) { + super(props, context); + + this.state = this.emptyState; + + if (this.props.community) { + this.state.communityForm = { + name: this.props.community.name, + title: this.props.community.title, + category_id: this.props.community.category_id, + description: this.props.community.description, + edit_id: this.props.community.id, + auth: null + } + } + + this.subscription = WebSocketService.Instance.subject + .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) + .subscribe( + (msg) => this.parseMessage(msg), + (err) => console.error(err), + () => console.log("complete") + ); + + WebSocketService.Instance.listCategories(); + } + + componentWillUnmount() { + this.subscription.unsubscribe(); + } + + + render() { + return ( +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+