Skip to content

Commit

Permalink
fix(links): make isSearchable a true toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneRifle committed Aug 28, 2020
1 parent c8eb7c4 commit 4b08c6c
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 53 deletions.
42 changes: 26 additions & 16 deletions src/client/actions/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
SET_CREATE_SHORT_LINK_ERROR,
SET_EDITED_CONTACT_EMAIL,
SET_EDITED_DESCRIPTION,
SET_EDITED_IS_SEARCHABLE,
SET_EDITED_LONG_URL,
SET_IS_UPLOADING,
SET_LAST_CREATED_LINK,
Expand All @@ -32,7 +31,6 @@ import {
SetCreateShortLinkErrorAction,
SetEditedContactEmailAction,
SetEditedDescriptionAction,
SetEditedIsSearchableAction,
SetEditedLongUrlAction,
SetIsUploadingAction,
SetLastCreatedLinkAction,
Expand Down Expand Up @@ -98,17 +96,6 @@ const setIsUploading: (payload: boolean) => SetIsUploadingAction = (
payload,
})

const setEditedIsSearchable: (
shortUrl: string,
editedIsSearchable: boolean,
) => SetEditedIsSearchableAction = (shortUrl, editedIsSearchable) => ({
type: SET_EDITED_IS_SEARCHABLE,
payload: {
shortUrl,
editedIsSearchable,
},
})

