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: add settings tab and toggle for multi lang feature #7561

Open
wants to merge 6 commits into
base: feat/multi-lang-feature
Choose a base branch
from

Conversation

siddarth2824
Copy link
Contributor

@siddarth2824 siddarth2824 commented Aug 1, 2024

Context

  • Add settings tab and toggle to allow users to turn on the multi-language translation feature for their form.
  • This PR is the second PR to introduce the settings tab and toggle view to allow users to turn on and off the multi-language translation feature.
  • This PR does not introduce any changes to the public form and only introduces the functionality to turn on/off support for multi-language translation.

Changes

  • Add views for settings tab and toggle.
  • Add mutations to update the following form's setting field:
    • Set boolean value for hasMultiLang to indicate if a form supports the multiple languages
    • Set the languages, in supportedLanguages field, that the a form supports

Before & After Screenshots

Description Before After
Addition of settings tab on the side bar and toggle for turning on/off support for multi-language translation feature Screenshot 2024-08-12 at 10 04 02 PM
Language support list when toggle is turned on and success toast Screenshot 2024-08-12 at 10 06 08 PM
Toast when support for multi-translation is turned off Screenshot 2024-08-12 at 10 20 23 PM
Tooltip when hovering over icon to able/disable language Screenshot 2024-08-12 at 10 24 19 PM
Tooltip when hovering over icon to edit translation Screenshot 2024-08-12 at 10 21 05 PM
Toast when disabling support for a specific language Screenshot 2024-08-12 at 10 21 29 PM
Toast when enabling support for a specific language Screenshot 2024-08-12 at 10 21 48 PM

Tests

TC 1: Turning on toggle adds support for default supported languages and sets the hasMultiLang attribute to be true

  1. Turn on the toggle and check the forms table to verify that the supportedLanguages field is populated with the default supported languages ([en-SG, zh-SG, ms-SG, ta-SG]) and the hasMultiLang value is true

TC 2: Disable and then enable support for a specific language

  1. Turn on toggle to add support for the multi-language translation feature
  2. Disable "Chinese"
  3. Verify in the forms table for the particular form that the supportedLanguages field now contains [en-SG, ms-SG, ta-SG]
  4. Then enable "Chinese" language
  5. Verify in the forms table for the particular form that the supportedLanguages field now contains [en-SG, ms-SG, ta-SG]

(language: Language) => {
if (supportedLanguages == null) return

// Remove support for this language if it exists in supportedLanguages
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: instead of using comments to describe what the code is doing, the code should probably be able to explain itself (i prefer reserving comments to describe why something is done)

The following code block (to line 76) could be replaced with something like this:

Suggested change
// Remove support for this language if it exists in supportedLanguages
const isLanguageSupportedPreviously = !!supportedLanguages.find(language)
const isAddSupport = !isLanguageSupportedPreviously
const updatedSupportedLanguages = isAddSupport
? supportedLanguage.concat(language)
: supportedLanguages.filter(l => l !== language)
setIsLanguageSupported(isAddSupport)
return mutateFormSupportedLanguages.mutate({
nextSupportedLanguages: updatedSupportedLanguages,
selectedLanguage: language,
})

</HStack>
)}
</Flex>
{!isLast && <Divider />}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formsg has a convention to use
{!!isLast ? <Divider /> : null} instead of using &&

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand. Just out of curiosity is there any reason why the convention is preferred over using &&?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, I think it was a practice that has been applied before throughout the codebase (due to the belief that it is more readable) and hence just maintained for overall consistency in the codebase

frontend/src/features/admin-form/settings/mutations.ts Outdated Show resolved Hide resolved
frontend/src/utils/multiLanguage.ts Outdated Show resolved Hide resolved

mutateFormSupportedLanguages.mutate({ nextSupportedLanguages })

return mutateFormHasMultiLang.mutate(nextHasMultiLang)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure but would this cause an inconsistency error if error occurs for this but previous mutation has succeeded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bringing up this scenario. I changed the implementation to only add the array of supported languages in the first instance that the user turns on the toggle for this feature. I also updated the model to have a default false value for the hasMultiLang attribute. Hence now if the mutation to update the hasMultiLang attribute fail, the data is not in an inconsistent state because the intended implementation was to still keep the list of supported languages even if the hasMultiLang value has not been updated. This is to be able to retrieve back the list of supported languages when the user turns the toggle back on.

I hope this clarifies your question.

@kevin9foong
Copy link
Member

kevin9foong commented Aug 12, 2024

hi @siddarth2824,

In order for us to ensure there are no regressions and everything is working as expected, could you add in the PR description the following:

  • manual tests to run to ensure no regressions/feature works
  • UT added to cover the new functionality
  • some screenshots of before/after so others in the future know what this PR is about without executing the code locally.

This also helps me as a reviewer who may not have much context of this feature to avoid missing anything out

You may refer to the following PR as reference: PR 7566

@siddarth2824
Copy link
Contributor Author

I have added the screenshots and manual tests that can be run to ensure the functionality works as it should as suggested. Do let me know if anything else is required from my side as I am still getting to used to the team's practices for making PRs. Thanks!

@kevin9foong
Copy link
Member

kevin9foong commented Aug 22, 2024

Hi @siddarth2824, could we add some Chromatic Visual tests for the new settings page tab (& other UI components) as well?

the rationale is to provide UI snapshots and prevent future regressions

@kevin9foong
Copy link
Member

Thanks for adding the chromatic tests!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants