-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor brand store state management (#4972)
- Loading branch information
Showing
28 changed files
with
302 additions
and
562 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
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
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
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
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
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
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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { atom, selectorFamily } from "recoil"; | ||
|
||
import type { Store } from "../types/shared"; | ||
|
||
const brandStoresState = atom({ | ||
key: "brandStores", | ||
default: [] as Array<Store>, | ||
}); | ||
|
||
const brandIdState = atom({ | ||
key: "brandId", | ||
default: "", | ||
}); | ||
|
||
const brandStoreState = selectorFamily({ | ||
key: "brandStore", | ||
get: | ||
(storeId) => | ||
({ get }) => { | ||
const brandStores = get(brandStoresState); | ||
return brandStores.find((store) => store.id === storeId); | ||
}, | ||
}); | ||
|
||
export { brandStoresState, brandIdState, brandStoreState }; |
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,81 @@ | ||
import { atom, selector, selectorFamily } from "recoil"; | ||
|
||
import { policiesListState } from "./policiesState"; | ||
|
||
import { Model } from "../types/shared"; | ||
|
||
function getFilteredModels(models: Array<Model>, filterQuery?: string | null) { | ||
if (!filterQuery) { | ||
return models; | ||
} | ||
|
||
return models.filter((model: Model) => { | ||
if ( | ||
(model.name && model.name.includes(filterQuery)) || | ||
(model["api-key"] && model["api-key"].includes(filterQuery)) || | ||
(model["created-at"] && model["created-at"].includes(filterQuery)) || | ||
(model["modified-at"] && model["modified-at"].includes(filterQuery)) | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
} | ||
|
||
const modelsListState = atom({ | ||
key: "modelsList", | ||
default: [] as Array<Model>, | ||
}); | ||
|
||
const modelsListFilterState = atom({ | ||
key: "modelsListFilter", | ||
default: "" as string, | ||
}); | ||
|
||
const newModelState = atom({ | ||
key: "newModel", | ||
default: { | ||
name: "", | ||
apiKey: "", | ||
}, | ||
}); | ||
|
||
const filteredModelsListState = selector<Array<Model>>({ | ||
key: "filteredModelsList", | ||
get: ({ get }) => { | ||
const filter = get(modelsListFilterState); | ||
const models = get(modelsListState); | ||
const policies = get(policiesListState); | ||
const modelsWithPolicies = models.map((model) => { | ||
const policy = policies.find( | ||
(policy) => policy["model-name"] === model.name, | ||
); | ||
|
||
return { | ||
...model, | ||
"policy-revision": policy ? policy.revision : undefined, | ||
}; | ||
}); | ||
|
||
return getFilteredModels(modelsWithPolicies, filter); | ||
}, | ||
}); | ||
|
||
const currentModelState = selectorFamily({ | ||
key: "currentModel", | ||
get: | ||
(modelId) => | ||
({ get }) => { | ||
const models = get(modelsListState); | ||
return models.find((model) => model.name === modelId); | ||
}, | ||
}); | ||
|
||
export { | ||
modelsListState, | ||
modelsListFilterState, | ||
newModelState, | ||
filteredModelsListState, | ||
currentModelState, | ||
}; |
Oops, something went wrong.