Skip to content

Commit

Permalink
feat(widget-chat): add flag to store after adding via sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
adamweeks committed Oct 28, 2016
1 parent f4c3c3b commit b14f818
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
15 changes: 7 additions & 8 deletions packages/widget-chat/src/actions/flags.js
Expand Up @@ -5,12 +5,11 @@ export function beginReceiveFlags() {
};
}

export const FLAG_ACTIVITY_IN_CONVERSATION = `FLAG_ACTIVITY_IN_CONVERSATION`;
export function flagActivityInConversation(conversation, activity) {
export const ADD_FLAG = `ADD_FLAG`;
export function addFlag(flag) {
return {
type: FLAG_ACTIVITY_IN_CONVERSATION,
conversation,
activity
type: ADD_FLAG,
flag
};
}

Expand All @@ -32,10 +31,10 @@ export function fetchFlagsForConversation(conversation, spark) {
};
}

export function flagActivity(conversation, activity, spark) {
export function flagActivity(activity, spark) {
return (dispatch) =>
spark.flag.create(activity)
.then(() => {
dispatch(flagActivityInConversation(conversation, activity));
.then((flag) => {
dispatch(addFlag(flag));
});
}
2 changes: 1 addition & 1 deletion packages/widget-chat/src/containers/chat-widget/index.js
Expand Up @@ -169,7 +169,7 @@ export class ChatWidget extends Component {
} = props;
const activity = conversation.activities.find((act) => act.id === activityId);
if (activity) {
this.props.flagActivity(conversation, activity, spark);
this.props.flagActivity(activity, spark);
}
}

Expand Down
24 changes: 17 additions & 7 deletions packages/widget-chat/src/reducers/flags.js
@@ -1,29 +1,39 @@
import {
ADD_FLAG,
BEGIN_RECEIVE_FLAGS,
RECEIVE_FLAGS
} from '../actions/flags';

function mapFlag(flag) {
return {
id: flag.id,
url: flag.url,
activityUrl: flag[`flag-item`]
};
}

export default function conversation(state = {
flags: [],
hasFetched: false,
isFetching: false
}, action) {
switch (action.type) {

case ADD_FLAG: {
const flag = mapFlag(action.flag);
return Object.assign({}, state, {
flags: [...state.flags, flag]
});
}

case BEGIN_RECEIVE_FLAGS: {
return Object.assign({}, state, {
isFetching: true
});
}

case RECEIVE_FLAGS: {
const flags = action.flags.map((flag) => (
{
id: flag.id,
url: flag.url,
activityUrl: flag[`flag-item`]
})
);
const flags = action.flags.map(mapFlag);
return Object.assign({}, state, {
hasFetched: true,
isFetching: false,
Expand Down

0 comments on commit b14f818

Please sign in to comment.