const setEditedContactEmail: (
shortUrl: string,
editedContactEmail: string,
Expand Down Expand Up @@ -288,7 +275,6 @@ const getUrlsForUser = (): ThunkAction<
/* eslint-disable no-param-reassign */
url.createdAt = moment(url.createdAt).tz('Singapore').format('D MMM YYYY')
url.editedLongUrl = removeHttpsProtocol(url.longUrl)
url.editedIsSearchable = url.isSearchable
url.editedContactEmail = url.contactEmail
url.editedDescription = url.description
/* eslint-enable no-param-reassign */
Expand Down Expand Up @@ -368,7 +354,6 @@ const updateUrlInformation = (shortUrl: string) => (
return null
}
return patch('/api/user/url', {
isSearchable: url.editedIsSearchable,
contactEmail: url.editedContactEmail ? url.editedContactEmail : null,
description: url.editedDescription,
shortUrl,
Expand Down Expand Up @@ -478,6 +463,31 @@ const toggleUrlState = (shortUrl: string, state: UrlState) => (
})
}

const toggleIsSearchable = (shortUrl: string, isSearchable: boolean) => (
dispatch: ThunkDispatch<
GoGovReduxState,
void,
SetErrorMessageAction | SetSuccessMessageAction
>,
) => {
patch('/api/user/url', { shortUrl, isSearchable }).then((response) => {
if (response.ok) {
dispatch<void>(getUrlsForUser())
dispatch<SetSuccessMessageAction>(
rootActions.setSuccessMessage(
`URL is now ${isSearchable ? '' : 'not'} searchable.`,
),
)
return null
}

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

const openCreateUrlModal: () => OpenCreateUrlModalAction = () => ({
type: OPEN_CREATE_URL_MODAL,
})
Expand Down Expand Up @@ -653,7 +663,7 @@ export default {
setCreateShortLinkError,
setUrlFilter,
replaceFile,
setEditedIsSearchable,
toggleIsSearchable,
setEditedContactEmail,
setEditedDescription,
getUserMessage,
Expand Down
10 changes: 0 additions & 10 deletions src/client/actions/user/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ 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_IS_SEARCHABLE = 'SET_EDITED_IS_SEARCHABLE'
export const SET_EDITED_CONTACT_EMAIL = 'SET_EDITED_CONTACT_EMAIL'
export const SET_EDITED_DESCRIPTION = 'SET_EDITED_DESCRIPTION'
export const SET_USER_MESSAGE = 'SET_USER_MESSAGE'
Expand All @@ -35,14 +34,6 @@ export type SetUserMessageAction = {
payload: string
}

export type SetEditedIsSearchableAction = {
type: typeof SET_EDITED_IS_SEARCHABLE
payload: {
shortUrl: string
editedIsSearchable: boolean
}
}

export type SetEditedContactEmailAction = {
type: typeof SET_EDITED_CONTACT_EMAIL
payload: {
Expand Down Expand Up @@ -176,7 +167,6 @@ export type UserActionType =
| SetUploadFileErrorAction
| SetLastCreatedLinkAction
| SetUrlFilterAction
| SetEditedIsSearchableAction
| SetEditedContactEmailAction
| SetEditedDescriptionAction
| SetUserMessageAction
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function LinkStatisticsGraphs() {
if (!Boolean(linkStatistics.contents)) {
return (
<Typography variant="body1">
There is no statistics to show right now.
There are no statistics to show right now.
</Typography>
)
}
Expand Down
9 changes: 3 additions & 6 deletions src/client/components/UserPage/Drawer/ControlPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ export default function ControlPanel() {
} = useShortLink(drawerStates.relevantShortLink!)

// Manage values in our text fields.
const originalIsSearchable = shortLinkState?.isSearchable || false
const editedIsSearchable = shortLinkState?.editedIsSearchable || false
const isSearchable = shortLinkState?.isSearchable || false
const originalLongUrl = removeHttpsProtocol(shortLinkState?.longUrl || '')
const editedLongUrl = shortLinkState?.editedLongUrl || ''
const editedContactEmail = shortLinkState?.editedContactEmail || ''
Expand All @@ -192,7 +191,6 @@ export default function ControlPanel() {

// Disposes any current unsaved changes and closes the modal.
const handleClose = () => {
shortLinkDispatch?.setEditIsSearchable(originalIsSearchable)
shortLinkDispatch?.setEditLongUrl(originalLongUrl)
shortLinkDispatch?.setEditDescription(originalDescription)
shortLinkDispatch?.setEditContactEmail(originalContactEmail)
Expand Down Expand Up @@ -426,10 +424,10 @@ export default function ControlPanel() {
/>
<Divider className={classes.dividerInformation} />
<LinkInfoEditor
isSearchable={editedIsSearchable}
isSearchable={isSearchable}
contactEmail={editedContactEmail}
description={editedDescription}
onIsSearchableChange={(event) => shortLinkDispatch?.setEditIsSearchable(event.target.checked)}
onIsSearchableChange={(event) => shortLinkDispatch?.toggleIsSearchable(event.target.checked)}
onContactEmailChange={(event) => shortLinkDispatch?.setEditContactEmail(event.target.value)}
onDescriptionChange={(event) =>
shortLinkDispatch?.setEditDescription(
Expand Down Expand Up @@ -464,7 +462,6 @@ export default function ControlPanel() {
disabled={
!isDescriptionValid ||
(
editedIsSearchable === originalIsSearchable &&
editedContactEmail === originalContactEmail &&
editedDescription === originalDescription
) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export default function useShortLink(shortLink: string) {
const dispatchOptions = {
toggleStatus: () =>
dispatch(userActions.toggleUrlState(shortLink, urlState.state)),
setEditIsSearchable: (editedIsSearchable: boolean) => {
dispatch(userActions.setEditedIsSearchable(shortLink, editedIsSearchable))
toggleIsSearchable: (isSearchable: boolean) => {
dispatch(userActions.toggleIsSearchable(shortLink, isSearchable))
},
setEditLongUrl: (editedUrl: string) => {
dispatch(userActions.setEditedLongUrl(shortLink, editedUrl))
Expand Down
16 changes: 0 additions & 16 deletions src/client/reducers/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
SET_CREATE_SHORT_LINK_ERROR,
SET_EDITED_CONTACT_EMAIL,
SET_EDITED_DESCRIPTION,
SET_EDITED_IS_SEARCHABLE,
SET_EDITED_LONG_URL,
SET_IS_UPLOADING,
SET_LAST_CREATED_LINK,
Expand Down Expand Up @@ -113,21 +112,6 @@ const user: (state: UserState, action: UserActionType) => UserState = (
}
break
}
case SET_EDITED_IS_SEARCHABLE: {
const { editedIsSearchable, shortUrl } = action.payload
nextState = {
urls: state.urls.map((url) => {
if (shortUrl !== url.shortUrl) {
return url
}
return {
...url,
editedIsSearchable,
}
}),
}
break
}
case SET_EDITED_CONTACT_EMAIL: {
const { editedContactEmail, shortUrl } = action.payload
nextState = {
Expand Down
1 change: 0 additions & 1 deletion src/client/reducers/user/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export type UrlType = {
updatedAt: string
userId: number
isSearchable: boolean
editedIsSearchable: boolean
description: string
editedDescription: string
contactEmail: string
Expand Down
2 changes: 1 addition & 1 deletion src/server/repositories/LinkStatisticsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class LinkStatisticsRepository
weekdayClicks,
} as LinkStatisticsInterface
}
// There is no statistics to show yet.
// There are no statistics to show yet.
return null
}
return null
Expand Down

0 comments on commit 4b08c6c

Please sign in to comment.