Skip to content

Commit

Permalink
Merge pull request #269 from rimorin/restructure
Browse files Browse the repository at this point in the history
refactor: data structure
  • Loading branch information
rimorin authored Oct 15, 2023
2 parents 3da68b1 + dfc77f6 commit a6d4286
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 96 deletions.
3 changes: 3 additions & 0 deletions src/components/modal/assignments.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const AllLinks: Story = {
<GetAssignments
defaultVisible
id="1"
congregation="test"
assignmentTerritory={assignmentTerritory}
assignments={assignments}
assignmentType={assignmentType}
Expand Down Expand Up @@ -86,6 +87,7 @@ export const PersonalLinks: Story = {
<GetAssignments
defaultVisible
id="1"
congregation="test"
assignmentTerritory={assignmentTerritory}
assignments={assignments}
assignmentType={assignmentType}
Expand Down Expand Up @@ -124,6 +126,7 @@ export const AssignmentLinks: Story = {
<GetAssignments
defaultVisible
id="1"
congregation="test"
assignmentTerritory={assignmentTerritory}
assignments={assignments}
assignmentType={assignmentType}
Expand Down
10 changes: 7 additions & 3 deletions src/components/modal/assignments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const GetAssignments = NiceModal.create(
({
assignments,
assignmentType,
assignmentTerritory
assignmentTerritory,
congregation
}: AssignmentModalProps) => {
const modal = useModal();

Expand Down Expand Up @@ -102,9 +103,12 @@ const GetAssignments = NiceModal.create(
onClick={async (event) => {
const { linkid, postal } = event.currentTarget.dataset;
await pollingVoidFunction(() =>
remove(ref(database, `links/${linkid}`))
remove(ref(database, `links/${congregation}/${linkid}`))
);
await triggerPostalCodeListeners(
congregation,
postal as string
);
await triggerPostalCodeListeners(postal as string);
setCurrentAssignments((currentAssignments) =>
currentAssignments.filter(
(assignment) => assignment.key !== linkid
Expand Down
1 change: 1 addition & 0 deletions src/components/modal/changeaddname.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const Default: Story = {
<NiceModal.Provider>
<ChangeAddressName
id="1"
congregation="test"
defaultVisible
name={name}
footerSaveAcl={footerSaveAcl}
Expand Down
8 changes: 6 additions & 2 deletions src/components/modal/changeaddname.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const ChangeAddressName = NiceModal.create(
({
name,
footerSaveAcl = USER_ACCESS_LEVELS.READ_ONLY.CODE,
postal
postal,
congregation
}: ChangeAddressNameModalProps) => {
const [addressName, setAddressName] = useState(name);
const [isSaving, setIsSaving] = useState(false);
Expand All @@ -28,7 +29,10 @@ const ChangeAddressName = NiceModal.create(
setIsSaving(true);
try {
await pollingVoidFunction(() =>
set(ref(database, `/${postal}/name`), addressName)
set(
ref(database, `addresses/${congregation}/${postal}/name`),
addressName
)
);
modal.hide();
} catch (error) {
Expand Down
16 changes: 12 additions & 4 deletions src/components/modal/changepostalcd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ const ChangeAddressPostalCode = NiceModal.create(
};

const deleteBlock = async (
congregation: string,
postalCode: string,
name: string,
showAlert: boolean
) => {
if (!territoryCode) return;
try {
await remove(ref(database, postalCode));
await remove(ref(database, `addresses/${congregation}/${postalCode}`));
await deleteTerritoryAddress(territoryCode, postalCode);
if (showAlert) alert(`Deleted address, ${name}.`);
} catch (error) {
Expand All @@ -79,13 +80,18 @@ const ChangeAddressPostalCode = NiceModal.create(
event.preventDefault();
setIsSaving(true);
try {
const newPostalRef = ref(database, newPostalCode);
const newPostalRef = ref(
database,
`addresses/${congregation}/${newPostalCode}`
);
const existingAddress = await get(newPostalRef);
if (existingAddress.exists()) {
alert(`Postal address, ${newPostalCode} already exist.`);
return;
}
const oldPostalData = await get(ref(database, postalCode));
const oldPostalData = await get(
ref(database, `addresses/${congregation}/${postalCode}`)
);
await pollingVoidFunction(() => set(newPostalRef, oldPostalData.val()));
await pollingVoidFunction(() =>
set(
Expand All @@ -98,7 +104,9 @@ const ChangeAddressPostalCode = NiceModal.create(
newPostalCode
)
);
await pollingVoidFunction(() => deleteBlock(postalCode, "", false));
await pollingVoidFunction(() =>
deleteBlock(congregation, postalCode, "", false)
);
modal.resolve();
modal.hide();
} catch (error) {
Expand Down
8 changes: 7 additions & 1 deletion src/components/modal/instructions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ const UpdateAddressInstructions = NiceModal.create(
event.preventDefault();
try {
await pollingVoidFunction(() =>
set(ref(database, `/${postalCode}/instructions`), addressInstructions)
set(
ref(
database,
`addresses/${congregation}/${postalCode}/instructions`
),
addressInstructions
)
);
await setNotification(
NOTIFICATION_TYPES.INSTRUCTIONS,
Expand Down
5 changes: 4 additions & 1 deletion src/components/modal/newprivateadd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ const NewPrivateAddress = NiceModal.create(

setIsSaving(true);
try {
const addressReference = ref(database, postalCode);
const addressReference = ref(
database,
`addresses/${congregation}/${postalCode}`
);
const existingAddress = await get(addressReference);
if (existingAddress.exists()) {
alert(`Postal address, ${postalCode} already exist.`);
Expand Down
5 changes: 4 additions & 1 deletion src/components/modal/newpublicadd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ const NewPublicAddress = NiceModal.create(

setIsSaving(true);
try {
const addressReference = ref(database, postalCode);
const addressReference = ref(
database,
`addresses/${congregation}/${postalCode}`
);
const existingAddress = await get(addressReference);
if (existingAddress.exists()) {
alert(`Postal address, ${postalCode} already exist.`);
Expand Down
1 change: 1 addition & 0 deletions src/components/modal/newunit.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const Default: Story = {
<NewUnit
id="1"
defaultVisible
congregation="test"
footerSaveAcl={footerSaveAcl}
postalCode={postalCode}
addressData={addressData}
Expand Down
4 changes: 3 additions & 1 deletion src/components/modal/newunit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const NewUnit = NiceModal.create(
footerSaveAcl = USER_ACCESS_LEVELS.READ_ONLY.CODE,
postalCode,
addressData,
defaultType
defaultType,
congregation
}: NewUnitModalProps) => {
const [unit, setUnit] = useState("");
const [isSaving, setIsSaving] = useState(false);
Expand All @@ -31,6 +32,7 @@ const NewUnit = NiceModal.create(
setIsSaving(true);
try {
processPostalUnitNumber(
congregation,
postalCode,
unit,
addressData,
Expand Down
5 changes: 4 additions & 1 deletion src/components/modal/updateaddfeedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ const UpdateAddressFeedback = NiceModal.create(
setIsSaving(true);
try {
await pollingVoidFunction(() =>
set(ref(database, `/${postalCode}/feedback`), feedback)
set(
ref(database, `addresses/${congregation}/${postalCode}/feedback`),
feedback
)
);
await setNotification(
NOTIFICATION_TYPES.FEEDBACK,
Expand Down
7 changes: 6 additions & 1 deletion src/components/modal/updatestatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import HelpButton from "../navigation/help";

const UpdateUnitStatus = NiceModal.create(
({
congregation,
addressName,
userAccessLevel = USER_ACCESS_LEVELS.READ_ONLY.CODE,
territoryType,
Expand Down Expand Up @@ -107,7 +108,10 @@ const UpdateUnitStatus = NiceModal.create(
try {
await pollingVoidFunction(() =>
update(
ref(database, `/${postalCode}/units/${floor}/${unitNo}`),
ref(
database,
`addresses/${congregation}/${postalCode}/units/${floor}/${unitNo}`
),
updateData
)
);
Expand Down Expand Up @@ -266,6 +270,7 @@ const UpdateUnitStatus = NiceModal.create(
variant="primary"
onClick={() => {
processPostalUnitNumber(
congregation,
postalCode,
unitNo,
addressData,
Expand Down
1 change: 1 addition & 0 deletions src/components/modal/updateunit.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const Default: Story = {
<UpdateUnit
id="1"
defaultVisible
congregation="test"
postalCode={postalCode}
unitNo={unitNo}
unitSequence={unitSequence}
Expand Down
4 changes: 3 additions & 1 deletion src/components/modal/updateunit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import HelpButton from "../navigation/help";

const UpdateUnit = NiceModal.create(
({
congregation,
postalCode,
unitNo,
unitSequence,
Expand All @@ -40,7 +41,7 @@ const UpdateUnit = NiceModal.create(
const floorDetails = addressData.floors[index];
floorDetails.units.forEach(() => {
unitUpdates[
`/${postalCode}/units/${floorDetails.floor}/${unitNumber}/sequence`
`addresses/${congregation}/${postalCode}/units/${floorDetails.floor}/${unitNumber}/sequence`
] = sequence === undefined ? {} : sequence;
});
}
Expand Down Expand Up @@ -119,6 +120,7 @@ const UpdateUnit = NiceModal.create(
variant="primary"
onClick={() => {
processPostalUnitNumber(
congregation,
postalCode,
unitNo,
addressData,
Expand Down
78 changes: 43 additions & 35 deletions src/pages/dashboard/admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,42 +294,48 @@ function Admin({ user }: adminProps) {
const postalCode = details.code;
setAccordionKeys((existingKeys) => [...existingKeys, postalCode]);
unsubscribers.current.push(
onValue(child(ref(database), `/${postalCode}`), async (snapshot) => {
clearInterval(pollerId);
if (snapshot.exists()) {
const postalSnapshot = snapshot.val();
const floorData = await processAddressData(
postalCode,
postalSnapshot.units
);
const linkDetails = await processLinkDetails(postalCode);
const addressData = {
assigneeDetailsList: linkDetails.assigneeDetailsList,
personalDetailsList: linkDetails.personalDetailsList,
x_zip: postalSnapshot.x_zip,
name: postalSnapshot.name,
postalCode: postalCode,
floors: floorData,
feedback: postalSnapshot.feedback,
type: postalSnapshot.type,
instructions: postalSnapshot.instructions
};
setAddressData(
(existingAddresses) =>
new Map<string, addressDetails>(
existingAddresses.set(postalCode, addressData)
)
);
onValue(
child(ref(database), `addresses/${code}/${postalCode}`),
async (snapshot) => {
clearInterval(pollerId);
if (snapshot.exists()) {
const postalSnapshot = snapshot.val();
console.log(postalSnapshot);
const floorData = await processAddressData(
code,
postalCode,
postalSnapshot.units
);
console.log(floorData);
const linkDetails = await processLinkDetails(code, postalCode);
const addressData = {
assigneeDetailsList: linkDetails.assigneeDetailsList,
personalDetailsList: linkDetails.personalDetailsList,
x_zip: postalSnapshot.x_zip,
name: postalSnapshot.name,
postalCode: postalCode,
floors: floorData,
feedback: postalSnapshot.feedback,
type: postalSnapshot.type,
instructions: postalSnapshot.instructions
};
setAddressData(
(existingAddresses) =>
new Map<string, addressDetails>(
existingAddresses.set(postalCode, addressData)
)
);
}
}
})
)
);
}
};

const deleteBlockFloor = async (postalcode: string, floor: string) => {
try {
await pollingVoidFunction(() =>
remove(ref(database, `${postalcode}/units/${floor}`))
remove(ref(database, `addresses/${code}/${postalcode}/units/${floor}`))
);
} catch (error) {
errorHandler(error, rollbar);
Expand Down Expand Up @@ -357,9 +363,8 @@ function Admin({ user }: adminProps) {
const addressData = addressesSnapshot.val();
for (const addkey in addressData) {
const postalcode = addressData[addkey];
await pollingVoidFunction(() =>
remove(ref(database, postalcode as string))
);
const postalPath = `addresses/${code}/${postalcode}`;
await pollingVoidFunction(() => remove(ref(database, postalPath)));
}
}
await pollingVoidFunction(() =>
Expand Down Expand Up @@ -436,7 +441,9 @@ function Admin({ user }: adminProps) {
newFloor = currentFloor - 1;
}
blockFloorDetails.units.forEach((element) => {
unitUpdates[`/${postalcode}/units/${newFloor}/${element.number}`] = {
unitUpdates[
`addresses/${code}/${postalcode}/units/${newFloor}/${element.number}`
] = {
status: STATUS_CODES.DEFAULT,
type: policy.defaultType,
note: "",
Expand Down Expand Up @@ -477,7 +484,7 @@ function Admin({ user }: adminProps) {
for (const index in blockAddresses.floors) {
const floorDetails = blockAddresses.floors[index];
floorDetails.units.forEach((element) => {
const unitPath = `/${postalcode}/units/${floorDetails.floor}/${element.number}`;
const unitPath = `addresses/${code}/${postalcode}/units/${floorDetails.floor}/${element.number}`;
let currentStatus = element.status;
if (MUTABLE_CODES.includes(currentStatus)) {
currentStatus = STATUS_CODES.DEFAULT;
Expand Down Expand Up @@ -545,8 +552,8 @@ function Admin({ user }: adminProps) {
link.name = postalName;
link.publisherName = publisherName;
return pollingVoidFunction(async () => {
await set(ref(database, `links/${addressLinkId}`), link);
await triggerPostalCodeListeners(link.postalCode);
await set(ref(database, `links/${code}/${addressLinkId}`), link);
await triggerPostalCodeListeners(code, link.postalCode);
});
};

Expand Down Expand Up @@ -1650,6 +1657,7 @@ function Admin({ user }: adminProps) {
ModalManager.show(
SuspenseComponent(ChangeAddressName),
{
congregation: code,
footerSaveAcl: userAccessLevel,
postal: currentPostalcode,
name: currentPostalname
Expand Down
Loading

0 comments on commit a6d4286

Please sign in to comment.