Skip to content

Commit

Permalink
Auto sort members sites
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnrivers committed Sep 26, 2020
1 parent 055a786 commit aef9549
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/server/actors/Members/raw/creator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MemberRaw } from 'server/actors/Members/models';
import { sortBySocialMedia } from 'utils/arrays';

type CreateMemberRawParams = {
name: MemberRaw['name'];
Expand Down Expand Up @@ -27,7 +28,16 @@ type CreateMemberRawParams = {
graduatedDate?: string;
};

export const createMemberRaw = (params: CreateMemberRawParams): MemberRaw => {
type CreateMemberOptions = {
autoSortSites?: boolean;
};

export const createMemberRaw = (
params: CreateMemberRawParams,
options: CreateMemberOptions = {
autoSortSites: true,
}
): MemberRaw => {
const graduation =
params.graduatedDate !== undefined
? {
Expand Down Expand Up @@ -58,7 +68,12 @@ export const createMemberRaw = (params: CreateMemberRawParams): MemberRaw => {
height: params.height,
bloodType: params.bloodType,
origin: params.origin,
sites: params.sites ?? [],
sites:
params.sites === undefined
? []
: options.autoSortSites
? sortBySocialMedia(params.sites, 'asc')
: params.sites,
photoAlbums:
params.photoAlbums?.map(photoAlbum => ({
title: photoAlbum.title,
Expand Down
35 changes: 33 additions & 2 deletions src/utils/arrays.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { JoinedGenerationType } from 'server/actors/Members/constants/joinedGeneration';
import { SocialMedia } from 'server/actors/Members/constants/socialMedia';
import { Site } from 'server/types/commons';

type SortOrder = 'asc' | 'desc';

export const arrayToObject = <T, K extends keyof T>(
array: T[],
Expand All @@ -20,7 +24,7 @@ export const arrayToObject = <T, K extends keyof T>(
export const sortByDate = <T>(
array: T[],
keyField: keyof T,
order: 'asc' | 'desc'
order: SortOrder
): T[] => {
return array.slice().sort((itemA, itemB) => {
return order === 'asc'
Expand All @@ -37,7 +41,7 @@ export const sortByJoin = <
}
>(
array: T[],
order: 'asc' | 'desc'
order: SortOrder
): T[] => {
const joinWeightMap: Record<JoinedGenerationType, number> = {
exchange: 100,
Expand All @@ -54,6 +58,33 @@ export const sortByJoin = <
});
};

export function sortBySocialMedia(
sites: readonly Site[],
order: SortOrder
): Site[] {
const socialMediaWeightMap: Record<SocialMedia | string, number> = {
[SocialMedia.Instagram]: 1,
[SocialMedia.Twitter]: 2,
[SocialMedia.YouTube]: 3,
[SocialMedia.Blog]: 4,
[SocialMedia.FourthGenBlog]: 5,
[SocialMedia.Official]: 6,
[SocialMedia.OnlineSalon]: 7,
[SocialMedia.Profile]: 8,
};

return sites.slice().sort((mediaA, mediaB) => {
const weightA = socialMediaWeightMap[mediaA.title];
const weightB = socialMediaWeightMap[mediaB.title];

if (weightA !== undefined && weightB !== undefined) {
return order === 'asc' ? weightA - weightB : weightB - weightA;
} else {
return 1;
}
});
}

export const filterDuplicate = <
T extends string | number | bigint | boolean | symbol
>(
Expand Down

0 comments on commit aef9549

Please sign in to comment.