Skip to content

Commit

Permalink
chore(CX-1947): Save user preferences follow-up (#6053)
Browse files Browse the repository at this point in the history
* move Metric type into UserPreferencesModel
fix default currency selection issue
add verification on updating currency and metric

* fix type issues
  • Loading branch information
MrSltun authored Jan 21, 2022
1 parent 9797511 commit 14c3165
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useArtworkForm } from "lib/Scenes/MyCollection/Screens/ArtworkForm/Form/useArtworkForm"
import { Metric } from "lib/Scenes/Search/UserPreferencesModel"
import { GlobalStore } from "lib/store/GlobalStore"
import { Flex, Input, RadioButton, Spacer, Text } from "palette"
import React from "react"
Expand Down Expand Up @@ -59,5 +60,3 @@ export const Dimensions: React.FC = () => {
</>
)
}

export type Metric = "in" | "cm" | ""
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { MyCollectionArtwork_sharedProps } from "__generated__/MyCollectionArtwork_sharedProps.graphql"
import { Action, action, thunk, Thunk } from "easy-peasy"
import { AutosuggestResult } from "lib/Scenes/Search/AutosuggestResults"
import { Metric } from "lib/Scenes/Search/UserPreferencesModel"
import { GlobalStoreModel } from "lib/store/GlobalStoreModel"
import { getAttributionClassValueByName } from "lib/utils/artworkRarityClassifications"
import { pick, uniqBy } from "lodash"
import { Metric } from "../Screens/ArtworkForm/Components/Dimensions"

export interface Image {
height?: number
Expand All @@ -30,7 +30,7 @@ export interface ArtworkFormValues {
height: string
isEdition: boolean
medium: string
metric: Metric
metric: Metric | ""
photos: Image[]
provenance: string
title: string
Expand Down
31 changes: 23 additions & 8 deletions src/lib/Scenes/Search/UserPreferencesModel.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Action, action } from "easy-peasy"
import { getCurrencies } from "react-native-localize"
import { Metric } from "../MyCollection/Screens/ArtworkForm/Components/Dimensions"

const currencies = ["USD", "EUR", "GBP"] as const
const metrics = ["in", "cm"] as const

export type Currency = typeof currencies[number]
export type Metric = typeof metrics[number]

// set default currency depending on device locale
export type Currency = "USD" | "EUR" | "GBP"
const currencyRgx = /USD|EUR|GBP/
const localCurrency = getCurrencies()?.[0]
const userCurrencies = getCurrencies()

const DEFAULT_CURRENCY = currencyRgx.test(localCurrency) ? (localCurrency as Currency) : "USD"
const DEFAULT_CURRENCY =
(userCurrencies.find((userCurrency) => (currencies as unknown as string[]).includes(userCurrency)) as Currency) ??
"USD"
const DEFAULT_METRIC = ""

// please update this when adding new user preferences
Expand All @@ -18,7 +23,7 @@ export interface UserPreferences {

export interface UserPreferencesModel {
currency: Currency
metric: Metric
metric: Metric | ""
setCurrency: Action<this, Currency>
setMetric: Action<this, Metric>
}
Expand All @@ -27,11 +32,21 @@ export const getUserPreferencesModel = (): UserPreferencesModel => ({
currency: DEFAULT_CURRENCY,
metric: DEFAULT_METRIC,
setCurrency: action((state, currency) => {
state.currency = currency
if (currencies.includes(currency)) {
state.currency = currency
} else {
console.warn("Currency not supported")
}

// TODO: update currency in gravity
}),
setMetric: action((state, metric) => {
state.metric = metric
if (metrics.includes(metric)) {
state.metric = metric
} else {
console.warn("Metric/Dimension Unit not supported")
}

// TODO: update unit in gravity
}),
})

0 comments on commit 14c3165

Please sign in to comment.