Skip to content

Commit

Permalink
Fix update bug for newly added elements
Browse files Browse the repository at this point in the history
  • Loading branch information
hampfh committed Nov 11, 2020
1 parent 19282cf commit 7f1a0a5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
1 change: 0 additions & 1 deletion app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import express, { Request, Response } from "express"
import cookieParser from "cookie-parser"
import path from "path"
import apiRoute from "./routes/api"
import RealTime from "./RealTime"

const app = express()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import DeadlineObject from './DeadlineObject'
import "./ContentObject.css"
import { deleteLocally, editLocal, IDeleteLocally, IEditLocal, IEditLocalObject } from "state/actions/local"
import { IReduxRootState } from "state/reducers"
import { ILocalState, indexOfLocal } from "state/reducers/local"
import { ILocalState } from "state/reducers/local"
import { IAppState } from "state/reducers/app"

class ContentObject extends Component<PropsForComponent, StateForComponent> {
Expand Down Expand Up @@ -101,11 +101,13 @@ class ContentObject extends Component<PropsForComponent, StateForComponent> {
window.location.reload()
}

// Update is made with sockets, this code is redundant

// Check if local item
if (indexOfLocal(this.props.local, append.id) >= 0)
this.props.editLocal(append.id, append)
else if (this.props.updateSubjects)
this.props.updateSubjects()
//if (indexOfLocal(this.props.local, append.id) >= 0)
// this.props.editLocal(append.id, append)
//else if (this.props.updateSubjects)
// this.props.updateSubjects()
}

_delete = async () => {
Expand All @@ -124,7 +126,8 @@ class ContentObject extends Component<PropsForComponent, StateForComponent> {
window.location.reload()
}

this.props.deleteLocally(this.props.id)
// Deletion is made with sockets
//this.props.deleteLocally(this.props.id)
}
}

Expand Down
21 changes: 18 additions & 3 deletions client/src/components/utilities/Subscriptions.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from 'react'
import { addLocal, deleteLocally, IAddLocal, IDeleteLocally } from 'state/actions/local'
import { addLocal, deleteLocally, editLocal, IAddLocal, IDeleteLocally, IEditLocal } from 'state/actions/local'
import { IReduxRootState } from 'state/reducers'
import { ILocalState } from 'state/reducers/local'
import { connect } from "react-redux"
Expand Down Expand Up @@ -52,7 +52,20 @@ class Subscriptions extends Component<PropsForComponent> {
appendObject.deadline = deadlines
}

this.props.updateRawData(appendObject)
// Is the item local?
if (this.props.local.added.find((element) => element.id?.toString() === data.id.toString()) != null) {

this.props.editLocal(data.id, {
id: data.id,
title: data.type as ContentType === "TEXT" ? data.fieldOne : undefined,
text: data.type as ContentType === "TEXT" ? data.fieldTwo : undefined,
displayText: data.type as ContentType === "LINK" || data.type as ContentType === "DEADLINE" ? data.fieldOne : undefined,
deadline: data.type as ContentType === "DEADLINE" ? data.fieldTwo : undefined,
link: data.type as ContentType === "LINK" ? data.fieldTwo : undefined,
start: data.type as ContentType === "DEADLINE" ? data.fieldThree : undefined
})
} else
this.props.updateRawData(appendObject)
}}
/>
<Socket subscribeTo="deleteElement" callback={(data: any) => {
Expand All @@ -70,6 +83,7 @@ export interface PropsForComponent {
local: ILocalState,
addLocal: IAddLocal,
deleteLocally: IDeleteLocally
editLocal: IEditLocal
}

const reduxSelect = (state: IReduxRootState) => {
Expand All @@ -81,7 +95,8 @@ const reduxSelect = (state: IReduxRootState) => {
const reduxDispatch = () => {
return {
addLocal,
deleteLocally
deleteLocally,
editLocal
}
}

Expand Down
4 changes: 2 additions & 2 deletions client/src/state/actions/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export interface IEditLocalObject {
deadline?: string,
start?: string
}
export interface IEditLocal { (id: string, object: IEditLocalObject): void }
export const editLocal = (id: string, object: IEditLocalObject) => {
export interface IEditLocal { (id: string, object: Partial<IEditLocalObject>): void }
export const editLocal = (id: string, object: Partial<IEditLocalObject>) => {
return {
type: 'EDIT_LOCAL',
payload: {
Expand Down

0 comments on commit 7f1a0a5

Please sign in to comment.