Skip to content
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

feat: data collection for search #207

Merged
merged 12 commits into from
Jun 24, 2020
77 changes: 73 additions & 4 deletions src/client/actions/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
RESET_USER_STATE,
ResetUserStateAction,
SET_CREATE_SHORT_LINK_ERROR,
SET_EDITED_CONTACT_EMAIL,
SET_EDITED_DESCRIPTION,
SET_EDITED_LONG_URL,
SET_IS_UPLOADING,
SET_LAST_CREATED_LINK,
Expand All @@ -26,6 +28,8 @@ import {
SET_URL_FILTER,
SET_URL_TABLE_CONFIG,
SetCreateShortLinkErrorAction,
SetEditedContactEmailAction,
SetEditedDescriptionAction,
SetEditedLongUrlAction,
SetIsUploadingAction,
SetLastCreatedLinkAction,
Expand Down Expand Up @@ -90,6 +94,28 @@ const setIsUploading: (payload: boolean) => SetIsUploadingAction = (
payload,
})

const setEditedContactEmail: (
shortUrl: string,
editedContactEmail: string,
) => SetEditedContactEmailAction = (shortUrl, editedContactEmail) => ({
type: SET_EDITED_CONTACT_EMAIL,
payload: {
shortUrl,
editedContactEmail,
},
})

const setEditedDescription: (
shortUrl: string,
editedDescription: string,
) => SetEditedDescriptionAction = (shortUrl, editedDescription) => ({
type: SET_EDITED_DESCRIPTION,
payload: {
shortUrl,
editedDescription,
},
})

const setCreateShortLinkError: (
payload: string | null,
) => SetCreateShortLinkErrorAction = (payload) => ({
Expand Down Expand Up @@ -227,11 +253,12 @@ const getUrlsForUser = (): ThunkAction<

if (isOk) {
json.urls.forEach((url: UrlType) => {
url.createdAt = moment(url.createdAt) // eslint-disable-line no-param-reassign
.tz('Singapore')
.format('D MMM YYYY')
// eslint-disable-next-line no-param-reassign
/* eslint-disable no-param-reassign */
url.createdAt = moment(url.createdAt).tz('Singapore').format('D MMM YYYY')
url.editedLongUrl = removeHttpsProtocol(url.longUrl)
url.editedContactEmail = url.contactEmail
url.editedDescription = url.description
/* eslint-enable no-param-reassign */
})
dispatch<GetUrlsForUserSuccessAction>(isGetUrlsForUserSuccess(json.urls))
dispatch<UpdateUrlCountAction>(updateUrlCount(json.count))
Expand Down Expand Up @@ -288,6 +315,45 @@ const updateLongUrl = (shortUrl: string, longUrl: string) => (
})
}

// API call to update description and contact email
const updateUrlInformation = (shortUrl: string) => (
dispatch: ThunkDispatch<
GoGovReduxState,
void,
SetErrorMessageAction | SetSuccessMessageAction
>,
getState: GetReduxState,
) => {
const { user } = getState()
const url = user.urls.filter(
(urlToCheck) => urlToCheck.shortUrl === shortUrl,
)[0]
JasonChong96 marked this conversation as resolved.
Show resolved Hide resolved
if (!url) {
dispatch<SetErrorMessageAction>(
rootActions.setErrorMessage('Url not found.'),
)
return null
}
return patch('/api/user/url', {
contactEmail: url.editedContactEmail ? url.editedContactEmail : null,
description: url.editedDescription,
shortUrl,
}).then((response) => {
if (response.ok) {
dispatch<void>(getUrlsForUser())
dispatch<SetSuccessMessageAction>(
rootActions.setSuccessMessage('URL is updated.'),
)
return null
}

return response.json().then((json) => {
dispatch<SetErrorMessageAction>(rootActions.setErrorMessage(json.message))
return null
})
})
}

// API call to replace file
const replaceFile = (
shortUrl: string,
Expand Down Expand Up @@ -553,4 +619,7 @@ export default {
setCreateShortLinkError,
setUrlFilter,
replaceFile,
setEditedContactEmail,
setEditedDescription,
updateUrlInformation,
}
20 changes: 20 additions & 0 deletions src/client/actions/user/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ export const SET_IS_UPLOADING = 'SET_IS_UPLOADING'
export const SET_UPLOAD_FILE_ERROR = 'SET_UPLOAD_FILE_ERROR'
export const SET_CREATE_SHORT_LINK_ERROR = 'SET_CREATE_SHORT_LINK_ERROR'
export const SET_LAST_CREATED_LINK = 'SET_LAST_CREATED_LINK'
export const SET_EDITED_CONTACT_EMAIL = 'SET_EDITED_CONTACT_EMAIL'
export const SET_EDITED_DESCRIPTION = 'SET_EDITED_DESCRIPTION'

export type SetEditedContactEmailAction = {
type: typeof SET_EDITED_CONTACT_EMAIL
payload: {
shortUrl: string
editedContactEmail: string
}
}

export type SetEditedDescriptionAction = {
type: typeof SET_EDITED_DESCRIPTION
payload: {
shortUrl: string
editedDescription: string
}
}

export type SetLastCreatedLinkAction = {
type: typeof SET_LAST_CREATED_LINK
Expand Down Expand Up @@ -143,3 +161,5 @@ export type UserActionType =
| SetUploadFileErrorAction
| SetLastCreatedLinkAction
| SetUrlFilterAction
| SetEditedContactEmailAction
| SetEditedDescriptionAction
3 changes: 2 additions & 1 deletion src/client/components/CollapsibleMessage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const CollapsibleMessage: FunctionComponent<CollapsibleMessageProps> = ({
type,
visible,
children,
timeout,
position = CollapsibleMessagePosition.Static,
}) => {
const classes = useStyles({ type, position })
return (
<Collapse className={classes.root} in={visible}>
<Collapse className={classes.root} in={visible} timeout={timeout}>
<div className={classes.message}>{children}</div>
</Collapse>
)
Expand Down
4 changes: 3 additions & 1 deletion src/client/components/CollapsibleMessage/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CollapseProps } from '@material-ui/core'

export enum CollapsibleMessageType {
Error,
Success,
Expand All @@ -7,7 +9,7 @@ export type CollapsibleMessageProps = {
type: CollapsibleMessageType
visible: boolean
position?: CollapsibleMessagePosition
}
} & Pick<CollapseProps, 'timeout'>

export type CollapsibleMessageStyles = {
type: CollapsibleMessageType
Expand Down
Loading