-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(engagement-ui-feat-like): Added optimistic update for like action
- Loading branch information
1 parent
17e7946
commit d1ced25
Showing
14 changed files
with
137 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use client"; | ||
|
||
import { PropsWithBase } from "@open-system/design-system-components"; | ||
import { LikeButton as LikeButtonInner } from "@open-system/engagement-ui-feat-like/like-button"; | ||
|
||
export type LikeButtonProps = PropsWithBase<{ | ||
pageId: string; | ||
count: number; | ||
}>; | ||
|
||
export default function LikeButton({ className, ...props }: LikeButtonProps) { | ||
return ( | ||
<div className={className}> | ||
<LikeButtonInner {...props} /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* It converts a boolean value to a string | ||
* @param {boolean | null} [value] - The value to be converted to a string. | ||
*/ | ||
export const stringifyBoolean = (value?: boolean | null): string => | ||
String(!!value); | ||
|
||
/** | ||
* It returns true if the string is "true" or "1", false if the string is "false" or "0", and false if | ||
* the string is anything else. | ||
* @param {string | null} [strValue] - string | null | ||
* @returns A function that takes a string and returns a boolean. | ||
*/ | ||
export const parseBoolean = (strValue?: string | null): boolean => { | ||
try { | ||
return !!strValue && Boolean(JSON.parse(strValue)); | ||
} catch (e) { | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { customAlphabet, nanoid } from "nanoid"; | ||
|
||
/** | ||
* Returns back a unique Id string | ||
* | ||
* @remarks Currently using uuidv4 to generate this string. Please see {@link https://www.npmjs.com/package/uuid | the uuid documentation} for more information. | ||
* @remarks Currently using nanoid to generate this string. Please see {@link https://www.npmjs.com/package/nanoid the Nano ID documentation} for more information. | ||
* | ||
* @returns A unique Id string | ||
*/ | ||
export const getUUID = (): string => uuidv4(); | ||
export const getUUID = (): string => nanoid(); | ||
|
||
/** | ||
* Returns back a unique numeric Id string | ||
* | ||
* @remarks Currently using Nano ID to generate this string. Please see {@link https://zelark.github.io/nano-id-cc/ | the Nano ID documentation} for more information. | ||
* @remarks Currently using Nano ID to generate this string. Please see {@link https://zelark.github.io/nano-id-cc/ the Nano ID documentation} for more information. | ||
* | ||
* @param size - The size of the Id. The default size is 21. | ||
* @returns A unique Id string | ||
*/ | ||
/*export const getUniqueNumericId = (size?: number | undefined): string => | ||
customAlphabet("1234567890", size)(size);*/ | ||
export const getUniqueNumericId = (size?: number | undefined): string => | ||
customAlphabet("1234567890", size)(size); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./use-is-liked"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use client"; | ||
|
||
import { | ||
parseBoolean, | ||
stringifyBoolean, | ||
} from "@open-system/core-typescript-utilities"; | ||
import { destroyCookie, parseCookies, setCookie } from "nookies"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
export const useIsLiked = ( | ||
pageId: string, | ||
initCount = 0 | ||
): [boolean, () => void, number] => { | ||
const [isLiked, setIsLiked] = useState(false); | ||
const [count, setCount] = useState(initCount); | ||
|
||
useEffect(() => { | ||
setIsLiked(!!parseBoolean(parseCookies()?.[`${pageId}_liked`])); | ||
}, [pageId]); | ||
|
||
return [ | ||
isLiked, | ||
useCallback(() => { | ||
if (isLiked) { | ||
destroyCookie(null, `${pageId}_liked`); | ||
setIsLiked(false); | ||
setCount(count - 1); | ||
} else { | ||
setCookie(null, `${pageId}_liked`, stringifyBoolean(true)); | ||
setIsLiked(true); | ||
setCount(count + 1); | ||
} | ||
}, [count, isLiked, pageId]), | ||
count, | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 14 additions & 8 deletions
22
libs/engagement/ui/feat-like/src/like-button/LikeButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
"use client"; | ||
|
||
export * from "./LikeButton"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters