Skip to content

Commit

Permalink
Finished duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
neketka committed May 1, 2024
1 parent ca4a7d2 commit bc462dc
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
56 changes: 56 additions & 0 deletions admin/src/components/Achievements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function AchiemementCard(props: {
onSelect: () => void;
onEdit: () => void;
onDelete: () => void;
onCopy: () => void;
}) {
return (
<>
Expand Down Expand Up @@ -80,6 +81,9 @@ function AchiemementCard(props: {
<HButton onClick={props.onEdit} float="right">
EDIT
</HButton>
<HButton onClick={props.onCopy} float="right">
COPY
</HButton>
</ListCardButtons>
</ListCardBox>
</>
Expand Down Expand Up @@ -146,13 +150,30 @@ function toForm(achievement: AchievementDto) {
] as EntryForm[];
}

function makeCopyForm(orgOptions: string[], initialIndex: number) {
return [
{
name: "Target Organization",
options: orgOptions,
value: initialIndex,
},
] as EntryForm[];
}

export function Achievements() {
const serverData = useContext(ServerDataContext);
const [isCreateModalOpen, setCreateModalOpen] = useState(false);
const [isEditModalOpen, setEditModalOpen] = useState(false);
const [isDeleteModalOpen, setDeleteModalOpen] = useState(false);
const [selectModalOpen, setSelectModalOpen] = useState(false);
const [isLinkedModalOpen, setLinkedModalOpen] = useState(false);

const [isCopyModalOpen, setCopyModalOpen] = useState(false);
const [copyForm, setCopyForm] = useState(() => ({
form: makeCopyForm([], 0),
orgIds: [] as string[],
}));

const [form, setForm] = useState(() => makeForm());
const [currentId, setCurrentId] = useState("");
const [query, setQuery] = useState("");
Expand Down Expand Up @@ -203,6 +224,26 @@ export function Achievements() {
}}
form={form}
/>
<EntryModal
title="Copy Achievement"
isOpen={isCopyModalOpen}
entryButtonText="COPY"
onEntry={async () => {
const ach = serverData.achievements.get(currentId)!;
serverData.updateAchievement({
...ach,
eventId: "",
initialOrganizationId:
copyForm.orgIds[(copyForm.form[0] as OptionEntryForm).value],
id: "",
});
setCopyModalOpen(false);
}}
onCancel={() => {
setCopyModalOpen(false);
}}
form={copyForm.form}
/>
<DeleteModal
objectName={serverData.achievements.get(currentId)?.name ?? ""}
isOpen={isDeleteModalOpen}
Expand Down Expand Up @@ -280,6 +321,21 @@ export function Achievements() {
setForm(toForm(ach));
setEditModalOpen(true);
}}
onCopy={() => {
const orgs = Array.from(serverData.organizations.values());
const myOrgIndex = orgs.findIndex(
(v) => v.id === selectedOrg?.id
);
setCurrentId(ach.id);
setCopyForm({
form: makeCopyForm(
orgs.map((org) => org.name ?? ""),
myOrgIndex
),
orgIds: orgs.map((org) => org.id),
});
setCopyModalOpen(true);
}}
/>
))}
</>
Expand Down
54 changes: 54 additions & 0 deletions admin/src/components/Challenges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function ChallengeCard(props: {
onDown: () => void;
onDelete: () => void;
onEdit: () => void;
onCopy: () => void;
}) {
return (
<ListCardBox>
Expand All @@ -79,6 +80,9 @@ function ChallengeCard(props: {
<HButton onClick={props.onEdit} float="right">
EDIT
</HButton>
<HButton onClick={props.onCopy} float="right">
COPY
</HButton>
</ListCardButtons>
</ListCardBox>
);
Expand Down Expand Up @@ -170,6 +174,16 @@ function fromForm(
};
}

function makeCopyForm(evOptions: string[], initialIndex: number) {
return [
{
name: "Target Event",
options: evOptions,
value: initialIndex,
},
] as EntryForm[];
}

export function Challenges() {
const [createModalOpen, setCreateModalOpen] = useState(false);
const [editModalOpen, setEditModalOpen] = useState(false);
Expand All @@ -180,6 +194,12 @@ export function Challenges() {
const [currentId, setCurrentId] = useState("");
const [query, setQuery] = useState("");

const [isCopyModalOpen, setCopyModalOpen] = useState(false);
const [copyForm, setCopyForm] = useState(() => ({
form: makeCopyForm([], 0),
evIds: [] as string[],
}));

const serverData = useContext(ServerDataContext);
const selectedEvent = serverData.events.get(serverData.selectedEvent);

Expand Down Expand Up @@ -229,6 +249,25 @@ export function Challenges() {
setDeleteModalOpen(false);
}}
/>
<EntryModal
title="Copy Challenge"
isOpen={isCopyModalOpen}
entryButtonText="COPY"
onEntry={async () => {
const chal = serverData.challenges.get(currentId)!;
serverData.updateChallenge({
...chal,
linkedEventId:
copyForm.evIds[(copyForm.form[0] as OptionEntryForm).value],
id: "",
});
setCopyModalOpen(false);
}}
onCancel={() => {
setCopyModalOpen(false);
}}
form={copyForm.form}
/>
<SearchBar
onCreate={() => {
setForm(makeForm());
Expand Down Expand Up @@ -292,6 +331,21 @@ export function Challenges() {
setCurrentId(chal.id);
setDeleteModalOpen(true);
}}
onCopy={() => {
const evs = Array.from(serverData.events.values());
const myEvIndex = evs.findIndex(
(v) => v.id === selectedEvent?.id
);
setCurrentId(chal.id);
setCopyForm({
form: makeCopyForm(
evs.map((ev) => ev.name ?? ""),
myEvIndex
),
evIds: evs.map((ev) => ev.id),
});
setCopyModalOpen(true);
}}
/>
))}
</>
Expand Down
4 changes: 3 additions & 1 deletion admin/src/components/Events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,14 @@ export function Events() {
const [isEditModalOpen, setEditModalOpen] = useState(false);
const [isDeleteModalOpen, setDeleteModalOpen] = useState(false);
const [selectModalOpen, setSelectModalOpen] = useState(false);

const [isCopyModalOpen, setCopyModalOpen] = useState(false);
const [form, setForm] = useState(() => makeForm());
const [copyForm, setCopyForm] = useState(() => ({
form: makeCopyForm([], 0),
orgIds: [] as string[],
}));

const [form, setForm] = useState(() => makeForm());
const [currentId, setCurrentId] = useState("");
const [query, setQuery] = useState("");
const selectedOrg = serverData.organizations.get(serverData.selectedOrg);
Expand Down

0 comments on commit bc462dc

Please sign in to comment.