Skip to content

Commit

Permalink
chore(web): add sorting functionality to assets (#728)
Browse files Browse the repository at this point in the history
Co-authored-by: nina992 <[email protected]>
  • Loading branch information
nina992 and nina992 authored Oct 16, 2023
1 parent 86d4b9c commit e1edd97
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
46 changes: 44 additions & 2 deletions web/src/beta/features/Modals/AssetModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useCallback, useEffect, useMemo } from "react";
import React, { useCallback, useEffect, useMemo, useState } from "react";

import Button from "@reearth/beta/components/Button";
import TextInput from "@reearth/beta/components/fields/common/TextInput";
import SelectField from "@reearth/beta/components/fields/SelectField";
import Loading from "@reearth/beta/components/Loading";
import Modal from "@reearth/beta/components/Modal";
import Text from "@reearth/beta/components/Text";
Expand All @@ -14,6 +15,15 @@ import { useT } from "@reearth/services/i18n";
import { useNotification, Workspace } from "@reearth/services/state";
import { styled } from "@reearth/services/theme";

const getValue: { [key: string]: string } = {
"date-reverse": "date",
"name-reverse": "name",
"size-reverse": "size",
date: "date",
size: "size",
name: "name",
};

export type Props = {
className?: string;
assetType?: "file" | "image";
Expand All @@ -36,6 +46,7 @@ const ChooseAssetModal: React.FC<Props> = ({
const t = useT();

const [, setNotification] = useNotification();
const [selectedSortOption, setSelectedSortOption] = useState("date");
const {
assets,
isLoading,
Expand All @@ -45,10 +56,12 @@ const ChooseAssetModal: React.FC<Props> = ({
selectAsset,
localSearchTerm,
wrapperRef,
sortOptions,
onScrollToBottom,
handleSearchInputChange,
handleSearch,
handleGetMoreAssets,
handleSortChange,
} = useHooks({ workspaceId: currentWorkspace?.id });

const filteredAssets = useMemo(() => {
Expand Down Expand Up @@ -93,6 +106,16 @@ const ChooseAssetModal: React.FC<Props> = ({
},
[selectedAssets, selectAsset],
);
const onSortChange = useCallback(
(selectedLabel: string) => {
console.log(selectedLabel);
setSelectedSortOption(selectedLabel);
const value = getValue[selectedLabel];
const reverse = selectedLabel.toLowerCase().includes("reverse");
handleSortChange?.(value, reverse);
},
[handleSortChange],
);

useEffect(() => {
handleReset();
Expand Down Expand Up @@ -124,6 +147,17 @@ const ChooseAssetModal: React.FC<Props> = ({
/>
}>
<ControlWarpper>
<SortWrapper>
<Text size="xFootnote">{t("Sort By")}</Text>
<SelectField
value={selectedSortOption}
options={sortOptions.map(option => ({
key: option.key,
label: option.label,
}))}
onChange={onSortChange}
/>
</SortWrapper>
<SearchWarpper>
<TextInput value={localSearchTerm} onChange={handleSearchInputChange} />
<SearchButton icon="search" margin="0" onClick={handleSearch} />
Expand Down Expand Up @@ -181,14 +215,22 @@ const AssetWrapper = styled.div`

const ControlWarpper = styled.div`
display: flex;
justify-content: flex-end;
justify-content: space-between;
align-items: flex-start;
`;

const SortWrapper = styled.div`
display: flex;
align-items: flex-start;
gap: 8px;
left: 0px;
`;

const SearchWarpper = styled.div`
display: flex;
align-items: center;
gap: 4px;
right: 0;
`;

const SearchButton = styled(Button)`
Expand Down
1 change: 1 addition & 0 deletions web/src/services/i18n/translations/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,4 @@ Failed to update widget.: Failed to update widget.
Failed to remove widget.: Failed to remove widget.
Failed to update widget alignment.: Failed to update widget alignment.
Failed to update the widget align system.: Failed to update the widget align system.
Sort By: Sort By
1 change: 1 addition & 0 deletions web/src/services/i18n/translations/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,4 @@ Failed to update widget.: ウィジェットのアップデートに失敗しま
Failed to remove widget.: ウィジェットの削除に失敗しました。
Failed to update widget alignment.: ウィジェットのアラインメントのアップデートに失敗しました。
Failed to update the widget align system.: ウィジェットのアラインシステムのアップデートに失敗しました。
Sort By: Sort By

0 comments on commit e1edd97

Please sign in to comment.