-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#163591901,#163591918,#163591935] Messages Inbox/Deadlines/Archive Tabs (v2) #823
Changes from all commits
c11a1e7
c9de91c
22200e4
9b266d9
c054f16
f2fbc7b
978573a
101dcb3
0e253ee
0e0f7ec
e0ca3e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { none, Option, some } from "fp-ts/lib/Option"; | ||
import hoistNonReactStatics from "hoist-non-react-statics"; | ||
import { Omit } from "italia-ts-commons/lib/types"; | ||
import React from "react"; | ||
|
||
type State = { | ||
selectedMessageIds: Option<Set<string>>; | ||
}; | ||
|
||
export type InjectedWithMessagesSelectionProps = { | ||
selectedMessageIds: Option<Set<string>>; | ||
toggleMessageSelection: (id: string) => void; | ||
resetSelection: () => void; | ||
}; | ||
|
||
/** | ||
* An HOC to maintain and manipulate the messages selection. | ||
*/ | ||
export function withMessagesSelection< | ||
P extends InjectedWithMessagesSelectionProps | ||
>(WrappedComponent: React.ComponentType<P>) { | ||
class WithMessagesSelection extends React.PureComponent< | ||
Omit<P, keyof InjectedWithMessagesSelectionProps>, | ||
State | ||
> { | ||
constructor(props: Omit<P, keyof InjectedWithMessagesSelectionProps>) { | ||
super(props); | ||
this.state = { | ||
selectedMessageIds: none | ||
}; | ||
} | ||
|
||
public render() { | ||
const { selectedMessageIds } = this.state; | ||
|
||
return ( | ||
<WrappedComponent | ||
{...this.props as P} | ||
selectedMessageIds={selectedMessageIds} | ||
toggleMessageSelection={this.toggleMessageSelection} | ||
resetSelection={this.resetSelection} | ||
/> | ||
); | ||
} | ||
|
||
// A function to add/remove an id from the selectedMessageIds Set. | ||
private toggleMessageSelection = (id: string) => { | ||
this.setState(({ selectedMessageIds }) => { | ||
return selectedMessageIds | ||
.map(_ => { | ||
const newSelectedMessageIds = new Set(_); | ||
newSelectedMessageIds.has(id) | ||
? newSelectedMessageIds.delete(id) | ||
: newSelectedMessageIds.add(id); | ||
|
||
return { | ||
selectedMessageIds: some(newSelectedMessageIds) | ||
}; | ||
}) | ||
.getOrElse({ selectedMessageIds: some(new Set().add(id)) }); | ||
}); | ||
}; | ||
|
||
private resetSelection = () => { | ||
this.setState({ | ||
selectedMessageIds: none | ||
}); | ||
}; | ||
} | ||
|
||
hoistNonReactStatics(WithMessagesSelection, WrappedComponent); | ||
|
||
return WithMessagesSelection; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { format } from "date-fns"; | ||
import { View } from "native-base"; | ||
import React from "react"; | ||
import { | ||
SectionList, | ||
SectionListData, | ||
SectionListRenderItem, | ||
StyleSheet | ||
} from "react-native"; | ||
|
||
import I18n from "../../i18n"; | ||
import customVariables from "../../theme/variables"; | ||
import { MessageWithContentAndDueDatePO } from "../../types/MessageWithContentAndDueDatePO"; | ||
import H5 from "../ui/H5"; | ||
import MessageAgendaItem from "./MessageAgendaItem"; | ||
|
||
const styles = StyleSheet.create({ | ||
sectionHeader: { | ||
paddingHorizontal: customVariables.contentPadding, | ||
paddingVertical: customVariables.contentPadding / 2, | ||
backgroundColor: customVariables.brandLightGray | ||
}, | ||
itemSeparator: { | ||
height: 1, | ||
backgroundColor: customVariables.brandLightGray | ||
} | ||
}); | ||
|
||
const keyExtractor = (_: MessageWithContentAndDueDatePO) => _.id; | ||
|
||
const ItemSeparatorComponent = () => <View style={styles.itemSeparator} />; | ||
|
||
export type MessageAgendaSection = SectionListData< | ||
MessageWithContentAndDueDatePO | ||
>; | ||
|
||
type Props = { | ||
// Can't use ReadonlyArray because of the SectionList section prop | ||
// typescript definition. | ||
// tslint:disable-next-line:readonly-array | ||
sections: MessageAgendaSection[]; | ||
isRefreshing: boolean; | ||
onRefresh: () => void; | ||
onPressItem: (id: string) => void; | ||
}; | ||
|
||
/** | ||
* A component to render messages with due_date in a agenda like form. | ||
*/ | ||
class MessageAgenda extends React.PureComponent<Props> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reference @matteodesanti gave me was the Agenda view of the Android Calendar app and is called "Agenda" also in the english version: |
||
public render() { | ||
const { sections, isRefreshing, onRefresh } = this.props; | ||
return ( | ||
<SectionList | ||
sections={sections} | ||
keyExtractor={keyExtractor} | ||
stickySectionHeadersEnabled={true} | ||
alwaysBounceVertical={false} | ||
ItemSeparatorComponent={ItemSeparatorComponent} | ||
refreshing={isRefreshing} | ||
onRefresh={onRefresh} | ||
renderSectionHeader={this.renderSectionHeader} | ||
renderItem={this.renderItem} | ||
/> | ||
); | ||
} | ||
|
||
private renderSectionHeader = (info: { section: MessageAgendaSection }) => { | ||
return ( | ||
<H5 style={styles.sectionHeader}> | ||
{format( | ||
info.section.title, | ||
I18n.t("global.dateFormats.dayAndMonth") | ||
).toUpperCase()} | ||
</H5> | ||
); | ||
}; | ||
|
||
private renderItem: SectionListRenderItem< | ||
MessageWithContentAndDueDatePO | ||
> = info => { | ||
const message = info.item; | ||
return ( | ||
<MessageAgendaItem | ||
id={message.id} | ||
subject={message.content.subject} | ||
due_date={message.content.due_date} | ||
onPress={this.props.onPressItem} | ||
/> | ||
); | ||
}; | ||
} | ||
|
||
export default MessageAgenda; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { format } from "date-fns"; | ||
import { Text, View } from "native-base"; | ||
import React from "react"; | ||
import { StyleSheet, TouchableOpacity } from "react-native"; | ||
|
||
import customVariables from "../../theme/variables"; | ||
import { MessageWithContentPO } from "../../types/MessageWithContentPO"; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
padding: customVariables.contentPadding, | ||
flexDirection: "row" | ||
}, | ||
subject: { | ||
flex: 1 | ||
}, | ||
hour: { | ||
flex: 0 | ||
} | ||
}); | ||
|
||
type Props = { | ||
id: string; | ||
subject: string; | ||
due_date: NonNullable<MessageWithContentPO["content"]["due_date"]>; | ||
onPress: (id: string) => void; | ||
}; | ||
|
||
/** | ||
* A component to render a single Agenda item. | ||
* Extends PureComponent to avoid unnecessary re-renders. | ||
*/ | ||
class MessageAgendaItem extends React.PureComponent<Props> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be an SFC There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It must be a PureComponent to avoid unnecessary rerender. |
||
public render() { | ||
const { subject, due_date } = this.props; | ||
|
||
return ( | ||
<TouchableOpacity onPress={this.handlePress}> | ||
<View style={styles.container}> | ||
<Text style={styles.subject}>{subject}</Text> | ||
<Text style={styles.hour}>{format(due_date, "HH:mm")}</Text> | ||
</View> | ||
</TouchableOpacity> | ||
); | ||
} | ||
|
||
private handlePress = () => this.props.onPress(this.props.id); | ||
} | ||
|
||
export default MessageAgendaItem; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment this function