generated from SatooRu65536/tpl-remix-vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from /issues/28-feat-edit-page
編集画面追加
- Loading branch information
Showing
16 changed files
with
596 additions
and
160 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import type { TYPES } from '@/consts/member'; | ||
import type { ActiveMember, AlumniMember, ExternalMember, Member, MemberBasePrivateInfo, MemberPublicInfo } from '@/types/member'; | ||
import { useCallback, useMemo, useState } from 'react'; | ||
|
||
export type SetPublicProperty = (key: keyof EditaingMember['public'], value: unknown) => void; | ||
export type SetPrivateProperty = (key: keyof EditaingMember['private'], value: unknown) => void; | ||
|
||
export interface EditaingMember { | ||
public: Omit< | ||
Partial<ActiveMember> | ||
& Partial<AlumniMember> | ||
& Partial<ExternalMember>, | ||
'type' | ||
> & { type: typeof TYPES[number]['key'] }; | ||
private: Partial<MemberBasePrivateInfo>; | ||
} | ||
|
||
export default function useEditMember(init: Member | MemberPublicInfo) { | ||
const [editing, setEditing] = useState<EditaingMember>({ private: {}, ...init }); | ||
|
||
const setPublicProperty: SetPublicProperty = useCallback((key: keyof EditaingMember['public'], value: unknown) => { | ||
setEditing((prev) => ({ ...prev, public: { ...prev.public, [key]: value } })); | ||
}, []); | ||
|
||
const setPrivateProperty: SetPrivateProperty = useCallback((key: keyof EditaingMember['private'], value: unknown) => { | ||
setEditing((prev) => ({ ...prev, private: { ...prev.private, [key]: value } })); | ||
}, []); | ||
|
||
const member = useMemo(() => toMember(editing), [editing]); | ||
return { member, setPublicProperty, setPrivateProperty } as const; | ||
} | ||
|
||
function toMember(editing: EditaingMember) { | ||
const { public: publicInfo, private: privateInfo } = editing; | ||
const baseInfo = { | ||
uuid: publicInfo.uuid, | ||
iconUrl: publicInfo.iconUrl, | ||
firstName: publicInfo.firstName, | ||
lastName: publicInfo.lastName, | ||
firstNameKana: publicInfo.firstNameKana, | ||
lastNameKana: publicInfo.lastNameKana, | ||
slackDisplayName: publicInfo.slackDisplayName, | ||
type: publicInfo.type, | ||
}; | ||
|
||
switch (publicInfo.type) { | ||
case 'active': | ||
return { | ||
private: privateInfo, | ||
public: { | ||
...baseInfo, | ||
studentId: publicInfo.studentId, | ||
grade: publicInfo.grade, | ||
expectedGraduationYear: publicInfo.expectedGraduationYear, | ||
position: publicInfo.position, | ||
}, | ||
}; | ||
|
||
case 'alumni': | ||
return { | ||
private: privateInfo, | ||
public: { | ||
...baseInfo, | ||
oldPosition: publicInfo.oldPosition, | ||
graduationYear: publicInfo.graduationYear, | ||
}, | ||
}; | ||
|
||
case 'external': | ||
return { | ||
private: privateInfo, | ||
public: { | ||
...baseInfo, | ||
schoolName: publicInfo.schoolName, | ||
organization: publicInfo.organization, | ||
expectedGraduationYear: publicInfo.expectedGraduationYear, | ||
}, | ||
}; | ||
} | ||
} |
Oops, something went wrong.