getDerivedStateFromProps refactored in contacts-view and chips-input, #35
This commit is contained in:
@@ -20,21 +20,21 @@ export default class ContactsView extends React.Component {
|
|||||||
|
|
||||||
this.handleAction = this.handleAction.bind(this);
|
this.handleAction = this.handleAction.bind(this);
|
||||||
|
|
||||||
this.state = ContactsView.getDerivedStateFromProps(props, {});
|
this.state = ContactsView.deriveStateFromProps(props);
|
||||||
}
|
}
|
||||||
|
|
||||||
static getDerivedStateFromProps(nextProps, prevState) {
|
static deriveStateFromProps(props) {
|
||||||
const contacts = [];
|
const contacts = [];
|
||||||
let unreadThreads = 0;
|
let unreadThreads = 0;
|
||||||
let archivedCount = 0;
|
let archivedCount = 0;
|
||||||
nextProps.chatList.map((c) => {
|
props.chatList.map((c) => {
|
||||||
if (c.private && c.private.arch) {
|
if (c.private && c.private.arch) {
|
||||||
if (nextProps.archive) {
|
if (props.archive) {
|
||||||
contacts.push(c);
|
contacts.push(c);
|
||||||
} else {
|
} else {
|
||||||
archivedCount ++;
|
archivedCount ++;
|
||||||
}
|
}
|
||||||
} else if (!nextProps.archive) {
|
} else if (!props.archive) {
|
||||||
contacts.push(c);
|
contacts.push(c);
|
||||||
unreadThreads += c.unread > 0 ? 1 : 0;
|
unreadThreads += c.unread > 0 ? 1 : 0;
|
||||||
}
|
}
|
||||||
@@ -51,14 +51,22 @@ export default class ContactsView extends React.Component {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
updateFavicon(unreadThreads);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
contactList: contacts,
|
contactList: contacts,
|
||||||
archivedCount: archivedCount
|
unreadThreads: unreadThreads
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentDidUpdate(prevProps, prevState) {
|
||||||
|
if (prevProps.chatList != this.props.chatList) {
|
||||||
|
const newState = ContactsView.deriveStateFromProps(this.props);
|
||||||
|
this.setState(newState);
|
||||||
|
if (newState.unreadThreads != prevState.unreadThreads) {
|
||||||
|
updateFavicon(newState.unreadThreads);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
handleAction(action_ignored) {
|
handleAction(action_ignored) {
|
||||||
this.props.onShowArchive();
|
this.props.onShowArchive();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -538,14 +538,14 @@ class TinodeWeb extends React.Component {
|
|||||||
|
|
||||||
// New message received
|
// New message received
|
||||||
if (document.hidden) {
|
if (document.hidden) {
|
||||||
if (this.state.messageSounds && !isArchived) {
|
if (this.state.messageSounds && !archived) {
|
||||||
POP_SOUND.play();
|
POP_SOUND.play();
|
||||||
}
|
}
|
||||||
this.resetContactList();
|
this.resetContactList();
|
||||||
|
|
||||||
// Skip update if the topic is currently open, otherwise the badge will annoyingly flash.
|
// Skip update if the topic is currently open, otherwise the badge will annoyingly flash.
|
||||||
} else if (this.state.topicSelected != cont.topic) {
|
} else if (this.state.topicSelected != cont.topic) {
|
||||||
if (this.state.messageSounds && !isArchived) {
|
if (this.state.messageSounds && !archived) {
|
||||||
POP_SOUND.play();
|
POP_SOUND.play();
|
||||||
}
|
}
|
||||||
this.resetContactList();
|
this.resetContactList();
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ export default class ChipInput extends React.Component {
|
|||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
this.state = ChipInput.getDerivedStateFromProps(props);
|
this.state = ChipInput.deriveStateFromProps(props);
|
||||||
|
this.state.input = '';
|
||||||
|
this.state.focused = false;
|
||||||
|
|
||||||
this.handleTextInput = this.handleTextInput.bind(this);
|
this.handleTextInput = this.handleTextInput.bind(this);
|
||||||
this.removeChipAt = this.removeChipAt.bind(this);
|
this.removeChipAt = this.removeChipAt.bind(this);
|
||||||
@@ -19,6 +21,25 @@ export default class ChipInput extends React.Component {
|
|||||||
this.handleKeyDown = this.handleKeyDown.bind(this);
|
this.handleKeyDown = this.handleKeyDown.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static deriveStateFromProps(props) {
|
||||||
|
return {
|
||||||
|
placeholder: props.chips ? '' : props.prompt,
|
||||||
|
sortedChips: ChipInput.sortChips(props.chips, props.required),
|
||||||
|
chipIndex: ChipInput.indexChips(props.chips)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidUpdate(prevProps, prevState) {
|
||||||
|
if (prevProps.chips != this.props.chips ||
|
||||||
|
prevProps.required != this.props.required ||
|
||||||
|
prevProps.prompt != this.props.prompt) {
|
||||||
|
this.setState(ChipInput.deriveStateFromProps(this.props));
|
||||||
|
}
|
||||||
|
if (!prevState || this.props.chips.length > prevState.sortedChips.length) {
|
||||||
|
this.setState({input: ''});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
static getDerivedStateFromProps(nextProps, prevState) {
|
static getDerivedStateFromProps(nextProps, prevState) {
|
||||||
const state = {
|
const state = {
|
||||||
placeholder: nextProps.chips ? '' : nextProps.prompt,
|
placeholder: nextProps.chips ? '' : nextProps.prompt,
|
||||||
@@ -34,7 +55,7 @@ export default class ChipInput extends React.Component {
|
|||||||
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
// Map chip index to user name
|
// Map chip index to user name
|
||||||
static indexChips(chips) {
|
static indexChips(chips) {
|
||||||
const index = {};
|
const index = {};
|
||||||
|
|||||||
+43
-24
@@ -916,11 +916,23 @@ var ContactsView = function (_React$Component) {
|
|||||||
|
|
||||||
_this = _possibleConstructorReturn(this, _getPrototypeOf(ContactsView).call(this, props));
|
_this = _possibleConstructorReturn(this, _getPrototypeOf(ContactsView).call(this, props));
|
||||||
_this.handleAction = _this.handleAction.bind(_assertThisInitialized(_this));
|
_this.handleAction = _this.handleAction.bind(_assertThisInitialized(_this));
|
||||||
_this.state = ContactsView.getDerivedStateFromProps(props, {});
|
_this.state = ContactsView.deriveStateFromProps(props);
|
||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
|
|
||||||
_createClass(ContactsView, [{
|
_createClass(ContactsView, [{
|
||||||
|
key: "componentDidUpdate",
|
||||||
|
value: function componentDidUpdate(prevProps, prevState) {
|
||||||
|
if (prevProps.chatList != this.props.chatList) {
|
||||||
|
var newState = ContactsView.deriveStateFromProps(this.props);
|
||||||
|
this.setState(newState);
|
||||||
|
|
||||||
|
if (newState.unreadThreads != prevState.unreadThreads) {
|
||||||
|
Object(_lib_utils_js__WEBPACK_IMPORTED_MODULE_3__["updateFavicon"])(newState.unreadThreads);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, {
|
||||||
key: "handleAction",
|
key: "handleAction",
|
||||||
value: function handleAction(action_ignored) {
|
value: function handleAction(action_ignored) {
|
||||||
this.props.onShowArchive();
|
this.props.onShowArchive();
|
||||||
@@ -949,19 +961,19 @@ var ContactsView = function (_React$Component) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}], [{
|
}], [{
|
||||||
key: "getDerivedStateFromProps",
|
key: "deriveStateFromProps",
|
||||||
value: function getDerivedStateFromProps(nextProps, prevState) {
|
value: function deriveStateFromProps(props) {
|
||||||
var contacts = [];
|
var contacts = [];
|
||||||
var unreadThreads = 0;
|
var unreadThreads = 0;
|
||||||
var archivedCount = 0;
|
var archivedCount = 0;
|
||||||
nextProps.chatList.map(function (c) {
|
props.chatList.map(function (c) {
|
||||||
if (c["private"] && c["private"].arch) {
|
if (c["private"] && c["private"].arch) {
|
||||||
if (nextProps.archive) {
|
if (props.archive) {
|
||||||
contacts.push(c);
|
contacts.push(c);
|
||||||
} else {
|
} else {
|
||||||
archivedCount++;
|
archivedCount++;
|
||||||
}
|
}
|
||||||
} else if (!nextProps.archive) {
|
} else if (!props.archive) {
|
||||||
contacts.push(c);
|
contacts.push(c);
|
||||||
unreadThreads += c.unread > 0 ? 1 : 0;
|
unreadThreads += c.unread > 0 ? 1 : 0;
|
||||||
}
|
}
|
||||||
@@ -980,10 +992,9 @@ var ContactsView = function (_React$Component) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Object(_lib_utils_js__WEBPACK_IMPORTED_MODULE_3__["updateFavicon"])(unreadThreads);
|
|
||||||
return {
|
return {
|
||||||
contactList: contacts,
|
contactList: contacts,
|
||||||
archivedCount: archivedCount
|
unreadThreads: unreadThreads
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}]);
|
}]);
|
||||||
@@ -4764,13 +4775,13 @@ var TinodeWeb = function (_React$Component) {
|
|||||||
var archived = topic && topic.isArchived();
|
var archived = topic && topic.isArchived();
|
||||||
|
|
||||||
if (document.hidden) {
|
if (document.hidden) {
|
||||||
if (this.state.messageSounds && !isArchived) {
|
if (this.state.messageSounds && !archived) {
|
||||||
POP_SOUND.play();
|
POP_SOUND.play();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.resetContactList();
|
this.resetContactList();
|
||||||
} else if (this.state.topicSelected != cont.topic) {
|
} else if (this.state.topicSelected != cont.topic) {
|
||||||
if (this.state.messageSounds && !isArchived) {
|
if (this.state.messageSounds && !archived) {
|
||||||
POP_SOUND.play();
|
POP_SOUND.play();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6752,7 +6763,9 @@ var ChipInput = function (_React$Component) {
|
|||||||
_classCallCheck(this, ChipInput);
|
_classCallCheck(this, ChipInput);
|
||||||
|
|
||||||
_this = _possibleConstructorReturn(this, _getPrototypeOf(ChipInput).call(this, props));
|
_this = _possibleConstructorReturn(this, _getPrototypeOf(ChipInput).call(this, props));
|
||||||
_this.state = ChipInput.getDerivedStateFromProps(props);
|
_this.state = ChipInput.deriveStateFromProps(props);
|
||||||
|
_this.state.input = '';
|
||||||
|
_this.state.focused = false;
|
||||||
_this.handleTextInput = _this.handleTextInput.bind(_assertThisInitialized(_this));
|
_this.handleTextInput = _this.handleTextInput.bind(_assertThisInitialized(_this));
|
||||||
_this.removeChipAt = _this.removeChipAt.bind(_assertThisInitialized(_this));
|
_this.removeChipAt = _this.removeChipAt.bind(_assertThisInitialized(_this));
|
||||||
_this.handleChipCancel = _this.handleChipCancel.bind(_assertThisInitialized(_this));
|
_this.handleChipCancel = _this.handleChipCancel.bind(_assertThisInitialized(_this));
|
||||||
@@ -6763,6 +6776,19 @@ var ChipInput = function (_React$Component) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_createClass(ChipInput, [{
|
_createClass(ChipInput, [{
|
||||||
|
key: "componentDidUpdate",
|
||||||
|
value: function componentDidUpdate(prevProps, prevState) {
|
||||||
|
if (prevProps.chips != this.props.chips || prevProps.required != this.props.required || prevProps.prompt != this.props.prompt) {
|
||||||
|
this.setState(ChipInput.deriveStateFromProps(this.props));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!prevState || this.props.chips.length > prevState.sortedChips.length) {
|
||||||
|
this.setState({
|
||||||
|
input: ''
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, {
|
||||||
key: "handleTextInput",
|
key: "handleTextInput",
|
||||||
value: function handleTextInput(e) {
|
value: function handleTextInput(e) {
|
||||||
this.setState({
|
this.setState({
|
||||||
@@ -6859,20 +6885,13 @@ var ChipInput = function (_React$Component) {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}], [{
|
}], [{
|
||||||
key: "getDerivedStateFromProps",
|
key: "deriveStateFromProps",
|
||||||
value: function getDerivedStateFromProps(nextProps, prevState) {
|
value: function deriveStateFromProps(props) {
|
||||||
var state = {
|
return {
|
||||||
placeholder: nextProps.chips ? '' : nextProps.prompt,
|
placeholder: props.chips ? '' : props.prompt,
|
||||||
sortedChips: ChipInput.sortChips(nextProps.chips, nextProps.required),
|
sortedChips: ChipInput.sortChips(props.chips, props.required),
|
||||||
chipIndex: ChipInput.indexChips(nextProps.chips),
|
chipIndex: ChipInput.indexChips(props.chips)
|
||||||
focused: prevState && prevState.focused
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!prevState || nextProps.chips.length > prevState.sortedChips.length) {
|
|
||||||
state.input = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return state;
|
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
key: "indexChips",
|
key: "indexChips",
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
+1
-1
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user